forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

emit.rs809 lines, 24.9 KiBHistoryRawDownload
1//! Queues webhook deliveries for repo events.
2//!
3//! Callers use the `emit_*` helpers, one per event. Each helper builds its payload and hands it
4//! to [`emit`], which looks up the active hooks subscribed to the event, inserts a delivery for
5//! each, and wakes the worker. Nothing here fails the caller: problems are logged.
6
7use forge_core::webhooks::{Webhook, WebhookEvent};
8use forge_core::{Comment, Issue, Label, PullRequest, Repo, Review, User};
9use forge_git::browse::CommitInfo;
10use serde_json::{Map, Value, json};
11
12use super::payload;
13use crate::internal::is_zero_oid;
14use crate::state::AppState;
15
16/// Commits read for a push that moves an existing ref.
17const MAX_PUSH_COMMITS: usize = 500;
18/// Commits read for a push that creates a ref, which reaches the whole history.
19const MAX_NEW_REF_COMMITS: usize = 50;
20
21/// Who made a change. An empty `id` is a system change, sent as `"sender": null`.
22#[derive(Clone)]
23pub struct Sender {
24 pub id: String,
25 pub username: String,
26}
27
28impl Sender {
29 pub fn of(user: &User) -> Self {
30 Self {
31 id: user.id.clone(),
32 username: user.username.clone(),
33 }
34 }
35
36 pub fn system() -> Self {
37 Self {
38 id: String::new(),
39 username: String::new(),
40 }
41 }
42}
43
44/// The sender for an actor id. Empty or unknown ids are system changes.
45pub async fn lookup_sender(state: &AppState, actor_id: &str) -> Sender {
46 if actor_id.is_empty() {
47 return Sender::system();
48 }
49 match state.store.user_by_id(actor_id).await {
50 Ok(Some(user)) => Sender::of(&user),
51 Ok(None) => Sender::system(),
52 Err(e) => {
53 tracing::error!(error = %e, actor_id, "webhooks: sender lookup failed");
54 Sender::system()
55 }
56 }
57}
58
59/// Queues `event` for every active hook of `repo` subscribed to it. `payload` is the event's
60/// own fields; `action` (when non-empty), `repository` and `sender` are added here.
61pub async fn emit(
62 state: &AppState,
63 repo: &Repo,
64 sender: &Sender,
65 event: WebhookEvent,
66 action: &str,
67 payload: Value,
68) {
69 let hooks = subscribed(state, repo, event).await;
70 if hooks.is_empty() {
71 return;
72 }
73 let body = envelope(state, repo, sender, action, payload).await;
74 enqueue(state, &hooks, event, action, &body).await;
75}
76
77/// The push, create and delete events for one ref update. Only `refs/heads/*` and `refs/tags/*`
78/// produce events; other refs are ignored. `old` is the zero oid for a create, and `new` is the
79/// zero oid for a delete.
80pub async fn emit_ref_update(
81 state: &AppState,
82 repo: &Repo,
83 sender: &Sender,
84 refname: &str,
85 old: &str,
86 new: &str,
87) {
88 let Some((ref_type, name)) = split_ref(refname) else {
89 return;
90 };
91 if !subscribed(state, repo, WebhookEvent::Push).await.is_empty() {
92 let (commits, total) = if is_zero_oid(new) {
93 (Vec::new(), 0)
94 } else {
95 new_commits(state, repo, old, new).await
96 };
97 let payload = payload::push(
98 &state.config.base_url,
99 repo,
100 refname,
101 old,
102 new,
103 &commits,
104 total,
105 );
106 emit(state, repo, sender, WebhookEvent::Push, "", payload).await;
107 }
108 if is_zero_oid(old) {
109 let payload = payload::ref_change(ref_type, name);
110 emit(state, repo, sender, WebhookEvent::Create, "", payload).await;
111 }
112 if is_zero_oid(new) {
113 let payload = payload::ref_change(ref_type, name);
114 emit(state, repo, sender, WebhookEvent::Delete, "", payload).await;
115 }
116}
117
118/// An `issues` event for an issue (not a pull request).
119pub async fn emit_issue(
120 state: &AppState,
121 repo: &Repo,
122 sender: &Sender,
123 action: &str,
124 issue: &Issue,
125) {
126 let payload = payload::issues(&state.config.base_url, repo, issue);
127 emit(state, repo, sender, WebhookEvent::Issues, action, payload).await;
128}
129
130/// A `pull_request` event.
131pub async fn emit_pull(
132 state: &AppState,
133 repo: &Repo,
134 sender: &Sender,
135 action: &str,
136 pr: &PullRequest,
137) {
138 let payload = payload::pull_request(&state.config.base_url, repo, pr);
139 emit(
140 state,
141 repo,
142 sender,
143 WebhookEvent::PullRequest,
144 action,
145 payload,
146 )
147 .await;
148}
149
150/// The `opened`, `closed`, `reopened`, `merged`, `edited` or `synchronize` event for item
151/// `number`, reloaded from the store so the payload has its current state. `pull` picks between
152/// a pull request and an issue. A missing item sends nothing.
153pub async fn emit_item_action(
154 state: &AppState,
155 repo: &Repo,
156 sender: &Sender,
157 pull: bool,
158 number: i64,
159 action: &str,
160) {
161 if pull {
162 match state.store.pull_by_number(&repo.id, number).await {
163 Ok(Some(pr)) => emit_pull(state, repo, sender, action, &pr).await,
164 Ok(None) => {}
165 Err(e) => {
166 tracing::error!(error = %e, repo_id = %repo.id, number, "webhooks: loading pull request failed");
167 }
168 }
169 } else {
170 match state.store.issue_by_number(&repo.id, number).await {
171 Ok(Some(issue)) => emit_issue(state, repo, sender, action, &issue).await,
172 Ok(None) => {}
173 Err(e) => {
174 tracing::error!(error = %e, repo_id = %repo.id, number, "webhooks: loading issue failed");
175 }
176 }
177 }
178}
179
180/// `labeled` or `unlabeled` for one label on an item. `pull` picks the `pull_request` event over
181/// `issues`.
182pub async fn emit_label(
183 state: &AppState,
184 repo: &Repo,
185 sender: &Sender,
186 action: &str,
187 pull: bool,
188 issue: &Issue,
189 label: &Label,
190) {
191 let field = ("label", json!({"name": label.name, "color": label.color}));
192 emit_item_field(state, repo, sender, action, pull, issue, field).await;
193}
194
195/// `assigned` or `unassigned` for one username on an item. `pull` picks the `pull_request` event
196/// over `issues`.
197pub async fn emit_assignee(
198 state: &AppState,
199 repo: &Repo,
200 sender: &Sender,
201 action: &str,
202 pull: bool,
203 issue: &Issue,
204 username: &str,
205) {
206 let field = ("assignee", json!({"username": username}));
207 emit_item_field(state, repo, sender, action, pull, issue, field).await;
208}
209
210/// Sends `labeled` for each label added and `unlabeled` for each removed. Labels are matched by
211/// name, so a recolored label is not a change.
212pub async fn emit_label_changes(
213 state: &AppState,
214 repo: &Repo,
215 sender: &Sender,
216 pull: bool,
217 issue: &Issue,
218 before: &[Label],
219 after: &[Label],
220) {
221 let has = |list: &[Label], name: &str| list.iter().any(|label| label.name == name);
222 for label in after.iter().filter(|label| !has(before, &label.name)) {
223 emit_label(state, repo, sender, "labeled", pull, issue, label).await;
224 }
225 for label in before.iter().filter(|label| !has(after, &label.name)) {
226 emit_label(state, repo, sender, "unlabeled", pull, issue, label).await;
227 }
228}
229
230/// Sends `assigned` for each username added and `unassigned` for each removed.
231pub async fn emit_assignee_changes(
232 state: &AppState,
233 repo: &Repo,
234 sender: &Sender,
235 pull: bool,
236 issue: &Issue,
237 before: &[String],
238 after: &[String],
239) {
240 for name in after.iter().filter(|name| !before.contains(name)) {
241 emit_assignee(state, repo, sender, "assigned", pull, issue, name).await;
242 }
243 for name in before.iter().filter(|name| !after.contains(name)) {
244 emit_assignee(state, repo, sender, "unassigned", pull, issue, name).await;
245 }
246}
247
248/// `milestoned` for the milestone `after` and `demilestoned` for the one `before`, given their
249/// titles (`None` for no milestone). Nothing is sent when the titles match.
250pub async fn emit_milestone_change(
251 state: &AppState,
252 repo: &Repo,
253 sender: &Sender,
254 pull: bool,
255 issue: &Issue,
256 before: Option<&str>,
257 after: Option<&str>,
258) {
259 if before == after {
260 return;
261 }
262 if let Some(title) = before {
263 let field = ("milestone", json!({"title": title}));
264 emit_item_field(state, repo, sender, "demilestoned", pull, issue, field).await;
265 }
266 if let Some(title) = after {
267 let field = ("milestone", json!({"title": title}));
268 emit_item_field(state, repo, sender, "milestoned", pull, issue, field).await;
269 }
270}
271
272/// An `issue_comment` event. `pull` says whether the comment is on a pull request.
273pub async fn emit_comment(
274 state: &AppState,
275 repo: &Repo,
276 sender: &Sender,
277 action: &str,
278 pull: bool,
279 issue: &Issue,
280 comment: &Comment,
281) {
282 let payload = payload::issue_comment(&state.config.base_url, repo, issue, pull, comment);
283 emit(
284 state,
285 repo,
286 sender,
287 WebhookEvent::IssueComment,
288 action,
289 payload,
290 )
291 .await;
292}
293
294/// A `pull_request_review` event with action `submitted`.
295pub async fn emit_review(
296 state: &AppState,
297 repo: &Repo,
298 sender: &Sender,
299 pr: &PullRequest,
300 review: &Review,
301) {
302 let payload = payload::pull_request_review(&state.config.base_url, repo, pr, review);
303 emit(
304 state,
305 repo,
306 sender,
307 WebhookEvent::PullRequestReview,
308 "submitted",
309 payload,
310 )
311 .await;
312}
313
314/// Queues a `ping` for one hook, whether or not it subscribes to pings. Used by the "Test"
315/// button and when a hook is created. Fails only when the repo is gone or the delivery cannot be
316/// queued.
317pub async fn emit_ping(state: &AppState, hook: &Webhook) -> forge_core::Result<()> {
318 let repo = state
319 .store
320 .repo_by_id(&hook.repo_id)
321 .await?
322 .ok_or(forge_core::Error::NotFound)?;
323 let body = envelope(state, &repo, &Sender::system(), "", payload::ping(&hook.id)).await;
324 if enqueue(
325 state,
326 std::slice::from_ref(hook),
327 WebhookEvent::Ping,
328 "",
329 &body,
330 )
331 .await
332 == 0
333 {
334 return Err(forge_core::Error::Database(
335 "could not queue the ping delivery".into(),
336 ));
337 }
338 Ok(())
339}
340
341/// Sends an item event (`pull_request` when `pull`, else `issues`) with one extra top-level
342/// field, `(key, value)`. A pull request is reloaded from the store so its payload is complete;
343/// a missing one sends nothing.
344async fn emit_item_field(
345 state: &AppState,
346 repo: &Repo,
347 sender: &Sender,
348 action: &str,
349 pull: bool,
350 issue: &Issue,
351 (key, field): (&str, Value),
352) {
353 let (event, mut payload) = if pull {
354 match state.store.pull_by_number(&repo.id, issue.number).await {
355 Ok(Some(pr)) => (
356 WebhookEvent::PullRequest,
357 payload::pull_request(&state.config.base_url, repo, &pr),
358 ),
359 Ok(None) => return,
360 Err(e) => {
361 tracing::error!(error = %e, repo_id = %repo.id, number = issue.number, "webhooks: loading pull request failed");
362 return;
363 }
364 }
365 } else {
366 (
367 WebhookEvent::Issues,
368 payload::issues(&state.config.base_url, repo, issue),
369 )
370 };
371 if let Value::Object(map) = &mut payload {
372 map.insert(key.to_owned(), field);
373 }
374 emit(state, repo, sender, event, action, payload).await;
375}
376
377/// Active hooks of `repo` subscribed to `event`. A lookup error is logged and reads as none.
378async fn subscribed(state: &AppState, repo: &Repo, event: WebhookEvent) -> Vec<Webhook> {
379 match state.store.hooks_for_event(&repo.id, event).await {
380 Ok(hooks) => hooks,
381 Err(e) => {
382 tracing::error!(error = %e, repo_id = %repo.id, event = event.as_str(), "webhooks: hook lookup failed");
383 Vec::new()
384 }
385 }
386}
387
388/// Whether the repo's owner is an organization (`"org"`) or a person (`"user"`). A failed lookup
389/// reads as a person, since the payload's owner name is correct either way.
390async fn owner_kind(state: &AppState, repo: &Repo) -> &'static str {
391 match state.store.user_by_id(&repo.owner_id).await {
392 Ok(Some(owner)) if owner.is_org() => "org",
393 _ => "user",
394 }
395}
396
397/// The full JSON body: the event fields plus `action`, `repository` and `sender`.
398async fn envelope(
399 state: &AppState,
400 repo: &Repo,
401 sender: &Sender,
402 action: &str,
403 payload: Value,
404) -> String {
405 let mut map = match payload {
406 Value::Object(map) => map,
407 _ => Map::new(),
408 };
409 if !action.is_empty() {
410 map.insert("action".to_owned(), json!(action));
411 }
412 let kind = owner_kind(state, repo).await;
413 map.insert(
414 "repository".to_owned(),
415 payload::repository(&state.config.base_url, repo, kind),
416 );
417 map.insert(
418 "sender".to_owned(),
419 payload::sender(&sender.id, &sender.username),
420 );
421 Value::Object(map).to_string()
422}
423
424/// Inserts a delivery per hook and wakes the worker after each one. Returns how many were
425/// queued. A failed insert is logged and skipped.
426async fn enqueue(
427 state: &AppState,
428 hooks: &[Webhook],
429 event: WebhookEvent,
430 action: &str,
431 body: &str,
432) -> usize {
433 // The worker adds the delivery GUID and the signature at send time, so they are not stored.
434 let headers = json!({
435 "Content-Type": "application/json",
436 "X-Forge-Event": event.as_str(),
437 })
438 .to_string();
439 let mut queued = 0;
440 for hook in hooks {
441 match state
442 .store
443 .enqueue_delivery(&hook.id, event, action, &headers, body)
444 .await
445 {
446 Ok(_) => {
447 queued += 1;
448 state.webhook_waker.notify_one();
449 crate::live::bump_settings(&state.live, &hook.repo_id);
450 }
451 Err(e) => {
452 tracing::error!(error = %e, hook_id = %hook.id, event = event.as_str(), "webhooks: enqueue failed");
453 }
454 }
455 }
456 queued
457}
458
459/// `refs/heads/<name>` as `("branch", name)` and `refs/tags/<name>` as `("tag", name)`.
460fn split_ref(refname: &str) -> Option<(&'static str, &str)> {
461 if let Some(name) = refname.strip_prefix("refs/heads/") {
462 Some(("branch", name))
463 } else {
464 refname.strip_prefix("refs/tags/").map(|name| ("tag", name))
465 }
466}
467
468/// The commits a push brought in, oldest first, and their count. A failure is logged and reads
469/// as no commits, so the push event still goes out.
470async fn new_commits(
471 state: &AppState,
472 repo: &Repo,
473 old: &str,
474 new: &str,
475) -> (Vec<CommitInfo>, usize) {
476 let dir = match forge_git::repo::repo_path(&state.git.data_dir, &repo.id) {
477 Ok(dir) => dir,
478 Err(e) => {
479 tracing::error!(error = %e, repo_id = %repo.id, "webhooks: bad repo path for push commits");
480 return (Vec::new(), 0);
481 }
482 };
483 let old = (!is_zero_oid(old)).then_some(old);
484 let max = if old.is_some() {
485 MAX_PUSH_COMMITS
486 } else {
487 MAX_NEW_REF_COMMITS
488 };
489 match forge_git::history::new_commits(&state.git, &dir, old, new, max).await {
490 Ok(commits) => {
491 let total = commits.len();
492 (commits, total)
493 }
494 Err(e) => {
495 tracing::error!(error = %e, repo_id = %repo.id, "webhooks: listing push commits failed");
496 (Vec::new(), 0)
497 }
498 }
499}
500
501#[cfg(test)]
502mod tests {
503 use super::*;
504 use crate::test_support::{KEY, test_state};
505 use forge_core::Visibility;
506
507 /// Three hooks on one repo: an active push hook, an active issues hook, and an inactive push
508 /// hook. Returns the state, the repo, and the hook ids in that order.
509 async fn fixture(dir: &std::path::Path) -> (AppState, Repo, [String; 3]) {
510 let state = test_state(dir).await;
511 let owner = state
512 .store
513 .create_user("tahinli", None, "unused", false)
514 .await
515 .expect("user");
516 let repo = state
517 .store
518 .create_repo(&owner.id, "proj", "", Visibility::Public, "main")
519 .await
520 .expect("repo");
521 let sealed = forge_core::secret::seal(&KEY, b"s").expect("seal");
522 let mut ids = Vec::new();
523 for (events, active) in [
524 (vec![WebhookEvent::Push], true),
525 (vec![WebhookEvent::Issues], true),
526 (vec![WebhookEvent::Push], false),
527 ] {
528 let hook = state
529 .store
530 .create_webhook(
531 &repo.id,
532 &owner.id,
533 "https://hooks.example/h",
534 sealed.as_bytes(),
535 &events,
536 active,
537 )
538 .await
539 .expect("hook");
540 ids.push(hook.id);
541 }
542 let ids: [String; 3] = ids.try_into().expect("three hooks");
543 (state, repo, ids)
544 }
545
546 #[tokio::test]
547 async fn emit_queues_only_for_subscribed_active_hooks() {
548 let tmp = tempfile::tempdir().expect("tempdir");
549 let (state, repo, [push_hook, issues_hook, inactive_push]) = fixture(tmp.path()).await;
550
551 emit(
552 &state,
553 &repo,
554 &Sender::system(),
555 WebhookEvent::Push,
556 "",
557 json!({"ref": "refs/heads/main"}),
558 )
559 .await;
560
561 assert_eq!(
562 state
563 .store
564 .list_deliveries(&push_hook, 10)
565 .await
566 .expect("list")
567 .len(),
568 1
569 );
570 assert!(
571 state
572 .store
573 .list_deliveries(&issues_hook, 10)
574 .await
575 .expect("list")
576 .is_empty()
577 );
578 assert!(
579 state
580 .store
581 .list_deliveries(&inactive_push, 10)
582 .await
583 .expect("list")
584 .is_empty()
585 );
586 }
587
588 #[tokio::test]
589 async fn emit_body_has_repository_sender_and_action_only_when_given() {
590 let tmp = tempfile::tempdir().expect("tempdir");
591 let (state, repo, [_, issues_hook, _]) = fixture(tmp.path()).await;
592 let sender = Sender {
593 id: "u1".into(),
594 username: "tahinli".into(),
595 };
596
597 emit(
598 &state,
599 &repo,
600 &sender,
601 WebhookEvent::Issues,
602 "opened",
603 json!({"issue": {"number": 3}}),
604 )
605 .await;
606
607 let rows = state
608 .store
609 .list_deliveries(&issues_hook, 10)
610 .await
611 .expect("list");
612 let row = rows.first().expect("one delivery");
613 assert_eq!(row.action, "opened");
614 let body: Value = serde_json::from_str(&row.request_body).expect("json");
615 assert_eq!(body["action"], "opened");
616 assert_eq!(body["issue"]["number"], 3);
617 assert_eq!(body["repository"]["full_name"], "tahinli/proj");
618 assert_eq!(body["sender"], json!({"id": "u1", "username": "tahinli"}));
619 let headers: Value = serde_json::from_str(&row.request_headers).expect("json");
620 assert_eq!(headers["X-Forge-Event"], "issues");
621 assert!(headers.get("X-Forge-Signature-256").is_none());
622 }
623
624 #[tokio::test]
625 async fn system_changes_send_null_sender_and_no_action() {
626 let tmp = tempfile::tempdir().expect("tempdir");
627 let (state, repo, [push_hook, _, _]) = fixture(tmp.path()).await;
628 emit(
629 &state,
630 &repo,
631 &Sender::system(),
632 WebhookEvent::Push,
633 "",
634 json!({}),
635 )
636 .await;
637 let rows = state
638 .store
639 .list_deliveries(&push_hook, 10)
640 .await
641 .expect("list");
642 let body: Value = serde_json::from_str(&rows[0].request_body).expect("json");
643 assert!(body["sender"].is_null());
644 assert!(body.get("action").is_none());
645 }
646
647 #[tokio::test]
648 async fn ping_reaches_its_hook_even_without_the_subscription() {
649 let tmp = tempfile::tempdir().expect("tempdir");
650 let (state, _repo, [_, issues_hook, _]) = fixture(tmp.path()).await;
651 let hook = state
652 .store
653 .webhook(&issues_hook)
654 .await
655 .expect("lookup")
656 .expect("hook");
657
658 emit_ping(&state, &hook).await.expect("ping queued");
659
660 let rows = state
661 .store
662 .list_deliveries(&issues_hook, 10)
663 .await
664 .expect("list");
665 assert_eq!(rows.len(), 1);
666 assert_eq!(rows[0].event, WebhookEvent::Ping);
667 let body: Value = serde_json::from_str(&rows[0].request_body).expect("json");
668 assert_eq!(body["hook_id"], issues_hook.as_str());
669 }
670
671 /// A hook on `repo` subscribed to `events`, returned by id.
672 async fn hook_for(state: &AppState, repo: &Repo, events: &[WebhookEvent]) -> String {
673 let sealed = forge_core::secret::seal(&KEY, b"s").expect("seal");
674 state
675 .store
676 .create_webhook(
677 &repo.id,
678 &repo.owner_id,
679 "https://hooks.example/h",
680 sealed.as_bytes(),
681 events,
682 true,
683 )
684 .await
685 .expect("hook")
686 .id
687 }
688
689 /// The parsed bodies of a hook's deliveries, newest first, with their event and action.
690 async fn bodies(state: &AppState, hook: &str) -> Vec<(WebhookEvent, String, Value)> {
691 state
692 .store
693 .list_deliveries(hook, 50)
694 .await
695 .expect("list")
696 .into_iter()
697 .map(|d| {
698 let body = serde_json::from_str(&d.request_body).expect("json");
699 (d.event, d.action, body)
700 })
701 .collect()
702 }
703
704 #[tokio::test]
705 async fn pull_request_label_change_queues_pull_request_labeled() {
706 let tmp = tempfile::tempdir().expect("tempdir");
707 let state = test_state(tmp.path()).await;
708 let owner = state
709 .store
710 .create_user("tahinli", None, "unused", false)
711 .await
712 .expect("user");
713 let repo = state
714 .store
715 .create_repo(&owner.id, "proj", "", Visibility::Public, "main")
716 .await
717 .expect("repo");
718 let zeros = "1".repeat(40);
719 let pr = state
720 .store
721 .create_pull(
722 &repo.id, &owner.id, "Title", "", "main", &repo.id, "feat", &zeros, &zeros,
723 )
724 .await
725 .expect("pr");
726 let hook = hook_for(
727 &state,
728 &repo,
729 &[WebhookEvent::PullRequest, WebhookEvent::Issues],
730 )
731 .await;
732 let label = Label {
733 id: "l1".into(),
734 repo_id: repo.id.clone(),
735 name: "bug".into(),
736 color: "#ff0000".into(),
737 description: String::new(),
738 };
739
740 emit_label_changes(
741 &state,
742 &repo,
743 &Sender::of(&owner),
744 true,
745 &pr.issue,
746 &[],
747 std::slice::from_ref(&label),
748 )
749 .await;
750
751 let rows = bodies(&state, &hook).await;
752 assert_eq!(rows.len(), 1);
753 let (event, action, body) = &rows[0];
754 assert_eq!(*event, WebhookEvent::PullRequest);
755 assert_eq!(action, "labeled");
756 assert_eq!(body["action"], "labeled");
757 assert_eq!(body["label"], json!({"name": "bug", "color": "#ff0000"}));
758 assert_eq!(body["number"], pr.issue.number);
759 assert_eq!(body["pull_request"]["title"], "Title");
760 }
761
762 #[tokio::test]
763 async fn issue_milestone_set_and_clear_queue_milestoned_and_demilestoned() {
764 let tmp = tempfile::tempdir().expect("tempdir");
765 let state = test_state(tmp.path()).await;
766 let owner = state
767 .store
768 .create_user("tahinli", None, "unused", false)
769 .await
770 .expect("user");
771 let repo = state
772 .store
773 .create_repo(&owner.id, "proj", "", Visibility::Public, "main")
774 .await
775 .expect("repo");
776 let issue = state
777 .store
778 .create_issue(&repo.id, &owner.id, "Broken", "")
779 .await
780 .expect("issue");
781 let hook = hook_for(&state, &repo, &[WebhookEvent::Issues]).await;
782 let sender = Sender::of(&owner);
783
784 emit_milestone_change(&state, &repo, &sender, false, &issue, None, Some("v1")).await;
785 emit_milestone_change(&state, &repo, &sender, false, &issue, Some("v1"), None).await;
786 // Same title on both sides is no change.
787 emit_milestone_change(
788 &state,
789 &repo,
790 &sender,
791 false,
792 &issue,
793 Some("v1"),
794 Some("v1"),
795 )
796 .await;
797
798 let rows = bodies(&state, &hook).await;
799 assert_eq!(rows.len(), 2);
800 // Newest first: the removal, then the set.
801 let (event, action, body) = &rows[0];
802 assert_eq!(*event, WebhookEvent::Issues);
803 assert_eq!(action, "demilestoned");
804 assert_eq!(body["milestone"], json!({"title": "v1"}));
805 assert_eq!(rows[1].1, "milestoned");
806 assert_eq!(rows[1].2["milestone"], json!({"title": "v1"}));
807 assert_eq!(rows[1].2["issue"]["number"], issue.number);
808 }
809}