Skip to content

Commit cfbf7bc

Browse files
committed
Refactor github handler to be able to handle more than 1 payload kind
1 parent f365f30 commit cfbf7bc

File tree

2 files changed

+41
-31
lines changed

2 files changed

+41
-31
lines changed

site/src/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,9 @@ pub mod github {
397397
}
398398

399399
#[derive(Debug, Clone, Serialize, Deserialize)]
400-
pub struct Request {
401-
pub issue: Issue,
402-
pub comment: Comment,
400+
#[serde(untagged)]
401+
pub enum Request {
402+
Issue { issue: Issue, comment: Comment },
403403
}
404404

405405
#[derive(Debug, Clone, Serialize, Deserialize)]

site/src/request_handlers/github.rs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,96 +25,106 @@ pub async fn handle_github(
2525
ctxt: Arc<SiteCtxt>,
2626
) -> ServerResult<github::Response> {
2727
log::info!("handle_github({:?})", request);
28-
if request.comment.body.contains(" homu: ") {
29-
if let Some(sha) = parse_homu_comment(&request.comment.body).await {
30-
enqueue_sha(request.issue, &ctxt, sha).await?;
28+
match request {
29+
github::Request::Issue { issue, comment } => handle_issue(ctxt, issue, comment).await,
30+
}
31+
}
32+
33+
async fn handle_issue(
34+
ctxt: Arc<SiteCtxt>,
35+
issue: github::Issue,
36+
comment: github::Comment,
37+
) -> ServerResult<github::Response> {
38+
if comment.body.contains(" homu: ") {
39+
if let Some(sha) = parse_homu_comment(&comment.body).await {
40+
enqueue_sha(issue, &ctxt, sha).await?;
3141
return Ok(github::Response);
3242
}
3343
}
3444

35-
if !request.comment.body.contains("@rust-timer ") {
36-
return Ok(github::Response);
45+
if comment.body.contains("@rust-timer ") {
46+
return handle_rust_timer(ctxt, comment, issue).await;
3747
}
3848

39-
if request.comment.author_association != github::Association::Owner
40-
&& !get_authorized_users()
41-
.await?
42-
.contains(&request.comment.user.id)
49+
Ok(github::Response)
50+
}
51+
52+
async fn handle_rust_timer(
53+
ctxt: Arc<SiteCtxt>,
54+
comment: github::Comment,
55+
issue: github::Issue,
56+
) -> ServerResult<github::Response> {
57+
if comment.author_association != github::Association::Owner
58+
&& !get_authorized_users().await?.contains(&comment.user.id)
4359
{
4460
post_comment(
4561
&ctxt.config,
46-
request.issue.number,
62+
issue.number,
4763
"Insufficient permissions to issue commands to rust-timer.",
4864
)
4965
.await;
5066
return Ok(github::Response);
5167
}
5268

53-
if let Some(captures) = BODY_QUEUE.captures(&request.comment.body) {
69+
if let Some(captures) = BODY_QUEUE.captures(&comment.body) {
5470
let include = captures.get(1).map(|v| v.as_str());
5571
let exclude = captures.get(2).map(|v| v.as_str());
5672
let runs = captures.get(3).and_then(|v| v.as_str().parse::<i32>().ok());
5773
{
5874
let conn = ctxt.conn().await;
59-
conn.queue_pr(request.issue.number, include, exclude, runs)
60-
.await;
75+
conn.queue_pr(issue.number, include, exclude, runs).await;
6176
}
6277
post_comment(
6378
&ctxt.config,
64-
request.issue.number,
79+
issue.number,
6580
"Awaiting bors try build completion.
6681
6782
@rustbot label: +S-waiting-on-perf",
6883
)
6984
.await;
7085
return Ok(github::Response);
7186
}
72-
73-
if let Some(captures) = BODY_TRY_COMMIT.captures(&request.comment.body) {
87+
if let Some(captures) = BODY_TRY_COMMIT.captures(&comment.body) {
7488
if let Some(commit) = captures.get(1).map(|c| c.as_str().to_owned()) {
7589
let include = captures.get(2).map(|v| v.as_str());
7690
let exclude = captures.get(3).map(|v| v.as_str());
7791
let runs = captures.get(4).and_then(|v| v.as_str().parse::<i32>().ok());
7892
let commit = commit.trim_start_matches("https://github.com/rust-lang/rust/commit/");
7993
{
8094
let conn = ctxt.conn().await;
81-
conn.queue_pr(request.issue.number, include, exclude, runs)
82-
.await;
95+
conn.queue_pr(issue.number, include, exclude, runs).await;
8396
}
84-
enqueue_sha(request.issue, &ctxt, commit.to_owned()).await?;
97+
enqueue_sha(issue, &ctxt, commit.to_owned()).await?;
8598
return Ok(github::Response);
8699
}
87100
}
88-
89-
for rollup_merge in extract_make_pr_for(&request.comment.body) {
101+
for rollup_merge in extract_make_pr_for(&comment.body) {
90102
let client = reqwest::Client::new();
91103
pr_and_try_for_rollup(
92104
&client,
93105
ctxt.clone(),
94-
&request.issue.repository_url,
106+
&issue.repository_url,
95107
&rollup_merge,
96-
&request.comment.html_url,
108+
&comment.html_url,
97109
)
98110
.await
99111
.map_err(|e| format!("{:?}", e))?;
100112
}
101-
102-
for rollup_merge in extract_update_pr_for(&request.comment.body) {
113+
for rollup_merge in extract_update_pr_for(&comment.body) {
103114
// This just creates or updates the branch for this merge commit.
104115
// Intended for resolving the race condition of master merging in
105116
// between us updating the commit and merging things.
106117
let client = reqwest::Client::new();
107-
let branch = branch_for_rollup(&client, &ctxt, &request.issue.repository_url, rollup_merge)
118+
let branch = branch_for_rollup(&client, &ctxt, &issue.repository_url, rollup_merge)
108119
.await
109120
.map_err(|e| e.to_string())?;
110121
post_comment(
111122
&ctxt.config,
112-
request.issue.number,
123+
issue.number,
113124
&format!("Master base SHA: {}", branch.master_base_sha),
114125
)
115126
.await;
116127
}
117-
118128
Ok(github::Response)
119129
}
120130

0 commit comments

Comments
 (0)