Skip to content

Commit aaf2652

Browse files
committed
add more config options for no_merges
- option for excluding PRs with certain labels - option for adding a label when merge commits are detected - option for setting a custom comment message
1 parent b96677b commit aaf2652

File tree

2 files changed

+49
-15
lines changed

2 files changed

+49
-15
lines changed

src/config.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,17 @@ pub(crate) struct AssignConfig {
9494

9595
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]
9696
pub(crate) struct NoMergesConfig {
97+
/// No action will be taken on PRs with these labels.
9798
#[serde(default)]
98-
_empty: (),
99+
pub(crate) exclude_labels: Vec<String>,
100+
/// Set this label on the PR when merge commits are detected.
101+
pub(crate) label: Option<String>,
102+
/// Override the default message to post when merge commits are detected.
103+
///
104+
/// This message will always be followed up with
105+
/// "The following commits are merge commits:" and then
106+
/// a list of the merge commits.
107+
pub(crate) message: Option<String>,
99108
}
100109

101110
#[derive(PartialEq, Eq, Debug, serde::Deserialize)]

src/handlers/no_merges.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::{
55
config::NoMergesConfig,
66
db::issue_data::IssueData,
7-
github::{IssuesAction, IssuesEvent},
7+
github::{IssuesAction, IssuesEvent, Label},
88
handlers::Context,
99
};
1010
use anyhow::Context as _;
@@ -38,16 +38,23 @@ pub(super) async fn parse_input(
3838
return Ok(None);
3939
}
4040

41-
// Require an empty configuration block to enable no-merges notifications.
42-
if config.is_none() {
41+
// Require a `[no_merges]` configuration block to enable no-merges notifications.
42+
let Some(config) = config else {
4343
return Ok(None);
44-
}
44+
};
4545

4646
// Don't ping on rollups or draft PRs.
4747
if event.issue.title.starts_with("Rollup of") || event.issue.draft {
4848
return Ok(None);
4949
}
5050

51+
// Don't trigger if the PR has any of the excluded labels.
52+
for label in event.issue.labels() {
53+
if config.exclude_labels.contains(&label.name) {
54+
return Ok(None);
55+
}
56+
}
57+
5158
let mut merge_commits = HashSet::new();
5259
let commits = event
5360
.issue
@@ -73,22 +80,17 @@ pub(super) async fn parse_input(
7380

7481
pub(super) async fn handle_input(
7582
ctx: &Context,
76-
_config: &NoMergesConfig,
83+
config: &NoMergesConfig,
7784
event: &IssuesEvent,
7885
input: NoMergesInput,
7986
) -> anyhow::Result<()> {
8087
let mut client = ctx.db.get().await;
8188
let mut state: IssueData<'_, NoMergesState> =
8289
IssueData::load(&mut client, &event.issue, NO_MERGES_KEY).await?;
8390

84-
let since_last_posted = if state.data.mentioned_merge_commits.is_empty() {
85-
""
91+
let mut message = if let Some(ref message) = config.message {
92+
message.clone()
8693
} else {
87-
" (since this message was last posted)"
88-
};
89-
90-
let mut should_send = false;
91-
let mut message = format!(
9294
"
9395
There are merge commits (commits with multiple parents) in your changes. We have a
9496
[no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) so
@@ -102,11 +104,24 @@ pub(super) async fn handle_input(
102104
$ # delete any merge commits in the editor that appears
103105
$ git push --force-with-lease
104106
```
107+
"
108+
.to_string()
109+
};
105110

111+
let since_last_posted = if state.data.mentioned_merge_commits.is_empty() {
112+
""
113+
} else {
114+
" (since this message was last posted)"
115+
};
116+
write!(
117+
message,
118+
"
106119
The following commits are merge commits{since_last_posted}:
120+
"
121+
)
122+
.unwrap();
107123

108-
"
109-
);
124+
let mut should_send = false;
110125
for commit in &input.merge_commits {
111126
if state.data.mentioned_merge_commits.contains(commit) {
112127
continue;
@@ -118,6 +133,16 @@ pub(super) async fn handle_input(
118133
}
119134

120135
if should_send {
136+
// Set label
137+
if let Some(ref name) = config.label {
138+
event
139+
.issue
140+
.add_labels(&ctx.github, vec![Label { name: name.clone() }])
141+
.await
142+
.context("failed to set no_merges labels")?;
143+
}
144+
145+
// Post comment
121146
event
122147
.issue
123148
.post_comment(&ctx.github, &message)

0 commit comments

Comments
 (0)