| 1 | // Copyright (c) 2019-2026 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by a GPL license that can be found in the LICENSE file. |
| 3 | module main |
| 4 | |
| 5 | import veb |
| 6 | import validation |
| 7 | |
| 8 | @['/:username/:repo_name/settings/webhooks'] |
| 9 | pub fn (mut app App) repo_webhooks(mut ctx Context, username string, repo_name string) veb.Result { |
| 10 | repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } |
| 11 | if !app.check_repo_owner(ctx.user.username, repo_name) { |
| 12 | return ctx.redirect_to_repository(username, repo_name) |
| 13 | } |
| 14 | webhooks := app.list_repo_webhooks(repo.id) |
| 15 | return $veb.html('templates/repo/webhooks.html') |
| 16 | } |
| 17 | |
| 18 | @['/:username/:repo_name/settings/webhooks/new'] |
| 19 | pub fn (mut app App) new_webhook(mut ctx Context, username string, repo_name string) veb.Result { |
| 20 | repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } |
| 21 | if !app.check_repo_owner(ctx.user.username, repo_name) { |
| 22 | return ctx.redirect_to_repository(username, repo_name) |
| 23 | } |
| 24 | return $veb.html('templates/new/webhook.html') |
| 25 | } |
| 26 | |
| 27 | @['/:username/:repo_name/settings/webhooks'; post] |
| 28 | pub fn (mut app App) handle_create_webhook(mut ctx Context, username string, repo_name string) veb.Result { |
| 29 | repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } |
| 30 | if !app.check_repo_owner(ctx.user.username, repo_name) { |
| 31 | return ctx.redirect_to_repository(username, repo_name) |
| 32 | } |
| 33 | url := ctx.form['url'].trim_space() |
| 34 | secret := ctx.form['secret'] |
| 35 | if validation.is_string_empty(url) || !(url.starts_with('http://') |
| 36 | || url.starts_with('https://')) { |
| 37 | return ctx.redirect('/${username}/${repo_name}/settings/webhooks/new') |
| 38 | } |
| 39 | mut events := []string{} |
| 40 | for ev in ['push', 'issue', 'pr', 'comment', 'release'] { |
| 41 | if ctx.form['event_${ev}'] == 'on' { |
| 42 | events << ev |
| 43 | } |
| 44 | } |
| 45 | events_str := if events.len == 0 { 'push,issue,pr,comment,release' } else { events.join(',') } |
| 46 | app.add_webhook(repo.id, url, secret, events_str) or { |
| 47 | ctx.error('Could not create webhook') |
| 48 | return ctx.redirect('/${username}/${repo_name}/settings/webhooks/new') |
| 49 | } |
| 50 | return ctx.redirect('/${username}/${repo_name}/settings/webhooks') |
| 51 | } |
| 52 | |
| 53 | @['/:username/:repo_name/settings/webhooks/:id/delete'; post] |
| 54 | pub fn (mut app App) handle_delete_webhook(mut ctx Context, username string, repo_name string, id string) veb.Result { |
| 55 | repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } |
| 56 | if !app.check_repo_owner(ctx.user.username, repo_name) { |
| 57 | return ctx.redirect_to_repository(username, repo_name) |
| 58 | } |
| 59 | wh := app.find_webhook_by_id(id.int()) or { return ctx.not_found() } |
| 60 | if wh.repo_id != repo.id { |
| 61 | return ctx.not_found() |
| 62 | } |
| 63 | app.delete_webhook(wh.id) or {} |
| 64 | return ctx.redirect('/${username}/${repo_name}/settings/webhooks') |
| 65 | } |
| 66 | |
| 67 | @['/:username/:repo_name/settings/webhooks/:id'] |
| 68 | pub fn (mut app App) view_webhook(mut ctx Context, username string, repo_name string, id string) veb.Result { |
| 69 | repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } |
| 70 | if !app.check_repo_owner(ctx.user.username, repo_name) { |
| 71 | return ctx.redirect_to_repository(username, repo_name) |
| 72 | } |
| 73 | webhook := app.find_webhook_by_id(id.int()) or { return ctx.not_found() } |
| 74 | if webhook.repo_id != repo.id { |
| 75 | return ctx.not_found() |
| 76 | } |
| 77 | deliveries := app.recent_webhook_deliveries(webhook.id, 30) |
| 78 | return $veb.html('templates/repo/webhook.html') |
| 79 | } |
| 80 | |