Skip to content

Commit

Permalink
eval: partially split up github interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
LnL7 committed Nov 4, 2020
1 parent 524cd74 commit 685fd94
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 34 deletions.
46 changes: 46 additions & 0 deletions ofborg/src/ghrepo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::commitstatus;
use crate::message;

use hubcaps::checks::CheckRunOptions;
use hubcaps::issues::IssueRef;
use hubcaps::repositories::Repository;
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_commitstatus(
&self,
pr: &message::Pr,
context: String,
description: String,
) -> commitstatus::CommitStatus {
commitstatus::CommitStatus::new(
self.repo.statuses(),
pr.head_sha.clone(),
context,
description,
None,
)
}

pub fn create_checkrun(&self, check: &CheckRunOptions) -> Result<(), hubcaps::Error> {
self.repo.checkruns().create(&check)?;
Ok(())
}
}
1 change: 1 addition & 0 deletions ofborg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
63 changes: 29 additions & 34 deletions ofborg/src/tasks/evaluate.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -108,8 +109,7 @@ impl<E: stats::SysEvents + 'static> worker::SimpleWorker for EvaluationWorker<E>
}

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,
Expand All @@ -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,
Expand Down Expand Up @@ -183,7 +182,9 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> {
&self.job.pr.number, &self.job.pr.head_sha, &description
);

self.repo
// TODO refactor
self.repo_client
.get_repo()
.statuses()
.create(&self.job.pr.head_sha, &builder.build())
.map(|_| ())
Expand Down Expand Up @@ -241,7 +242,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)
Expand All @@ -253,15 +254,10 @@ impl<'a, E: stats::SysEvents + 'static> OneEval<'a, E> {
#[allow(clippy::cognitive_complexity)]
fn evaluate_job(&mut self) -> Result<worker::Actions, EvalWorkerError> {
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<systems::System>;

let issue_ref = self.repo_client.get_issue_ref(job.pr.number);
match issue_ref.get() {
Ok(iss) => {
if iss.state == "closed" {
Expand Down Expand Up @@ -290,6 +286,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<dyn eval::EvaluationStrategy> = if job.is_nixpkgs() {
Box::new(eval::NixpkgsStrategy::new(
&job,
Expand All @@ -305,12 +306,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)?;
Expand Down Expand Up @@ -389,13 +388,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)
Expand Down Expand Up @@ -438,7 +433,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)?;
Expand All @@ -452,13 +447,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<CheckRunOptions>, 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<CheckRunOptions>) {
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),
}
}
}
}
Expand Down

0 comments on commit 685fd94

Please sign in to comment.