diff --git a/ofborg/src/ghrepo.rs b/ofborg/src/ghrepo.rs new file mode 100644 index 00000000..d4700574 --- /dev/null +++ b/ofborg/src/ghrepo.rs @@ -0,0 +1,50 @@ +use crate::commitstatus; +use crate::message; + +use hubcaps::checks::{CheckRun, CheckRunOptions}; +use hubcaps::issues::IssueRef; +use hubcaps::repositories::Repository; +use hubcaps::statuses::{Status, StatusOptions}; +use hubcaps::Github; + +pub struct Client<'a> { + repo: Repository<'a>, +} + +impl<'a> Client<'a> { + pub fn new(github: &'a Github, repo: &message::Repo) -> Self { + let repo = github.repo(repo.owner.clone(), repo.name.clone()); + Client { repo } + } + + pub fn get_repo(&self) -> &Repository<'a> { + &self.repo + } + + pub fn get_issue_ref(&self, number: u64) -> IssueRef { + self.repo.issue(number) + } + + pub fn create_status(&self, sha: &str, status: &StatusOptions) -> hubcaps::Result { + self.repo.statuses().create(sha, status) + } + + pub fn create_checkrun(&self, check: &CheckRunOptions) -> hubcaps::Result { + self.repo.checkruns().create(&check) + } + + pub fn create_commitstatus( + &self, + pr: &message::Pr, + context: String, + description: String, + ) -> commitstatus::CommitStatus { + commitstatus::CommitStatus::new( + self.repo.statuses(), + pr.head_sha.clone(), + context, + description, + None, + ) + } +} diff --git a/ofborg/src/lib.rs b/ofborg/src/lib.rs index 35928c3c..28c687d4 100644 --- a/ofborg/src/lib.rs +++ b/ofborg/src/lib.rs @@ -28,6 +28,7 @@ pub mod easylapin; pub mod evalchecker; pub mod files; pub mod ghevent; +pub mod ghrepo; pub mod locks; pub mod maintainers; pub mod message; diff --git a/ofborg/src/tasks/evaluate.rs b/ofborg/src/tasks/evaluate.rs index 82c962c4..37cd42bb 100644 --- a/ofborg/src/tasks/evaluate.rs +++ b/ofborg/src/tasks/evaluate.rs @@ -1,9 +1,10 @@ /// This is what evaluates every pull-request use crate::acl::ACL; use crate::checkout; -use crate::commitstatus::{CommitStatus, CommitStatusError}; +use crate::commitstatus::CommitStatusError; use crate::config::GithubAppVendingMachine; use crate::files::file_to_str; +use crate::ghrepo; use crate::message::{buildjob, evaluationjob}; use crate::nix; use crate::stats::{self, Event}; @@ -108,8 +109,7 @@ impl worker::SimpleWorker for EvaluationWorker } struct OneEval<'a, E> { - client_app: &'a hubcaps::Github, - repo: hubcaps::repositories::Repository<'a>, + repo_client: ghrepo::Client<'a>, gists: Gists<'a>, nix: &'a nix::Nix, acl: &'a ACL, @@ -135,10 +135,9 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { ) -> OneEval<'a, E> { let gists = client_legacy.gists(); - let repo = client_app.repo(job.repo.owner.clone(), job.repo.name.clone()); + let repo_client = ghrepo::Client::new(client_app, &job.repo); OneEval { - client_app, - repo, + repo_client, gists, nix, acl, @@ -183,11 +182,10 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { &self.job.pr.number, &self.job.pr.head_sha, &description ); - self.repo - .statuses() - .create(&self.job.pr.head_sha, &builder.build()) - .map(|_| ()) - .map_err(|e| CommitStatusError::from(e)) + self.repo_client + .create_status(&self.job.pr.head_sha, &builder.build()) + .map_err(|e| CommitStatusError::from(e))?; + Ok(()) } fn make_gist( @@ -241,7 +239,7 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { "Internal error writing commit status: {:?}, marking internal error", cswerr ); - let issue_ref = self.repo.issue(self.job.pr.number); + let issue_ref = self.repo_client.get_issue_ref(self.job.pr.number); update_labels(&issue_ref, &[String::from("ofborg-internal-error")], &[]); self.actions().skip(&self.job) @@ -253,15 +251,10 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { #[allow(clippy::cognitive_complexity)] fn evaluate_job(&mut self) -> Result { let job = self.job; - let repo = self - .client_app - .repo(self.job.repo.owner.clone(), self.job.repo.name.clone()); - let pulls = repo.pulls(); - let pull = pulls.get(job.pr.number); - let issue_ref = repo.issue(job.pr.number); let issue: Issue; let auto_schedule_build_archs: Vec; + let issue_ref = self.repo_client.get_issue_ref(job.pr.number); match issue_ref.get() { Ok(iss) => { if iss.state == "closed" { @@ -290,6 +283,11 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { } }; + // TODO don't pass hubcaps types directly + let repo = self.repo_client.get_repo(); + let pulls = repo.pulls(); + let pull = pulls.get(job.pr.number); + let mut evaluation_strategy: Box = if job.is_nixpkgs() { Box::new(eval::NixpkgsStrategy::new( &job, @@ -305,12 +303,10 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { Box::new(eval::GenericStrategy::new()) }; - let mut overall_status = CommitStatus::new( - repo.statuses(), - job.pr.head_sha.clone(), - "grahamcofborg-eval".to_owned(), - "Starting".to_owned(), - None, + let mut overall_status = self.repo_client.create_commitstatus( + &job.pr, + "grahamcofborg-eval".to_string(), + "Starting".to_string(), ); overall_status.set_with_description("Starting", hubcaps::statuses::State::Pending)?; @@ -389,13 +385,9 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { .evaluation_checks() .into_iter() .map(|check| { - let mut status = CommitStatus::new( - repo.statuses(), - job.pr.head_sha.clone(), - check.name(), - check.cli_cmd(), - None, - ); + let mut status = + self.repo_client + .create_commitstatus(&job.pr, check.name(), check.cli_cmd()); status .set(hubcaps::statuses::State::Pending) @@ -438,7 +430,7 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { let complete = evaluation_strategy .all_evaluations_passed(&Path::new(&refpath), &mut overall_status)?; - send_check_statuses(complete.checks, &repo); + self.send_check_statuses(complete.checks); response.extend(schedule_builds(complete.builds, auto_schedule_build_archs)); overall_status.set_with_description("^.^!", hubcaps::statuses::State::Success)?; @@ -452,13 +444,13 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> { info!("Evaluations done!"); Ok(self.actions().done(&job, response)) } -} -fn send_check_statuses(checks: Vec, repo: &hubcaps::repositories::Repository) { - for check in checks { - match repo.checkruns().create(&check) { - Ok(_) => debug!("Sent check update"), - Err(e) => warn!("Failed to send check update: {:?}", e), + fn send_check_statuses(&self, checks: Vec) { + for check in checks { + match self.repo_client.create_checkrun(&check) { + Ok(_) => debug!("Sent check update"), + Err(e) => warn!("Failed to send check update: {:?}", e), + } } } }