Skip to content

Commit 69a2371

Browse files
committed
Fix issue with PR number extraction
1 parent 37c646d commit 69a2371

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

site/src/github.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,44 @@ pub async fn enqueue_unrolled_try_builds<'a>(
8484
Ok(mapping)
8585
}
8686

87+
lazy_static::lazy_static! {
88+
static ref ROLLUP_PR_NUMBER: regex::Regex =
89+
regex::Regex::new(r#"^Auto merge of #(\d+)"#).unwrap();
90+
}
91+
92+
// Gets the pr number for the associated rollup PR message. Returns None if this is not a rollup PR
93+
pub async fn rollup_pr(client: &client::Client, message: &str) -> Result<Option<u32>, String> {
94+
if !message.starts_with("Auto merge of") {
95+
return Ok(None);
96+
}
97+
98+
let number = ROLLUP_PR_NUMBER
99+
.captures(&message)
100+
.and_then(|c| c.get(1))
101+
.map(|m| {
102+
println!("{}", m.as_str());
103+
m.as_str().parse::<u64>()
104+
})
105+
.transpose()
106+
.map_err(|e| format!("Error parsing PR number from '{message}': {e:?}"))?;
107+
108+
let number = match number {
109+
Some(n) => n,
110+
None => return Ok(None),
111+
};
112+
113+
let issue = client
114+
.get_issue(number)
115+
.await
116+
.map_err(|e| format!("Error fetching PR #{number} {e:?}"))?;
117+
118+
Ok(issue
119+
.labels
120+
.iter()
121+
.any(|l| l.name == "rollup")
122+
.then(|| issue.number))
123+
}
124+
87125
// Returns the PR number
88126
pub async fn pr_and_try_for_rollup(
89127
ctxt: Arc<SiteCtxt>,

site/src/request_handlers/github.rs

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::api::{github, ServerResult};
22
use crate::github::{
33
branch_for_rollup, client, enqueue_sha, enqueue_unrolled_try_builds, get_authorized_users,
4-
parse_homu_comment, pr_and_try_for_rollup,
4+
parse_homu_comment, pr_and_try_for_rollup, rollup_pr,
55
};
66
use crate::load::SiteCtxt;
77

@@ -44,10 +44,10 @@ async fn handle_push(ctxt: Arc<SiteCtxt>, push: github::Push) -> ServerResult<gi
4444
&ctxt,
4545
"https://api.github.com/repos/rust-lang/rust".to_owned(),
4646
);
47-
if push.r#ref != "refs/heads/master" {
47+
if push.r#ref != "refs/heads/master" || push.sender.login != "bors" {
4848
return Ok(github::Response);
4949
}
50-
let rollup_pr = rollup_pr(&main_repo_client, &push).await?;
50+
let rollup_pr = rollup_pr(&main_repo_client, &push.head_commit.message).await?;
5151
let rollup_pr = match rollup_pr {
5252
Some(pr) => pr,
5353
None => return Ok(github::Response),
@@ -68,7 +68,7 @@ async fn handle_push(ctxt: Arc<SiteCtxt>, push: github::Push) -> ServerResult<gi
6868
.map(|(rollup_merge, sha)| {
6969
ROLLUPED_PR_NUMBER
7070
.captures(&rollup_merge.message)
71-
.and_then(|c| c.get(0))
71+
.and_then(|c| c.get(1))
7272
.map(|m| (m.as_str(), sha))
7373
.ok_or_else(|| {
7474
format!(
@@ -90,37 +90,6 @@ async fn handle_push(ctxt: Arc<SiteCtxt>, push: github::Push) -> ServerResult<gi
9090
Ok(github::Response)
9191
}
9292

93-
// Gets the pr number for the associated rollup PR. Returns None if this is not a rollup PR
94-
async fn rollup_pr(client: &client::Client, push: &github::Push) -> ServerResult<Option<u32>> {
95-
macro_rules! get {
96-
($x:expr) => {
97-
match $x {
98-
Some(x) => x,
99-
None => return Ok(None),
100-
}
101-
};
102-
}
103-
let is_bors =
104-
push.sender.login == "bors" && push.head_commit.message.starts_with("Auto merge of");
105-
106-
if !is_bors {
107-
return Ok(None);
108-
}
109-
let captures = get!(ROLLUP_PR_NUMBER.captures(&push.head_commit.message));
110-
let number = get!(get!(captures.get(0)).as_str().parse::<u64>().ok());
111-
112-
let issue = client
113-
.get_issue(number)
114-
.await
115-
.map_err(|e| format!("Error fetching PR #{number} {e:?}"))?;
116-
117-
Ok(issue
118-
.labels
119-
.iter()
120-
.any(|l| l.name == "rollup")
121-
.then(|| issue.number))
122-
}
123-
12493
async fn handle_issue(
12594
ctxt: Arc<SiteCtxt>,
12695
issue: github::Issue,

0 commit comments

Comments
 (0)