Skip to content

Commit 33c0966

Browse files
authored
Merge pull request #2028 from Kobzol/lookup-zulip-username
Simplify implementation of `lookup zulip`
2 parents 1b98d07 + d149302 commit 33c0966

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

src/zulip.rs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,10 @@ async fn lookup_github_username(ctx: &Context, zulip_username: &str) -> anyhow::
548548

549549
/// Tries to find a Zulip username from a GitHub username.
550550
async fn lookup_zulip_username(ctx: &Context, gh_username: &str) -> anyhow::Result<String> {
551-
async fn lookup_from_zulip(ctx: &Context, gh_username: &str) -> anyhow::Result<Option<String>> {
551+
async fn lookup_zulip_id_from_zulip(
552+
ctx: &Context,
553+
gh_username: &str,
554+
) -> anyhow::Result<Option<u64>> {
552555
let username_lowercase = gh_username.to_lowercase();
553556
let users = get_zulip_users(ctx.github.raw()).await?;
554557
Ok(users
@@ -559,10 +562,13 @@ async fn lookup_zulip_username(ctx: &Context, gh_username: &str) -> anyhow::Resu
559562
.as_deref()
560563
== Some(username_lowercase.as_str())
561564
})
562-
.map(|u| u.name))
565+
.map(|u| u.user_id))
563566
}
564567

565-
async fn lookup_from_team(ctx: &Context, gh_username: &str) -> anyhow::Result<Option<String>> {
568+
async fn lookup_zulip_id_from_team(
569+
ctx: &Context,
570+
gh_username: &str,
571+
) -> anyhow::Result<Option<u64>> {
566572
let people = people(&ctx.github).await?.people;
567573

568574
// Lookup the person in the team DB
@@ -579,25 +585,24 @@ async fn lookup_zulip_username(ctx: &Context, gh_username: &str) -> anyhow::Resu
579585
let Some(zulip_id) = to_zulip_id(&ctx.github, person.github_id).await? else {
580586
return Ok(None);
581587
};
582-
let Ok(zulip_user) = get_zulip_user(&ctx.github.raw(), zulip_id).await else {
583-
return Ok(None);
584-
};
585-
Ok(Some(zulip_user.name))
588+
Ok(Some(zulip_id))
586589
}
587590

588-
let zulip_username = match lookup_from_team(ctx, gh_username).await? {
589-
Some(username) => username,
590-
None => match lookup_from_zulip(ctx, gh_username).await? {
591-
Some(username) => username,
591+
let zulip_id = match lookup_zulip_id_from_team(ctx, gh_username).await? {
592+
Some(id) => id,
593+
None => match lookup_zulip_id_from_zulip(ctx, gh_username).await? {
594+
Some(id) => id,
592595
None => {
593596
return Ok(format!(
594597
"No Zulip account found for GitHub username `{gh_username}`."
595598
))
596599
}
597600
},
598601
};
602+
// @**|<zulip-id>** is Zulip syntax that will render as the username (and a link) of the user
603+
// with the given Zulip ID.
599604
Ok(format!(
600-
"The GitHub user `{gh_username}` has the following Zulip account: @**{zulip_username}**"
605+
"The GitHub user `{gh_username}` has the following Zulip account: @**|{zulip_id}**"
601606
))
602607
}
603608

@@ -658,29 +663,6 @@ async fn get_zulip_users(client: &reqwest::Client) -> anyhow::Result<Vec<ZulipUs
658663
}
659664
}
660665

661-
async fn get_zulip_user(client: &reqwest::Client, zulip_id: u64) -> anyhow::Result<ZulipUser> {
662-
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
663-
664-
let resp = client
665-
.get(&format!("{}/api/v1/users/{zulip_id}", *ZULIP_URL))
666-
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
667-
.send()
668-
.await?;
669-
670-
let status = resp.status();
671-
672-
if !status.is_success() {
673-
let body = resp
674-
.text()
675-
.await
676-
.context("fail receiving Zulip API response (when getting Zulip user)")?;
677-
678-
anyhow::bail!(body)
679-
} else {
680-
Ok(resp.json::<ZulipUser>().await?)
681-
}
682-
}
683-
684666
// This does two things:
685667
// * execute the command for the other user
686668
// * tell the user executed for that a command was run as them by the user

0 commit comments

Comments
 (0)