| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use std::time::SystemTime; |
| 13 | |
| 14 | use serde::Deserialize; |
| 15 | use topcoat::{ |
| 16 | Result, |
| 17 | context::Cx, |
| 18 | icon::{icon, iconify::iconify_icon}, |
| 19 | router::{StatusCode, content::Form, error::not_found, page, path_param, query_params}, |
| 20 | view::{attributes, component, view}, |
| 21 | }; |
| 22 | |
| 23 | use crate::{ |
| 24 | application::{ |
| 25 | Error, RepoView, WebhookDetail, WebhookSummary, create_webhook, delete_webhook, |
| 26 | list_webhooks, redeliver, set_webhook_active, view_webhook, |
| 27 | }, |
| 28 | components::{ |
| 29 | badge::{BadgeVariant, badge}, |
| 30 | button::{ButtonSize, ButtonVariant, button}, |
| 31 | flash::{FlashKind, flash}, |
| 32 | input::input, |
| 33 | label::label, |
| 34 | }, |
| 35 | domain::{DomainError, WebhookDelivery, WebhookDeliveryId, WebhookId, WebhookUrl}, |
| 36 | infrastructure::clock::SystemClock, |
| 37 | }; |
| 38 | |
| 39 | use super::{ |
| 40 | browse::timestamp, |
| 41 | context::{ |
| 42 | current_actor, deliveries, location, memberships, orgs, private_targets, public_origin, |
| 43 | push_events, queries, repos, server_error, users, webhook_sender, webhooks, |
| 44 | }, |
| 45 | layout::narrow, |
| 46 | paging::{Pages, list_pages, requested_page, showing}, |
| 47 | repo::repo_for, |
| 48 | }; |
| 49 | |
| 50 | |
| 51 | #[path_param] |
| 52 | struct Hook(str); |
| 53 | |
| 54 | |
| 55 | #[path_param] |
| 56 | struct Delivery(str); |
| 57 | |
| 58 | #[derive(Debug, Deserialize)] |
| 59 | struct AddForm { |
| 60 | url: String, |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | #[query_params(error = bad_request)] |
| 69 | struct Notices { |
| 70 | removed: Option<String>, |
| 71 | |
| 72 | state: Option<String>, |
| 73 | |
| 74 | sent: Option<String>, |
| 75 | |
| 76 | |
| 77 | page: Option<String>, |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | #[derive(Debug, Deserialize)] |
| 86 | struct ToggleForm { |
| 87 | active: String, |
| 88 | |
| 89 | |
| 90 | detail: Option<String>, |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | async fn owned(cx: &Cx) -> Result<RepoView> { |
| 98 | let repo = repo_for(cx).await?; |
| 99 | |
| 100 | if !repo.viewer_is_owner { |
| 101 | return Err(not_found().into()); |
| 102 | } |
| 103 | |
| 104 | Ok(repo) |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | fn explain(error: Error) -> Result<String> { |
| 112 | match error { |
| 113 | Error::Domain(DomainError::Validation { field, reason }) => { |
| 114 | Ok(format!("That {field} is no good: {reason}.")) |
| 115 | } |
| 116 | Error::Domain(DomainError::Forbidden | DomainError::NotFound { .. }) => { |
| 117 | Err(not_found().into()) |
| 118 | } |
| 119 | other => Err(server_error(std::io::Error::other(other.to_string()))), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | async fn listing(cx: &Cx, repo: &RepoView) -> Result<Vec<WebhookSummary>> { |
| 124 | list_webhooks( |
| 125 | ¤t_actor(cx).await?, |
| 126 | &repo.handle, |
| 127 | &repo.name, |
| 128 | &orgs(cx), |
| 129 | &memberships(cx), |
| 130 | &repos(cx), |
| 131 | &webhooks(cx), |
| 132 | &deliveries(cx), |
| 133 | ) |
| 134 | .await |
| 135 | .map_err(|error| match error { |
| 136 | |
| 137 | |
| 138 | Error::Domain(DomainError::Forbidden | DomainError::NotFound { .. }) => not_found().into(), |
| 139 | other => server_error(std::io::Error::other(other.to_string())), |
| 140 | }) |
| 141 | } |
| 142 | |
| 143 | #[page("/{handle}/repos/{name}/settings/hooks")] |
| 144 | async fn hooks_page(cx: &Cx) -> Result { |
| 145 | let repo = owned(cx).await?; |
| 146 | let listed = listing(cx, &repo).await?; |
| 147 | let notices = query_params::<Notices>(cx)?; |
| 148 | |
| 149 | view! { |
| 150 | hooks_view( |
| 151 | handle: repo.handle.as_str(), |
| 152 | name: repo.name.as_str(), |
| 153 | hooks: &listed, |
| 154 | notice: Notice { |
| 155 | removed: notices.removed.is_some(), |
| 156 | state: notices.state.as_deref().unwrap_or_default(), |
| 157 | ..Notice::default() |
| 158 | }, |
| 159 | ) |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | #[page(POST "/{handle}/repos/{name}/settings/hooks")] |
| 169 | async fn add(cx: &Cx, Form(submitted): Form<AddForm>) -> Result { |
| 170 | let repo = owned(cx).await?; |
| 171 | |
| 172 | let outcome = create_webhook( |
| 173 | ¤t_actor(cx).await?, |
| 174 | &repo.handle, |
| 175 | &repo.name, |
| 176 | submitted.url.trim(), |
| 177 | SystemTime::now(), |
| 178 | private_targets(cx), |
| 179 | &orgs(cx), |
| 180 | &memberships(cx), |
| 181 | &repos(cx), |
| 182 | &webhooks(cx), |
| 183 | ) |
| 184 | .await; |
| 185 | |
| 186 | let (created, error) = match outcome { |
| 187 | Ok(created) => (created.secret.reveal().to_owned(), String::new()), |
| 188 | Err(error) => (String::new(), explain(error)?), |
| 189 | }; |
| 190 | |
| 191 | let listed = listing(cx, &repo).await?; |
| 192 | |
| 193 | view! { |
| 194 | hooks_view( |
| 195 | handle: repo.handle.as_str(), |
| 196 | name: repo.name.as_str(), |
| 197 | hooks: &listed, |
| 198 | notice: Notice { |
| 199 | created: created.as_str(), |
| 200 | removed: false, |
| 201 | state: "", |
| 202 | error: error.as_str(), |
| 203 | |
| 204 | |
| 205 | url: if created.is_empty() { submitted.url.as_str() } else { "" }, |
| 206 | }, |
| 207 | ) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | #[page(POST "/{handle}/repos/{name}/settings/hooks/{hook}/delete")] |
| 213 | async fn remove(cx: &Cx) -> Result { |
| 214 | let repo = owned(cx).await?; |
| 215 | let id = WebhookId::from_trusted(path_param::<Hook>(cx)); |
| 216 | |
| 217 | match delete_webhook( |
| 218 | ¤t_actor(cx).await?, |
| 219 | &repo.handle, |
| 220 | &repo.name, |
| 221 | &id, |
| 222 | &orgs(cx), |
| 223 | &memberships(cx), |
| 224 | &repos(cx), |
| 225 | &webhooks(cx), |
| 226 | ) |
| 227 | .await |
| 228 | { |
| 229 | Ok(()) => {} |
| 230 | |
| 231 | |
| 232 | Err(Error::Domain(DomainError::NotFound { .. })) => {} |
| 233 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 234 | } |
| 235 | |
| 236 | view! { |
| 237 | (StatusCode::SEE_OTHER) |
| 238 | (location(&format!( |
| 239 | "/{}/repos/{}/settings/hooks?removed", |
| 240 | repo.handle, repo.name |
| 241 | ))?) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | #[page(POST "/{handle}/repos/{name}/settings/hooks/{hook}/toggle")] |
| 252 | async fn toggle(cx: &Cx, Form(submitted): Form<ToggleForm>) -> Result { |
| 253 | let repo = owned(cx).await?; |
| 254 | let id = WebhookId::from_trusted(path_param::<Hook>(cx)); |
| 255 | let wanted = submitted.active == "true"; |
| 256 | |
| 257 | match set_webhook_active( |
| 258 | ¤t_actor(cx).await?, |
| 259 | &repo.handle, |
| 260 | &repo.name, |
| 261 | &id, |
| 262 | wanted, |
| 263 | &orgs(cx), |
| 264 | &memberships(cx), |
| 265 | &repos(cx), |
| 266 | &webhooks(cx), |
| 267 | ) |
| 268 | .await |
| 269 | { |
| 270 | Ok(_) => {} |
| 271 | |
| 272 | Err(Error::Domain(DomainError::NotFound { .. })) => {} |
| 273 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 274 | } |
| 275 | |
| 276 | let state = if wanted { "on" } else { "off" }; |
| 277 | |
| 278 | let back = match submitted.detail.is_some() { |
| 279 | true => format!( |
| 280 | "/{}/repos/{}/settings/hooks/{id}?state={state}", |
| 281 | repo.handle, repo.name |
| 282 | ), |
| 283 | false => format!( |
| 284 | "/{}/repos/{}/settings/hooks?state={state}", |
| 285 | repo.handle, repo.name |
| 286 | ), |
| 287 | }; |
| 288 | |
| 289 | view! { |
| 290 | (StatusCode::SEE_OTHER) |
| 291 | (location(&back)?) |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | #[page(POST "/{handle}/repos/{name}/settings/hooks/{hook}/deliveries/{delivery}/redeliver")] |
| 303 | async fn resend(cx: &Cx) -> Result { |
| 304 | let repo = owned(cx).await?; |
| 305 | let hook = WebhookId::from_trusted(path_param::<Hook>(cx)); |
| 306 | let delivery = WebhookDeliveryId::from_trusted(path_param::<Delivery>(cx)); |
| 307 | |
| 308 | let outcome = redeliver( |
| 309 | ¤t_actor(cx).await?, |
| 310 | &repo.handle, |
| 311 | &repo.name, |
| 312 | &hook, |
| 313 | &delivery, |
| 314 | &public_origin(cx), |
| 315 | &orgs(cx), |
| 316 | &memberships(cx), |
| 317 | &repos(cx), |
| 318 | &users(cx), |
| 319 | &webhooks(cx), |
| 320 | &deliveries(cx), |
| 321 | &push_events(cx), |
| 322 | &queries(cx), |
| 323 | &webhook_sender(cx), |
| 324 | &SystemClock, |
| 325 | ) |
| 326 | .await; |
| 327 | |
| 328 | let sent = match outcome { |
| 329 | Ok(delivery) if delivery.ok => "ok", |
| 330 | Ok(_) => "failed", |
| 331 | |
| 332 | |
| 333 | Err(Error::Domain(DomainError::NotFound { .. })) => "failed", |
| 334 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 335 | }; |
| 336 | |
| 337 | view! { |
| 338 | (StatusCode::SEE_OTHER) |
| 339 | (location(&format!( |
| 340 | "/{}/repos/{}/settings/hooks/{hook}?sent={sent}", |
| 341 | repo.handle, repo.name |
| 342 | ))?) |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | #[page("/{handle}/repos/{name}/settings/hooks/{hook}")] |
| 347 | async fn hook_page(cx: &Cx) -> Result { |
| 348 | let repo = owned(cx).await?; |
| 349 | let id = WebhookId::from_trusted(path_param::<Hook>(cx)); |
| 350 | |
| 351 | let detail = view_webhook( |
| 352 | ¤t_actor(cx).await?, |
| 353 | &repo.handle, |
| 354 | &repo.name, |
| 355 | &id, |
| 356 | requested_page(query_params::<Notices>(cx)?.page.as_deref()), |
| 357 | &orgs(cx), |
| 358 | &memberships(cx), |
| 359 | &repos(cx), |
| 360 | &webhooks(cx), |
| 361 | &deliveries(cx), |
| 362 | ) |
| 363 | .await; |
| 364 | |
| 365 | let detail = match detail { |
| 366 | Ok(detail) => detail, |
| 367 | |
| 368 | Err(_) => return Err(not_found().into()), |
| 369 | }; |
| 370 | |
| 371 | let notices = query_params::<Notices>(cx)?; |
| 372 | |
| 373 | view! { |
| 374 | hook_view( |
| 375 | handle: repo.handle.as_str(), |
| 376 | name: repo.name.as_str(), |
| 377 | hook: &detail, |
| 378 | state: notices.state.as_deref().unwrap_or_default(), |
| 379 | sent: notices.sent.as_deref().unwrap_or_default(), |
| 380 | ) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | fn status_of(last: Option<&WebhookDelivery>) -> (&'static str, String) { |
| 390 | match last { |
| 391 | Some(delivery) if delivery.ok => { |
| 392 | ("bg-success", format!("{} · ok", delivery.status_label())) |
| 393 | } |
| 394 | Some(delivery) => ("bg-destructive", delivery.status_label()), |
| 395 | None => ("bg-muted-foreground/40", "no deliveries yet".to_owned()), |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | #[derive(Debug, Clone, Copy, Default)] |
| 403 | struct Notice<'a> { |
| 404 | |
| 405 | created: &'a str, |
| 406 | removed: bool, |
| 407 | |
| 408 | state: &'a str, |
| 409 | error: &'a str, |
| 410 | |
| 411 | url: &'a str, |
| 412 | } |
| 413 | |
| 414 | |
| 415 | #[component] |
| 416 | async fn hooks_view( |
| 417 | handle: &str, |
| 418 | name: &str, |
| 419 | hooks: &[WebhookSummary], |
| 420 | notice: Notice<'_>, |
| 421 | ) -> Result { |
| 422 | let Notice { |
| 423 | created, |
| 424 | removed, |
| 425 | state, |
| 426 | error, |
| 427 | url, |
| 428 | } = notice; |
| 429 | |
| 430 | view! { |
| 431 | narrow( |
| 432 | <h1 class="text-xl font-semibold tracking-tight">"Webhooks"</h1> |
| 433 | <p class="mt-1 font-mono text-sm text-muted-foreground"> |
| 434 | "@" (handle) " / " (name) |
| 435 | </p> |
| 436 | <p class="mt-2 text-sm text-muted-foreground"> |
| 437 | "Every push to this repository is POSTed to each hook as signed JSON, " |
| 438 | "once per ref that moved." |
| 439 | </p> |
| 440 | |
| 441 | if !created.is_empty() { |
| 442 | <div class="mt-6 space-y-2"> |
| 443 | flash( |
| 444 | kind: FlashKind::Success, |
| 445 | "Copy this secret now — it is not shown again." |
| 446 | ) |
| 447 | <pre class="overflow-x-auto rounded-lg border border-border bg-surface px-4 py-3 font-mono text-sm">(created)</pre> |
| 448 | <p class="text-xs text-muted-foreground"> |
| 449 | "Sent as " |
| 450 | <code class="font-mono">"X-Steid-Signature-256: sha256=<hex>"</code> |
| 451 | " — the HMAC-SHA-256 of the exact body, keyed with this." |
| 452 | </p> |
| 453 | </div> |
| 454 | } |
| 455 | |
| 456 | if removed { |
| 457 | <div class="mt-6"> |
| 458 | flash(kind: FlashKind::Success, "Webhook removed.") |
| 459 | </div> |
| 460 | } |
| 461 | |
| 462 | if !state.is_empty() { |
| 463 | <div class="mt-6"> |
| 464 | flash( |
| 465 | kind: FlashKind::Success, |
| 466 | (if state == "on" { |
| 467 | "Webhook turned on. The next push will be delivered." |
| 468 | } else { |
| 469 | "Webhook turned off. Its deliveries are kept." |
| 470 | }) |
| 471 | ) |
| 472 | </div> |
| 473 | } |
| 474 | |
| 475 | if !error.is_empty() { |
| 476 | <div class="mt-6"> |
| 477 | flash(kind: FlashKind::Error, (error)) |
| 478 | </div> |
| 479 | } |
| 480 | |
| 481 | <form |
| 482 | method="post" |
| 483 | action=(format!("/{handle}/repos/{name}/settings/hooks")) |
| 484 | class="mt-6 space-y-5" |
| 485 | > |
| 486 | <div class="space-y-2"> |
| 487 | label(attrs: attributes! { for="url" }, "Payload URL") |
| 488 | input(attrs: attributes! { |
| 489 | id="url" |
| 490 | name="url" |
| 491 | type="text" |
| 492 | value=(url) |
| 493 | placeholder="https://example.com/steid-hook" |
| 494 | required=(true) |
| 495 | autocomplete="off" |
| 496 | maxlength=(WebhookUrl::MAX_LEN.to_string()) |
| 497 | }) |
| 498 | <p class="text-xs text-muted-foreground"> |
| 499 | "Must be reachable from the internet. An address on this machine " |
| 500 | "or its private network is refused." |
| 501 | </p> |
| 502 | </div> |
| 503 | |
| 504 | button(attrs: attributes! { type="submit" }, "Add webhook") |
| 505 | </form> |
| 506 | |
| 507 | <section class="mt-10"> |
| 508 | <h2 class="text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 509 | "Hooks" |
| 510 | </h2> |
| 511 | |
| 512 | if hooks.is_empty() { |
| 513 | <p class="mt-2 rounded-lg border border-border px-4 py-6 text-center text-sm text-muted-foreground"> |
| 514 | "No webhooks yet." |
| 515 | </p> |
| 516 | } else { |
| 517 | <ul class="mt-2 divide-y divide-border rounded-lg border border-border"> |
| 518 | for hook in hooks { |
| 519 | <li class="flex items-center gap-3 px-4 py-3"> |
| 520 | <span |
| 521 | class=(format!("size-2 shrink-0 rounded-full {}", status_of(hook.last.as_ref()).0)) |
| 522 | title=(status_of(hook.last.as_ref()).1) |
| 523 | ></span> |
| 524 | <a |
| 525 | href=(format!("/{handle}/repos/{name}/settings/hooks/{}", hook.id)) |
| 526 | class="min-w-0 flex-1 truncate font-mono text-sm hover:underline" |
| 527 | >(hook.url.as_str())</a> |
| 528 | if !hook.active { |
| 529 | badge(variant: BadgeVariant::Outline, "Off") |
| 530 | } |
| 531 | <span class="hidden shrink-0 font-mono text-xs text-muted-foreground sm:inline"> |
| 532 | (status_of(hook.last.as_ref()).1) |
| 533 | </span> |
| 534 | |
| 535 | |
| 536 | |
| 537 | <form |
| 538 | method="post" |
| 539 | action=(format!( |
| 540 | "/{handle}/repos/{name}/settings/hooks/{}/toggle", |
| 541 | hook.id |
| 542 | )) |
| 543 | class="shrink-0" |
| 544 | > |
| 545 | <input |
| 546 | type="hidden" |
| 547 | name="active" |
| 548 | value=(if hook.active { "false" } else { "true" }) |
| 549 | /> |
| 550 | button( |
| 551 | attrs: attributes! { type="submit" }, |
| 552 | variant: ButtonVariant::Ghost, |
| 553 | size: ButtonSize::Sm, |
| 554 | (if hook.active { "Turn off" } else { "Turn on" }) |
| 555 | ) |
| 556 | </form> |
| 557 | <form |
| 558 | method="post" |
| 559 | action=(format!( |
| 560 | "/{handle}/repos/{name}/settings/hooks/{}/delete", |
| 561 | hook.id |
| 562 | )) |
| 563 | class="shrink-0" |
| 564 | > |
| 565 | |
| 566 | |
| 567 | |
| 568 | button( |
| 569 | attrs: attributes! { type="submit" }, |
| 570 | variant: ButtonVariant::Ghost, |
| 571 | size: ButtonSize::Sm, |
| 572 | "Delete" |
| 573 | ) |
| 574 | </form> |
| 575 | </li> |
| 576 | } |
| 577 | </ul> |
| 578 | } |
| 579 | </section> |
| 580 | |
| 581 | <p class="mt-8"> |
| 582 | <a |
| 583 | href=(format!("/{handle}/repos/{name}/settings")) |
| 584 | class="text-sm text-muted-foreground hover:text-foreground" |
| 585 | >"Back to repository settings"</a> |
| 586 | </p> |
| 587 | ) |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | |
| 592 | #[component] |
| 593 | async fn hook_view( |
| 594 | handle: &str, |
| 595 | name: &str, |
| 596 | hook: &WebhookDetail, |
| 597 | |
| 598 | state: &str, |
| 599 | |
| 600 | sent: &str, |
| 601 | ) -> Result { |
| 602 | let deliveries = &hook.deliveries; |
| 603 | let sentence = showing( |
| 604 | deliveries.offset(), |
| 605 | deliveries.items.len(), |
| 606 | deliveries.total, |
| 607 | "attempts", |
| 608 | ); |
| 609 | let pages = Pages::numbered(deliveries.page, deliveries.pages, |page| { |
| 610 | format!( |
| 611 | "/{handle}/repos/{name}/settings/hooks/{}?page={page}", |
| 612 | hook.id |
| 613 | ) |
| 614 | }); |
| 615 | |
| 616 | view! { |
| 617 | narrow( |
| 618 | <div class="flex items-start justify-between gap-4"> |
| 619 | <div class="min-w-0"> |
| 620 | <h1 class="text-xl font-semibold tracking-tight">"Webhook"</h1> |
| 621 | <p class="mt-1 break-all font-mono text-sm text-muted-foreground"> |
| 622 | (hook.url.as_str()) |
| 623 | </p> |
| 624 | <p class="mt-1 text-xs text-muted-foreground"> |
| 625 | "Added " (timestamp(hook.created_at)) |
| 626 | if !hook.active { " · not active" } |
| 627 | </p> |
| 628 | </div> |
| 629 | |
| 630 | |
| 631 | <form |
| 632 | method="post" |
| 633 | action=(format!("/{handle}/repos/{name}/settings/hooks/{}/toggle", hook.id)) |
| 634 | class="shrink-0" |
| 635 | > |
| 636 | <input |
| 637 | type="hidden" |
| 638 | name="active" |
| 639 | value=(if hook.active { "false" } else { "true" }) |
| 640 | /> |
| 641 | <input type="hidden" name="detail" value="1" /> |
| 642 | button( |
| 643 | attrs: attributes! { type="submit" }, |
| 644 | variant: ButtonVariant::Outline, |
| 645 | size: ButtonSize::Sm, |
| 646 | (if hook.active { "Turn off" } else { "Turn on" }) |
| 647 | ) |
| 648 | </form> |
| 649 | </div> |
| 650 | |
| 651 | if !state.is_empty() { |
| 652 | <div class="mt-4"> |
| 653 | flash( |
| 654 | kind: FlashKind::Success, |
| 655 | (if state == "on" { |
| 656 | "Webhook turned on. The next push will be delivered." |
| 657 | } else { |
| 658 | "Webhook turned off. Its deliveries are kept." |
| 659 | }) |
| 660 | ) |
| 661 | </div> |
| 662 | } |
| 663 | |
| 664 | if !sent.is_empty() { |
| 665 | <div class="mt-4"> |
| 666 | if sent == "ok" { |
| 667 | flash(kind: FlashKind::Success, "Redelivered — the endpoint accepted it.") |
| 668 | } else { |
| 669 | flash( |
| 670 | kind: FlashKind::Error, |
| 671 | "Redelivery failed. The newest attempt below says what came back." |
| 672 | ) |
| 673 | } |
| 674 | </div> |
| 675 | } |
| 676 | |
| 677 | <section class="mt-6"> |
| 678 | <div class="flex items-baseline justify-between gap-4"> |
| 679 | <h2 class="text-xs font-medium uppercase tracking-wider text-muted-foreground"> |
| 680 | "Deliveries" |
| 681 | </h2> |
| 682 | </div> |
| 683 | |
| 684 | if hook.deliveries.items.is_empty() { |
| 685 | <p class="mt-2 rounded-lg border border-border px-4 py-6 text-center text-sm text-muted-foreground"> |
| 686 | "Nothing has been delivered yet. Push to this repository to try it." |
| 687 | </p> |
| 688 | } else { |
| 689 | <ul class="mt-2 divide-y divide-border rounded-lg border border-border"> |
| 690 | for delivery in &hook.deliveries.items { |
| 691 | delivery_row( |
| 692 | handle: handle, |
| 693 | name: name, |
| 694 | hook: &hook.id, |
| 695 | delivery: delivery, |
| 696 | ) |
| 697 | } |
| 698 | </ul> |
| 699 | list_pages(summary: sentence.as_str(), pages: &pages) |
| 700 | } |
| 701 | </section> |
| 702 | |
| 703 | <p class="mt-8"> |
| 704 | <a |
| 705 | href=(format!("/{handle}/repos/{name}/settings/hooks")) |
| 706 | class="text-sm text-muted-foreground hover:text-foreground" |
| 707 | >"Back to webhooks"</a> |
| 708 | </p> |
| 709 | ) |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | #[component] |
| 718 | async fn delivery_row( |
| 719 | handle: &str, |
| 720 | name: &str, |
| 721 | hook: &WebhookId, |
| 722 | delivery: &WebhookDelivery, |
| 723 | ) -> Result { |
| 724 | let (dot, _) = status_of(Some(delivery)); |
| 725 | |
| 726 | view! { |
| 727 | <li class="px-4 py-2.5"> |
| 728 | <div class="flex items-center gap-3 text-xs"> |
| 729 | <span class=(format!("size-2 shrink-0 rounded-full {dot}"))></span> |
| 730 | <span class="font-mono text-muted-foreground"> |
| 731 | (timestamp(delivery.delivered_at)) |
| 732 | </span> |
| 733 | <span class="font-mono">(delivery.status_label())</span> |
| 734 | <span class="ml-auto shrink-0 font-mono text-muted-foreground"> |
| 735 | "try " (delivery.attempt.to_string()) |
| 736 | " · " (delivery.duration_ms.to_string()) "ms" |
| 737 | </span> |
| 738 | |
| 739 | |
| 740 | |
| 741 | <form |
| 742 | method="post" |
| 743 | action=(format!( |
| 744 | "/{handle}/repos/{name}/settings/hooks/{hook}/deliveries/{}/redeliver", |
| 745 | delivery.id |
| 746 | )) |
| 747 | class="shrink-0" |
| 748 | > |
| 749 | button( |
| 750 | attrs: attributes! { type="submit" }, |
| 751 | variant: ButtonVariant::Ghost, |
| 752 | size: ButtonSize::Sm, |
| 753 | "Redeliver" |
| 754 | ) |
| 755 | </form> |
| 756 | </div> |
| 757 | |
| 758 | if !delivery.response.is_empty() { |
| 759 | <details class="mt-1.5"> |
| 760 | <summary class="inline-flex cursor-pointer items-center gap-1 text-xs text-muted-foreground hover:text-foreground"> |
| 761 | icon(data: iconify_icon!("feather:chevron-right"), attrs: attributes! { |
| 762 | class="size-3.5" |
| 763 | }) |
| 764 | "Response" |
| 765 | </summary> |
| 766 | <pre class="mt-1.5 max-h-48 overflow-auto rounded-md border border-border bg-surface px-3 py-2 font-mono text-xs whitespace-pre-wrap">(delivery.response.as_str())</pre> |
| 767 | </details> |
| 768 | } |
| 769 | </li> |
| 770 | } |
| 771 | } |