Skip to content

Commit

Permalink
Only consider approvals for the last commit in a PR
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperSandro2000 committed Aug 16, 2024
1 parent f307ad2 commit ef24e69
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ edition = "2021"
anyhow = "^1"
clap = { version = "^4", features = ["derive", "env"] }
git2 = "^0"
octocrab = "^0"
# change back to crates.io after https://github.com/XAMPPRocky/octocrab/pull/680 got released
octocrab = { version = "^0", git = "https://github.com/SuperSandro2000/octocrab.git", branch = "pr_commits" }
serde = "^1"
serde_yml = "^0"
tokio = { version = "^1", features = ["macros", "rt-multi-thread"] }
Expand Down
33 changes: 29 additions & 4 deletions src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,26 @@ impl RepoChangeset {
.to_string(),
);

let mut pr_commits_page = octocrab
.pulls(&self.remote.owner, &self.remote.repository)
.pr_commits(associated_pr.number)
.await
.context("failed to get pr commits")?;
assert!(
pr_commits_page.next.is_none(),
"found more than one page for associated_prs"
);

let pr_commits = pr_commits_page.take_items();
assert!(
pr_commits.len() <= 250,
"found more than 250 commits which requires a different api endpoint per doc"
);
let head_sha = pr_commits.last().ok_or(anyhow!("PR contains no commits?"))?.sha.clone();

if let Some(changeset) = self.changes.iter_mut().find(|cs| cs.pr_link == associated_pr_link) {
changeset.commits.push(change_commit.clone());
changeset.collect_approved_reviews(&pr_reviews);
changeset.collect_approved_reviews(&pr_reviews, &head_sha);
continue;
}

Expand All @@ -97,7 +114,7 @@ impl RepoChangeset {
approvals: Vec::new(),
};

changeset.collect_approved_reviews(&pr_reviews);
changeset.collect_approved_reviews(&pr_reviews, &head_sha);
self.changes.push(changeset);
}

Expand All @@ -114,7 +131,7 @@ pub struct Changeset {

impl Changeset {
// pr_reviews must be sorted by key submitted_at!
pub fn collect_approved_reviews(&mut self, pr_reviews: &[Review]) {
pub fn collect_approved_reviews(&mut self, pr_reviews: &[Review], head_sha: &String) {
let mut last_review_by: Vec<&String> = vec![];

// reverse the order of reviews to start with the oldest
Expand All @@ -123,12 +140,20 @@ impl Changeset {
continue;
};

// only consider the last review of any user
// Only consider the last review of any user.
// For example a user might have requested changes early on in the PR and later approved it
// or requested additional changes after supplying an approval first.
if last_review_by.contains(&&user.login) {
continue;
}
last_review_by.push(&user.login);

// Only account for reviews done on the last commit of the PR.
// We could count the PR as partly reviewed but that is to complicated to present at the moment.
if pr_review.commit_id != Some(head_sha.to_string()) {
continue;
}

// in case it isn't approve, ignore it
if pr_review.state != Some(Approved) {
continue;
Expand Down

0 comments on commit ef24e69

Please sign in to comment.