Skip to content

Commit e8e9941

Browse files
committed
Read Zulip config from env vars
1 parent 0400514 commit e8e9941

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

.env.sample

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,11 @@ GITHUB_WEBHOOK_SECRET=MUST_BE_CONFIGURED
1111
# For example write blahblahblah here, if you want for this bot to
1212
# respond to @blahblahblah claim.
1313
# TRIAGEBOT_USERNAME=CAN_BE_CONFIGURED
14+
15+
# Set your own Zulip instance (local testing only)
16+
# ZULIP_URL=https://testinstance.zulichat.com
17+
18+
# Used for authenticating a bot
19+
# ZULIP_BOT_EMAIL=bot@testinstance.zulipchat.com
20+
# ZULIP_TOKEN=xxx
21+
# ZULIP_API_TOKEN=yyy

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ gh webhook forward --repo=ehuss/triagebot-test --events=* \
9595

9696
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.
9797

98+
### Zulip testing
99+
100+
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`).
101+
98102
#### ngrok
99103

100104
The following is an example of using <https://ngrok.com/> to provide webhook forwarding.

src/zulip.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ use anyhow::{format_err, Context as _};
99
use std::env;
1010
use std::fmt::Write as _;
1111
use std::str::FromStr;
12+
use std::sync::LazyLock;
1213
use tracing as log;
1314

15+
static ZULIP_URL: LazyLock<String> =
16+
LazyLock::new(|| env::var("ZULIP_URL").unwrap_or("https://rust-lang.zulipchat.com".into()));
17+
1418
#[derive(Debug, serde::Deserialize)]
1519
pub struct Request {
1620
/// Markdown body of the sent message.
@@ -71,8 +75,6 @@ struct Response {
7175
content: String,
7276
}
7377

74-
pub const BOT_EMAIL: &str = "triage-rust-lang-bot@zulipchat.com";
75-
7678
pub async fn to_github_id(client: &GithubClient, zulip_id: u64) -> anyhow::Result<Option<u64>> {
7779
let map = crate::team_data::zulip_map(client).await?;
7880
Ok(map.users.get(&zulip_id).copied())
@@ -295,12 +297,14 @@ async fn execute_for_other_user(
295297
command
296298
};
297299
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
300+
let bot_email =
301+
env::var("ZULIP_BOT_EMAIL").unwrap_or("triage-rust-lang-bot@zulipchat.com".into());
298302

299303
let members = ctx
300304
.github
301305
.raw()
302-
.get("https://rust-lang.zulipchat.com/api/v1/users")
303-
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
306+
.get(format!("{}/api/v1/users", *ZULIP_URL))
307+
.basic_auth(bot_email, Some(&bot_api_token))
304308
.send()
305309
.await
306310
.map_err(|e| format_err!("Failed to get list of zulip users: {e:?}."))?;
@@ -414,7 +418,7 @@ impl Recipient<'_> {
414418
}
415419

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

@@ -458,6 +462,8 @@ impl<'a> MessageApiRequest<'a> {
458462

459463
pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
460464
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
465+
let bot_email =
466+
env::var("ZULIP_BOT_EMAIL").unwrap_or("triage-rust-lang-bot@zulipchat.com".into());
461467

462468
#[derive(serde::Serialize)]
463469
struct SerializedApi<'a> {
@@ -470,8 +476,8 @@ impl<'a> MessageApiRequest<'a> {
470476
}
471477

472478
Ok(client
473-
.post("https://rust-lang.zulipchat.com/api/v1/messages")
474-
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
479+
.post(format!("{}/api/v1/messages", *ZULIP_URL))
480+
.basic_auth(bot_email, Some(&bot_api_token))
475481
.form(&SerializedApi {
476482
type_: match self.recipient {
477483
Recipient::Stream { .. } => "stream",
@@ -509,6 +515,8 @@ pub struct UpdateMessageApiRequest<'a> {
509515
impl<'a> UpdateMessageApiRequest<'a> {
510516
pub async fn send(&self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
511517
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
518+
let bot_email =
519+
env::var("ZULIP_BOT_EMAIL").unwrap_or("triage-rust-lang-bot@zulipchat.com".into());
512520

513521
#[derive(serde::Serialize)]
514522
struct SerializedApi<'a> {
@@ -522,10 +530,10 @@ impl<'a> UpdateMessageApiRequest<'a> {
522530

523531
Ok(client
524532
.patch(&format!(
525-
"https://rust-lang.zulipchat.com/api/v1/messages/{}",
526-
self.message_id
533+
"{}/api/v1/messages/{}",
534+
*ZULIP_URL, self.message_id
527535
))
528-
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
536+
.basic_auth(bot_email, Some(&bot_api_token))
529537
.form(&SerializedApi {
530538
topic: self.topic,
531539
propagate_mode: self.propagate_mode,
@@ -712,13 +720,15 @@ struct AddReaction<'a> {
712720
impl<'a> AddReaction<'a> {
713721
pub async fn send(self, client: &reqwest::Client) -> anyhow::Result<reqwest::Response> {
714722
let bot_api_token = env::var("ZULIP_API_TOKEN").expect("ZULIP_API_TOKEN");
723+
let bot_email =
724+
env::var("ZULIP_BOT_EMAIL").unwrap_or("triage-rust-lang-bot@zulipchat.com".into());
715725

716726
Ok(client
717727
.post(&format!(
718-
"https://rust-lang.zulipchat.com/api/v1/messages/{}/reactions",
719-
self.message_id
728+
"{}/api/v1/messages/{}/reactions",
729+
*ZULIP_URL, self.message_id
720730
))
721-
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
731+
.basic_auth(bot_email, Some(&bot_api_token))
722732
.form(&self)
723733
.send()
724734
.await?)

0 commit comments

Comments
 (0)