Skip to content

Commit 56f8f3a

Browse files
committed
Post mapping between PR and new try commit
1 parent 2d0b8db commit 56f8f3a

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

site/src/request_handlers/github.rs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ use regex::{Captures, Regex};
1212
lazy_static::lazy_static! {
1313
static ref ROLLUP_PR_NUMBER: Regex =
1414
Regex::new(r#"^Auto merge of #(\d+)"#).unwrap();
15+
static ref ROLLUPED_PR_NUMBER: Regex =
16+
Regex::new(r#"^Rollup merge of #(\d+)"#).unwrap();
1517
static ref BODY_TRY_COMMIT: Regex =
1618
Regex::new(r#"(?:\W|^)@rust-timer\s+build\s+(\w+)(?:\W|$)(?:include=(\S+))?\s*(?:exclude=(\S+))?\s*(?:runs=(\d+))?"#).unwrap();
1719
static ref BODY_QUEUE: Regex =
@@ -36,32 +38,50 @@ pub async fn handle_github(
3638
async fn handle_push(ctxt: Arc<SiteCtxt>, push: github::Push) -> ServerResult<github::Response> {
3739
let client = reqwest::Client::new();
3840
let repository_url = "https://github.com/rust-lang-ci/rust";
39-
if push.r#ref != "refs/heads/master"
40-
|| !is_rollup(&client, &ctxt, repository_url, &push).await?
41-
{
41+
if push.r#ref != "refs/heads/master" {
4242
return Ok(github::Response);
4343
}
44+
let pr = rollup_pr(&client, &ctxt, repository_url, &push).await?;
45+
let pr = match pr {
46+
Some(pr) => pr,
47+
None => return Ok(github::Response),
48+
};
4449

4550
let previous_master = &push.before;
4651
let rollup_merges = push
4752
.commits
4853
.iter()
4954
.rev()
5055
.skip(1) // skip the head commit
51-
.take_while(|c| c.message.starts_with("Rollup merge of "))
52-
.map(|c| &c.sha);
56+
.take_while(|c| c.message.starts_with("Rollup merge of "));
5357

58+
let mut prs = Vec::new();
5459
for rollup_merge in rollup_merges {
60+
let pr_num = ROLLUPED_PR_NUMBER
61+
.captures(&rollup_merge.message)
62+
.and_then(|c| c.get(0))
63+
.map(|m| m.as_str())
64+
.ok_or_else(|| {
65+
format!(
66+
"Could not get PR number from message: '{}'",
67+
rollup_merge.message
68+
)
69+
})?;
5570
// Fetch the rollup merge commit which should have two parents.
5671
// The first parent is in the chain of rollup merge commits all the way back to `previous_master`.
5772
// The second parent is the head of the PR that was rolled up. We want the second parent.
58-
let commit = get_commit(&client, &ctxt, repository_url, &rollup_merge)
73+
let commit = get_commit(&client, &ctxt, repository_url, &rollup_merge.sha)
5974
.await
60-
.map_err(|e| format!("Error getting rollup merge commit '{rollup_merge}': {e:?}"))?;
75+
.map_err(|e| {
76+
format!(
77+
"Error getting rollup merge commit '{}': {e:?}",
78+
rollup_merge.sha
79+
)
80+
})?;
6181
assert!(
6282
commit.parents.len() == 2,
6383
"What we thought was a merge commit was not a merge commit. sha: {}",
64-
rollup_merge
84+
rollup_merge.sha
6585
);
6686
let rolled_up_head = &commit.parents[1].sha;
6787

@@ -87,31 +107,45 @@ async fn handle_push(ctxt: Arc<SiteCtxt>, push: github::Push) -> ServerResult<gi
87107
.await
88108
.map_err(|e| format!("Error updating the try-perf branch: {e:?}"))?;
89109

110+
prs.push((pr_num, sha));
90111
// Wait to ensure there's enough time for GitHub to checkout these changes before they are overwritten
91112
tokio::time::sleep(std::time::Duration::from_secs(15)).await
92113
}
114+
115+
// Post comment to the rollup PR with the mapping between individual PRs and the new try commits
116+
let mapping = prs
117+
.into_iter()
118+
.fold(String::new(), |mut string, (pr, commit)| {
119+
use std::fmt::Write;
120+
write!(&mut string, "#{pr}: {commit}\n").unwrap();
121+
string
122+
});
123+
let msg =
124+
format!("Try perf builds for each individual rolled up PR have been enqueued:\n{mapping}");
125+
post_comment(&ctxt.config, pr, msg).await;
93126
Ok(github::Response)
94127
}
95128

96-
async fn is_rollup(
129+
// Gets the pr number for the associated rollup PR. Returns None if this is not a rollup PR
130+
async fn rollup_pr(
97131
client: &reqwest::Client,
98132
ctxt: &SiteCtxt,
99133
repository_url: &str,
100134
push: &github::Push,
101-
) -> ServerResult<bool> {
135+
) -> ServerResult<Option<u32>> {
102136
macro_rules! get {
103137
($x:expr) => {
104138
match $x {
105139
Some(x) => x,
106-
None => return Ok(false),
140+
None => return Ok(None),
107141
}
108142
};
109143
}
110144
let is_bors =
111145
push.sender.login == "bors" && push.head_commit.message.starts_with("Auto merge of");
112146

113147
if !is_bors {
114-
return Ok(false);
148+
return Ok(None);
115149
}
116150
let captures = get!(ROLLUP_PR_NUMBER.captures(&push.head_commit.message));
117151
let number = get!(get!(captures.get(0)).as_str().parse::<u64>().ok());
@@ -120,7 +154,11 @@ async fn is_rollup(
120154
.await
121155
.map_err(|e| format!("Error fetching PR #{number} {e:?}"))?;
122156

123-
Ok(issue.labels.iter().any(|l| l.name == "rollup"))
157+
Ok(issue
158+
.labels
159+
.iter()
160+
.any(|l| l.name == "rollup")
161+
.then(|| issue.number))
124162
}
125163

126164
async fn handle_issue(

0 commit comments

Comments
 (0)