Skip to content

Commit 8854eb7

Browse files
committed
refactor QueueItem enum
1 parent aebdde1 commit 8854eb7

File tree

3 files changed

+68
-43
lines changed

3 files changed

+68
-43
lines changed

src/bin/server/mod.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,27 @@ pub use self::worker::Worker;
66
mod service;
77
mod worker;
88

9-
pub struct QueueItem {
10-
pub kind: QueueItemKind,
11-
pub delivery_id: String,
9+
pub enum QueueItem {
10+
GitHubStatus {
11+
payload: rla::github::CommitStatusEvent,
12+
delivery_id: String,
13+
},
14+
GitHubCheckRun {
15+
payload: rla::github::CheckRunEvent,
16+
delivery_id: String,
17+
},
18+
GitHubPullRequest {
19+
payload: rla::github::PullRequestEvent,
20+
delivery_id: String,
21+
},
1222
}
1323

14-
pub enum QueueItemKind {
15-
GitHubStatus(rla::github::CommitStatusEvent),
16-
GitHubCheckRun(rla::github::CheckRunEvent),
17-
GitHubPullRequest(rla::github::PullRequestEvent),
24+
impl QueueItem {
25+
fn delivery_id(&self) -> Option<&str> {
26+
match self {
27+
QueueItem::GitHubStatus { delivery_id, .. } => Some(&delivery_id),
28+
QueueItem::GitHubCheckRun { delivery_id, .. } => Some(&delivery_id),
29+
QueueItem::GitHubPullRequest { delivery_id, .. } => Some(&delivery_id),
30+
}
31+
}
1832
}

src/bin/server/service.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{QueueItem, QueueItemKind};
1+
use super::QueueItem;
22

33
use crate::rla;
44
use anyhow::bail;
@@ -76,7 +76,7 @@ impl RlaService {
7676
return reply(StatusCode::BAD_REQUEST, "Missing delivery ID.\n");
7777
};
7878

79-
let item_kind = match event {
79+
let item = match event {
8080
"status" => {
8181
let payload = match serde_json::from_slice(body) {
8282
Ok(p) => p,
@@ -85,7 +85,10 @@ impl RlaService {
8585
return reply(StatusCode::BAD_REQUEST, "Failed to decode payload.\n");
8686
}
8787
};
88-
QueueItemKind::GitHubStatus(payload)
88+
QueueItem::GitHubStatus {
89+
payload,
90+
delivery_id,
91+
}
8992
}
9093
"check_run" => {
9194
let payload = match serde_json::from_slice(body) {
@@ -96,10 +99,16 @@ impl RlaService {
9699
}
97100
};
98101

99-
QueueItemKind::GitHubCheckRun(payload)
102+
QueueItem::GitHubCheckRun {
103+
payload,
104+
delivery_id,
105+
}
100106
}
101107
"pull_request" => match serde_json::from_slice(body) {
102-
Ok(payload) => QueueItemKind::GitHubPullRequest(payload),
108+
Ok(payload) => QueueItem::GitHubPullRequest {
109+
payload,
110+
delivery_id,
111+
},
103112
Err(err) => {
104113
error!("Failed to decode 'pull_request' webhook payload: {}", err);
105114
return reply(StatusCode::BAD_REQUEST, "Failed to decode payload\n");
@@ -115,10 +124,6 @@ impl RlaService {
115124
}
116125
};
117126

118-
let item = QueueItem {
119-
kind: item_kind,
120-
delivery_id,
121-
};
122127
match self.queue.send(item) {
123128
Ok(()) => reply(StatusCode::OK, "Event processed.\n"),
124129
Err(e) => {

src/bin/server/worker.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{QueueItem, QueueItemKind};
1+
use super::QueueItem;
22

33
use crate::rla;
44
use crate::rla::ci::{self, BuildCommit, CiPlatform};
@@ -7,7 +7,7 @@ use rla::index::IndexStorage;
77
use std::collections::{HashSet, VecDeque};
88
use std::hash::Hash;
99
use std::str;
10-
use std::time::{Instant, Duration};
10+
use std::time::{Duration, Instant};
1111

1212
const MINIMUM_DELAY_BETWEEN_INDEX_BACKUPS: Duration = Duration::from_secs(60 * 60);
1313

@@ -78,7 +78,7 @@ impl Worker {
7878
let span = span!(
7979
tracing::Level::INFO,
8080
"request",
81-
delivery = item.delivery_id.as_str(),
81+
delivery = item.delivery_id(),
8282
build_id = tracing::field::Empty
8383
);
8484
let _enter = span.enter();
@@ -98,32 +98,38 @@ impl Worker {
9898
}
9999

100100
fn process(&mut self, item: QueueItem, span: &tracing::Span) -> rla::Result<()> {
101-
let (repo, build_id, outcome) = match &item.kind {
102-
QueueItemKind::GitHubStatus(ev) => match self.ci.build_id_from_github_status(&ev) {
103-
Some(id) if self.is_repo_valid(&ev.repository.full_name) => {
104-
(&ev.repository.full_name, id, None)
105-
}
106-
_ => {
107-
info!(
108-
"Ignoring invalid event (ctx: {:?}, url: {:?}).",
109-
ev.context, ev.target_url
110-
);
111-
return Ok(());
112-
}
113-
},
114-
QueueItemKind::GitHubCheckRun(ev) => match self.ci.build_id_from_github_check(&ev) {
115-
Some(id) if self.is_repo_valid(&ev.repository.full_name) => {
116-
(&ev.repository.full_name, id, Some(&ev.check_run.outcome))
101+
let (repo, build_id, outcome) = match &item {
102+
QueueItem::GitHubStatus { payload, .. } => {
103+
match self.ci.build_id_from_github_status(&payload) {
104+
Some(id) if self.is_repo_valid(&payload.repository.full_name) => {
105+
(&payload.repository.full_name, id, None)
106+
}
107+
_ => {
108+
info!(
109+
"Ignoring invalid event (ctx: {:?}, url: {:?}).",
110+
payload.context, payload.target_url
111+
);
112+
return Ok(());
113+
}
117114
}
118-
_ => {
119-
info!(
120-
"Ignoring invalid event (app id: {:?}, url: {:?}).",
121-
ev.check_run.app.id, ev.check_run.details_url
122-
);
123-
return Ok(());
115+
}
116+
QueueItem::GitHubCheckRun { payload, .. } => {
117+
match self.ci.build_id_from_github_check(&payload) {
118+
Some(id) if self.is_repo_valid(&payload.repository.full_name) => (
119+
&payload.repository.full_name,
120+
id,
121+
Some(&payload.check_run.outcome),
122+
),
123+
_ => {
124+
info!(
125+
"Ignoring invalid event (app id: {:?}, url: {:?}).",
126+
payload.check_run.app.id, payload.check_run.details_url
127+
);
128+
return Ok(());
129+
}
124130
}
125-
},
126-
QueueItemKind::GitHubPullRequest(ev) => return self.process_pr(ev),
131+
}
132+
QueueItem::GitHubPullRequest { payload, .. } => return self.process_pr(payload),
127133
};
128134

129135
span.record("build_id", &build_id);

0 commit comments

Comments
 (0)