Skip to content

Commit abc9991

Browse files
committed
Move github client
1 parent 56f8f3a commit abc9991

File tree

3 files changed

+367
-358
lines changed

3 files changed

+367
-358
lines changed

site/src/github.rs

Lines changed: 5 additions & 347 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1+
pub mod client;
2+
13
use crate::api::github::Issue;
24
use crate::comparison::{
35
deserves_attention_icount, write_summary_table, write_summary_table_footer, ArtifactComparison,
46
ArtifactComparisonSummary, Direction, Metric,
57
};
6-
use crate::load::{Config, SiteCtxt, TryCommit};
8+
use crate::load::{SiteCtxt, TryCommit};
9+
use client::*;
710

811
use anyhow::Context as _;
912
use database::{ArtifactId, QueuedCommit};
10-
use reqwest::header::USER_AGENT;
11-
use serde::{Deserialize, Serialize};
13+
use serde::Deserialize;
1214

1315
use std::collections::HashSet;
1416

@@ -201,318 +203,6 @@ pub async fn branch_for_rollup(
201203
})
202204
}
203205

204-
#[derive(serde::Serialize)]
205-
struct CreateRefRequest<'a> {
206-
// Must start with `refs/` and have at least two slashes.
207-
// e.g. `refs/heads/master`.
208-
#[serde(rename = "ref")]
209-
ref_: &'a str,
210-
sha: &'a str,
211-
}
212-
213-
pub async fn create_ref(
214-
client: &reqwest::Client,
215-
ctxt: &SiteCtxt,
216-
repository_url: &str,
217-
ref_: &str,
218-
sha: &str,
219-
) -> anyhow::Result<()> {
220-
let timer_token = ctxt
221-
.config
222-
.keys
223-
.github_api_token
224-
.clone()
225-
.expect("needs github API token");
226-
let url = format!("{}/git/refs", repository_url);
227-
let response = client
228-
.post(&url)
229-
.json(&CreateRefRequest { ref_, sha })
230-
.header(USER_AGENT, "perf-rust-lang-org-server")
231-
.basic_auth("rust-timer", Some(timer_token))
232-
.send()
233-
.await
234-
.context("POST git/refs failed")?;
235-
if response.status() != reqwest::StatusCode::CREATED {
236-
anyhow::bail!("{:?} != 201 CREATED", response.status());
237-
}
238-
239-
Ok(())
240-
}
241-
242-
#[derive(serde::Serialize)]
243-
struct CreatePrRequest<'a> {
244-
title: &'a str,
245-
// username:branch if cross-repo
246-
head: &'a str,
247-
// branch to pull into (e.g, master)
248-
base: &'a str,
249-
#[serde(rename = "body")]
250-
description: &'a str,
251-
draft: bool,
252-
}
253-
254-
#[derive(Debug, serde::Deserialize)]
255-
pub struct CreatePrResponse {
256-
pub number: u32,
257-
pub html_url: String,
258-
pub comments_url: String,
259-
}
260-
261-
pub async fn create_pr(
262-
client: &reqwest::Client,
263-
ctxt: &SiteCtxt,
264-
repository_url: &str,
265-
title: &str,
266-
head: &str,
267-
base: &str,
268-
description: &str,
269-
draft: bool,
270-
) -> anyhow::Result<CreatePrResponse> {
271-
let timer_token = ctxt
272-
.config
273-
.keys
274-
.github_api_token
275-
.clone()
276-
.expect("needs github API token");
277-
let url = format!("{}/pulls", repository_url);
278-
let response = client
279-
.post(&url)
280-
.json(&CreatePrRequest {
281-
title,
282-
head,
283-
base,
284-
description,
285-
draft,
286-
})
287-
.header(USER_AGENT, "perf-rust-lang-org-server")
288-
.basic_auth("rust-timer", Some(timer_token))
289-
.send()
290-
.await
291-
.context("POST pulls failed")?;
292-
if response.status() != reqwest::StatusCode::CREATED {
293-
anyhow::bail!("{:?} != 201 CREATED", response.status());
294-
}
295-
296-
Ok(response.json().await.context("deserializing failed")?)
297-
}
298-
299-
#[derive(serde::Serialize)]
300-
struct UpdateBranchRequest<'a> {
301-
sha: &'a str,
302-
force: bool,
303-
}
304-
305-
pub async fn update_branch(
306-
client: &reqwest::Client,
307-
ctxt: &SiteCtxt,
308-
repository_url: &str,
309-
branch: &str,
310-
sha: &str,
311-
) -> anyhow::Result<()> {
312-
let timer_token = ctxt
313-
.config
314-
.keys
315-
.github_api_token
316-
.clone()
317-
.expect("needs github API token");
318-
let url = format!("{}/git/refs/{}", repository_url, branch);
319-
let commit_response = client
320-
.patch(&url)
321-
.json(&UpdateBranchRequest { sha, force: true })
322-
.header(USER_AGENT, "perf-rust-lang-org-server")
323-
.basic_auth("rust-timer", Some(timer_token))
324-
.send()
325-
.await
326-
.context("PATCH git/refs failed")?;
327-
if commit_response.status() != reqwest::StatusCode::OK {
328-
anyhow::bail!("{:?} != 200 OK", commit_response.status());
329-
}
330-
331-
Ok(())
332-
}
333-
334-
#[derive(serde::Serialize)]
335-
struct MergeBranchRequest<'a> {
336-
base: &'a str,
337-
head: &'a str,
338-
commit_message: &'a str,
339-
}
340-
#[derive(serde::Deserialize)]
341-
struct MergeBranchResponse {
342-
sha: String,
343-
}
344-
345-
pub async fn merge_branch(
346-
client: &reqwest::Client,
347-
ctxt: &SiteCtxt,
348-
repository_url: &str,
349-
branch: &str,
350-
sha: &str,
351-
commit_message: &str,
352-
) -> anyhow::Result<String> {
353-
let timer_token = ctxt
354-
.config
355-
.keys
356-
.github_api_token
357-
.clone()
358-
.expect("needs github API token");
359-
let url = format!("{}/merges", repository_url);
360-
let response = client
361-
.patch(&url)
362-
.json(&MergeBranchRequest {
363-
base: branch,
364-
head: sha,
365-
commit_message,
366-
})
367-
.header(USER_AGENT, "perf-rust-lang-org-server")
368-
.basic_auth("rust-timer", Some(timer_token))
369-
.send()
370-
.await
371-
.context("PATCH /merges failed")?;
372-
if !response.status().is_success() {
373-
anyhow::bail!("{:?} != 201 CREATED", response.status());
374-
}
375-
376-
Ok(response.json::<MergeBranchResponse>().await?.sha)
377-
}
378-
379-
#[derive(serde::Serialize)]
380-
struct CreateCommitRequest<'a> {
381-
message: &'a str,
382-
tree: &'a str,
383-
parents: &'a [&'a str],
384-
}
385-
386-
#[derive(serde::Deserialize)]
387-
struct CreateCommitResponse {
388-
sha: String,
389-
}
390-
391-
pub async fn create_commit(
392-
client: &reqwest::Client,
393-
ctxt: &SiteCtxt,
394-
repository_url: &str,
395-
message: &str,
396-
tree: &str,
397-
parents: &[&str],
398-
) -> anyhow::Result<String> {
399-
let timer_token = ctxt
400-
.config
401-
.keys
402-
.github_api_token
403-
.clone()
404-
.expect("needs github API token");
405-
let url = format!("{}/git/commits", repository_url);
406-
let commit_response = client
407-
.post(&url)
408-
.json(&CreateCommitRequest {
409-
message,
410-
tree,
411-
parents,
412-
})
413-
.header(USER_AGENT, "perf-rust-lang-org-server")
414-
.basic_auth("rust-timer", Some(timer_token))
415-
.send()
416-
.await
417-
.context("POST git/commits failed")?;
418-
if commit_response.status() != reqwest::StatusCode::CREATED {
419-
anyhow::bail!("{:?} != 201 CREATED", commit_response.status());
420-
}
421-
422-
Ok(commit_response
423-
.json::<CreateCommitResponse>()
424-
.await
425-
.context("deserializing failed")?
426-
.sha)
427-
}
428-
429-
pub async fn get_issue(
430-
client: &reqwest::Client,
431-
ctxt: &SiteCtxt,
432-
repository_url: &str,
433-
number: u64,
434-
) -> anyhow::Result<Issue> {
435-
let timer_token = ctxt
436-
.config
437-
.keys
438-
.github_api_token
439-
.clone()
440-
.expect("needs github API token");
441-
let url = format!("{}/issues/{}", repository_url, number);
442-
let response = client
443-
.get(&url)
444-
.header(USER_AGENT, "perf-rust-lang-org-server")
445-
.basic_auth("rust-timer", Some(timer_token))
446-
.send()
447-
.await
448-
.context("cannot get issue")?;
449-
if !response.status().is_success() {
450-
anyhow::bail!("{:?} != 200 OK", response.status());
451-
}
452-
453-
Ok(response.json().await?)
454-
}
455-
456-
pub async fn get_commit(
457-
client: &reqwest::Client,
458-
ctxt: &SiteCtxt,
459-
repository_url: &str,
460-
sha: &str,
461-
) -> anyhow::Result<Commit> {
462-
let timer_token = ctxt
463-
.config
464-
.keys
465-
.github_api_token
466-
.clone()
467-
.expect("needs github API token");
468-
let url = format!("{}/commits/{}", repository_url, sha);
469-
let commit_response = client
470-
.get(&url)
471-
.header(USER_AGENT, "perf-rust-lang-org-server")
472-
.basic_auth("rust-timer", Some(timer_token))
473-
.send()
474-
.await
475-
.context("cannot get commit")?;
476-
let commit_response = match commit_response.text().await {
477-
Ok(c) => c,
478-
Err(err) => {
479-
anyhow::bail!("Failed to decode response for {}: {:?}", url, err);
480-
}
481-
};
482-
match serde_json::from_str(&commit_response) {
483-
Ok(c) => Ok(c),
484-
Err(e) => Err(anyhow::anyhow!(
485-
"cannot deserialize commit ({}): {:?}",
486-
commit_response,
487-
e
488-
)),
489-
}
490-
}
491-
492-
#[derive(Debug, Clone, Deserialize)]
493-
pub struct Commit {
494-
pub sha: String,
495-
pub commit: InnerCommit,
496-
pub parents: Vec<CommitParent>,
497-
}
498-
499-
#[derive(Debug, Clone, Deserialize)]
500-
pub struct InnerCommit {
501-
#[serde(default)]
502-
pub message: String,
503-
pub tree: CommitTree,
504-
}
505-
506-
#[derive(Debug, Clone, Deserialize)]
507-
pub struct CommitTree {
508-
pub sha: String,
509-
}
510-
511-
#[derive(Debug, Clone, Deserialize)]
512-
pub struct CommitParent {
513-
pub sha: String,
514-
}
515-
516206
pub async fn enqueue_sha(issue: Issue, ctxt: &SiteCtxt, commit: String) -> Result<(), String> {
517207
let client = reqwest::Client::new();
518208
let commit_response = get_commit(&client, ctxt, &issue.repository_url, &commit)
@@ -584,38 +274,6 @@ pub async fn parse_homu_comment(comment_body: &str) -> Option<String> {
584274
Some(sha)
585275
}
586276

587-
pub async fn post_comment<B>(cfg: &Config, pr: u32, body: B)
588-
where
589-
B: Into<String>,
590-
{
591-
let body = body.into();
592-
let timer_token = cfg
593-
.keys
594-
.github_api_token
595-
.clone()
596-
.expect("needs github API token");
597-
let client = reqwest::Client::new();
598-
let req = client
599-
.post(&format!(
600-
"https://api.github.com/repos/rust-lang/rust/issues/{}/comments",
601-
pr
602-
))
603-
.json(&PostComment {
604-
body: body.to_owned(),
605-
})
606-
.header(USER_AGENT, "perf-rust-lang-org-server")
607-
.basic_auth("rust-timer", Some(timer_token));
608-
609-
if let Err(e) = req.send().await {
610-
eprintln!("failed to post comment: {:?}", e);
611-
}
612-
}
613-
614-
#[derive(Debug, Clone, Serialize)]
615-
pub struct PostComment {
616-
pub body: String,
617-
}
618-
619277
/// Post messages to GitHub for all queued commits that have
620278
/// not yet been marked as completed.
621279
pub async fn post_finished(ctxt: &SiteCtxt) {

0 commit comments

Comments
 (0)