Skip to content

Commit

Permalink
Add support for labeling draft PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Feb 6, 2024
1 parent df574c1 commit b264de0
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 8 deletions.
18 changes: 18 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub(crate) struct Config {
pub(crate) github_releases: Option<GitHubReleasesConfig>,
pub(crate) review_submitted: Option<ReviewSubmittedConfig>,
pub(crate) review_requested: Option<ReviewRequestedConfig>,
pub(crate) converted_to_draft: Option<ConvertedToDraftConfig>,
pub(crate) ready_for_review: Option<ReadyForReviewConfig>,
pub(crate) shortcut: Option<ShortcutConfig>,
pub(crate) note: Option<NoteConfig>,
pub(crate) mentions: Option<MentionsConfig>,
Expand Down Expand Up @@ -209,6 +211,8 @@ pub(crate) struct AutolabelLabelConfig {
#[serde(default)]
pub(crate) new_pr: bool,
#[serde(default)]
pub(crate) new_draft_pr: bool,
#[serde(default)]
pub(crate) new_issue: bool,
}

Expand Down Expand Up @@ -289,6 +293,20 @@ pub(crate) struct ReviewRequestedConfig {
pub(crate) add_labels: Vec<String>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ConvertedToDraftConfig {
pub(crate) remove_labels: Vec<String>,
pub(crate) add_labels: Vec<String>,
}

#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub(crate) struct ReadyForReviewConfig {
pub(crate) remove_labels: Vec<String>,
pub(crate) add_labels: Vec<String>,
}

pub(crate) async fn get(
gh: &GithubClient,
repo: &Repository,
Expand Down
4 changes: 4 additions & 0 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl fmt::Display for HandlerError {
mod assign;
mod autolabel;
mod close;
mod converted_to_draft;
pub mod docs_update;
mod github_releases;
mod glacier;
Expand All @@ -39,6 +40,7 @@ mod notification;
mod notify_zulip;
mod ping;
mod prioritize;
mod ready_for_review;
mod relabel;
mod review_requested;
mod review_submitted;
Expand Down Expand Up @@ -167,6 +169,8 @@ issue_handlers! {
no_merges,
notify_zulip,
review_requested,
converted_to_draft,
ready_for_review,
validate_config,
}

Expand Down
17 changes: 9 additions & 8 deletions src/handlers/autolabel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,22 @@ pub(super) async fn parse_input(
name: label.to_owned(),
});
}
if cfg.new_pr && event.action == IssuesAction::Opened {
autolabels.push(Label {
name: label.to_owned(),
});
}
}

if event.issue.pull_request.is_none()
&& cfg.new_issue
&& event.action == IssuesAction::Opened
if event.issue.is_pr()
&& matches!(event.action, IssuesAction::Opened)
&& ((cfg.new_pr && !event.issue.draft) || (cfg.new_draft_pr && event.issue.draft))
{
autolabels.push(Label {
name: label.to_owned(),
});
}

if !event.issue.is_pr() && cfg.new_issue && event.action == IssuesAction::Opened {
autolabels.push(Label {
name: label.to_owned(),
});
}
}

if !autolabels.is_empty() {
Expand Down
44 changes: 44 additions & 0 deletions src/handlers/converted_to_draft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::config::ConvertedToDraftConfig;
use crate::github::{IssuesAction, IssuesEvent, Label};
use crate::handlers::Context;

pub(crate) struct ConvertedToDraftInput {}

pub(crate) async fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
_config: Option<&ConvertedToDraftConfig>,
) -> Result<Option<ConvertedToDraftInput>, String> {
// PR author requests a review from one of the assignees

match &event.action {
IssuesAction::ConvertedToDraft => Ok(Some(ConvertedToDraftInput {})),
_ => Ok(None),
}
}

pub(crate) async fn handle_input(
ctx: &Context,
config: &ConvertedToDraftConfig,
event: &IssuesEvent,
ConvertedToDraftInput {}: ConvertedToDraftInput,
) -> anyhow::Result<()> {
event
.issue
.add_labels(
&ctx.github,
config
.add_labels
.iter()
.cloned()
.map(|name| Label { name })
.collect(),
)
.await?;

for label in &config.remove_labels {
event.issue.remove_label(&ctx.github, label).await?;
}

Ok(())
}
44 changes: 44 additions & 0 deletions src/handlers/ready_for_review.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::config::ReadyForReviewConfig;
use crate::github::{IssuesAction, IssuesEvent, Label};
use crate::handlers::Context;

pub(crate) struct ReadyForReviewInput {}

pub(crate) async fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
_config: Option<&ReadyForReviewConfig>,
) -> Result<Option<ReadyForReviewInput>, String> {
// PR author requests a review from one of the assignees

match &event.action {
IssuesAction::ReadyForReview => Ok(Some(ReadyForReviewInput {})),
_ => Ok(None),
}
}

pub(crate) async fn handle_input(
ctx: &Context,
config: &ReadyForReviewConfig,
event: &IssuesEvent,
ReadyForReviewInput {}: ReadyForReviewInput,
) -> anyhow::Result<()> {
event
.issue
.add_labels(
&ctx.github,
config
.add_labels
.iter()
.cloned()
.map(|name| Label { name })
.collect(),
)
.await?;

for label in &config.remove_labels {
event.issue.remove_label(&ctx.github, label).await?;
}

Ok(())
}

0 comments on commit b264de0

Please sign in to comment.