From 555cb074e2bfab1786cbb1f175b372c4d683db93 Mon Sep 17 00:00:00 2001 From: Mauricio Cassola Date: Fri, 2 Jun 2023 15:31:27 +0200 Subject: [PATCH] Do not perform a merge instead post comment (#12) --- src/github.rs | 21 --------------------- src/handlers/jobs.rs | 28 +++++++++++++++++++++++++--- src/interactions.rs | 14 ++++++++++++-- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/github.rs b/src/github.rs index 4bf0e2c2..8b9dafbd 100644 --- a/src/github.rs +++ b/src/github.rs @@ -779,27 +779,6 @@ impl Issue { Ok(()) } - pub async fn merge(&self, client: &GithubClient) -> anyhow::Result<()> { - let merge_url = format!("{}/pulls/{}/merge", self.repository().url(), self.number); - - // change defaults by reading from somewhere, maybe in .toml? - #[derive(serde::Serialize)] - struct MergeIssue<'a> { - commit_title: &'a str, - merge_method: &'a str, - } - - client - ._send_req(client.put(&merge_url).json(&MergeIssue { - commit_title: "Merged by the bot!", - merge_method: "merge", - })) - .await - .context("failed to merge issue")?; - - Ok(()) - } - /// Returns the diff in this event, for Open and Synchronize events for now. pub async fn diff(&self, client: &GithubClient) -> anyhow::Result> { let (before, after) = if let (Some(base), Some(head)) = (&self.base, &self.head) { diff --git a/src/handlers/jobs.rs b/src/handlers/jobs.rs index 480b1c29..33d25c08 100644 --- a/src/handlers/jobs.rs +++ b/src/handlers/jobs.rs @@ -4,10 +4,13 @@ // Further info could be find in src/jobs.rs use super::Context; +use crate::db::issue_decision_state::get_issue_decision_state; use crate::github::*; use crate::handlers::decision::{DecisionProcessActionMetadata, DECISION_PROCESS_JOB_NAME}; +use crate::interactions::PingComment; use parser::command::decision::Resolution::{Hold, Merge}; use reqwest::Client; +use tokio_postgres::Client as DbClient; use tracing as log; pub async fn handle_job( @@ -22,7 +25,8 @@ pub async fn handle_job( Ok(()) } matched_name if *matched_name == DECISION_PROCESS_JOB_NAME.to_string() => { - decision_process_handler(&metadata).await + let db = ctx.db.get().await; + decision_process_handler(&db, &metadata).await } _ => default(&name, &metadata), } @@ -38,7 +42,10 @@ fn default(name: &String, metadata: &serde_json::Value) -> anyhow::Result<()> { Ok(()) } -async fn decision_process_handler(metadata: &serde_json::Value) -> anyhow::Result<()> { +async fn decision_process_handler( + db: &DbClient, + metadata: &serde_json::Value, +) -> anyhow::Result<()> { tracing::trace!( "handle_job fell into decision process case: (metadata={:?})", metadata @@ -50,7 +57,22 @@ async fn decision_process_handler(metadata: &serde_json::Value) -> anyhow::Resul match gh_client.json::(request).await { Ok(issue) => match metadata.status { - Merge => issue.merge(&gh_client).await?, + Merge => { + let users: Vec = get_issue_decision_state(&db, &issue.number) + .await + .unwrap() + .current + .into_keys() + .collect(); + let users_ref: Vec<&str> = users.iter().map(|x| x.as_ref()).collect(); + + let cmnt = PingComment::new( + &issue, + &users_ref, + "The final comment period has resolved, with a decision to **merge**. Ping involved once again.", + ); + cmnt.post(&gh_client).await?; + } Hold => issue.close(&gh_client).await?, }, Err(e) => log::error!( diff --git a/src/interactions.rs b/src/interactions.rs index f3d6a115..e78050d7 100644 --- a/src/interactions.rs +++ b/src/interactions.rs @@ -33,15 +33,25 @@ impl<'a> ErrorComment<'a> { pub struct PingComment<'a> { issue: &'a Issue, users: &'a [&'a str], + message: String, } impl<'a> PingComment<'a> { - pub fn new(issue: &'a Issue, users: &'a [&str]) -> PingComment<'a> { - PingComment { issue, users } + pub fn new(issue: &'a Issue, users: &'a [&str], message: T) -> PingComment<'a> + where + T: Into, + { + PingComment { + issue, + users, + message: message.into(), + } } pub async fn post(&self, client: &GithubClient) -> anyhow::Result<()> { let mut body = String::new(); + writeln!(body, "{}", self.message)?; + writeln!(body)?; for user in self.users { write!(body, "@{} ", user)?; }