Skip to content

Commit ee3ca11

Browse files
committed
Make a client type
1 parent abc9991 commit ee3ca11

File tree

3 files changed

+348
-421
lines changed

3 files changed

+348
-421
lines changed

site/src/github.rs

Lines changed: 67 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::comparison::{
66
ArtifactComparisonSummary, Direction, Metric,
77
};
88
use crate::load::{SiteCtxt, TryCommit};
9-
use client::*;
109

1110
use anyhow::Context as _;
1211
use database::{ArtifactId, QueuedCommit};
@@ -36,40 +35,38 @@ pub async fn get_authorized_users() -> Result<Vec<usize>, String> {
3635

3736
// Returns the PR number
3837
pub async fn pr_and_try_for_rollup(
39-
client: &reqwest::Client,
4038
ctxt: Arc<SiteCtxt>,
4139
repository_url: &str,
4240
rollup_merge_sha: &str,
4341
origin_url: &str,
4442
) -> anyhow::Result<u32> {
43+
let client = client::Client::from_ctxt(&ctxt, repository_url.to_owned());
4544
log::trace!(
4645
"creating PR for {:?} {:?}",
4746
repository_url,
4847
rollup_merge_sha
4948
);
50-
let branch = branch_for_rollup(client, &ctxt, repository_url, rollup_merge_sha).await?;
49+
let branch = branch_for_rollup(&ctxt, repository_url, rollup_merge_sha).await?;
5150

52-
let pr = create_pr(
53-
client,
54-
&ctxt,
55-
repository_url,
56-
&format!(
57-
"[DO NOT MERGE] perf-test for #{}",
58-
branch.rolled_up_pr_number
59-
),
60-
&format!("rust-timer:{}", branch.name),
61-
"master",
62-
&format!(
63-
"This is an automatically generated pull request (from [here]({})) to \
51+
let pr = client
52+
.create_pr(
53+
&format!(
54+
"[DO NOT MERGE] perf-test for #{}",
55+
branch.rolled_up_pr_number
56+
),
57+
&format!("rust-timer:{}", branch.name),
58+
"master",
59+
&format!(
60+
"This is an automatically generated pull request (from [here]({})) to \
6461
run perf tests for #{} which merged in a rollup.
6562
6663
r? @ghost",
67-
origin_url, branch.rolled_up_pr_number
68-
),
69-
true,
70-
)
71-
.await
72-
.context("Created PR")?;
64+
origin_url, branch.rolled_up_pr_number
65+
),
66+
true,
67+
)
68+
.await
69+
.context("Created PR")?;
7370

7471
let pr_number = pr.number;
7572
let rollup_merge_sha = rollup_merge_sha.to_owned();
@@ -84,11 +81,11 @@ r? @ghost",
8481
// Eventually we'll want to handle this automatically, but that's a ways
8582
// off: we'd need to store the state in the database and handle the try
8683
// build starting and generally that's a lot of work for not too much gain.
87-
post_comment(
88-
&ctxt.config,
89-
pr.number,
90-
&format!(
91-
"@bors try @rust-timer queue
84+
client
85+
.post_comment(
86+
pr.number,
87+
&format!(
88+
"@bors try @rust-timer queue
9289
9390
The try commit's (master) parent should be {master}. If it isn't, \
9491
then please:
@@ -99,11 +96,11 @@ then please:
9996
10097
You do not need to reinvoke the queue command as long as the perf \
10198
build hasn't yet started.",
102-
master = branch.master_base_sha,
103-
merge = rollup_merge_sha,
104-
),
105-
)
106-
.await;
99+
master = branch.master_base_sha,
100+
merge = rollup_merge_sha,
101+
),
102+
)
103+
.await;
107104
});
108105

109106
Ok(pr_number)
@@ -116,12 +113,15 @@ pub struct RollupBranch {
116113
}
117114

118115
pub async fn branch_for_rollup(
119-
client: &reqwest::Client,
120116
ctxt: &SiteCtxt,
121117
repository_url: &str,
122118
rollup_merge_sha: &str,
123119
) -> anyhow::Result<RollupBranch> {
124-
let rollup_merge = get_commit(&client, &ctxt, repository_url, rollup_merge_sha)
120+
let client = client::Client::from_ctxt(ctxt, repository_url.to_owned());
121+
let timer = "https://api.github.com/repos/rust-timer/rust";
122+
let timer_client = client::Client::from_ctxt(ctxt, timer.to_owned());
123+
let rollup_merge = client
124+
.get_commit(rollup_merge_sha)
125125
.await
126126
.context("got rollup merge")?;
127127

@@ -132,40 +132,38 @@ pub async fn branch_for_rollup(
132132
break;
133133
}
134134
assert_eq!(current.parents.len(), 2);
135-
current = get_commit(&client, &ctxt, repository_url, &current.parents[0].sha)
135+
current = client
136+
.get_commit(&current.parents[0].sha)
136137
.await
137138
.context("success master get")?;
138139
}
139140
let old_master_commit = current;
140141

