From cfbf7bc048170f481838fc3b627e72e599bc90f1 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Fri, 29 Jul 2022 18:29:55 +0200 Subject: [PATCH 01/12] Refactor github handler to be able to handle more than 1 payload kind --- site/src/api.rs | 6 +-- site/src/request_handlers/github.rs | 66 +++++++++++++++++------------ 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/site/src/api.rs b/site/src/api.rs index f5a849e4f..1ccb3167d 100644 --- a/site/src/api.rs +++ b/site/src/api.rs @@ -397,9 +397,9 @@ pub mod github { } #[derive(Debug, Clone, Serialize, Deserialize)] - pub struct Request { - pub issue: Issue, - pub comment: Comment, + #[serde(untagged)] + pub enum Request { + Issue { issue: Issue, comment: Comment }, } #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/site/src/request_handlers/github.rs b/site/src/request_handlers/github.rs index a0c5a4983..ece24f33b 100644 --- a/site/src/request_handlers/github.rs +++ b/site/src/request_handlers/github.rs @@ -25,43 +25,58 @@ pub async fn handle_github( ctxt: Arc, ) -> ServerResult { log::info!("handle_github({:?})", request); - if request.comment.body.contains(" homu: ") { - if let Some(sha) = parse_homu_comment(&request.comment.body).await { - enqueue_sha(request.issue, &ctxt, sha).await?; + match request { + github::Request::Issue { issue, comment } => handle_issue(ctxt, issue, comment).await, + } +} + +async fn handle_issue( + ctxt: Arc, + issue: github::Issue, + comment: github::Comment, +) -> ServerResult { + if comment.body.contains(" homu: ") { + if let Some(sha) = parse_homu_comment(&comment.body).await { + enqueue_sha(issue, &ctxt, sha).await?; return Ok(github::Response); } } - if !request.comment.body.contains("@rust-timer ") { - return Ok(github::Response); + if comment.body.contains("@rust-timer ") { + return handle_rust_timer(ctxt, comment, issue).await; } - if request.comment.author_association != github::Association::Owner - && !get_authorized_users() - .await? - .contains(&request.comment.user.id) + Ok(github::Response) +} + +async fn handle_rust_timer( + ctxt: Arc, + comment: github::Comment, + issue: github::Issue, +) -> ServerResult { + if comment.author_association != github::Association::Owner + && !get_authorized_users().await?.contains(&comment.user.id) { post_comment( &ctxt.config, - request.issue.number, + issue.number, "Insufficient permissions to issue commands to rust-timer.", ) .await; return Ok(github::Response); } - if let Some(captures) = BODY_QUEUE.captures(&request.comment.body) { + if let Some(captures) = BODY_QUEUE.captures(&comment.body) { let include = captures.get(1).map(|v| v.as_str()); let exclude = captures.get(2).map(|v| v.as_str()); let runs = captures.get(3).and_then(|v| v.as_str().parse::().ok()); { let conn = ctxt.conn().await; - conn.queue_pr(request.issue.number, include, exclude, runs) - .await; + conn.queue_pr(issue.number, include, exclude, runs).await; } post_comment( &ctxt.config, - request.issue.number, + issue.number, "Awaiting bors try build completion. @rustbot label: +S-waiting-on-perf", @@ -69,8 +84,7 @@ pub async fn handle_github( .await; return Ok(github::Response); } - - if let Some(captures) = BODY_TRY_COMMIT.captures(&request.comment.body) { + if let Some(captures) = BODY_TRY_COMMIT.captures(&comment.body) { if let Some(commit) = captures.get(1).map(|c| c.as_str().to_owned()) { let include = captures.get(2).map(|v| v.as_str()); let exclude = captures.get(3).map(|v| v.as_str()); @@ -78,43 +92,39 @@ pub async fn handle_github( let commit = commit.trim_start_matches("https://github.com/rust-lang/rust/commit/"); { let conn = ctxt.conn().await; - conn.queue_pr(request.issue.number, include, exclude, runs) - .await; + conn.queue_pr(issue.number, include, exclude, runs).await; } - enqueue_sha(request.issue, &ctxt, commit.to_owned()).await?; + enqueue_sha(issue, &ctxt, commit.to_owned()).await?; return Ok(github::Response); } } - - for rollup_merge in extract_make_pr_for(&request.comment.body) { + for rollup_merge in extract_make_pr_for(&comment.body) { let client = reqwest::Client::new(); pr_and_try_for_rollup( &client, ctxt.clone(), - &request.issue.repository_url, + &issue.repository_url, &rollup_merge, - &request.comment.html_url, + &comment.html_url, ) .await .map_err(|e| format!("{:?}", e))?; } - - for rollup_merge in extract_update_pr_for(&request.comment.body) { + for rollup_merge in extract_update_pr_for(&comment.body) { // This just creates or updates the branch for this merge commit. // Intended for resolving the race condition of master merging in // between us updating the commit and merging things. let client = reqwest::Client::new(); - let branch = branch_for_rollup(&client, &ctxt, &request.issue.repository_url, rollup_merge) + let branch = branch_for_rollup(&client, &ctxt, &issue.repository_url, rollup_merge) .await .map_err(|e| e.to_string())?; post_comment( &ctxt.config, - request.issue.number, + issue.number, &format!("Master base SHA: {}", branch.master_base_sha), ) .await; } - Ok(github::Response) } From f8706329c94086f153baf7781140533e56f756d4 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Sun, 31 Jul 2022 19:41:28 +0200 Subject: [PATCH 02/12] Add handler for push event --- site/src/api.rs | 39 +++++++++++ site/src/github.rs | 104 ++++++++++++++++++++++++++++ site/src/request_handlers/github.rs | 98 +++++++++++++++++++++++++- site/src/server.rs | 2 +- 4 files changed, 239 insertions(+), 4 deletions(-) diff --git a/site/src/api.rs b/site/src/api.rs index 1ccb3167d..318220704 100644 --- a/site/src/api.rs +++ b/site/src/api.rs @@ -394,12 +394,51 @@ pub mod github { pub number: u32, pub comments_url: String, pub repository_url: String, + pub labels: Vec