Repository navigation
Expand file tree
/
Copy pathconcern.rs
More file actions
267 lines (239 loc) · 8.05 KB
/
Copy pathconcern.rs
File metadata and controls
267 lines (239 loc) · 8.05 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
use std::fmt::Write;
use anyhow::{Context as _, bail};
use crate::{
config::ConcernConfig,
errors::user_error,
github::{Event, Label},
handlers::Context,
interactions::EditIssueBody,
utils::is_issue_under_rfcbot_fcp,
};
use parser::command::concern::ConcernCommand;
const CONCERN_ISSUE_KEY: &str = "CONCERN-ISSUE";
#[derive(Debug, PartialEq, Eq, Default, Clone, serde::Serialize, serde::Deserialize)]
struct ConcernData {
concerns: Vec<Concern>,
}
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)]
struct Concern {
title: String,
comment_url: String,
status: ConcernStatus,
}
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize, serde::Deserialize)]
enum ConcernStatus {
Active,
Resolved { comment_url: String },
}
pub(super) async fn handle_command(
ctx: &Context,
config: &ConcernConfig,
event: &Event,
cmd: ConcernCommand,
) -> anyhow::Result<()> {
let Event::IssueComment(issue_comment) = event else {
return user_error!("Concerns can only be issued on an issue");
};
let Some(comment_url) = event.html_url() else {
bail!("unable to retrieve the comment url")
};
let issue = &issue_comment.issue;
// Verify that this issue isn't a rfcbot FCP
if is_issue_under_rfcbot_fcp(&issue_comment.repository.full_name, issue.number).await {
return user_error!(
"Cannot set `@rustbot concern` on an active [rfcbot](https://rfcbot.rs/) FCP, use `@rfcbot concern` instead."
);
}
// Verify that the comment author is a team member in our team repo
if !ctx
.team
.is_team_member(&issue_comment.comment.user.login)
.await
.context("failed to verify that the user is a team member")?
{
tracing::info!(
"{}#{} tried to register a concern, but comment author {:?} is not part of the team repo",
issue_comment.repository.full_name,
issue.number,
issue_comment.comment.user,
);
return user_error!(
"Only team members in the [team repo](https://github.com/rust-lang/team) can add or resolve concerns."
);
}
let mut client = ctx.db.get().await;
let mut edit: EditIssueBody<'_, ConcernData> =
EditIssueBody::load(&mut client, issue, CONCERN_ISSUE_KEY)
.await
.context("unable to fetch the concerns data")?;
let concern_data = edit.data_mut();
// Process the command by either adding a new concern or resolving the old one
match cmd {
ConcernCommand::Concern { title } => {
// Only add a concern if it wasn't already added, we could be in an edit
if concern_data.concerns.iter().any(|c| c.title == title) {
tracing::info!(
"concern with the same name ({title}) already exists ({:?})",
&concern_data.concerns
);
} else {
concern_data.concerns.push(Concern {
title,
status: ConcernStatus::Active,
comment_url: comment_url.to_string(),
});
}
}
ConcernCommand::Resolve { title } => concern_data
.concerns
.iter_mut()
.filter(|c| c.title == title)
.for_each(|c| {
c.status = ConcernStatus::Resolved {
comment_url: comment_url.to_string(),
}
}),
}
// Create the new markdown content listing all the concerns
let new_content = markdown_content(&concern_data.concerns, &ctx.username);
// Add or remove the labels
if concern_data
.concerns
.iter()
.any(|c| matches!(c.status, ConcernStatus::Active))
{
if let Err(err) = issue
.add_labels(
&ctx.github,
config
.labels
.iter()
.map(|l| Label {
name: l.to_string(),
})
.collect(),
)
.await
{
tracing::error!("unable to add concern labels: {err:?}");
let labels = config.labels.join(", ");
issue.post_comment(
&ctx.github,
&format!("*Psst, I was unable to add the labels ({labels}), could someone do it for me.*"),
).await.context("unable to post the comment failure it-self")?;
}
} else {
issue
.remove_labels(
&ctx.github,
config
.labels
.iter()
.map(|l| Label {
name: l.to_string(),
})
.collect(),
)
.await
.context("unable to remove the concern labels")?;
}
// Apply the new markdown concerns list to the issue
edit.apply(&ctx.github, new_content)
.await
.context("failed to apply the new concerns section markdown")?;
Ok(())
}
fn markdown_content(concerns: &[Concern], bot: &str) -> String {
if concerns.is_empty() {
return String::new();
}
let active_concerns = concerns
.iter()
.filter(|c| matches!(c.status, ConcernStatus::Active))
.count();
let mut md = String::new();
let _ = writeln!(md);
if active_concerns > 0 {
let _ = writeln!(md, "> [!CAUTION]");
} else {
let _ = writeln!(md, "> [!NOTE]");
}
let _ = writeln!(md, "> # Concerns ({active_concerns} active)");
let _ = writeln!(md, ">");
for Concern {
title,
status,
comment_url,
} in concerns
{
let _ = match status {
ConcernStatus::Active => {
writeln!(md, "> - [{title}]({comment_url})")
}
ConcernStatus::Resolved {
comment_url: resolved_comment_url,
} => {
writeln!(
md,
"> - ~~[{title}]({comment_url})~~ resolved in [this comment]({resolved_comment_url})"
)
}
};
}
let _ = writeln!(md, ">");
let _ = writeln!(
md,
"> *Managed by `@{bot}`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*"
);
md
}
#[test]
fn simple_markdown_content() {
let concerns = &[
Concern {
title: "This is my concern about concern".to_string(),
status: ConcernStatus::Active,
comment_url: "https://github.com/fake-comment-1234".to_string(),
},
Concern {
title: "This is a resolved concern".to_string(),
status: ConcernStatus::Resolved {
comment_url: "https://github.com/fake-comment-8888".to_string(),
},
comment_url: "https://github.com/fake-comment-4561".to_string(),
},
];
assert_eq!(
markdown_content(concerns, "rustbot"),
r#"
> [!CAUTION]
> # Concerns (1 active)
>
> - [This is my concern about concern](https://github.com/fake-comment-1234)
> - ~~[This is a resolved concern](https://github.com/fake-comment-4561)~~ resolved in [this comment](https://github.com/fake-comment-8888)
>
> *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*
"#
);
}
#[test]
fn resolved_concerns_markdown_content() {
let concerns = &[Concern {
title: "This is a resolved concern".to_string(),
status: ConcernStatus::Resolved {
comment_url: "https://github.com/fake-comment-8888".to_string(),
},
comment_url: "https://github.com/fake-comment-4561".to_string(),
}];
assert_eq!(
markdown_content(concerns, "rustbot"),
r#"
> [!NOTE]
> # Concerns (0 active)
>
> - ~~[This is a resolved concern](https://github.com/fake-comment-4561)~~ resolved in [this comment](https://github.com/fake-comment-8888)
>
> *Managed by `@rustbot`—see [help](https://forge.rust-lang.org/triagebot/concern.html) for details.*
"#
);
}