Skip to content

Commit 6765f22

Browse files
committed
Zulip stream type check
1 parent 1983552 commit 6765f22

File tree

4 files changed

+50
-47
lines changed

4 files changed

+50
-47
lines changed

README.md

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ To use Postgres, you will need to install it and configure it:
8282
I recommend at least skimming the [GitHub webhook documentation](https://docs.github.com/en/developers/webhooks-and-events/webhooks/about-webhooks) if you are not familiar with webhooks.
8383
In order for GitHub's webhooks to reach your triagebot server, you'll need to figure out some way to route them to your machine.
8484
There are various options on how to do this.
85-
You can poke holes into your firewall or use a proxy, but you shouldn't expose your machine to the the internet.
85+
You can poke holes into your firewall or use a proxy, but you shouldn't expose your machine to the internet.
8686
There are various services which help with this problem.
8787
These generally involve running a program on your machine that connects to an external server which relays the hooks into your machine.
8888
There are several to choose from:
@@ -108,36 +108,6 @@ gh webhook forward --repo=ehuss/triagebot-test --events=* \
108108

109109
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.
110110

111-
### Zulip testing
112-
113-
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`).
114-
115-
You can also test Zulip webhooks locally with `curl`. For example, to test the Zulip hooks (commands sent to the
116-
Triagebot from the Rust lang Zulip), you start the triagebot on `localhost:8000` and then simulate a
117-
Zulip hook payload:
118-
``` sh
119-
curl http://localhost:8000/zulip-hook \
120-
-H "Content-Type: application/json" \
121-
-d '{
122-
"data": "<CMD>",
123-
"token": "<ZULIP_WEBHOOK_SECRET>",
124-
"message": {
125-
"sender_id": <YOUR_ID>,
126-
"recipient_id": <YOUR_ID>,
127-
"sender_full_name": "Randolph Carter",
128-
"sender_email": "[email protected]",
129-
"type": "stream"
130-
}
131-
}'
132-
```
133-
134-
Where:
135-
- `CMD` is the exact command you would issue @triagebot on Zulip (ex. open a direct chat with the
136-
bot and send "work show")
137-
- `ZULIP_WEBHOOK_SECRET`: can be anything. Must correspond to the env var `$ZULIP_WEBHOOK_SECRET` on your workstation
138-
- `YOUR_ID`: your GitHub user ID. Must be existing in your local triagebot database (table `users` and as
139-
foreign key also in `review_prefs`)
140-
141111
#### ngrok
142112

143113
The following is an example of using <https://ngrok.com/> to provide webhook forwarding.
@@ -158,6 +128,39 @@ You need to sign up for a free account, and also deal with configuring the GitHu
158128
* Secret: Enter a shared secret (some longish random text)
159129
* Events: "Send me everything"
160130

131+
### Zulip testing
132+
133+
If you want a test Zulip instance, you can [register a free one](https://zulip.com/new/). To have your local triagebot talk to this Zulip instance you need to:
134+
- Configure a (webhook forwarding service)[#configure-webhook-forwarding]
135+
- Create in your Zulip instance a webhook bot, using the forwarding address created before.
136+
- Launch your local triagebot setting `ZULIP_WEBHOOK_SECRET` to the webhook bot `key` value (you get that as part of the Zulip webhook bot configuration)
137+
- Set the other Zulip env vars as needed (see example in `.env.sample`).
138+
139+
You can also simulate a Zulip webhook payload with `cURL`. For example, this is the payload sent to the triagebot server when tagging a Zulip bot in a stream.
140+
``` sh
141+
curl http://localhost:8000/zulip-hook \
142+
-H "Content-Type: application/json" \
143+
-d '{
144+
"data": "<CMD>",
145+
"token": "<ZULIP_WEBHOOK_SECRET>",
146+
"message": {
147+
"sender_id": <YOUR_ZULIP_ID>,
148+
"recipient_id": <BOT_ZULIP_ID>,
149+
"sender_full_name": "Randolph Carter",
150+
"sender_email": "[email protected]",
151+
"type": "stream",
152+
"stream_id": 1234567,
153+
"subject": "Topic subject"
154+
}
155+
}'
156+
```
157+
158+
Where:
159+
- `CMD` is the full command you would issue on Zulip (ex. `@**triagebot** work show`)
160+
- `ZULIP_WEBHOOK_SECRET`: can be anything. Must correspond to the env var `$ZULIP_WEBHOOK_SECRET` on your workstation
161+
- `sender_*`: the Zulip user data sending the message. `sender_id` must be mapped to a GitHub user in this mapping: https://team-api.infra.rust-lang.org/v1/zulip-map.json
162+
- `recipient_id`: Zulip ID of the recipient of the message (in this case the Zulip bot)
163+
161164
### Cargo tests
162165

163166
You can run Cargo tests using `cargo test`. If you also want to run tests that access a Postgres database, you can specify an environment variables `TEST_DB_URL`, which should contain a connection string pointing to a running Postgres database instance:

src/zulip.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ struct Message {
5252
sender_email: String,
5353
/// The ID of the stream.
5454
///
55-
/// `None` if it is a private message.
55+
/// `None` if it is a direct message.
5656
stream_id: Option<u64>,
5757
/// The topic of the incoming message. Not the stream name.
5858
///
59-
/// Not currently set for private messages (though Zulip may change this in
60-
/// the future if it adds topics to private messages).
59+
/// Not currently set for direct messages (though Zulip may change this in
60+
/// the future if it adds topics to direct messages).
6161
subject: Option<String>,
62-
/// The type of the message: stream or private.
63-
#[allow(unused)]
62+
/// The type of the message: stream or direct.
6463
#[serde(rename = "type")]
6564
type_: String,
6665
}
@@ -76,7 +75,7 @@ impl Message {
7675
.as_ref()
7776
.expect("stream messages should have a topic"),
7877
},
79-
None => Recipient::Private {
78+
None => Recipient::Direct {
8079
id: self.sender_id,
8180
email: &self.sender_email,
8281
},
@@ -177,8 +176,9 @@ async fn handle_command<'a>(
177176
log::trace!("handling zulip command {:?}", command);
178177
let mut words: Vec<&str> = command.split_whitespace().collect();
179178

180-
// Missing stream means that this is a direct message
181-
if message_data.stream_id.is_none() {
179+
// Is this a direct or a stream message?
180+
// See: https://zulip.com/api/send-message#parameter-type
181+
if message_data.type_ == "direct" {
182182
// Handle impersonation
183183
let mut impersonated = false;
184184
if let Some(&"as") = words.get(0) {
@@ -253,7 +253,7 @@ async fn handle_command<'a>(
253253
);
254254

255255
MessageApiRequest {
256-
recipient: Recipient::Private {
256+
recipient: Recipient::Direct {
257257
id: user.user_id,
258258
email: &user.email,
259259
},
@@ -965,11 +965,11 @@ async fn post_waiter(
965965
recipient: Recipient::Stream {
966966
id: message
967967
.stream_id
968-
.ok_or_else(|| format_err!("private waiting not supported, missing stream id"))?,
968+
.ok_or_else(|| format_err!("direct waiting not supported, missing stream id"))?,
969969
topic: message
970970
.subject
971971
.as_deref()
972-
.ok_or_else(|| format_err!("private waiting not supported, missing topic"))?,
972+
.ok_or_else(|| format_err!("direct waiting not supported, missing topic"))?,
973973
},
974974
content: waiting.primary,
975975
}

src/zulip/api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) enum Recipient<'a> {
4747
id: u64,
4848
topic: &'a str,
4949
},
50-
Private {
50+
Direct {
5151
#[serde(skip)]
5252
id: u64,
5353
#[serde(rename = "to")]
@@ -81,7 +81,7 @@ impl Recipient<'_> {
8181
}
8282
format!("stream/{}-xxx/topic/{}", id, encoded_topic)
8383
}
84-
Recipient::Private { id, .. } => format!("pm-with/{}-xxx", id),
84+
Recipient::Direct { id, .. } => format!("pm-with/{}-xxx", id),
8585
}
8686
}
8787

src/zulip/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,15 @@ impl ZulipClient {
7070
.form(&SerializedApi {
7171
type_: match recipient {
7272
Recipient::Stream { .. } => "stream",
73-
Recipient::Private { .. } => "private",
73+
Recipient::Direct { .. } => "direct",
7474
},
7575
to: match recipient {
7676
Recipient::Stream { id, .. } => id.to_string(),
77-
Recipient::Private { email, .. } => email.to_string(),
77+
Recipient::Direct { email, .. } => email.to_string(),
7878
},
7979
topic: match recipient {
8080
Recipient::Stream { topic, .. } => Some(topic),
81-
Recipient::Private { .. } => None,
81+
Recipient::Direct { .. } => None,
8282
},
8383
content,
8484
})

0 commit comments

Comments
 (0)