Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,8 @@ pub struct Issue {
// User performing an `action` (or PR/issue author)
pub user: User,
pub labels: Vec<Label>,
// Users assigned to the issue/pr after `action` has been performed
// Users assigned to the issue/pr after `action` has been performed issue
// (PR reviewers or issue assignees)
// These are NOT the same as `IssueEvent.assignee`
pub assignees: Vec<User>,
/// Indicator if this is a pull request.
Expand Down
35 changes: 35 additions & 0 deletions src/handlers/notify_zulip.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::github::User;
use crate::zulip::api::Recipient;
use crate::zulip::render_zulip_username;
use crate::{
config::{NotifyZulipConfig, NotifyZulipLabelConfig, NotifyZulipTablesConfig},
github::{Issue, IssuesAction, IssuesEvent, Label},
handlers::Context,
};
use futures::future::join_all;
use tracing as log;

pub(super) struct NotifyZulipInput {
Expand Down Expand Up @@ -214,10 +217,15 @@ pub(super) async fn handle_input<'a>(
topic: &topic,
};

// Issue/PR authors/reviewers will receive a mention if the template has `{recipients}`.
let recipients = &mut event.issue.assignees.clone();
recipients.push(event.issue.user.clone());

Comment on lines +220 to +223
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different from before: if the template has a {recipients} placeholder, author and reviewers of the PR/issue will receive a mention.

(this change is very specific to my use-case but I think it can be used in other contexts)

for msg in msgs {
let msg = msg.replace("{number}", &event.issue.number.to_string());
let msg = msg.replace("{title}", &event.issue.title);
let msg = replace_team_to_be_nominated(&event.issue.labels, msg);
let msg = msg.replace("{recipients}", &get_zulip_ids(ctx, &recipients).await);

let req = crate::zulip::MessageApiRequest {
recipient,
Expand All @@ -236,6 +244,33 @@ pub(super) async fn handle_input<'a>(
Ok(())
}

async fn get_zulip_ids(ctx: &Context, recipients: &[User]) -> String {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code here is duplicating what we have in src/zulip.rs so a better way would be to refactor that module and expose functions. Maybe another time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to generate the mention it's using the trick to write @|12345 where 12345 is the Zulip ID of the user.

let gh_ids_fut = recipients
.iter()
.map(|recipient| async move { ctx.team.github_to_zulip_id(recipient.id).await });
let zulip_ids = join_all(gh_ids_fut).await;

let zulip_ids = zulip_ids
.iter()
.filter_map(|x| {
if let Ok(id2) = x.as_ref()
&& let Some(id) = *id2
{
Some(render_zulip_username(id))
} else {
None
}
})
.collect::<Vec<String>>();

if !zulip_ids.is_empty() {
zulip_ids.join(", ")
} else {
"".to_string()
}
}

/// Replace the placeholder "{team}" with the correct team name
fn replace_team_to_be_nominated(labels: &[Label], msg: String) -> String {
let teams = labels
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/zulip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ async fn lookup_github_username(ctx: &Context, zulip_username: &str) -> anyhow::
))
}

fn render_zulip_username(zulip_id: u64) -> String {
pub fn render_zulip_username(zulip_id: u64) -> String {
// Rendering the username directly was running into some encoding issues, so we use
// the special `|<user-id>` syntax instead.
// @**|<zulip-id>** is Zulip syntax that will render as the username (and a link) of the user
Expand Down