Skip to content

Commit

Permalink
Show Zulip details in 'show-person'.
Browse files Browse the repository at this point in the history
Show `zulip_id` if present.

In addition, if ZULIP_USER and ZULIP_TOKEN are set, try to look up the
user's name (as shown in Zulip itself). This is useful for finding
people whose Zulip name differs from their GitHub name.
  • Loading branch information
jacobbramley committed Feb 28, 2024
1 parent b465f42 commit 2cd7457
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const USER_AGENT: &str = "https://github.com/rust-lang/team ([email protected]

use data::Data;
use schema::{Email, Team, TeamKind};
use zulip::ZulipApi;

use crate::schema::RepoPermission;
use anyhow::{bail, format_err, Error};
Expand Down Expand Up @@ -292,8 +293,23 @@ fn run() -> Result<(), Error> {
println!();

println!("github: @{}", person.github());
if let Some(zulip_id) = person.zulip_id() {
let zulip = ZulipApi::new();
match zulip.require_auth() {
Ok(()) => match zulip.get_user(zulip_id) {
Ok(user) => println!("zulip: {} ({zulip_id})", user.name),
Err(err) => {
println!("zulip_id: {zulip_id} # Failed to look up Zulip name: {err}")
}
},
Err(err) => {
// We have no authentication credentials, so don't even attempt the network access.
println!("zulip_id: {zulip_id} # Skipped name lookup: {err}");
}
}
}
if let Email::Present(email) = person.email() {
println!("email: {}", email);
println!("email: {}", email);
}
println!();

Expand Down
25 changes: 19 additions & 6 deletions src/zulip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ impl ZulipApi {

pub(crate) fn require_auth(&self) -> Result<(), Error> {
if self.auth.is_none() {
bail!(
"missing {} and/or {} environment variables",
USER_VAR,
TOKEN_VAR
);
bail!("missing {USER_VAR} and/or {TOKEN_VAR} environment variables");
}
Ok(())
}
Expand All @@ -56,6 +52,17 @@ impl ZulipApi {
Ok(response)
}

/// Get a single user of the Rust Zulip instance
pub(crate) fn get_user(&self, user_id: u64) -> Result<ZulipUser, Error> {
let response = self
.req(Method::GET, &format!("/users/{user_id}"), None)?
.error_for_status()?
.json::<ZulipOneUser>()?
.user;

Ok(response)
}

/// Perform a request against the Zulip API
fn req(
&self,
Expand All @@ -78,12 +85,18 @@ impl ZulipApi {
}
}

/// A collection of Zulip users
/// A collection of Zulip users, as returned from '/users'
#[derive(Deserialize)]
struct ZulipUsers {
members: Vec<ZulipUser>,
}

/// A collection of exactly one Zulip user, as returned from '/users/{user_id}'
#[derive(Deserialize)]
struct ZulipOneUser {
user: ZulipUser,
}

/// A single Zulip user
#[derive(Clone, Deserialize, PartialEq, Eq, Hash)]
pub(crate) struct ZulipUser {
Expand Down

0 comments on commit 2cd7457

Please sign in to comment.