Skip to content
Merged
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
23 changes: 14 additions & 9 deletions src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ use git2::build::RepoBuilder;
use git2::{Commit as Git2Commit, Repository};
use log::debug;

use crate::{Author, Commit, GitDate, BORS_AUTHOR};
use crate::{Author, Commit, GitDate, BORS_AUTHORS};

impl Commit {
// Takes &mut because libgit2 internally caches summaries
fn from_git2_commit(commit: &mut Git2Commit<'_>) -> Self {
let committer = commit.committer();
let author = commit.author();
Commit {
sha: commit.id().to_string(),
date: time_to_date(&commit.time()),
summary: String::from_utf8_lossy(commit.summary_bytes().unwrap()).to_string(),
committer: Author {
name: committer.name().unwrap_or("").to_string(),
email: committer.email().unwrap_or("").to_string(),
date: time_to_date(&committer.when()),
author: Author {
name: author.name().unwrap_or("").to_string(),
email: author.email().unwrap_or("").to_string(),
date: time_to_date(&author.when()),
},
}
}
Expand Down Expand Up @@ -153,9 +153,9 @@ pub fn get_commits_between(first_commit: &str, last_commit: &str) -> anyhow::Res
// two commits are merge commits made by bors
let assert_by_bors = |c: &Git2Commit<'_>| -> anyhow::Result<()> {
match c.author().name() {
Some(author) if author == BORS_AUTHOR => Ok(()),
Some(author) if BORS_AUTHORS.contains(&author) => Ok(()),
Some(author) => bail!(
"Expected author {author} to be {BORS_AUTHOR} for {}.\n \
"Expected author {author} to be one of {BORS_AUTHORS:?} for {}.\n \
Make sure specified commits are on the default branch!",
c.id()
),
Expand All @@ -179,7 +179,12 @@ pub fn get_commits_between(first_commit: &str, last_commit: &str) -> anyhow::Res
res.push(Commit::from_git2_commit(&mut current));
match current.parents().next() {
Some(c) => {
if c.author().name() != Some(BORS_AUTHOR) {
if !c
.author()
.name()
.map(|name| BORS_AUTHORS.contains(&name))
.unwrap_or(false)
{
debug!(
"{:?} has non-bors author: {:?}, skipping",
c.id(),
Expand Down
84 changes: 43 additions & 41 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use reqwest::header::{HeaderMap, HeaderValue, InvalidHeaderValue, AUTHORIZATION,
use reqwest::{blocking::Client, blocking::Response};
use serde::{Deserialize, Serialize};

use crate::{parse_to_naive_date, Author, Commit, GitDate, BORS_AUTHOR};
use crate::{parse_to_naive_date, Author, Commit, GitDate, BORS_AUTHORS};

#[derive(Serialize, Deserialize, Debug)]
struct GithubCommitComparison {
Expand Down Expand Up @@ -51,20 +51,20 @@ impl GithubCommitElem {

fn git_commit(self) -> anyhow::Result<Commit> {
let date = self.date()?;
let committer = self
let author = self
.commit
.committer
.ok_or_else(|| anyhow::anyhow!("commit should have committer"))?;
let committer = Author {
name: committer.name,
email: committer.email,
.author
.ok_or_else(|| anyhow::anyhow!("commit should have author"))?;
let author = Author {
name: author.name,
email: author.email,
date,
};
Ok(Commit {
sha: self.sha,
date,
summary: self.commit.message,
committer,
author,
})
}
}
Expand Down Expand Up @@ -141,42 +141,44 @@ impl CommitsQuery<'_> {

// focus on Pull Request merges, all authored and committed by bors.
let client = Client::builder().default_headers(headers()?).build()?;
for page in 1.. {
let url = CommitsUrl {
page,
author: BORS_AUTHOR,
since: self.since_date,
sha: self.most_recent_sha,
}
.url();

let response: Response = client.get(&url).send()?;
let status = response.status();
if !status.is_success() {
bail!(
"error: url <{}> response {}: {}",
url,
status,
response.text().unwrap_or_else(|_| format!("<empty>"))
);
}
for bors_author in BORS_AUTHORS {
for page in 1.. {
let url = CommitsUrl {
page,
author: bors_author,
since: self.since_date,
sha: self.most_recent_sha,
}
.url();

let action = parse_paged_elems(response, |elem: GithubCommitElem| {
let found_last = elem.sha == self.earliest_sha;
if found_last {
eprintln!(
"ending github query because we found starting sha: {}",
elem.sha
let response: Response = client.get(&url).send()?;
let status = response.status();
if !status.is_success() {
bail!(
"error: url <{}> response {}: {}",
url,
status,
response.text().unwrap_or_else(|_| format!("<empty>"))
);
}
let commit = elem.git_commit()?;
commits.push(commit);

Ok(if found_last { Loop::Break } else { Loop::Next })
})?;
let action = parse_paged_elems(response, |elem: GithubCommitElem| {
let found_last = elem.sha == self.earliest_sha;
if found_last {
eprintln!(
"ending github query because we found starting sha: {}",
elem.sha
);
}
let commit = elem.git_commit()?;
commits.push(commit);

Ok(if found_last { Loop::Break } else { Loop::Next })
})?;

if let Loop::Break = action {
break;
if let Loop::Break = action {
break;
}
}
}

Expand Down Expand Up @@ -266,15 +268,15 @@ mod tests {
#[test]
fn test_github() {
let c = get_commit("25674202bb7415e0c0ecd07856749cfb7f591be6").unwrap();
let committer = Author {
let author = Author {
name: String::from("bors"),
email: String::from("bors@rust-lang.org"),
date: GitDate::from_ymd_opt(2022, 5, 4).unwrap(),
};
let expected_c = Commit { sha: "25674202bb7415e0c0ecd07856749cfb7f591be6".to_string(),
date: parse_to_naive_date("2022-05-04").unwrap(),
summary: "Auto merge of #96695 - JohnTitor:rollup-oo4fc1h, r=JohnTitor\n\nRollup of 6 pull requests\n\nSuccessful merges:\n\n - #96597 (openbsd: unbreak build on native platform)\n - #96662 (Fix typo in lint levels doc)\n - #96668 (Fix flaky rustdoc-ui test because it did not replace time result)\n - #96679 (Quick fix for #96223.)\n - #96684 (Update `ProjectionElem::Downcast` documentation)\n - #96686 (Add some TAIT-related tests)\n\nFailed merges:\n\nr? `@ghost`\n`@rustbot` modify labels: rollup".to_string(),
committer,
author,
};
assert_eq!(c, expected_c)
}
Expand Down
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ use crate::toolchains::{
ToolchainSpec, YYYY_MM_DD,
};

const BORS_AUTHOR: &str = "bors";
/// Git usernames used by bors.
const BORS_AUTHORS: &[&str] = &["bors", "rust-bors[bot]"];

#[derive(Debug, Clone, PartialEq)]
pub struct Commit {
pub sha: String,
pub date: GitDate,
pub summary: String,
pub committer: Author,
pub author: Author,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -997,12 +998,12 @@ impl Config {
let start = access.commit(start_sha)?;
let end = access.commit(end_sha)?;
let assert_by_bors = |c: &Commit| -> anyhow::Result<()> {
if c.committer.name != BORS_AUTHOR {
if !BORS_AUTHORS.contains(&c.author.name.as_str()) {
bail!(
"Expected author {} to be {BORS_AUTHOR} for {}.\n \
"Expected author {} to be one of {BORS_AUTHORS:?} for {}.\n \
Make sure specified commits are on the default branch \
and refer to a bors merge commit!",
c.committer.name,
c.author.name,
c.sha
);
}
Expand Down