Skip to content

Commit

Permalink
Merge pull request #1872 from apiraino/move-zulip-config-to-env-vars
Browse files Browse the repository at this point in the history
Read Zulip config from env vars
  • Loading branch information
ehuss authored Jan 4, 2025
2 parents 7f35093 + ec08941 commit cde54de
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ GITHUB_WEBHOOK_SECRET=MUST_BE_CONFIGURED
# For example write blahblahblah here, if you want for this bot to
# respond to @blahblahblah claim.
# TRIAGEBOT_USERNAME=CAN_BE_CONFIGURED

# Set your own Zulip instance (local testing only)
# ZULIP_URL=https://testinstance.zulichat.com

# Used for authenticating a bot
# [email protected]
# ZULIP_API_TOKEN=yyy

# Authenticates inbound webhooks from Github
# ZULIP_TOKEN=xxx
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ gh webhook forward --repo=ehuss/triagebot-test --events=* \

Where the value in `--secret` is the secret value you place in `GITHUB_WEBHOOK_SECRET` in the `.env` file, and `--repo` is the repo you want to test against.

### Zulip testing

If you are modifying code that sends message to Zulip and want to test your changes, you can register a [new free Zulip instance](https://zulip.com/new/). Before launching the triagebot locally, set the Zulip env vars to connect to your test instance (see example in `.env.sample`).

#### ngrok

The following is an example of using <https://ngrok.com/> to provide webhook forwarding.
Expand Down
31 changes: 18 additions & 13 deletions src/zulip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ use anyhow::{format_err, Context as _};
use std::env;
use std::fmt::Write as _;
use std::str::FromStr;
use std::sync::LazyLock;
use tracing as log;

static ZULIP_URL: LazyLock<String> =
LazyLock::new(|| env::var("ZULIP_URL").unwrap_or("https://rust-lang.zulipchat.com".into()));
static ZULIP_BOT_EMAIL: LazyLock<String> = LazyLock::new(|| {
env::var("ZULIP_BOT_EMAIL").unwrap_or("[email protected]".into())
});

#[derive(Debug, serde::Deserialize)]
pub struct Request {
/// Markdown body of the sent message.
Expand Down Expand Up @@ -71,8 +78,6 @@ struct Response {
content: String,
}

pub const BOT_EMAIL: &str = "[email protected]";

pub async fn to_github_id(client: &GithubClient, zulip_id: u64) -> anyhow::Result<Option<u64>> {
let map = crate::team_data::zulip_map(client).await?;
Ok(map.users.get(&zulip_id).copied())
Expand Down Expand Up @@ -299,8 +304,8 @@ async fn execute_for_other_user(
let members = ctx
.github
.raw()
.get("https://rust-lang.zulipchat.com/api/v1/users")
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
.get(format!("{}/api/v1/users", *ZULIP_URL))
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
.send()
.await
.map_err(|e| format_err!("Failed to get list of zulip users: {e:?}."))?;
Expand Down Expand Up @@ -414,7 +419,7 @@ impl Recipient<'_> {
}

pub fn url(&self) -> String {
format!("https://rust-lang.zulipchat.com/#narrow/{}", self.narrow())
format!("{}/#narrow/{}", *ZULIP_URL, self.narrow())
}
}

Expand Down Expand Up @@ -470,8 +475,8 @@ impl<'a> MessageApiRequest<'a> {
}

Ok(client
.post("https://rust-lang.zulipchat.com/api/v1/messages")
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
.post(format!("{}/api/v1/messages", *ZULIP_URL))
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
.form(&SerializedApi {
type_: match self.recipient {
Recipient::Stream { .. } => "stream",
Expand Down Expand Up @@ -522,10 +527,10 @@ impl<'a> UpdateMessageApiRequest<'a> {

Ok(client
.patch(&format!(
"https://rust-lang.zulipchat.com/api/v1/messages/{}",
self.message_id
"{}/api/v1/messages/{}",
*ZULIP_URL, self.message_id
))
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
.form(&SerializedApi {
topic: self.topic,
propagate_mode: self.propagate_mode,
Expand Down Expand Up @@ -715,10 +720,10 @@ impl<'a> AddReaction<'a> {

Ok(client
.post(&format!(
"https://rust-lang.zulipchat.com/api/v1/messages/{}/reactions",
self.message_id
"{}/api/v1/messages/{}/reactions",
*ZULIP_URL, self.message_id
))
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
.form(&self)
.send()
.await?)
Expand Down

0 comments on commit cde54de

Please sign in to comment.