Skip to content

Commit

Permalink
feat(template): support using PR title in the GitHub template (#418)
Browse files Browse the repository at this point in the history
* feat(template): support using PR title in the GitHub template

* refactor(test): add PR title to the tests
  • Loading branch information
orhun committed Dec 31, 2023
1 parent faa00c6 commit 6f32f33
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
6 changes: 5 additions & 1 deletion examples/github-keepachangelog.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ conventional_commits = true
filter_unconventional = true
# process each line of a commit as an individual commit
split_commits = false
commit_preprocessors = [{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "" }]
# regex for preprocessing the commit messages
commit_preprocessors = [
# remove issue numbers from commits
{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "" },
]
# regex for parsing and grouping commits
commit_parsers = [
{ message = "^.*: add", group = "Added" },
Expand Down
12 changes: 10 additions & 2 deletions examples/github.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ body = """
{%- if version %} in {{ version }}{%- endif -%}
{% for commit in commits %}
* {{ commit.message | split(pat="\n") | first | trim }}\
{% if commit.github.pr_title -%}
{%- set commit_message = commit.github.pr_title -%}
{%- else -%}
{%- set commit_message = commit.message -%}
{%- endif -%}
* {{ commit_message | split(pat="\n") | first | trim }}\
{% if commit.github.username %} by @{{ commit.github.username }}{%- endif -%}
{% if commit.github.pr_number %} in #{{ commit.github.pr_number }}{%- endif %}
{%- endfor -%}
Expand Down Expand Up @@ -52,7 +57,10 @@ filter_unconventional = true
# process each line of a commit as an individual commit
split_commits = false
# regex for preprocessing the commit messages
commit_preprocessors = [{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "" }]
commit_preprocessors = [
# remove issue numbers from commits
{ pattern = '\((\w+\s)?#([0-9]+)\)', replace = "" },
]
# protect breaking changes from being skipped due to matching a skipping commit_parser
protect_breaking_commits = false
# filter out the commits that are not matched by commit parsers
Expand Down
4 changes: 4 additions & 0 deletions git-cliff-core/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub struct GitHubCommitAuthor {
pub struct GitHubPullRequest {
/// Pull request number.
pub number: i64,
/// Pull request title.
pub title: Option<String>,
/// SHA of the merge commit.
pub merge_commit_sha: Option<String>,
}
Expand Down Expand Up @@ -128,6 +130,8 @@ pub struct GitHubReleaseMetadata {
pub struct GitHubContributor {
/// Username.
pub username: Option<String>,
/// Title of the pull request.
pub pr_title: Option<String>,
/// The pull request that the user created.
pub pr_number: Option<i64>,
/// Whether if the user contributed for the first time.
Expand Down
26 changes: 22 additions & 4 deletions git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ impl<'a> Release<'a> {
if let Some(commit) =
self.commits.iter_mut().find(|commit| commit.id == v.sha)
{
commit.github.username = v.author.clone().and_then(|v| v.login);
commit.github.pr_number = github_pull_requests
let pull_request = github_pull_requests
.iter()
.find(|pr| pr.merge_commit_sha == Some(v.sha.clone()))
.map(|v| v.number);
.find(|pr| pr.merge_commit_sha == Some(v.sha.clone()));

commit.github.username = v.author.clone().and_then(|v| v.login);
commit.github.pr_number = pull_request.map(|v| v.number);
commit.github.pr_title = pull_request.and_then(|v| v.title.clone());
contributors.insert(GitHubContributor {
username: v.author.clone().and_then(|v| v.login),
pr_title: commit.github.pr_title.clone(),
pr_number: commit.github.pr_number,
is_first_time: false,
});
Expand Down Expand Up @@ -317,30 +320,35 @@ mod test {
],
vec![
GitHubPullRequest {
title: Some(String::from("1")),
number: 42,
merge_commit_sha: Some(String::from(
"1d244937ee6ceb8e0314a4a201ba93a7a61f2071",
)),
},
GitHubPullRequest {
title: Some(String::from("2")),
number: 66,
merge_commit_sha: Some(String::from(
"21f6aa587fcb772de13f2fde0e92697c51f84162",
)),
},
GitHubPullRequest {
title: Some(String::from("3")),
number: 53,
merge_commit_sha: Some(String::from(
"35d8c6b6329ecbcf131d7df02f93c3bbc5ba5973",
)),
},
GitHubPullRequest {
title: Some(String::from("4")),
number: 1000,
merge_commit_sha: Some(String::from(
"4d3ffe4753b923f4d7807c490e650e6624a12074",
)),
},
GitHubPullRequest {
title: Some(String::from("5")),
number: 999999,
merge_commit_sha: Some(String::from(
"5a55e92e5a62dc5bf9872ffb2566959fad98bd05",
Expand All @@ -356,6 +364,7 @@ mod test {
message: String::from("add github integration"),
github: GitHubContributor {
username: Some(String::from("orhun")),
pr_title: Some(String::from("1")),
pr_number: Some(42),
is_first_time: false,
},
Expand All @@ -366,6 +375,7 @@ mod test {
message: String::from("fix github integration"),
github: GitHubContributor {
username: Some(String::from("orhun")),
pr_title: Some(String::from("2")),
pr_number: Some(66),
is_first_time: false,
},
Expand All @@ -376,6 +386,7 @@ mod test {
message: String::from("update metadata"),
github: GitHubContributor {
username: Some(String::from("nuhro")),
pr_title: Some(String::from("3")),
pr_number: Some(53),
is_first_time: false,
},
Expand All @@ -386,6 +397,7 @@ mod test {
message: String::from("do some stuff"),
github: GitHubContributor {
username: Some(String::from("awesome_contributor")),
pr_title: Some(String::from("4")),
pr_number: Some(1000),
is_first_time: false,
},
Expand All @@ -396,6 +408,7 @@ mod test {
message: String::from("alright"),
github: GitHubContributor {
username: Some(String::from("orhun")),
pr_title: Some(String::from("5")),
pr_number: Some(999999),
is_first_time: false,
},
Expand All @@ -406,6 +419,7 @@ mod test {
message: String::from("should be fine"),
github: GitHubContributor {
username: Some(String::from("someone")),
pr_title: Some(String::from("6")),
pr_number: None,
is_first_time: false,
},
Expand All @@ -425,21 +439,25 @@ mod test {
contributors: vec![
GitHubContributor {
username: Some(String::from("someone")),
pr_title: Some(String::from("6")),
pr_number: None,
is_first_time: true,
},
GitHubContributor {
username: Some(String::from("orhun")),
pr_title: Some(String::from("5")),
pr_number: Some(42),
is_first_time: true,
},
GitHubContributor {
username: Some(String::from("nuhro")),
pr_title: Some(String::from("3")),
pr_number: Some(53),
is_first_time: true,
},
GitHubContributor {
username: Some(String::from("awesome_contributor")),
pr_title: Some(String::from("4")),
pr_number: Some(1000),
is_first_time: true,
},
Expand Down
4 changes: 3 additions & 1 deletion website/docs/integration/github.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
sidebar_position: 1
---

# GitHub 🆕

:::warning
Expand Down Expand Up @@ -104,6 +103,7 @@ For each commit, GitHub related values are added as a nested object (named `gith

"github": {
"username": "orhun",
"pr_title": "some things have changed",
"pr_number": 420,
"is_first_time": false
}
Expand Down Expand Up @@ -142,11 +142,13 @@ For each release, following contributors data is added to the [template context]
"contributors": [
{
"username": "orhun",
"pr_title": "some things have changed",
"pr_number": 420,
"is_first_time": true
},
{
"username": "cliffjumper",
"pr_title": "I love jumping",
"pr_number": 999,
"is_first_time": true
}
Expand Down

0 comments on commit 6f32f33

Please sign in to comment.