This commit is contained in:
Graham Christensen 2019-04-11 23:07:00 -04:00
parent 6cce1a6e62
commit 37484b7763
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C
3 changed files with 45 additions and 34 deletions

View file

@ -3,12 +3,12 @@ use hyper::net::HttpsConnector;
use hyper::Client; use hyper::Client;
use hyper_native_tls::NativeTlsClient; use hyper_native_tls::NativeTlsClient;
use nix::Nix; use nix::Nix;
use ofborg::acl;
use serde_json; use serde_json;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use ofborg::acl;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct Config { pub struct Config {
@ -114,8 +114,7 @@ impl Config {
) )
} }
pub fn github_app_vendingmachine(&self) -> GithubAppVendingMachine pub fn github_app_vendingmachine(&self) -> GithubAppVendingMachine {
{
GithubAppVendingMachine { GithubAppVendingMachine {
conf: self.github_app.clone().unwrap(), conf: self.github_app.clone().unwrap(),
id_cache: HashMap::new(), id_cache: HashMap::new(),
@ -167,50 +166,58 @@ pub fn load(filename: &Path) -> Config {
pub struct GithubAppVendingMachine { pub struct GithubAppVendingMachine {
conf: GithubAppConfig, conf: GithubAppConfig,
id_cache: HashMap<(String, String), i32>, id_cache: HashMap<(String, String), Option<i32>>,
client_cache: HashMap<i32, Github>, client_cache: HashMap<i32, Github>,
} }
impl GithubAppVendingMachine { impl GithubAppVendingMachine {
pub fn for_repo<'a>(&'a mut self, owner: &str, repo: &str) -> Result<&'a Github, hubcaps::Error> { fn useragent(&self) -> &'static str {
let useragent = "github.com/grahamc/ofborg (app)"; "github.com/grahamc/ofborg (app)"
let jwt = JWTCredentials::new(self.conf.app_id, }
self.conf.private_key.clone());
let install_id: i32; fn jwt(&self) -> JWTCredentials {
JWTCredentials::new(self.conf.app_id, self.conf.private_key.clone())
}
fn install_id_for_repo(&mut self, owner: &str, repo: &str) -> Option<i32> {
let useragent = self.useragent();
let jwt = self.jwt();
let key = (owner.to_owned(), repo.to_owned()); let key = (owner.to_owned(), repo.to_owned());
if self.id_cache.contains_key(&key) {
install_id = *self.id_cache.get(&key).unwrap(); *self.id_cache.entry(key).or_insert_with(|| {
debug!("Found install ID for {:?} in cache", key);
} else {
info!("Looking up install ID for {}/{}", owner, repo); info!("Looking up install ID for {}/{}", owner, repo);
let lookup_gh = Github::new( let lookup_gh = Github::new(
useragent, useragent,
Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())), Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())),
Credentials::JWT(jwt.clone()) Credentials::JWT(jwt),
); );
install_id = lookup_gh match lookup_gh.app().find_repo_installation(owner, repo) {
.app() Ok(install_id) => {
.find_repo_installation(owner, repo)?.id; debug!("Received install ID {:#?}", install_id);
self.id_cache.insert(key, install_id); Some(install_id.id)
debug!("Received install ID {}", install_id); }
Err(e) => {
warn!("Error during install ID lookup: {:#?}", e);
None
}
}
})
} }
if ! self.client_cache.contains_key(&install_id) { pub fn for_repo<'a>(&'a mut self, owner: &str, repo: &str) -> Option<&'a Github> {
let new_client = Github::new( let useragent = self.useragent();
let jwt = self.jwt();
let install_id = self.install_id_for_repo(owner, repo)?;
Some(self.client_cache.entry(install_id).or_insert_with(|| {
Github::new(
useragent, useragent,
Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())), Client::with_connector(HttpsConnector::new(NativeTlsClient::new().unwrap())),
Credentials::InstallationToken(InstallationTokenGenerator::new( Credentials::InstallationToken(InstallationTokenGenerator::new(install_id, jwt)),
install_id, )
jwt }))
)),
);
self.client_cache.insert(install_id, new_client);
}
Ok(self.client_cache.get(&install_id).unwrap())
} }
} }

View file

@ -321,7 +321,10 @@ impl<E: stats::SysEvents + 'static> worker::SimpleWorker for EvaluationWorker<E>
.all_evaluations_passed(&Path::new(&refpath), &mut overall_status); .all_evaluations_passed(&Path::new(&refpath), &mut overall_status);
match ret { match ret {
Ok(builds) => { Ok(builds) => {
info!("Scheduling build jobs {:#?} on arches {:#?}", builds, auto_schedule_build_archs); info!(
"Scheduling build jobs {:#?} on arches {:#?}",
builds, auto_schedule_build_archs
);
for buildjob in builds { for buildjob in builds {
for arch in auto_schedule_build_archs.iter() { for arch in auto_schedule_build_archs.iter() {
let (exchange, routingkey) = arch.as_build_destination(); let (exchange, routingkey) = arch.as_build_destination();

View file

@ -7,10 +7,10 @@ use amqp::protocol::basic::{BasicProperties, Deliver};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hubcaps::checks::{CheckRunOptions, CheckRunState, Conclusion, Output}; use hubcaps::checks::{CheckRunOptions, CheckRunState, Conclusion, Output};
use message::buildjob::{BuildJob, QueuedBuildJobs}; use message::buildjob::{BuildJob, QueuedBuildJobs};
use ofborg::config::GithubAppVendingMachine;
use ofborg::message::buildresult::{BuildResult, BuildStatus, LegacyBuildResult}; use ofborg::message::buildresult::{BuildResult, BuildStatus, LegacyBuildResult};
use ofborg::message::Repo; use ofborg::message::Repo;
use ofborg::worker; use ofborg::worker;
use ofborg::config::GithubAppVendingMachine;
pub struct GitHubCommentPoster { pub struct GitHubCommentPoster {
github_vend: GithubAppVendingMachine, github_vend: GithubAppVendingMachine,
@ -77,7 +77,8 @@ impl worker::SimpleWorker for GitHubCommentPoster {
println!(":{:?}", check); println!(":{:?}", check);
let check_create_attempt = self let check_create_attempt = self
.github_vend.for_repo(&repo.owner, &repo.name) .github_vend
.for_repo(&repo.owner, &repo.name)
.unwrap() .unwrap()
.repo(repo.owner.clone(), repo.name.clone()) .repo(repo.owner.clone(), repo.name.clone())
.checkruns() .checkruns()