Skip to content

Commit 6ace834

Browse files
Merge pull request #1370 from rylev/fix-commit-extraction
Fix commit extraction for rust-timer commands
2 parents c24b489 + 0a439d5 commit 6ace834

File tree

1 file changed

+87
-44
lines changed

1 file changed

+87
-44
lines changed

site/src/request_handlers/github.rs

+87-44
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ use crate::load::SiteCtxt;
77

88
use std::sync::Arc;
99

10-
use regex::Regex;
10+
use regex::{Captures, Regex};
1111

1212
lazy_static::lazy_static! {
1313
static ref BODY_TRY_COMMIT: Regex =
1414
Regex::new(r#"(?:\W|^)@rust-timer\s+build\s+(\w+)(?:\W|$)(?:include=(\S+))?\s*(?:exclude=(\S+))?\s*(?:runs=(\d+))?"#).unwrap();
1515
static ref BODY_QUEUE: Regex =
1616
Regex::new(r#"(?:\W|^)@rust-timer\s+queue(?:\W|$)(?:include=(\S+))?\s*(?:exclude=(\S+))?\s*(?:runs=(\d+))?"#).unwrap();
1717
static ref BODY_MAKE_PR_FOR: Regex =
18-
Regex::new(r#"(?:\W|^)@rust-timer\s+make-pr-for\s+(\w+)(?:\W|$)"#).unwrap();
18+
Regex::new(r#"(?:\W|^)@rust-timer\s+make-pr-for\s+([\w:/\.\-]+)(?:\W|$)"#).unwrap();
1919
static ref BODY_UDPATE_PR_FOR: Regex =
20-
Regex::new(r#"(?:\W|^)@rust-timer\s+update-branch-for\s+(\w+)(?:\W|$)"#).unwrap();
20+
Regex::new(r#"(?:\W|^)@rust-timer\s+update-branch-for\s+([\w:/\.\-]+)(?:\W|$)"#).unwrap();
2121
}
2222

2323
pub async fn handle_github(
@@ -86,50 +86,93 @@ pub async fn handle_github(
8686
}
8787
}
8888

89-
let captures = BODY_MAKE_PR_FOR
90-
.captures_iter(&request.comment.body)
91-
.collect::<Vec<_>>();
92-
for capture in captures {
93-
if let Some(rollup_merge) = capture.get(1).map(|c| c.as_str().to_owned()) {
94-
let rollup_merge =
95-
rollup_merge.trim_start_matches("https://github.com/rust-lang/rust/commit/");
96-
let client = reqwest::Client::new();
97-
pr_and_try_for_rollup(
98-
&client,
99-
ctxt.clone(),
100-
&request.issue.repository_url,
101-
&rollup_merge,
102-
&request.comment.html_url,
103-
)
104-
.await
105-
.map_err(|e| format!("{:?}", e))?;
106-
}
89+
for rollup_merge in extract_make_pr_for(&request.comment.body) {
90+
let client = reqwest::Client::new();
91+
pr_and_try_for_rollup(
92+
&client,
93+
ctxt.clone(),
94+
&request.issue.repository_url,
95+
&rollup_merge,
96+
&request.comment.html_url,
97+
)
98+
.await
99+
.map_err(|e| format!("{:?}", e))?;
107100
}
108101

109-
let captures = BODY_UDPATE_PR_FOR
110-
.captures_iter(&request.comment.body)
111-
.collect::<Vec<_>>();
112-
for capture in captures {
113-
if let Some(rollup_merge) = capture.get(1).map(|c| c.as_str().to_owned()) {
114-
let rollup_merge =
115-
rollup_merge.trim_start_matches("https://github.com/rust-lang/rust/commit/");
116-
117-
// This just creates or updates the branch for this merge commit.
118-
// Intended for resolving the race condition of master merging in
119-
// between us updating the commit and merging things.
120-
let client = reqwest::Client::new();
121-
let branch =
122-
branch_for_rollup(&client, &ctxt, &request.issue.repository_url, rollup_merge)
123-
.await
124-
.map_err(|e| e.to_string())?;
125-
post_comment(
126-
&ctxt.config,
127-
request.issue.number,
128-
&format!("Master base SHA: {}", branch.master_base_sha),
129-
)
130-
.await;
131-
}
102+
for rollup_merge in extract_update_pr_for(&request.comment.body) {
103+
// This just creates or updates the branch for this merge commit.
104+
// Intended for resolving the race condition of master merging in
105+
// between us updating the commit and merging things.
106+
let client = reqwest::Client::new();
107+
let branch = branch_for_rollup(&client, &ctxt, &request.issue.repository_url, rollup_merge)
108+
.await
109+
.map_err(|e| e.to_string())?;
110+
post_comment(
111+
&ctxt.config,
112+
request.issue.number,
113+
&format!("Master base SHA: {}", branch.master_base_sha),
114+
)
115+
.await;
132116
}
133117

134118
Ok(github::Response)
135119
}
120+
121+
fn extract_make_pr_for(body: &str) -> impl Iterator<Item = &str> + '_ {
122+
BODY_MAKE_PR_FOR
123+
.captures_iter(body)
124+
.filter_map(|c| extract_rollup_merge(c))
125+
}
126+
127+
fn extract_update_pr_for(body: &str) -> impl Iterator<Item = &str> + '_ {
128+
BODY_UDPATE_PR_FOR
129+
.captures_iter(body)
130+
.filter_map(|c| extract_rollup_merge(c))
131+
}
132+
133+
fn extract_rollup_merge(capture: Captures) -> Option<&str> {
134+
capture.get(1).map(|c| {
135+
println!("{}", c.as_str());
136+
c.as_str()
137+
.trim_start_matches("https://github.com/rust-lang/rust/commit/")
138+
})
139+
}
140+
141+
#[cfg(test)]
142+
mod tests {
143+
use super::*;
144+
145+
#[test]
146+
fn captures_the_right_sha() {
147+
let message = r#"This is a message.
148+
149+
@rust-timer make-pr-for https://github.com/rust-lang/rust/commit/857afc75e6ca69cc7dcae36a6fac8c093ee6fa31
150+
@rust-timer make-pr-for https://github.com/rust-lang/rust/commit/857afc75e6ca69cc7dcae36a6fac8c093ee6fa31
151+
"#;
152+
153+
let mut iter = extract_make_pr_for(message);
154+
assert_eq!(
155+
iter.next().unwrap(),
156+
"857afc75e6ca69cc7dcae36a6fac8c093ee6fa31",
157+
"sha did not match"
158+
);
159+
assert_eq!(
160+
iter.next().unwrap(),
161+
"857afc75e6ca69cc7dcae36a6fac8c093ee6fa31",
162+
"sha did not match"
163+
);
164+
assert!(iter.next().is_none(), "there were more rollup merges");
165+
let message = r#"This is a message.
166+
167+
@rust-timer update-branch-for https://github.com/rust-lang/rust/commit/857afc75e6ca69cc7dcae36a6fac8c093ee6fa31"#;
168+
169+
let mut iter = extract_update_pr_for(message);
170+
let sha = iter.next().unwrap();
171+
println!("{sha}");
172+
assert_eq!(
173+
sha, "857afc75e6ca69cc7dcae36a6fac8c093ee6fa31",
174+
"sha did not match"
175+
);
176+
assert!(iter.next().is_none(), "there were more rollup merges");
177+
}
178+
}

0 commit comments

Comments
 (0)