Skip to content

Commit 7eb9d72

Browse files
authored
Merge pull request #948 from rylev/server-refactor
Server refactor
2 parents 66b689c + bc51665 commit 7eb9d72

13 files changed

+1654
-1573
lines changed

site/src/api.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -323,35 +323,6 @@ pub mod github {
323323

324324
#[derive(Debug, Clone, Serialize, Deserialize)]
325325
pub struct Response;
326-
327-
#[derive(Debug, Clone, Serialize, Deserialize)]
328-
pub struct CommitParent {
329-
pub sha: String,
330-
}
331-
332-
#[derive(Debug, Clone, Serialize, Deserialize)]
333-
pub struct CommitTree {
334-
pub sha: String,
335-
}
336-
337-
#[derive(Debug, Clone, Serialize, Deserialize)]
338-
pub struct InnerCommit {
339-
#[serde(default)]
340-
pub message: String,
341-
pub tree: CommitTree,
342-
}
343-
344-
#[derive(Debug, Clone, Serialize, Deserialize)]
345-
pub struct Commit {
346-
pub sha: String,
347-
pub commit: InnerCommit,
348-
pub parents: Vec<CommitParent>,
349-
}
350-
351-
#[derive(Debug, Clone, Serialize)]
352-
pub struct PostComment {
353-
pub body: String,
354-
}
355326
}
356327

