-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read Zulip config from env vars #1872
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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()) | ||
|
@@ -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:?}."))?; | ||
|
@@ -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()) | ||
} | ||
} | ||
|
||
|
@@ -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", | ||
|
@@ -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, | ||
|
@@ -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?) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to also set this global but then it needs to be cloned multiple times, so unsure what's the best solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I see the concern about needing to be cloned. If you do it in a LazyLock like you did the URL, then you can do
&*ZULIP_BOT_EMAIL
to get a reference. Thebasic_auth
method does not need an owned value.An alternative would be to place all these environment values inside a structure stored in the
Context
, and just pass that around as an argument (or pass in the whole Context).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for a second opinion! I've amended the code with just adding another LazyLock to keep changes as contained here as possible.