Skip to content

Commit c0b0eeb

Browse files
committed
Add review_submitted handler which changes labels after a review
When an assigned reviewer submits a review to a PR, automatically remove waiting-on-review and add waiting-on-author label
1 parent daf3da3 commit c0b0eeb

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

src/config.rs

+8
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) struct Config {
2828
pub(crate) autolabel: Option<AutolabelConfig>,
2929
pub(crate) notify_zulip: Option<NotifyZulipConfig>,
3030
pub(crate) github_releases: Option<GitHubReleasesConfig>,
31+
pub(crate) review_submitted: Option<ReviewSubmittedConfig>,
3132
}
3233

3334
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
@@ -142,6 +143,12 @@ pub(crate) struct GlacierConfig {}
142143
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
143144
pub(crate) struct CloseConfig {}
144145

146+
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
147+
pub(crate) struct ReviewSubmittedConfig {
148+
pub(crate) review_labels: Vec<String>,
149+
pub(crate) reviewed_label: String,
150+
}
151+
145152
pub(crate) async fn get(gh: &GithubClient, repo: &str) -> Result<Arc<Config>, ConfigurationError> {
146153
if let Some(config) = get_cached_config(repo) {
147154
log::trace!("returning config for {} from cache", repo);
@@ -290,6 +297,7 @@ mod tests {
290297
autolabel: None,
291298
notify_zulip: None,
292299
github_releases: None,
300+
review_submitted: None,
293301
}
294302
);
295303
}

src/handlers.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod notify_zulip;
3636
mod ping;
3737
mod prioritize;
3838
mod relabel;
39+
mod review_submitted;
3940
mod rustc_commits;
4041

4142
pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
@@ -74,6 +75,20 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
7475
);
7576
}
7677

78+
if let Some(config) = config
79+
.as_ref()
80+
.ok()
81+
.and_then(|c| c.review_submitted.as_ref())
82+
{
83+
if let Err(e) = review_submitted::handle(ctx, event, config).await {
84+
log::error!(
85+
"failed to process event {:?} with review_submitted handler: {:?}",
86+
event,
87+
e
88+
)
89+
}
90+
}
91+
7792
if let Some(ghr_config) = config
7893
.as_ref()
7994
.ok()
@@ -120,9 +135,9 @@ macro_rules! issue_handlers {
120135
}
121136
}
122137

123-
// Handle events that happend on issues
138+
// Handle events that happened on issues
124139
//
125-
// This is for events that happends only on issues (e.g. label changes).
140+
// This is for events that happen only on issues (e.g. label changes).
126141
// Each module in the list must contain the functions `parse_input` and `handle_input`.
127142
issue_handlers! {
128143
autolabel,

src/handlers/review_submitted.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label};
2+
use crate::{config::ReviewSubmittedConfig, github::Event, handlers::Context};
3+
4+
pub(crate) async fn handle(
5+
ctx: &Context,
6+
event: &Event,
7+
config: &ReviewSubmittedConfig,
8+
) -> anyhow::Result<()> {
9+
if let Event::IssueComment(
10+
event
11+
@
12+
IssueCommentEvent {
13+
action: IssueCommentAction::Created,
14+
issue: Issue {
15+
pull_request: Some(_),
16+
..
17+
},
18+
..
19+
},
20+
) = event
21+
{
22+
if event.issue.assignees.contains(&event.comment.user) {
23+
let labels = event
24+
.issue
25+
.labels()
26+
.iter()
27+
// Remove review related labels
28+
.filter(|label| !config.review_labels.contains(&label.name))
29+
.cloned()
30+
// Add waiting on author label
31+
.chain(std::iter::once(Label {
32+
name: config.reviewed_label.clone(),
33+
}));
34+
event
35+
.issue
36+
.set_labels(&ctx.github, labels.collect())
37+
.await?;
38+
}
39+
}
40+
41+
Ok(())
42+
}

0 commit comments

Comments
 (0)