357328
pub mod triage {

site/src/github.rs

Lines changed: 55 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
1-
use crate::api::{github, ServerResult};
1+
use crate::api::github::Issue;
22
use crate::comparison::{ComparisonSummary, Direction};
33
use crate::load::{Config, SiteCtxt, TryCommit};
44

55
use anyhow::Context as _;
66
use database::ArtifactId;
77
use hashbrown::HashSet;
8-
use regex::Regex;
98
use reqwest::header::USER_AGENT;
10-
use serde::Deserialize;
9+
use serde::{Deserialize, Serialize};
1110

1211
use std::{fmt::Write, sync::Arc, time::Duration};
1312

1413
type BoxedError = Box<dyn std::error::Error + Send + Sync>;
1514

16-
lazy_static::lazy_static! {
17-
static ref BODY_TRY_COMMIT: Regex =
18-
Regex::new(r#"(?:\W|^)@rust-timer\s+build\s+(\w+)(?:\W|$)(?:include=(\S+))?\s*(?:exclude=(\S+))?\s*(?:runs=(\d+))?"#).unwrap();
19-
static ref BODY_QUEUE: Regex =
20-
Regex::new(r#"(?:\W|^)@rust-timer\s+queue(?:\W|$)(?:include=(\S+))?\s*(?:exclude=(\S+))?\s*(?:runs=(\d+))?"#).unwrap();
21-
static ref BODY_MAKE_PR_FOR: Regex =
22-
Regex::new(r#"(?:\W|^)@rust-timer\s+make-pr-for\s+(\w+)(?:\W|$)"#).unwrap();
23-
static ref BODY_UDPATE_PR_FOR: Regex =
24-
Regex::new(r#"(?:\W|^)@rust-timer\s+update-branch-for\s+(\w+)(?:\W|$)"#).unwrap();
25-
}
26-
27-
async fn get_authorized_users() -> ServerResult<Vec<usize>> {
15+
pub async fn get_authorized_users() -> Result<Vec<usize>, String> {
2816
let url = format!("{}/permissions/perf.json", ::rust_team_data::v1::BASE_URL);
2917
let client = reqwest::Client::new();
3018
client
@@ -40,120 +28,8 @@ async fn get_authorized_users() -> ServerResult<Vec<usize>> {
4028
.map(|perms| perms.github_ids)
4129
}
4230

43-
pub async fn handle_github(
44-
request: github::Request,
45-
ctxt: Arc<SiteCtxt>,
46-
) -> ServerResult<github::Response> {
47-
if request.comment.body.contains(" homu: ") {
48-
if let Some(sha) = handle_homu_res(&request).await {
49-
return enqueue_sha(request, &ctxt, sha).await;
50-
}
51-
}
52-
53-
if !request.comment.body.contains("@rust-timer ") {
54-
return Ok(github::Response);
55-
}
56-
57-
if request.comment.author_association != github::Association::Owner
58-
&& !get_authorized_users()
59-
.await?
60-
.contains(&request.comment.user.id)
61-
{
62-
post_comment(
63-
&ctxt.config,
64-
request.issue.number,
65-
"Insufficient permissions to issue commands to rust-timer.",
66-
)
67-
.await;
68-
return Ok(github::Response);
69-
}
70-
71-
if let Some(captures) = BODY_QUEUE.captures(&request.comment.body) {
72-
let include = captures.get(1).map(|v| v.as_str());
73-
let exclude = captures.get(2).map(|v| v.as_str());
74-
let runs = captures.get(3).and_then(|v| v.as_str().parse::<i32>().ok());
75-
{
76-
let conn = ctxt.conn().await;
77-
conn.queue_pr(request.issue.number, include, exclude, runs)
78-
.await;
79-
}
80-
post_comment(
81-
&ctxt.config,
82-
request.issue.number,
83-
"Awaiting bors try build completion.
84-
85-
@rustbot label: +S-waiting-on-perf",
86-
)
87-
.await;
88-
return Ok(github::Response);
89-
}
90-
91-
if let Some(captures) = BODY_TRY_COMMIT.captures(&request.comment.body) {
92-
if let Some(commit) = captures.get(1).map(|c| c.as_str().to_owned()) {
93-
let include = captures.get(2).map(|v| v.as_str());
94-
let exclude = captures.get(3).map(|v| v.as_str());
95-
let runs = captures.get(4).and_then(|v| v.as_str().parse::<i32>().ok());
96-
let commit = commit.trim_start_matches("https://github.com/rust-lang/rust/commit/");
97-
{
98-
let conn = ctxt.conn().await;
99-
conn.queue_pr(request.issue.number, include, exclude, runs)
100-
.await;
101-
}
102-
let f = enqueue_sha(request, &ctxt, commit.to_owned());
103-
return f.await;
104-
}
105-
}
106-
107-
let captures = BODY_MAKE_PR_FOR
108-
.captures_iter(&request.comment.body)
109-
.collect::<Vec<_>>();
110-
for capture in captures {
111-
if let Some(rollup_merge) = capture.get(1).map(|c| c.as_str().to_owned()) {
112-
let rollup_merge =
113-
rollup_merge.trim_start_matches("https://github.com/rust-lang/rust/commit/");
114-
let client = reqwest::Client::new();
115-
pr_and_try_for_rollup(
116-
&client,
117-
ctxt.clone(),
118-
&request.issue.repository_url,
119-
&rollup_merge,
120-
&request.comment.html_url,
121-
)
122-
.await
123-
.map_err(|e| format!("{:?}", e))?;
124-
}
125-
}
126-
127-
let captures = BODY_UDPATE_PR_FOR
128-
.captures_iter(&request.comment.body)
129-
.collect::<Vec<_>>();
130-
for capture in captures {
131-
if let Some(rollup_merge) = capture.get(1).map(|c| c.as_str().to_owned()) {
132-
let rollup_merge =
133-
rollup_merge.trim_start_matches("https://github.com/rust-lang/rust/commit/");
134-
135-
// This just creates or updates the branch for this merge commit.
136-
// Intended for resolving the race condition of master merging in
137-
// between us updating the commit and merging things.
138-
let client = reqwest::Client::new();
139-
let branch =
140-
branch_for_rollup(&client, &ctxt, &request.issue.repository_url, rollup_merge)
141-
.await
142-
.map_err(|e| e.to_string())?;
143-
post_comment(
144-
&ctxt.config,
145-
request.issue.number,
146-
&format!("Master base SHA: {}", branch.master_base_sha),
147-
)
148-
.await;
149-
}
150-
}
151-
152-
Ok(github::Response)
153-
}
154-
15531
// Returns the PR number
156-
async fn pr_and_try_for_rollup(
32+
pub async fn pr_and_try_for_rollup(
15733
client: &reqwest::Client,
15834
ctxt: Arc<SiteCtxt>,
15935
repository_url: &str,
@@ -226,13 +102,13 @@ build hasn't yet started.",
226102
Ok(pr_number)
227103
}
228104

229-
struct RollupBranch {
230-
master_base_sha: String,
231-
rolled_up_pr_number: u32,
232-
name: String,
105+
pub struct RollupBranch {
106+
pub master_base_sha: String,
107+
pub rolled_up_pr_number: u32,
108+
pub name: String,
233109
}
234110

235-
async fn branch_for_rollup(
111+
pub async fn branch_for_rollup(
236112
client: &reqwest::Client,
237113
ctxt: &SiteCtxt,
238114
repository_url: &str,
@@ -467,7 +343,7 @@ pub async fn get_commit(
467343
ctxt: &SiteCtxt,
468344
repository_url: &str,
469345
sha: &str,
470-
) -> anyhow::Result<github::Commit> {
346+
) -> anyhow::Result<Commit> {
471347
let timer_token = ctxt
472348
.config
473349
.keys
@@ -498,13 +374,33 @@ pub async fn get_commit(
498374
}
499375
}
500376

501-
async fn enqueue_sha(
502-
request: github::Request,
503-
ctxt: &SiteCtxt,
504-
commit: String,
505-
) -> ServerResult<github::Response> {
377+
#[derive(Debug, Clone, Deserialize)]
378+
pub struct Commit {
379+
pub sha: String,
380+
pub commit: InnerCommit,
381+
pub parents: Vec<CommitParent>,
382+
}
383+
384+
#[derive(Debug, Clone, Deserialize)]
385+
pub struct InnerCommit {
386+
#[serde(default)]
387+
pub message: String,
388+
pub tree: CommitTree,
389+
}
390+
391+
#[derive(Debug, Clone, Deserialize)]
392+
pub struct CommitTree {
393+
pub sha: String,
394+
}
395+
396+
#[derive(Debug, Clone, Deserialize)]
397+
pub struct CommitParent {
398+
pub sha: String,
399+
}
400+
401+
pub async fn enqueue_sha(issue: Issue, ctxt: &SiteCtxt, commit: String) -> Result<(), String> {
506402
let client = reqwest::Client::new();
507-
let commit_response = get_commit(&client, ctxt, &request.issue.repository_url, &commit)
403+
let commit_response = get_commit(&client, ctxt, &issue.repository_url, &commit)
508404
.await
509405
.map_err(|e| e.to_string())?;
510406
if commit_response.parents.len() != 2 {
@@ -513,17 +409,17 @@ async fn enqueue_sha(
513409
commit_response.sha,
514410
commit_response.parents.len()
515411
);
516-
return Ok(github::Response);
412+
return Ok(());
517413
}
518414
let try_commit = TryCommit {
519415
sha: commit_response.sha.clone(),
520416
parent_sha: commit_response.parents[0].sha.clone(),
521-
issue: request.issue.clone(),
417+
issue: issue.clone(),
522418
};
523419
let queued = {
524420
let conn = ctxt.conn().await;
525421
conn.pr_attach_commit(
526-
request.issue.number,
422+
issue.number,
527423
&commit_response.sha,
528424
&commit_response.parents[0].sha,
529425
)
@@ -536,9 +432,9 @@ async fn enqueue_sha(
536432
commit_response.parents[0].sha,
537433
try_commit.comparison_url(),
538434
);
539-
post_comment(&ctxt.config, request.issue.number, msg).await;
435+
post_comment(&ctxt.config, issue.number, msg).await;
540436
}
541-
Ok(github::Response)
437+
Ok(())
542438
}
543439

544440
#[derive(Debug, Deserialize)]
@@ -547,22 +443,23 @@ enum HomuComment {
547443
TryBuildCompleted { merge_sha: String },
548444
}
549445

550-
async fn handle_homu_res(request: &github::Request) -> Option<String> {
551-
if !request.comment.body.contains("Try build successful") {
446+
/// Parse comment from homu containing try build sha
447+
pub async fn parse_homu_comment(comment_body: &str) -> Option<String> {
448+
if !comment_body.contains("Try build successful") {
552449
return None;
553450
}
554451

555452
let start = "<!-- homu: ";
556-
let start_idx = request.comment.body.find(start).expect("found homu") + start.len();
557-
let end_idx = start_idx + request.comment.body[start_idx..].find(" -->").unwrap();
453+
let start_idx = comment_body.find(start).expect("found homu") + start.len();
454+
let end_idx = start_idx + comment_body[start_idx..].find(" -->").unwrap();
558455

559-
let sha = match serde_json::from_str(&request.comment.body[start_idx..end_idx]) {
456+
let sha = match serde_json::from_str(&comment_body[start_idx..end_idx]) {
560457
Ok(HomuComment::TryBuildCompleted { merge_sha }) => merge_sha,
561458
Err(err) => {
562459
log::warn!(
563460
"failed to parse try build result; comment: {:?}, part: {:?}, err: {:?}",
564-
request.comment.body,
565-
&request.comment.body[start_idx..end_idx],
461+
comment_body,
462+
&comment_body[start_idx..end_idx],
566463
err
567464
);
568465
return None;
@@ -588,7 +485,7 @@ where
588485
"https://api.github.com/repos/rust-lang/rust/issues/{}/comments",
589486
pr
590487
))
591-
.json(&github::PostComment {
488+
.json(&PostComment {
592489
body: body.to_owned(),
593490
})
594491
.header(USER_AGENT, "perf-rust-lang-org-server")
@@ -599,6 +496,11 @@ where
599496
}
600497
}
601498

499+
#[derive(Debug, Clone, Serialize)]
500+
pub struct PostComment {
501+
pub body: String,
502+
}
503+
602504
pub async fn post_finished(ctxt: &SiteCtxt) {
603505
// If the github token is not configured, do not run this -- we don't want
604506
// to mark things as complete without posting the comment.

site/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ pub mod server;
1818
mod average;
1919
mod comparison;
2020
mod interpolate;
21+
mod request_handlers;
2122
mod selector;
2223
mod self_profile;

site/src/request_handlers.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
mod bootstrap;
2+
mod dashboard;
3+
mod github;
4+
mod graph;
5+
mod next_commit;
6+
mod self_profile;
7+
mod status_page;
8+
9+
pub use bootstrap::handle_bootstrap;
10+
pub use dashboard::handle_dashboard;
11+
pub use github::handle_github;
12+
pub use graph::{handle_graph, handle_graph_new};
13+
pub use next_commit::handle_next_commit;
14+
pub use self_profile::{
15+
get_self_profile_raw, handle_self_profile, handle_self_profile_processed_download,
16+
handle_self_profile_raw, handle_self_profile_raw_download,
17+
};
18+
pub use status_page::handle_status_page;
19+
20+
use crate::api::{info, ServerResult};
21+
use crate::load::SiteCtxt;
22+
23+
pub fn handle_info(ctxt: &SiteCtxt) -> info::Response {
24+
let mut metrics = ctxt.index.load().metrics();
25+
metrics.sort();
26+
info::Response {
27+
stats: metrics,
28+
as_of: ctxt.index.load().commits().last().map(|d| d.date),
29+
}
30+
}
31+
32+
pub async fn handle_collected() -> ServerResult<()> {
33+
Ok(())
34+
}

0 commit comments

Comments
 (0)