141-
let current_master_commit = get_commit(&client, &ctxt, repository_url, "master")
142+
let current_master_commit = client
143+
.get_commit("master")
142144
.await
143145
.context("success master get")?;
144146

145-
let revert_sha = create_commit(
146-
&client,
147-
&ctxt,
148-
"https://api.github.com/repos/rust-timer/rust",
149-
&format!("Revert to {}", old_master_commit.sha),
150-
&old_master_commit.commit.tree.sha,
151-
&[&current_master_commit.sha],
152-
)
153-
.await
154-
.context("create revert")?;
155-
156-
let merge_sha = create_commit(
157-
&client,
158-
&ctxt,
159-
"https://api.github.com/repos/rust-timer/rust",
160-
&format!(
161-
"rust-timer simulated merge of {}\n\nOriginal message:\n{}",
162-
rollup_merge.sha, rollup_merge.commit.message
163-
),
164-
&rollup_merge.commit.tree.sha,
165-
&[&revert_sha],
166-
)
167-
.await
168-
.context("create merge commit")?;
147+
let revert_sha = timer_client
148+
.create_commit(
149+
&format!("Revert to {}", old_master_commit.sha),
150+
&old_master_commit.commit.tree.sha,
151+
&[&current_master_commit.sha],
152+
)
153+
.await
154+
.context("create revert")?;
155+
156+
let merge_sha = timer_client
157+
.create_commit(
158+
&format!(
159+
"rust-timer simulated merge of {}\n\nOriginal message:\n{}",
160+
rollup_merge.sha, rollup_merge.commit.message
161+
),
162+
&rollup_merge.commit.tree.sha,
163+
&[&revert_sha],
164+
)
165+
.await
166+
.context("create merge commit")?;
169167

170168
let rolled_up_pr_number = if let Some(stripped) = rollup_merge
171169
.commit
@@ -186,15 +184,10 @@ pub async fn branch_for_rollup(
186184
};
187185

188186
let branch = format!("try-for-{}", rolled_up_pr_number);
189-
create_ref(
190-
&client,
191-
&ctxt,
192-
"https://api.github.com/repos/rust-timer/rust",
193-
&format!("refs/heads/{}", branch),
194-
&merge_sha,
195-
)
196-
.await
197-
.context("created branch")?;
187+
timer_client
188+
.create_ref(&format!("refs/heads/{}", branch), &merge_sha)
189+
.await
190+
.context("created branch")?;
198191

199192
Ok(RollupBranch {
200193
rolled_up_pr_number,
@@ -204,8 +197,9 @@ pub async fn branch_for_rollup(
204197
}
205198

206199
pub async fn enqueue_sha(issue: Issue, ctxt: &SiteCtxt, commit: String) -> Result<(), String> {
207-
let client = reqwest::Client::new();
208-
let commit_response = get_commit(&client, ctxt, &issue.repository_url, &commit)
200+
let client = client::Client::from_ctxt(ctxt, issue.repository_url.clone());
201+
let commit_response = client
202+
.get_commit(&commit)
209203
.await
210204
.map_err(|e| e.to_string())?;
211205
if commit_response.parents.len() != 2 {
@@ -237,7 +231,7 @@ pub async fn enqueue_sha(issue: Issue, ctxt: &SiteCtxt, commit: String) -> Resul
237231
commit_response.parents[0].sha,
238232
try_commit.comparison_url(),
239233
);
240-
post_comment(&ctxt.config, issue.number, msg).await;
234+
client.post_comment(issue.number, msg).await;
241235
}
242236
Ok(())
243237
}
@@ -331,13 +325,14 @@ pub async fn post_finished(ctxt: &SiteCtxt) {
331325
///
332326
/// `is_master_commit` is used to differentiate messages for try runs and post-merge runs.
333327
async fn post_comparison_comment(ctxt: &SiteCtxt, commit: QueuedCommit, is_master_commit: bool) {
328+
let client = client::Client::from_ctxt(ctxt, "https://github.com/rust-lang/rust".to_owned());
334329
let pr = commit.pr;
335330
let body = match summarize_run(ctxt, commit, is_master_commit).await {
336331
Ok(message) => message,
337332
Err(error) => error,
338333
};
339334

340-
post_comment(&ctxt.config, pr, body).await;
335+
client.post_comment(pr, body).await;
341336
}
342337

343338
fn make_comparison_url(commit: &QueuedCommit, stat: Metric) -> String {

0 commit comments

Comments
 (0)