Skip to content

Commit

Permalink
Cancel job when bot is removed as a reviewer on a PR
Browse files Browse the repository at this point in the history
  • Loading branch information
roxelo committed Jun 25, 2020
1 parent 5c17ee1 commit a71697e
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 3 deletions.
75 changes: 75 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pretty_env_logger = "0.4"
serde_json = "1.0"
uuid = { version = "0.8", features = ["v4"] }
nix = "0.17"
lazy_static = "1.4"
lazy_static = "1.4"
futures = "0.3"
1 change: 0 additions & 1 deletion src/lib/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::process::{Command, Stdio};
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;
use nix::sys::signal::{self, Signal};

use crate::lib::job;

Expand Down
2 changes: 2 additions & 0 deletions src/lib/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::env;
use lazy_static::lazy_static;

pub const REVIEW_REQUESTED: &'static str = "review_requested";
pub const REVIEW_REQUEST_REMOVED: &'static str = "review_request_removed";
pub const REVIEWER: &'static str = "sexxi-bot";
// TODO(azhng): const REPO: &'static str = "rust";

Expand All @@ -17,6 +18,7 @@ pub const SEXXI_TEST_PROJECT: &'static str = "sexxi-webhook-test";

pub const SEXXI_REMOTE_HOST: &'static str = "sorbitol";
pub const SEXXI_LOG_FILE_DIR: &'static str = "www/build-logs";
pub const SEXXI_TIMEOUT_JOB_IN_SEC: u64 = 60 * 60 * 2;

lazy_static! {
pub static ref MACHINE_USER: String = env::var("USER").unwrap();
Expand Down
22 changes: 22 additions & 0 deletions src/lib/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,28 @@ async fn parse_and_handle(
return Ok::<_, hyper::Error>(gen_response(500));
};
},
config::REVIEW_REQUEST_REMOVED => {
let head_ref = json["pull_request"]["head"]["ref"].as_str().unwrap();
let mut job_registry = job_registry.write().await;

let c_job: Arc<RwLock<job::JobDesc>>;
let c_job_id: Uuid;
if let Some(c_id) = job_registry.running_jobs.get(head_ref.clone()) {
c_job_id = c_id.clone();
if let Some(j) = job_registry.jobs.get(&c_job_id) {
c_job = j.clone();
let curr_job = c_job.read().await;
if head_ref == curr_job.head_ref {
info!("Request to run job on {} has been removed. Cancelling running job.",head_ref);
// [To-Do] Fix, could potentially kill a process which is already dead
signal::kill(Pid::from_raw(curr_job.pid as i32), Signal::SIGINT);

// Remove cancelled job
job_registry.running_jobs.remove(head_ref);
}
}
}
},
_ => {
warn!("Action: {} not handled", action);
}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub async fn process_job(job_id: &Uuid, job_registry: Arc<RwLock<JobRegistry>>)
in removing a running job not the canceled job.
*/
if job.status != JobStatus::Canceled {
info!("Removing job from job_registry!");
{
let mut job_registry = job_registry.write().await;
job_registry.running_jobs.remove(&job.head_ref.clone());
Expand Down Expand Up @@ -218,11 +219,11 @@ async fn run_and_build(job: &JobDesc, job_registry: &Arc<RwLock<JobRegistry>>) -
Ok(())
}


async fn start_build_job(job: &JobDesc, job_registry: &Arc<RwLock<JobRegistry>>) -> Result<(), String> {
let comment = format!("{}, job id: {}", config::COMMENT_JOB_START, &job.id);
if let Err(e) = api::post_comment(&comment, job.pr_num).await {
return Err(format!("failed to post comment to pr {}: {}", &job.pr_num, e));
}

run_and_build(job, job_registry).await
}

0 comments on commit a71697e

Please sign in to comment.