Skip to content

Commit d671884

Browse files
apirainoUrgau
andcommitted
Retrieve team that handles a backport
Co-authored-by: Urgau <3616612+Urgau@users.noreply.github.com>
1 parent c62bfaa commit d671884

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

src/zulip.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,21 @@ use tracing::log;
4343
fn get_text_backport_approved(
4444
channel: &BackportChannelArgs,
4545
verb: &BackportVerbArgs,
46+
team_name: &str,
4647
zulip_link: &str,
4748
) -> String {
4849
format!(
49-
"{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 are handled by them."
50+
"{channel} backport {verb} as per {team_name} 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 are handled by them."
5051
)
5152
}
5253

5354
fn get_text_backport_declined(
5455
channel: &BackportChannelArgs,
5556
verb: &BackportVerbArgs,
57+
team_name: &str,
5658
zulip_link: &str,
5759
) -> String {
58-
format!("{channel} backport {verb} as per compiler team [on Zulip]({zulip_link}).")
60+
format!("{channel} backport {verb} as per {team_name} team [on Zulip]({zulip_link}).")
5961
}
6062

6163
#[derive(Debug, serde::Deserialize)]
@@ -407,6 +409,14 @@ async fn accept_decline_backport(
407409
let stream_id = message.stream_id.unwrap();
408410
let subject = message.subject.unwrap();
409411
let zulip_client = &ctx.zulip;
412+
let zulip_stream = zulip_client.get_zulip_channel_by_id(stream_id).await?;
413+
// expected something like `t-{team_name}/foo` or `t-{team_name}`
414+
let team_name = &zulip_stream
415+
.name
416+
.rsplit_once('/')
417+
.map_or(&*zulip_stream.name, |s| s.0)
418+
.strip_prefix("t-")
419+
.unwrap_or_default();
410420

411421
// Repository owner and name are hardcoded
412422
// This command is only used in this repository
@@ -443,11 +453,11 @@ async fn accept_decline_backport(
443453
| BackportVerbArgs::Accepted
444454
| BackportVerbArgs::Approve
445455
| BackportVerbArgs::Approved => (
446-
get_text_backport_approved(&args.channel, &args.verb, &zulip_link),
456+
get_text_backport_approved(&args.channel, &args.verb, team_name, &zulip_link),
447457
true,
448458
),
449459
BackportVerbArgs::Decline | BackportVerbArgs::Declined => (
450-
get_text_backport_declined(&args.channel, &args.verb, &zulip_link),
460+
get_text_backport_declined(&args.channel, &args.verb, team_name, &zulip_link),
451461
false,
452462
),
453463
};

src/zulip/api.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ impl Recipient<'_> {
9393
}
9494
}
9595

96+
#[derive(serde::Deserialize)]
97+
pub struct ZulipChannel {
98+
pub stream: ZulipChannelData,
99+
}
100+
101+
#[derive(serde::Deserialize)]
102+
pub struct ZulipChannelData {
103+
pub name: String,
104+
pub stream_id: u64,
105+
}
106+
96107
#[cfg(test)]
97108
fn check_encode(topic: &str, expected: &str) {
98109
const PREFIX: &str = "stream/0-xxx/topic/";

src/zulip/client.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
//! Documentation: https://zulip.com/api/send-message
33
44
use crate::zulip::Recipient;
5-
use crate::zulip::api::{MessageApiResponse, ZulipUser, ZulipUsers};
5+
use crate::zulip::api::{
6+
MessageApiResponse, ZulipChannel, ZulipChannelData, ZulipUser, ZulipUsers,
7+
};
68
use anyhow::Context;
79
use reqwest::{Client, Method, RequestBuilder, Response};
810
use secrecy::{ExposeSecret, SecretString};
@@ -170,6 +172,21 @@ impl ZulipClient {
170172
Ok(())
171173
}
172174

175+
/// Retrieve a stream (channel) from its ID
176+
/// https://zulip.com/api/get-stream-by-id
177+
pub async fn get_zulip_channel_by_id(
178+
&self,
179+
stream_id: u64,
180+
) -> anyhow::Result<ZulipChannelData> {
181+
let resp = self
182+
.make_request(Method::GET, &format!("streams/{stream_id}"))
183+
.send()
184+
.await?;
185+
deserialize_response::<ZulipChannel>(resp)
186+
.await
187+
.map(|channel| channel.stream)
188+
}
189+
173190
fn make_request(&self, method: Method, url: &str) -> RequestBuilder {
174191
let api_token = self.get_api_token();
175192
self.client

0 commit comments

Comments
 (0)