Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(436): Fix Missing/wrong pull requests for github commits #831

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 89 additions & 1 deletion git-cliff-core/src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,76 @@ 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()));
debug!("-------------------------");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these will be removed don't worry

debug!("{:?}", pull_request);
debug!("{:?}", commit);
commit.github.username = pull_request
.map(|v| v.try_get_author())
.unwrap_or(v.username());

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 let Some(contributor) = contributors
.iter_mut()
.find(|v| commit.github.username == v.username)
{
trace!("{:?}", contributor);
contributor.username = commit.github.username.clone();
contributor.pr_number.get_or_insert(commit.github.pr_number.unwrap_or_default());
} else {
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 +562,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("2")),
Expand All @@ -503,6 +573,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("3")),
Expand All @@ -513,6 +584,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("4")),
Expand All @@ -523,6 +595,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GitHubCommitAuthor { login: None },
},
GitHubPullRequest {
title: Some(String::from("5")),
Expand All @@ -533,6 +606,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 +726,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 +1210,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("2")),
Expand All @@ -1137,6 +1221,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("rust"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("3")),
Expand All @@ -1147,6 +1232,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("4")),
Expand All @@ -1157,6 +1243,7 @@ mod test {
labels: vec![PullRequestLabel {
name: String::from("deps"),
}],
user: GiteaCommitAuthor { login: None },
},
GiteaPullRequest {
title: Some(String::from("5")),
Expand All @@ -1167,6 +1254,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
6 changes: 4 additions & 2 deletions git-cliff-core/src/remote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub trait RemoteEntry {
}

/// Trait for handling remote commits.
pub trait RemoteCommit: DynClone {
pub trait RemoteCommit: DynClone + Debug {
/// Commit SHA.
fn id(&self) -> String;
/// Commit author.
Expand All @@ -90,7 +90,7 @@ pub trait RemoteCommit: DynClone {
dyn_clone::clone_trait_object!(RemoteCommit);

/// Trait for handling remote pull requests.
pub trait RemotePullRequest: DynClone {
pub trait RemotePullRequest: DynClone + Debug {
/// Number.
fn number(&self) -> i64;
/// Title.
Expand All @@ -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
Loading