Skip to content

Commit cde54de

Browse files
authored
Merge pull request #1872 from apiraino/move-zulip-config-to-env-vars
Read Zulip config from env vars
2 parents 7f35093 + ec08941 commit cde54de

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

.env.sample

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ 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+
20+
# ZULIP_API_TOKEN=yyy
21+
22+
# Authenticates inbound webhooks from Github
23+
# ZULIP_TOKEN=xxx

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: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,15 @@ 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+
static ZULIP_BOT_EMAIL: LazyLock<String> = LazyLock::new(|| {
18+
env::var("ZULIP_BOT_EMAIL").unwrap_or("[email protected]".into())
19+
});
20+
1421
#[derive(Debug, serde::Deserialize)]
1522
pub struct Request {
1623
/// Markdown body of the sent message.
@@ -71,8 +78,6 @@ struct Response {
7178
content: String,
7279
}
7380

74-
pub const BOT_EMAIL: &str = "[email protected]";
75-
7681
pub async fn to_github_id(client: &GithubClient, zulip_id: u64) -> anyhow::Result<Option<u64>> {
7782
let map = crate::team_data::zulip_map(client).await?;
7883
Ok(map.users.get(&zulip_id).copied())
@@ -299,8 +304,8 @@ async fn execute_for_other_user(
299304
let members = ctx
300305
.github
301306
.raw()
302-
.get("https://rust-lang.zulipchat.com/api/v1/users")
303-
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
307+
.get(format!("{}/api/v1/users", *ZULIP_URL))
308+
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
304309
.send()
305310
.await
306311
.map_err(|e| format_err!("Failed to get list of zulip users: {e:?}."))?;
@@ -414,7 +419,7 @@ impl Recipient<'_> {
414419
}
415420

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

@@ -470,8 +475,8 @@ impl<'a> MessageApiRequest<'a> {
470475
}
471476

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

523528
Ok(client
524529
.patch(&format!(
525-
"https://rust-lang.zulipchat.com/api/v1/messages/{}",
526-
self.message_id
530+
"{}/api/v1/messages/{}",
531+
*ZULIP_URL, self.message_id
527532
))
528-
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
533+
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
529534
.form(&SerializedApi {
530535
topic: self.topic,
531536
propagate_mode: self.propagate_mode,
@@ -715,10 +720,10 @@ impl<'a> AddReaction<'a> {
715720

716721
Ok(client
717722
.post(&format!(
718-
"https://rust-lang.zulipchat.com/api/v1/messages/{}/reactions",
719-
self.message_id
723+
"{}/api/v1/messages/{}/reactions",
724+
*ZULIP_URL, self.message_id
720725
))
721-
.basic_auth(BOT_EMAIL, Some(&bot_api_token))
726+
.basic_auth(&*ZULIP_BOT_EMAIL, Some(&bot_api_token))
722727
.form(&self)
723728
.send()
724729
.await?)

0 commit comments

Comments
 (0)