4
4
use crate :: {
5
5
config:: NoMergesConfig ,
6
6
db:: issue_data:: IssueData ,
7
- github:: { IssuesAction , IssuesEvent } ,
7
+ github:: { IssuesAction , IssuesEvent , Label } ,
8
8
handlers:: Context ,
9
9
} ;
10
10
use anyhow:: Context as _;
@@ -38,16 +38,23 @@ pub(super) async fn parse_input(
38
38
return Ok ( None ) ;
39
39
}
40
40
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 {
43
43
return Ok ( None ) ;
44
- }
44
+ } ;
45
45
46
46
// Don't ping on rollups or draft PRs.
47
47
if event. issue . title . starts_with ( "Rollup of" ) || event. issue . draft {
48
48
return Ok ( None ) ;
49
49
}
50
50
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
+
51
58
let mut merge_commits = HashSet :: new ( ) ;
52
59
let commits = event
53
60
. issue
@@ -71,53 +78,74 @@ pub(super) async fn parse_input(
71
78
} )
72
79
}
73
80
81
+ const DEFAULT_MESSAGE : & str = "
82
+ There are merge commits (commits with multiple parents) in your changes. We have a \
83
+ [no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) \
84
+ so these commits will need to be removed for this pull request to be merged.
85
+
86
+ You can start a rebase with the following commands:
87
+ ```shell-session
88
+ $ # rebase
89
+ $ git rebase -i master
90
+ $ # delete any merge commits in the editor that appears
91
+ $ git push --force-with-lease
92
+ ```
93
+
94
+ " ;
95
+
74
96
pub ( super ) async fn handle_input (
75
97
ctx : & Context ,
76
- _config : & NoMergesConfig ,
98
+ config : & NoMergesConfig ,
77
99
event : & IssuesEvent ,
78
100
input : NoMergesInput ,
79
101
) -> anyhow:: Result < ( ) > {
80
102
let mut client = ctx. db . get ( ) . await ;
81
103
let mut state: IssueData < ' _ , NoMergesState > =
82
104
IssueData :: load ( & mut client, & event. issue , NO_MERGES_KEY ) . await ?;
83
105
106
+ let mut message = config
107
+ . message
108
+ . as_deref ( )
109
+ . unwrap_or ( DEFAULT_MESSAGE )
110
+ . to_string ( ) ;
111
+
84
112
let since_last_posted = if state. data . mentioned_merge_commits . is_empty ( ) {
85
113
""
86
114
} else {
87
115
" (since this message was last posted)"
88
116
} ;
117
+ writeln ! (
118
+ message,
119
+ "The following commits are merge commits{since_last_posted}:"
120
+ )
121
+ . unwrap ( ) ;
89
122
90
123
let mut should_send = false ;
91
- let mut message = format ! (
92
- "
93
- There are merge commits (commits with multiple parents) in your changes. We have a
94
- [no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) so
95
- these commits will need to be removed for this pull request to be merged.
96
-
97
- You can start a rebase with the following commands:
98
-
99
- ```shell-session
100
- $ # rebase
101
- $ git rebase -i master
102
- $ # delete any merge commits in the editor that appears
103
- $ git push --force-with-lease
104
- ```
105
-
106
- The following commits are merge commits{since_last_posted}:
107
-
108
- "
109
- ) ;
110
124
for commit in & input. merge_commits {
111
125
if state. data . mentioned_merge_commits . contains ( commit) {
112
126
continue ;
113
127
}
114
128
115
129
should_send = true ;
116
130
state. data . mentioned_merge_commits . insert ( ( * commit) . clone ( ) ) ;
117
- write ! ( message, "- {commit}" ) . unwrap ( ) ;
131
+ writeln ! ( message, "- {commit}" ) . unwrap ( ) ;
118
132
}
119
133
120
134
if should_send {
135
+ // Set labels
136
+ let labels = config
137
+ . labels
138
+ . iter ( )
139
+ . cloned ( )
140
+ . map ( |name| Label { name } )
141
+ . collect ( ) ;
142
+ event
143
+ . issue
144
+ . add_labels ( & ctx. github , labels)
145
+ . await
146
+ . context ( "failed to set no_merges labels" ) ?;
147
+
148
+ // Post comment
121
149
event
122
150
. issue
123
151
. post_comment ( & ctx. github , & message)
@@ -127,3 +155,40 @@ pub(super) async fn handle_input(
127
155
}
128
156
Ok ( ( ) )
129
157
}
158
+
159
+ #[ cfg( test) ]
160
+ mod test {
161
+ use super :: * ;
162
+
163
+ #[ test]
164
+ fn message ( ) {
165
+ let mut message = DEFAULT_MESSAGE . to_string ( ) ;
166
+
167
+ writeln ! ( message, "The following commits are merge commits:" ) . unwrap ( ) ;
168
+
169
+ for n in 1 ..5 {
170
+ writeln ! ( message, "- commit{n}" ) . unwrap ( ) ;
171
+ }
172
+
173
+ assert_eq ! (
174
+ message,
175
+ "
176
+ There are merge commits (commits with multiple parents) in your changes. We have a [no merge policy](https://rustc-dev-guide.rust-lang.org/git.html#no-merge-policy) so these commits will need to be removed for this pull request to be merged.
177
+
178
+ You can start a rebase with the following commands:
179
+ ```shell-session
180
+ $ # rebase
181
+ $ git rebase -i master
182
+ $ # delete any merge commits in the editor that appears
183
+ $ git push --force-with-lease
184
+ ```
185
+
186
+ The following commits are merge commits:
187
+ - commit1
188
+ - commit2
189
+ - commit3
190
+ - commit4
191
+ "
192
+ ) ;
193
+ }
194
+ }
0 commit comments