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 7, 2020
1 parent 524cd74 commit 6d6fb0f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 38 deletions.
50 changes: 50 additions & 0 deletions ofborg/src/ghrepo.rs
Original file line number Diff line number Diff line change
@@ -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<Status> {
self.repo.statuses().create(sha, status)
}

pub fn create_checkrun(&self, check: &CheckRunOptions) -> hubcaps::Result<CheckRun> {
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,
)
}
}
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
68 changes: 30 additions & 38 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,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(
Expand Down Expand Up @@ -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)
Expand All @@ -253,15 +251,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 +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<dyn eval::EvaluationStrategy> = if job.is_nixpkgs() {
Box::new(eval::NixpkgsStrategy::new(
&job,
Expand All @@ -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)?;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)?;
Expand All @@ -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<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 6d6fb0f

Please sign in to comment.