From cb899fd322af4cf2e8b15587fe0133440afe6d6a Mon Sep 17 00:00:00 2001 From: Graham Christensen Date: Mon, 6 Nov 2017 12:36:47 -0500 Subject: [PATCH] rust hackin' --- ofborg/src/bin/builder.rs | 67 ++++++++++++++++++++++++++------------- ofborg/src/lib.rs | 3 ++ ofborg/src/worker.rs | 49 ++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 ofborg/src/worker.rs diff --git a/ofborg/src/bin/builder.rs b/ofborg/src/bin/builder.rs index 8e4eacc..3fa0738 100644 --- a/ofborg/src/bin/builder.rs +++ b/ofborg/src/bin/builder.rs @@ -7,35 +7,16 @@ use amqp::protocol; use amqp::Session; use amqp::Table; use std::process; +use std::io::Error; use ofborg::checkout; +use ofborg::worker; +use ofborg::worker::{Actions,StdPr,StdRepo,BuildJob}; fn main() { println!("Hello, world!"); - let cloner = checkout::cached_cloner(Path::new("/home/grahamc/.nix-test-rs")); - let project = cloner.project("NixOS/nixpkgs".to_string(), - "https://github.com/nixos/nixpkgs.git".to_string() - ); - let co = project.clone_for("builder".to_string(), - "1234".to_string()).unwrap(); - - let refpath = co.checkout_ref("origin/master".as_ref()); - co.fetch_pr(31228).unwrap(); - co.merge_commit("7214d0f6f7a6467205761f87973140727154e1b3".as_ref()).unwrap(); - - match refpath { - Ok(path) => { - println!("Got path: {:?}", path); - } - Err(wat) => { - println!("Failed to do a checkout of ref : {:?}", wat); - } - } - - - if false { let mut session = Session::open_url("amqps://grahamc:cCbKQmwnRcd8kvPW9cjmMSkp@events.nix.gsc.io//").unwrap(); let mut channel = session.open_channel(1).unwrap(); @@ -46,6 +27,21 @@ fn main() { process::exit(1); } + let cloner = checkout::cached_cloner(Path::new("/home/grahamc/.nix-test-rs")); + + channel.basic_consume( + worker::new(BuildWorker{ + cloner: cloner + }), + "my_queue_name", + "lmao1", + false, + false, + false, + false, + Table::new() + ); + if let Err(result) = channel.basic_publish("", "my_queue_name", true, false, protocol::basic::BasicProperties{ content_type: Some("text".to_string()), ..Default::default()}, (b"Hello from rust!").to_vec()) { println!("Failed to publish: {:?}", result); @@ -53,3 +49,30 @@ fn main() { } } } + +struct BuildWorker { + cloner: checkout::CachedCloner, +} + +impl worker::SimpleWorker for BuildWorker { + fn consumer(&self, job: BuildJob, resp: Actions) -> Result<(), Error> { + let project = self.cloner.project(job.repo.full_name, job.repo.clone_url); + let co = project.clone_for("builder".to_string(), + job.pr.number.to_string())?; + + let refpath = co.checkout_ref(job.pr.target_branch.as_ref()); + co.fetch_pr(job.pr.number).unwrap(); + co.merge_commit(job.pr.head_sha.as_ref()).unwrap(); + + match refpath { + Ok(path) => { + println!("Got path: {:?}", path); + } + Err(wat) => { + println!("Failed to do a checkout of ref : {:?}", wat); + } + } + + return Ok(()) + } +} diff --git a/ofborg/src/lib.rs b/ofborg/src/lib.rs index 0256ca1..8e2e17f 100644 --- a/ofborg/src/lib.rs +++ b/ofborg/src/lib.rs @@ -1,12 +1,15 @@ +extern crate amqp; extern crate fs2; extern crate md5; pub mod checkout; pub mod locks; pub mod clone; +pub mod worker; pub mod ofborg { pub use checkout; pub use locks; pub use clone; + pub use worker; } diff --git a/ofborg/src/worker.rs b/ofborg/src/worker.rs new file mode 100644 index 0000000..b47e1f7 --- /dev/null +++ b/ofborg/src/worker.rs @@ -0,0 +1,49 @@ + +use amqp::{Consumer, Channel}; +use amqp::protocol::basic::{Deliver,BasicProperties}; +use std::marker::Send; +use std::io::Error; + +pub struct Worker { + internal: T +} + +pub struct StdRepo { + pub full_name: String, + pub clone_url: String, +} + +pub struct StdPr { + pub target_branch: String, + pub number: i64, + pub head_sha: String, +} + + +pub struct BuildJob { + pub repo: StdRepo, + pub pr: StdPr, +} + + +pub struct Actions { +} + +pub trait SimpleWorker { + fn consumer(&self, job: BuildJob, resp: Actions) -> Result<(), Error>; +} + +pub fn new(worker: T) -> Worker { + return Worker{ + internal: worker, + }; +} + +impl Consumer for Worker { + fn handle_delivery(&mut self, + channel: &mut Channel, + method: Deliver, + headers: BasicProperties, + body: Vec) { + } +}