Skip to content

Commit 207242e

Browse files
committed
make the github bot optional
1 parent c414d73 commit 207242e

File tree

9 files changed

+211
-135
lines changed

9 files changed

+211
-135
lines changed

src/server/auth.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::config::Config;
22
use crate::prelude::*;
33
use crate::server::github::{GitHub, GitHubApi};
4-
use crate::server::{Data, HttpError};
4+
use crate::server::{Data, GithubData, HttpError};
55
use http::header::{HeaderMap, AUTHORIZATION, USER_AGENT};
66
use regex::Regex;
77
use rust_team_data::v1 as team_data;
@@ -98,7 +98,7 @@ pub struct ACL {
9898
}
9999

100100
impl ACL {
101-
pub fn new(config: &Config, github: &GitHubApi) -> Fallible<Self> {
101+
pub fn new(config: &Config, github: Option<&GithubData>) -> Fallible<Self> {
102102
let mut users = Vec::new();
103103
let mut teams = Vec::new();
104104

@@ -119,7 +119,9 @@ impl ACL {
119119
teams,
120120
};
121121

122-
acl.refresh_cache(github)?;
122+
if let Some(github) = github {
123+
acl.refresh_cache(&github.github)?;
124+
}
123125
Ok(acl)
124126
}
125127

src/server/github.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::prelude::*;
2-
use crate::server::tokens::Tokens;
2+
use crate::server::tokens::BotTokens;
33
use crate::utils;
44
use http::header::AUTHORIZATION;
55
use http::Method;
@@ -32,9 +32,9 @@ pub struct GitHubApi {
3232
}
3333

3434
impl GitHubApi {
35-
pub fn new(tokens: &Tokens) -> Self {
35+
pub fn new(tokens: &BotTokens) -> Self {
3636
GitHubApi {
37-
token: tokens.bot.api_token.clone(),
37+
token: tokens.api_token.clone(),
3838
}
3939
}
4040

src/server/messages.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::prelude::*;
22
use crate::server::github::GitHub;
3-
use crate::server::Data;
3+
use crate::server::{Data, GithubData};
44

55
pub enum Label {
66
ExperimentQueued,
@@ -48,7 +48,7 @@ impl Message {
4848
self
4949
}
5050

51-
pub fn send(mut self, issue_url: &str, data: &Data) -> Fallible<()> {
51+
pub fn send(mut self, issue_url: &str, data: &Data, github_data: &GithubData) -> Fallible<()> {
5252
// Always add a note at the bottom explaining what this is
5353
self = self.note(
5454
"information_source",
@@ -67,7 +67,7 @@ impl Message {
6767
message.push_str(&format!("\n:{}: {}", line.emoji, line.content));
6868
}
6969

70-
data.github.post_comment(issue_url, &message)?;
70+
github_data.github.post_comment(issue_url, &message)?;
7171

7272
if let Some(label) = self.new_label {
7373
let label = match label {
@@ -78,18 +78,20 @@ impl Message {
7878
// Remove all the labels matching the provided regex
7979
// If the label is already present don't reapply it though
8080
let regex = &data.config.server.labels.remove;
81-
let current_labels = data.github.list_labels(issue_url)?;
81+
let current_labels = github_data.github.list_labels(issue_url)?;
8282
let mut label_already_present = false;
8383
for current_label in &current_labels {
8484
if current_label.name == *label {
8585
label_already_present = true;
8686
} else if regex.is_match(&current_label.name) {
87-
data.github.remove_label(issue_url, &current_label.name)?;
87+
github_data
88+
.github
89+
.remove_label(issue_url, &current_label.name)?;
8890
}
8991
}
9092

9193
if !label_already_present {
92-
data.github.add_label(issue_url, label)?;
94+
github_data.github.add_label(issue_url, label)?;
9395
}
9496
}
9597

src/server/mod.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::prelude::*;
1616
use crate::server::agents::Agents;
1717
use crate::server::auth::ACL;
1818
use crate::server::github::{GitHub, GitHubApi};
19-
use crate::server::tokens::Tokens;
19+
use crate::server::tokens::{BotTokens, Tokens};
2020
use http::{self, header::HeaderValue, Response};
2121
use hyper::Body;
2222
use metrics::Metrics;
@@ -39,9 +39,7 @@ pub enum HttpError {
3939

4040
#[derive(Clone)]
4141
pub struct Data {
42-
pub bot_username: String,
4342
pub config: Config,
44-
pub github: GitHubApi,
4543
pub tokens: Tokens,
4644
pub agents: Agents,
4745
pub db: Database,
@@ -50,21 +48,37 @@ pub struct Data {
5048
pub metrics: Metrics,
5149
}
5250

51+
#[derive(Clone)]
52+
pub struct GithubData {
53+
pub bot_username: String,
54+
pub github: GitHubApi,
55+
pub tokens: BotTokens,
56+
}
57+
5358
pub fn run(config: Config, bind: SocketAddr) -> Fallible<()> {
5459
let db = Database::open()?;
5560
let tokens = tokens::Tokens::load()?;
56-
let github = GitHubApi::new(&tokens);
61+
let github_data = tokens
62+
.bot
63+
.as_ref()
64+
.cloned()
65+
.map(|tokens| {
66+
let github = GitHubApi::new(&tokens);
67+
let bot_username = github.username()?;
68+
info!("bot username: {}", bot_username);
69+
Fallible::Ok(GithubData {
70+
github,
71+
bot_username,
72+
tokens,
73+
})
74+
})
75+
.transpose()?;
5776
let agents = Agents::new(db.clone(), &tokens)?;
58-
let bot_username = github.username()?;
59-
let acl = ACL::new(&config, &github)?;
77+
let acl = ACL::new(&config, github_data.as_ref())?;
6078
let metrics = Metrics::new()?;
6179

62-
info!("bot username: {}", bot_username);
63-
6480
let data = Data {
65-
bot_username,
6681
config,
67-
github,
6882
tokens,
6983
agents,
7084
db,
@@ -75,18 +89,26 @@ pub fn run(config: Config, bind: SocketAddr) -> Fallible<()> {
7589

7690
let mutex = Arc::new(Mutex::new(data.clone()));
7791

78-
data.reports_worker.spawn(data.clone());
92+
data.reports_worker.spawn(data.clone(), github_data.clone());
7993
cronjobs::spawn(data.clone());
8094

8195
info!("running server on {}...", bind);
8296

8397
let data = Arc::new(data);
98+
let github_data = github_data.map(Arc::new);
8499

85100
let routes = warp::any()
86101
.and(
87102
warp::any()
88-
.and(warp::path("webhooks").and(routes::webhooks::routes(data.clone())))
89-
.or(warp::path("agent-api").and(routes::agent::routes(data.clone(), mutex)))
103+
.and(
104+
warp::path("webhooks")
105+
.and(routes::webhooks::routes(data.clone(), github_data.clone())),
106+
)
107+
.or(warp::path("agent-api").and(routes::agent::routes(
108+
data.clone(),
109+
mutex,
110+
github_data,
111+
)))
90112
.unify()
91113
.or(warp::path("metrics").and(routes::metrics::routes(data.clone())))
92114
.unify()

src/server/reports.rs

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::prelude::*;
33
use crate::report::{self, Comparison, TestResults};
44
use crate::results::DatabaseDB;
55
use crate::server::messages::{Label, Message};
6-
use crate::server::Data;
6+
use crate::server::{Data, GithubData};
77
use crate::utils;
88
use rusoto_core::request::HttpClient;
99
use rusoto_s3::S3Client;
@@ -32,7 +32,7 @@ fn generate_report(data: &Data, ex: &Experiment, results: &DatabaseDB) -> Fallib
3232
Ok(res)
3333
}
3434

35-
fn reports_thread(data: &Data) -> Fallible<()> {
35+
fn reports_thread(data: &Data, github_data: Option<&GithubData>) -> Fallible<()> {
3636
let timeout = Duration::from_secs(AUTOMATIC_THREAD_WAKEUP);
3737
let results = DatabaseDB::new(&data.db);
3838

@@ -57,8 +57,9 @@ fn reports_thread(data: &Data) -> Fallible<()> {
5757
error!("failed to generate the report of {}", name);
5858
utils::report_failure(&err);
5959

60-
if let Some(ref github_issue) = ex.github_issue {
61-
Message::new()
60+
if let Some(github_data) = github_data {
61+
if let Some(ref github_issue) = ex.github_issue {
62+
Message::new()
6263
.line(
6364
"rotating_light",
6465
format!("Report generation of **`{}`** failed: {}", name, err),
@@ -71,7 +72,8 @@ fn reports_thread(data: &Data) -> Fallible<()> {
7172
"sos",
7273
"Can someone from the infra team check in on this? @rust-lang/infra",
7374
)
74-
.send(&github_issue.api_url, data)?;
75+
.send(&github_issue.api_url, data, github_data)?;
76+
}
7577
}
7678

7779
continue;
@@ -93,32 +95,34 @@ fn reports_thread(data: &Data) -> Fallible<()> {
9395
res.info.get(&Comparison::Fixed).unwrap_or(&0),
9496
);
9597

96-
if let Some(ref github_issue) = ex.github_issue {
97-
Message::new()
98-
.line("tada", format!("Experiment **`{}`** is completed!", name))
99-
.line(
100-
"bar_chart",
101-
format!(
102-
" {} regressed and {} fixed ({} total)",
103-
regressed,
104-
fixed,
105-
res.info.values().sum::<u32>(),
106-
),
107-
)
108-
.line(
109-
"newspaper",
110-
format!("[Open the full report]({}).", report_url),
111-
)
112-
.note(
113-
"warning",
114-
format!(
115-
"If you notice any spurious failure [please add them to the \
98+
if let Some(github_data) = github_data {
99+
if let Some(ref github_issue) = ex.github_issue {
100+
Message::new()
101+
.line("tada", format!("Experiment **`{}`** is completed!", name))
102+
.line(
103+
"bar_chart",
104+
format!(
105+
" {} regressed and {} fixed ({} total)",
106+
regressed,
107+
fixed,
108+
res.info.values().sum::<u32>(),
109+
),
110+
)
111+
.line(
112+
"newspaper",
113+
format!("[Open the full report]({}).", report_url),
114+
)
115+
.note(
116+
"warning",
117+
format!(
118+
"If you notice any spurious failure [please add them to the \
116119
blacklist]({}/blob/master/config.toml)!",
117-
crate::CRATER_REPO_URL,
118-
),
119-
)
120-
.set_label(Label::ExperimentCompleted)
121-
.send(&github_issue.api_url, data)?;
120+
crate::CRATER_REPO_URL,
121+
),
122+
)
123+
.set_label(Label::ExperimentCompleted)
124+
.send(&github_issue.api_url, data, github_data)?;
125+
}
122126
}
123127
}
124128
}
@@ -133,9 +137,9 @@ impl ReportsWorker {
133137
ReportsWorker(Arc::new(Mutex::new(None)))
134138
}
135139

136-
pub fn spawn(&self, data: Data) {
140+
pub fn spawn(&self, data: Data, github_data: Option<GithubData>) {
137141
let joiner = thread::spawn(move || loop {
138-
let result = reports_thread(&data.clone())
142+
let result = reports_thread(&data.clone(), github_data.as_ref())
139143
.with_context(|_| "the reports generator thread crashed");
140144
if let Err(e) = result {
141145
utils::report_failure(&e);

0 commit comments

Comments
 (0)