Skip to content

Commit

Permalink
fix(436): Fix Missing/wrong pull requests for github commits
Browse files Browse the repository at this point in the history
Signed-off-by: dark0dave <dark0dave@mykolab.com>
  • Loading branch information
dark0dave committed Aug 30, 2024
1 parent 79619d0 commit 7f85647
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 1 deletion.
81 changes: 80 additions & 1 deletion git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,67 @@ pub struct Release<'a> {
}

#[cfg(feature = "github")]
crate::update_release_metadata!(github, update_github_metadata);
impl<'a> Release<'a> {
/// Updates the remote metadata that is contained in the release.
///
/// This function takes two arguments:
///
/// - Commits: needed for associating the Git user with the GitHub username.
/// - Pull requests: needed for generating the contributor list for the
/// release.
pub fn update_github_metadata(
&mut self,
mut commits: Vec<Box<dyn RemoteCommit>>,
pull_requests: Vec<Box<dyn RemotePullRequest>>,
) -> Result<()> {
let mut contributors: Vec<RemoteContributor> = Vec::new();
// retain the commits that are not a part of this release for later
// on checking the first contributors.
commits.retain(|v| {
if let Some(commit) =
self.commits.iter_mut().find(|commit| commit.id == v.id())
{
let pull_request = pull_requests
.iter()
.find(|pr| pr.merge_commit() == Some(v.id().clone()));
commit.github.username =
pull_request.map(|v| v.try_get_author()).unwrap_or_default();
commit.github.pr_number = pull_request.map(|v| v.number());
commit.github.pr_title =
pull_request.and_then(|v| v.title().clone());
commit.github.pr_labels =
pull_request.map(|v| v.labels().clone()).unwrap_or_default();
if !contributors
.iter()
.any(|v| commit.github.username == v.username)
{
contributors.push(RemoteContributor {
username: commit.github.username.clone(),
pr_title: commit.github.pr_title.clone(),
pr_number: commit.github.pr_number,
pr_labels: commit.github.pr_labels.clone(),
is_first_time: false,
});
}
false
} else {
true
}
});
// mark contributors as first-time
self.github.contributors = contributors
.into_iter()
.map(|mut v| {
v.is_first_time = !commits
.iter()
.map(|v| v.username())
.any(|login| login == v.username);
v
})
.collect();
Ok(())
}
}

#[cfg(feature = "gitlab")]
crate::update_release_metadata!(gitlab, update_gitlab_metadata);
Expand Down Expand Up @@ -493,6 +553,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("2")),
Expand All @@ -503,6 +564,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("3")),
Expand All @@ -513,6 +575,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("4")),
Expand All @@ -523,6 +586,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("5")),
Expand All @@ -533,6 +597,9 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("github"),
}],
user: GitHubCommitAuthor {
login: Some(String::from("idk")),
},
},
]
.into_iter()
Expand Down Expand Up @@ -650,6 +717,13 @@ mod test {
pr_labels: vec![String::from("deps")],
is_first_time: true,
},
RemoteContributor {
username: Some(String::from("idk")),
pr_title: Some(String::from("5")),
pr_number: Some(999999),
pr_labels: vec![String::from("github")],
is_first_time: true,
},
],
};
assert_eq!(expected_metadata, release.github);
Expand Down Expand Up @@ -1127,6 +1201,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("2")),
Expand All @@ -1137,6 +1212,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("3")),
Expand All @@ -1147,6 +1223,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("4")),
Expand All @@ -1157,6 +1234,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("5")),
Expand All @@ -1167,6 +1245,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("github"),
}],
user: GiteaCommitAuthor { login: None },
},
]
.into_iter()
Expand Down
4 changes: 4 additions & 0 deletions git-cliff-core/src/remote/bitbucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ impl RemotePullRequest for BitbucketPullRequest {
fn merge_commit(&self) -> Option<String> {
Some(self.merge_commit_sha.hash.clone())
}

fn try_get_author(&self) -> Option<String> {
self.author.login.clone()
}
}

/// <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-get>
Expand Down
8 changes: 8 additions & 0 deletions git-cliff-core/src/remote/gitea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ pub struct PullRequestLabel {
}

/// Representation of a single pull request.
///
/// <https://docs.gitea.com/api/1.22/#tag/repository/operation/repoListPullRequests>
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GiteaPullRequest {
/// Pull request number.
Expand All @@ -85,6 +87,8 @@ pub struct GiteaPullRequest {
pub merge_commit_sha: Option<String>,
/// Labels of the pull request.
pub labels: Vec<PullRequestLabel>,
/// Author of the pull request
pub user: GiteaCommitAuthor,
}

impl RemotePullRequest for GiteaPullRequest {
Expand All @@ -103,6 +107,10 @@ impl RemotePullRequest for GiteaPullRequest {
fn merge_commit(&self) -> Option<String> {
self.merge_commit_sha.clone()
}

fn try_get_author(&self) -> Option<String> {
self.user.login.clone()
}
}

impl RemoteEntry for GiteaPullRequest {
Expand Down
6 changes: 6 additions & 0 deletions git-cliff-core/src/remote/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub struct GitHubPullRequest {
pub merge_commit_sha: Option<String>,
/// Labels of the pull request.
pub labels: Vec<PullRequestLabel>,
/// Author of the pull request.
pub user: GitHubCommitAuthor,
}

impl RemotePullRequest for GitHubPullRequest {
Expand All @@ -103,6 +105,10 @@ impl RemotePullRequest for GitHubPullRequest {
fn merge_commit(&self) -> Option<String> {
self.merge_commit_sha.clone()
}

fn try_get_author(&self) -> Option<String> {
self.user.login.clone()
}
}

impl RemoteEntry for GitHubPullRequest {
Expand Down
4 changes: 4 additions & 0 deletions git-cliff-core/src/remote/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ impl RemotePullRequest for GitLabMergeRequest {
fn merge_commit(&self) -> Option<String> {
self.merge_commit_sha.clone()
}

fn try_get_author(&self) -> Option<String> {
Some(self.author.name.clone())
}
}

impl RemoteEntry for GitLabMergeRequest {
Expand Down
2 changes: 2 additions & 0 deletions git-cliff-core/src/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ pub trait RemotePullRequest: DynClone {
fn labels(&self) -> Vec<String>;
/// Merge commit SHA.
fn merge_commit(&self) -> Option<String>;
/// Try to get author from pull request
fn try_get_author(&self) -> Option<String>;
}

dyn_clone::clone_trait_object!(RemotePullRequest);
Expand Down

0 comments on commit 7f85647

Please sign in to comment.