Skip to content

Commit

Permalink
Do not perform a merge instead post comment (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcass19 authored Jun 2, 2023
1 parent 050821a commit 555cb07
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 26 deletions.
21 changes: 0 additions & 21 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>> {
let (before, after) = if let (Some(base), Some(head)) = (&self.base, &self.head) {
Expand Down
28 changes: 25 additions & 3 deletions src/handlers/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
}
Expand All @@ -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
Expand All @@ -50,7 +57,22 @@ async fn decision_process_handler(metadata: &serde_json::Value) -> anyhow::Resul

match gh_client.json::<Issue>(request).await {
Ok(issue) => match metadata.status {
Merge => issue.merge(&gh_client).await?,
Merge => {
let users: Vec<String> = 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!(
Expand Down
14 changes: 12 additions & 2 deletions src/interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(issue: &'a Issue, users: &'a [&str], message: T) -> PingComment<'a>
where
T: Into<String>,
{
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)?;
}
Expand Down

0 comments on commit 555cb07

Please sign in to comment.