Skip to content

Commit 327d583

Browse files
authored
Merge pull request #1379 from mibac138/master
Add review_submitted handler which changes labels after a review
2 parents 880513c + 5490a59 commit 327d583

File tree

5 files changed

+84
-5
lines changed

5 files changed

+84
-5
lines changed

src/actions.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ impl<'a> Action for Step<'a> {
5757
let mut context = Context::new();
5858
let mut results = HashMap::new();
5959

60-
for Query { repos, queries} in &self.actions {
61-
60+
for Query { repos, queries } in &self.actions {
6261
for repo in repos {
6362
let repository = Repository {
6463
full_name: repo.to_string(),
@@ -118,7 +117,6 @@ impl<'a> Action for Step<'a> {
118117

119118
match count {
120119
Ok(count) => {
121-
122120
let result = if let Some(value) = context.get(*name) {
123121
value.as_u64().unwrap() + count as u64
124122
} else {

src/config.rs

Lines changed: 8 additions & 0 deletions
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/github.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,18 @@ pub struct Comment {
292292
pub user: User,
293293
#[serde(alias = "submitted_at")] // for pull request reviews
294294
pub updated_at: chrono::DateTime<Utc>,
295+
#[serde(rename = "state")]
296+
pub pr_review_state: PullRequestReviewState,
297+
}
298+
299+
#[derive(Debug, serde::Deserialize, Eq, PartialEq)]
300+
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
301+
pub enum PullRequestReviewState {
302+
Approved,
303+
ChangesRequested,
304+
Commented,
305+
Dismissed,
306+
Pending,
295307
}
296308

297309
fn opt_string<'de, D>(deserializer: D) -> Result<String, D::Error>

src/handlers.rs

Lines changed: 17 additions & 2 deletions
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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label, PullRequestReviewState};
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.comment.pr_review_state != PullRequestReviewState::ChangesRequested {
23+
return Ok(());
24+
}
25+
26+
if event.issue.assignees.contains(&event.comment.user) {
27+
let labels = event
28+
.issue
29+
.labels()
30+
.iter()
31+
// Remove review related labels
32+
.filter(|label| !config.review_labels.contains(&label.name))
33+
.cloned()
34+
// Add waiting on author label
35+
.chain(std::iter::once(Label {
36+
name: config.reviewed_label.clone(),
37+
}));
38+
event
39+
.issue
40+
.set_labels(&ctx.github, labels.collect())
41+
.await?;
42+
}
43+
}
44+
45+
Ok(())
46+
}

0 commit comments

Comments
 (0)