@@ -13,7 +13,7 @@ use crate::handlers::docs_update::docs_update;
13
13
use crate :: handlers:: pr_tracking:: get_assigned_prs;
14
14
use crate :: handlers:: project_goals:: { self , ping_project_goals_owners} ;
15
15
use crate :: interactions:: ErrorComment ;
16
- use crate :: utils:: pluralize;
16
+ use crate :: utils:: { contains_any , pluralize} ;
17
17
use crate :: zulip:: api:: { MessageApiResponse , Recipient } ;
18
18
use crate :: zulip:: client:: ZulipClient ;
19
19
use crate :: zulip:: commands:: {
@@ -24,12 +24,34 @@ use axum::Json;
24
24
use axum:: extract:: State ;
25
25
use axum:: extract:: rejection:: JsonRejection ;
26
26
use axum:: response:: IntoResponse ;
27
+ use commands:: BackportArgs ;
28
+ use octocrab:: Octocrab ;
27
29
use rust_team_data:: v1:: { TeamKind , TeamMember } ;
28
30
use std:: cmp:: Reverse ;
29
31
use std:: fmt:: Write as _;
30
32
use std:: sync:: Arc ;
31
33
use subtle:: ConstantTimeEq ;
32
- use tracing as log;
34
+ use tracing:: log;
35
+
36
+ fn get_text_backport_approved ( channel : & str , verb : & str , zulip_link : & str ) -> String {
37
+ format ! ( "
38
+ {channel} backport {verb} as per compiler team [on Zulip]({zulip_link}). A backport PR will be authored by the release team at the end of the current development cycle. Backport labels handled by them.
39
+
40
+ @rustbot label +{channel}-accepted" )
41
+ }
42
+
43
+ fn get_text_backport_declined ( channel : & str , verb : & str , zulip_link : & str ) -> String {
44
+ format ! (
45
+ "
46
+ {channel} backport {verb} as per compiler team [on Zulip]({zulip_link}).
47
+
48
+ @rustbot label -{channel}-nominated"
49
+ )
50
+ }
51
+
52
+ const BACKPORT_CHANNELS : [ & str ; 2 ] = [ "beta" , "stable" ] ;
53
+ const BACKPORT_VERBS_APPROVE : [ & str ; 4 ] = [ "accept" , "accepted" , "approve" , "approved" ] ;
54
+ const BACKPORT_VERBS_DECLINE : [ & str ; 2 ] = [ "decline" , "declined" ] ;
33
55
34
56
#[ derive( Debug , serde:: Deserialize ) ]
35
57
pub struct Request {
@@ -302,10 +324,72 @@ async fn handle_command<'a>(
302
324
. map_err ( |e| format_err ! ( "Failed to await at this time: {e:?}" ) ) ,
303
325
StreamCommand :: PingGoals ( args) => ping_goals_cmd ( ctx, gh_id, message_data, & args) . await ,
304
326
StreamCommand :: DocsUpdate => trigger_docs_update ( message_data, & ctx. zulip ) ,
327
+ StreamCommand :: Backport ( args) => {
328
+ accept_decline_backport ( message_data, & ctx. octocrab , & ctx. zulip , & args) . await
329
+ }
305
330
}
306
331
}
307
332
}
308
333
334
+ // TODO: shorter variant of this command (f.e. `backport accept` or even `accept`) that infers everything from the Message payload
335
+ async fn accept_decline_backport (
336
+ message_data : & Message ,
337
+ octo_client : & Octocrab ,
338
+ zulip_client : & ZulipClient ,
339
+ args_data : & BackportArgs ,
340
+ ) -> anyhow:: Result < Option < String > > {
341
+ let message = message_data. clone ( ) ;
342
+ let args = args_data. clone ( ) ;
343
+ let stream_id = message. stream_id . unwrap ( ) ;
344
+ let subject = message. subject . unwrap ( ) ;
345
+ let verb = args. verb . to_lowercase ( ) ;
346
+ let octo_client = octo_client. clone ( ) ;
347
+
348
+ // Repository owner and name are hardcoded
349
+ // This command is only used in this repository
350
+ let repo_owner = "rust-lang" ;
351
+ let repo_name = "rust" ;
352
+
353
+ // validate command parameters
354
+ if !contains_any ( & [ args. channel . to_lowercase ( ) . as_str ( ) ] , & BACKPORT_CHANNELS ) {
355
+ return Err ( anyhow:: anyhow!(
356
+ "Parser error: unknown channel (allowed: {BACKPORT_CHANNELS:?})."
357
+ ) ) ;
358
+ }
359
+
360
+ // TODO: factor out the Zulip "URL encoder" to make it practical to use
361
+ let zulip_send_req = crate :: zulip:: MessageApiRequest {
362
+ recipient : Recipient :: Stream {
363
+ id : stream_id,
364
+ topic : & subject,
365
+ } ,
366
+ content : "" ,
367
+ } ;
368
+ let zulip_link = zulip_send_req. url ( zulip_client) ;
369
+
370
+ let message_body = if contains_any ( & [ verb. as_str ( ) ] , & BACKPORT_VERBS_APPROVE ) {
371
+ get_text_backport_approved ( & args. channel , & verb, & zulip_link)
372
+ } else if contains_any ( & [ verb. as_str ( ) ] , & BACKPORT_VERBS_DECLINE ) {
373
+ get_text_backport_declined ( & args. channel , & verb, & zulip_link)
374
+ } else {
375
+ return Err ( anyhow:: anyhow!(
376
+ "Parser error: unknown verb (allowed: {BACKPORT_VERBS_APPROVE:?} or {BACKPORT_VERBS_DECLINE:?})"
377
+ ) ) ;
378
+ } ;
379
+
380
+ tokio:: spawn ( async move {
381
+ let res = octo_client
382
+ . issues ( repo_owner, repo_name)
383
+ . create_comment ( args. pr_num , & message_body)
384
+ . await
385
+ . context ( "unable to post comment on #{args.pr_num}" ) ;
386
+ if res. is_err ( ) {
387
+ tracing:: error!( "failed to post comment: {0:?}" , res. err( ) ) ;
388
+ }
389
+ } ) ;
390
+ Ok ( Some ( "" . to_string ( ) ) )
391
+ }
392
+
309
393
async fn ping_goals_cmd (
310
394
ctx : Arc < Context > ,
311
395
gh_id : u64 ,
0 commit comments