-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathautolabel.rs
More file actions
248 lines (222 loc) · 8.81 KB
/
autolabel.rs
File metadata and controls
248 lines (222 loc) · 8.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use crate::{
config::AutolabelConfig,
github::{IssuesAction, IssuesEvent, Label},
handlers::Context,
};
use anyhow::Context as _;
use tracing as log;
pub(super) struct AutolabelInput {
add: Vec<Label>,
remove: Vec<Label>,
}
pub(super) async fn parse_input(
ctx: &Context,
event: &IssuesEvent,
config: Option<&AutolabelConfig>,
) -> Result<Option<AutolabelInput>, String> {
let Some(config) = config else {
return Ok(None);
};
// On opening a new PR or sync'ing the branch, look at the diff and try to
// add any appropriate labels.
//
// FIXME: This will re-apply labels after a push that the user had tried to
// remove. Not much can be done about that currently; the before/after on
// synchronize may be straddling a rebase, which will break diff generation.
let can_trigger_files = matches!(
event.action,
IssuesAction::Opened | IssuesAction::Synchronize
);
if can_trigger_files
|| matches!(
event.action,
IssuesAction::Closed
| IssuesAction::Reopened
| IssuesAction::ReadyForReview
| IssuesAction::ConvertedToDraft
)
|| event.has_base_changed()
{
let files = if can_trigger_files {
event
.issue
.diff(&ctx.github)
.await
.map_err(|e| log::error!("failed to fetch diff: {e:?}"))
.unwrap_or_default()
} else {
Default::default()
};
let mut autolabels = Vec::new();
let mut to_remove = Vec::new();
'outer: for (label, cfg) in &config.labels {
let exclude_patterns =
cfg.exclude_labels
.iter()
.filter_map(|label| match globset::Glob::new(label) {
Ok(exclude_glob) => Some(exclude_glob),
Err(error) => {
log::error!("invalid glob pattern: {error}");
None
}
});
let exclude_patterns = match globset::GlobSet::new(exclude_patterns) {
Ok(exclude_patterns) => exclude_patterns,
Err(err) => {
log::error!("failed to create a glob set: {err:?}");
continue;
}
};
for label in event.issue.labels() {
if exclude_patterns.is_match(&label.name) {
// If we hit an excluded label, ignore this autolabel and check the next
continue 'outer;
}
}
if event.issue.is_pr() {
if let Some(files) = &files {
// This a PR with modified files.
// Add the matching labels for the modified files paths
if cfg.trigger_files.iter().any(|f| {
files
.iter()
.any(|file_diff| file_diff.filename.starts_with(f))
}) {
autolabels.push(Label {
name: label.to_owned(),
});
}
}
let is_opened =
matches!(event.action, IssuesAction::Opened | IssuesAction::Reopened);
let is_closed = matches!(event.action, IssuesAction::Closed);
// Treat the following situations as a "new PR":
// 1) PRs that were (re)opened and are not draft
// 2) PRs that have been converted from a draft to being "ready for review"
let is_opened_non_draft = is_opened && !event.issue.draft;
let is_ready_for_review = event.action == IssuesAction::ReadyForReview;
// Treat the following situations as a "new draft":
// 1) PRs that were (re)opened and are draft
// 2) PRs that have been converted to a draft
let is_opened_as_draft = is_opened && event.issue.draft;
let is_converted_to_draft = event.action == IssuesAction::ConvertedToDraft;
// Treat the following situations as a "pr merged":
// 1) PRs that were closed and have the merged status
let is_closed_as_merged = is_closed && event.issue.merged;
#[expect(clippy::if_same_then_else, reason = "suggested code looks ugly")]
if cfg.new_pr && (is_opened_non_draft || is_ready_for_review) {
autolabels.push(Label {
name: label.to_owned(),
});
} else if cfg.new_draft && (is_opened_as_draft || is_converted_to_draft) {
autolabels.push(Label {
name: label.to_owned(),
});
} else if cfg.pr_merged && is_closed_as_merged {
autolabels.push(Label {
name: label.to_owned(),
});
}
// If a PR is converted to draft or closed, remove all the "new PR" labels.
// Same for "new draft" labels when the PR is ready for review or closed.
#[expect(clippy::if_same_then_else, reason = "suggested code looks ugly")]
if cfg.new_pr
&& matches!(
event.action,
IssuesAction::ConvertedToDraft | IssuesAction::Closed
)
{
to_remove.push(Label {
name: label.to_owned(),
});
} else if cfg.new_draft
&& matches!(
event.action,
IssuesAction::ReadyForReview | IssuesAction::Closed
)
{
to_remove.push(Label {
name: label.to_owned(),
});
}
} else {
if cfg.new_issue && event.action == IssuesAction::Opened {
autolabels.push(Label {
name: label.to_owned(),
});
}
// If an issue is closed, remove all the "new issue" labels.
if cfg.new_issue && event.action == IssuesAction::Closed {
to_remove.push(Label {
name: label.to_owned(),
});
}
}
}
if !autolabels.is_empty() || !to_remove.is_empty() {
return Ok(Some(AutolabelInput {
add: autolabels,
remove: to_remove,
}));
}
}
if let IssuesAction::Labeled { label } = &event.action {
let mut autolabels = Vec::new();
let applied_label = &label.name;
'outer: for (label, config) in config.get_by_trigger(applied_label) {
let exclude_patterns =
config
.exclude_labels
.iter()
.filter_map(|label| match globset::Glob::new(label) {
Ok(exclude_glob) => Some(exclude_glob),
Err(error) => {
log::error!("invalid glob pattern: {error}");
None
}
});
let exclude_patterns = match globset::GlobSet::new(exclude_patterns) {
Ok(exclude_patterns) => exclude_patterns,
Err(err) => {
log::error!("failed to create a glob set: {err:?}");
continue;
}
};
for label in event.issue.labels() {
if exclude_patterns.is_match(&label.name) {
// If we hit an excluded label, ignore this autolabel and check the next
continue 'outer;
}
}
// If we reach here, no excluded labels were found, so we should apply the autolabel.
autolabels.push(Label {
name: label.to_owned(),
});
}
if !autolabels.is_empty() {
return Ok(Some(AutolabelInput {
add: autolabels,
remove: vec![],
}));
}
}
Ok(None)
}
pub(super) async fn handle_input(
ctx: &Context,
_config: &AutolabelConfig,
event: &IssuesEvent,
input: AutolabelInput,
) -> anyhow::Result<()> {
event
.issue
.add_labels(&ctx.github, input.add)
.await
.context("failed to add the labels to the issue")?;
event
.issue
.remove_labels(&ctx.github, input.remove)
.await
.context("failed to remove labels from the issue")?;
Ok(())
}