-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathmajor_change.rs
318 lines (288 loc) · 10.3 KB
/
major_change.rs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use crate::{
config::MajorChangeConfig,
github::{Event, Issue, IssuesAction, IssuesEvent, Label, ZulipGitHubReference},
handlers::Context,
interactions::ErrorComment,
};
use anyhow::Context as _;
use parser::command::second::SecondCommand;
use tracing as log;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum Invocation {
NewProposal,
AcceptedProposal,
Rename { prev_issue: ZulipGitHubReference },
}
pub(super) async fn parse_input(
_ctx: &Context,
event: &IssuesEvent,
config: Option<&MajorChangeConfig>,
) -> Result<Option<Invocation>, String> {
let config = if let Some(config) = config {
config
} else {
return Ok(None);
};
let enabling_label = config.enabling_label.as_str();
if event.action == IssuesAction::Edited {
if let Some(changes) = &event.changes {
if let Some(previous_title) = &changes.title {
let prev_issue = ZulipGitHubReference {
number: event.issue.number,
title: previous_title.from.clone(),
repository: event.issue.repository().clone(),
};
if event
.issue
.labels()
.iter()
.any(|l| l.name == enabling_label)
{
return Ok(Some(Invocation::Rename { prev_issue }));
} else {
// Ignore renamed issues without primary label (e.g., major-change)
// to avoid warning about the feature not being enabled.
return Ok(None);
}
}
} else {
log::warn!("Did not note changes in edited issue?");
return Ok(None);
}
}
// If we were labeled with accepted, then issue that event
if matches!(&event.action, IssuesAction::Labeled { label } if label.name == config.accept_label)
{
return Ok(Some(Invocation::AcceptedProposal));
}
// Opening an issue with a label assigned triggers both
// "Opened" and "Labeled" events.
//
// We want to treat reopened issues as new proposals but if the
// issue is freshly opened, we only want to trigger once;
// currently we do so on the label event.
if matches!(event.action, IssuesAction::Reopened if event.issue.labels().iter().any(|l| l.name == enabling_label))
|| matches!(&event.action, IssuesAction::Labeled { label } if label.name == enabling_label)
{
return Ok(Some(Invocation::NewProposal));
}
// All other issue events are ignored
return Ok(None);
}
pub(super) async fn handle_input(
ctx: &Context,
config: &MajorChangeConfig,
event: &IssuesEvent,
cmd: Invocation,
) -> anyhow::Result<()> {
if !event
.issue
.labels()
.iter()
.any(|l| l.name == config.enabling_label)
{
let cmnt = ErrorComment::new(
&event.issue,
format!(
"This issue is not ready for proposals; it lacks the `{}` label.",
config.enabling_label
),
);
cmnt.post(&ctx.github).await?;
return Ok(());
}
let zulip_msg = match cmd {
Invocation::NewProposal => format!(
"A new proposal has been announced: [{} #{}]({}). It will be \
announced at the next meeting to try and draw attention to it, \
but usually MCPs are not discussed during triage meetings. If \
you think this would benefit from discussion amongst the \
team, consider proposing a design meeting.",
event.issue.title, event.issue.number, event.issue.html_url,
),
Invocation::AcceptedProposal => format!(
"This proposal has been accepted: [#{}]({}).",
event.issue.number, event.issue.html_url,
),
Invocation::Rename { prev_issue } => {
let issue = &event.issue;
let prev_topic = zulip_topic_from_issue(&prev_issue);
let partial_issue = issue.to_zulip_github_reference();
let new_topic = zulip_topic_from_issue(&partial_issue);
let zulip_send_req = crate::zulip::MessageApiRequest {
recipient: crate::zulip::Recipient::Stream {
id: config.zulip_stream,
topic: &prev_topic,
},
content: "The associated GitHub issue has been renamed. Renaming this Zulip topic.",
};
let zulip_send_res = zulip_send_req
.send(&ctx.github.raw())
.await
.context("zulip post failed")?;
let zulip_send_res: crate::zulip::MessageApiResponse = zulip_send_res.json().await?;
let zulip_update_req = crate::zulip::UpdateMessageApiRequest {
message_id: zulip_send_res.message_id,
topic: Some(&new_topic),
propagate_mode: Some("change_all"),
content: None,
};
zulip_update_req
.send(&ctx.github.raw())
.await
.context("zulip message update failed")?;
// after renaming the zulip topic, post an additional comment under the old topic with a url to the new, renamed topic
// this is necessary due to the lack of topic permalinks, see https://github.com/zulip/zulip/issues/15290
let new_topic_url = crate::zulip::Recipient::Stream {
id: config.zulip_stream,
topic: &new_topic,
}
.url();
let breadcrumb_comment = format!(
"The associated GitHub issue has been renamed. Please see the [renamed Zulip topic]({}).",
new_topic_url
);
let zulip_send_breadcrumb_req = crate::zulip::MessageApiRequest {
recipient: crate::zulip::Recipient::Stream {
id: config.zulip_stream,
topic: &prev_topic,
},
content: &breadcrumb_comment,
};
zulip_send_breadcrumb_req
.send(&ctx.github.raw())
.await
.context("zulip post failed")?;
return Ok(());
}
};
handle(
ctx,
config,
&event.issue,
zulip_msg,
config.meeting_label.clone(),
cmd == Invocation::NewProposal,
)
.await
}
pub(super) async fn handle_command(
ctx: &Context,
config: &MajorChangeConfig,
event: &Event,
_cmd: SecondCommand,
) -> anyhow::Result<()> {
let issue = event.issue().unwrap();
if !issue
.labels()
.iter()
.any(|l| l.name == config.enabling_label)
{
let cmnt = ErrorComment::new(
&issue,
&format!(
"This issue cannot be seconded; it lacks the `{}` label.",
config.enabling_label
),
);
cmnt.post(&ctx.github).await?;
return Ok(());
}
let is_team_member = event
.user()
.is_team_member(&ctx.github)
.await
.ok()
.unwrap_or(false);
if !is_team_member {
let cmnt = ErrorComment::new(&issue, "Only team members can second issues.");
cmnt.post(&ctx.github).await?;
return Ok(());
}
let zulip_msg = format!(
"@*{}*: Proposal [#{}]({}) has been seconded, and will be approved in 10 days if no objections are raised.",
config.zulip_ping,
issue.number,
event.html_url().unwrap()
);
handle(
ctx,
config,
issue,
zulip_msg,
config.second_label.clone(),
false,
)
.await
}
async fn handle(
ctx: &Context,
config: &MajorChangeConfig,
issue: &Issue,
zulip_msg: String,
label_to_add: String,
new_proposal: bool,
) -> anyhow::Result<()> {
let github_req = issue.add_labels(&ctx.github, vec![Label { name: label_to_add }]);
let partial_issue = issue.to_zulip_github_reference();
let zulip_topic = zulip_topic_from_issue(&partial_issue);
let zulip_req = crate::zulip::MessageApiRequest {
recipient: crate::zulip::Recipient::Stream {
id: config.zulip_stream,
topic: &zulip_topic,
},
content: &zulip_msg,
};
if new_proposal {
let topic_url = zulip_req.url();
let comment = format!(
r#"> [!IMPORTANT]
> This issue is *not meant to be used for technical discussion*. There is a **Zulip [stream]** for that.
> Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.
<details>
<summary>Concerns or objections can formally be registered here by adding a comment.</summary>
<p>
```
@rfcbot concern reason-for-concern
<description of the concern>
```
Concerns can be lifted with:
```
@rfcbot resolve reason-for-concern
```
See documentation at [https://forge.rust-lang.org](https://forge.rust-lang.org/compiler/proposals-and-stabilization.html#what-kinds-of-comments-should-go-on-a-mcp-in-the-compiler-team-repo)
</p>
</details>
{}
[stream]: {}"#,
config.open_extra_text.as_deref().unwrap_or_default(),
topic_url
);
issue
.post_comment(&ctx.github, &comment)
.await
.context("post major change comment")?;
}
let zulip_req = zulip_req.send(&ctx.github.raw());
let (gh_res, zulip_res) = futures::join!(github_req, zulip_req);
zulip_res.context("zulip post failed")?;
gh_res.context("label setting failed")?;
Ok(())
}
fn zulip_topic_from_issue(issue: &ZulipGitHubReference) -> String {
// Concatenate the issue title and the topic reference, truncating such that
// the overall length does not exceed 60 characters (a Zulip limitation).
let topic_ref = issue.zulip_topic_reference();
// Skip chars until the last characters that can be written:
// Maximum 60, minus the reference, minus the elipsis and the space
let mut chars = issue
.title
.char_indices()
.skip(60 - topic_ref.chars().count() - 2);
match chars.next() {
Some((len, _)) if chars.next().is_some() => {
format!("{}… {}", &issue.title[..len], topic_ref)
}
_ => format!("{} {}", issue.title, topic_ref),
}
}