浏览代码

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
mibac138 3 年之前
父节点
当前提交
c0b0eeb748
共有 3 个文件被更改,包括 67 次插入2 次删除
  1. 8 0
      src/config.rs
  2. 17 2
      src/handlers.rs
  3. 42 0
      src/handlers/review_submitted.rs

+ 8 - 0
src/config.rs

@@ -28,6 +28,7 @@ pub(crate) struct Config {
     pub(crate) autolabel: Option<AutolabelConfig>,
     pub(crate) notify_zulip: Option<NotifyZulipConfig>,
     pub(crate) github_releases: Option<GitHubReleasesConfig>,
+    pub(crate) review_submitted: Option<ReviewSubmittedConfig>,
 }
 
 #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
@@ -142,6 +143,12 @@ pub(crate) struct GlacierConfig {}
 #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
 pub(crate) struct CloseConfig {}
 
+#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
+pub(crate) struct ReviewSubmittedConfig {
+    pub(crate) review_labels: Vec<String>,
+    pub(crate) reviewed_label: String,
+}
+
 pub(crate) async fn get(gh: &GithubClient, repo: &str) -> Result<Arc<Config>, ConfigurationError> {
     if let Some(config) = get_cached_config(repo) {
         log::trace!("returning config for {} from cache", repo);
@@ -290,6 +297,7 @@ mod tests {
                 autolabel: None,
                 notify_zulip: None,
                 github_releases: None,
+                review_submitted: None,
             }
         );
     }

+ 17 - 2
src/handlers.rs

@@ -36,6 +36,7 @@ mod notify_zulip;
 mod ping;
 mod prioritize;
 mod relabel;
+mod review_submitted;
 mod rustc_commits;
 
 pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
@@ -74,6 +75,20 @@ pub async fn handle(ctx: &Context, event: &Event) -> Vec<HandlerError> {
         );
     }
 
+    if let Some(config) = config
+        .as_ref()
+        .ok()
+        .and_then(|c| c.review_submitted.as_ref())
+    {
+        if let Err(e) = review_submitted::handle(ctx, event, config).await {
+            log::error!(
+                "failed to process event {:?} with review_submitted handler: {:?}",
+                event,
+                e
+            )
+        }
+    }
+
     if let Some(ghr_config) = config
         .as_ref()
         .ok()
@@ -120,9 +135,9 @@ macro_rules! issue_handlers {
     }
 }
 
-// Handle events that happend on issues
+// Handle events that happened on issues
 //
-// This is for events that happends only on issues (e.g. label changes).
+// This is for events that happen only on issues (e.g. label changes).
 // Each module in the list must contain the functions `parse_input` and `handle_input`.
 issue_handlers! {
     autolabel,

+ 42 - 0
src/handlers/review_submitted.rs

@@ -0,0 +1,42 @@
+use crate::github::{Issue, IssueCommentAction, IssueCommentEvent, Label};
+use crate::{config::ReviewSubmittedConfig, github::Event, handlers::Context};
+
+pub(crate) async fn handle(
+    ctx: &Context,
+    event: &Event,
+    config: &ReviewSubmittedConfig,
+) -> anyhow::Result<()> {
+    if let Event::IssueComment(
+        event
+        @
+        IssueCommentEvent {
+            action: IssueCommentAction::Created,
+            issue: Issue {
+                pull_request: Some(_),
+                ..
+            },
+            ..
+        },
+    ) = event
+    {
+        if event.issue.assignees.contains(&event.comment.user) {
+            let labels = event
+                .issue
+                .labels()
+                .iter()
+                // Remove review related labels
+                .filter(|label| !config.review_labels.contains(&label.name))
+                .cloned()
+                // Add waiting on author label
+                .chain(std::iter::once(Label {
+                    name: config.reviewed_label.clone(),
+                }));
+            event
+                .issue
+                .set_labels(&ctx.github, labels.collect())
+                .await?;
+        }
+    }
+
+    Ok(())
+}