2015-12-05 13:24:13 -05:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2017-05-29 09:17:15 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2015-12-05 13:24:13 -05:00
|
|
|
|
2023-07-02 08:59:32 +08:00
|
|
|
package setting
|
2015-12-05 13:24:13 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-04-05 17:30:52 +02:00
|
|
|
"net/http"
|
2021-11-16 18:18:25 +00:00
|
|
|
"net/url"
|
2019-03-18 22:33:20 -04:00
|
|
|
"path"
|
2015-12-05 13:24:13 -05:00
|
|
|
|
2025-03-27 19:40:14 +00:00
|
|
|
"forgejo.org/models/db"
|
|
|
|
"forgejo.org/models/perm"
|
|
|
|
access_model "forgejo.org/models/perm/access"
|
|
|
|
user_model "forgejo.org/models/user"
|
|
|
|
"forgejo.org/models/webhook"
|
|
|
|
"forgejo.org/modules/base"
|
|
|
|
"forgejo.org/modules/git"
|
|
|
|
"forgejo.org/modules/json"
|
|
|
|
"forgejo.org/modules/setting"
|
|
|
|
api "forgejo.org/modules/structs"
|
|
|
|
"forgejo.org/modules/web/middleware"
|
|
|
|
webhook_module "forgejo.org/modules/webhook"
|
|
|
|
"forgejo.org/services/context"
|
|
|
|
"forgejo.org/services/convert"
|
|
|
|
"forgejo.org/services/forms"
|
|
|
|
webhook_service "forgejo.org/services/webhook"
|
2024-03-21 13:23:27 +01:00
|
|
|
|
2024-11-05 21:40:44 +01:00
|
|
|
"code.forgejo.org/go-chi/binding"
|
2015-12-05 13:24:13 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-18 22:33:20 -04:00
|
|
|
tplHooks base.TplName = "repo/settings/webhook/base"
|
|
|
|
tplHookNew base.TplName = "repo/settings/webhook/new"
|
|
|
|
tplOrgHookNew base.TplName = "org/settings/hook_new"
|
2023-03-10 15:28:32 +01:00
|
|
|
tplUserHookNew base.TplName = "user/settings/hook_new"
|
2019-03-18 22:33:20 -04:00
|
|
|
tplAdminHookNew base.TplName = "admin/hook_new"
|
2015-12-05 13:24:13 -05:00
|
|
|
)
|
|
|
|
|
2024-03-21 14:43:43 +01:00
|
|
|
// WebhookList render web hooks list page
|
|
|
|
func WebhookList(ctx *context.Context) {
|
2015-12-05 13:24:13 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
2019-03-18 22:33:20 -04:00
|
|
|
ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
|
2021-01-15 01:24:03 +02:00
|
|
|
ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks"
|
2024-03-23 21:28:30 +01:00
|
|
|
ctx.Data["WebhookList"] = webhook_service.List()
|
2023-01-14 10:07:01 +01:00
|
|
|
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://forgejo.org/docs/latest/user/webhooks/")
|
2015-12-05 13:24:13 -05:00
|
|
|
|
2023-11-24 11:49:41 +08:00
|
|
|
ws, err := db.Find[webhook.Webhook](ctx, webhook.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
|
2015-12-05 13:24:13 -05:00
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("GetWebhooksByRepoID", err)
|
2015-12-05 13:24:13 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhooks"] = ws
|
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplHooks)
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 15:28:32 +01:00
|
|
|
type ownerRepoCtx struct {
|
|
|
|
OwnerID int64
|
2020-03-08 22:08:05 +00:00
|
|
|
RepoID int64
|
|
|
|
IsAdmin bool
|
|
|
|
IsSystemWebhook bool
|
|
|
|
Link string
|
2021-01-15 01:24:03 +02:00
|
|
|
LinkNew string
|
2020-03-08 22:08:05 +00:00
|
|
|
NewTemplate base.TplName
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 15:28:32 +01:00
|
|
|
// getOwnerRepoCtx determines whether this is a repo, owner, or admin (both default and system) context.
|
|
|
|
func getOwnerRepoCtx(ctx *context.Context) (*ownerRepoCtx, error) {
|
2023-04-28 08:08:47 +08:00
|
|
|
if ctx.Data["PageIsRepoSettings"] == true {
|
2023-03-10 15:28:32 +01:00
|
|
|
return &ownerRepoCtx{
|
2015-12-05 13:24:13 -05:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2019-03-18 22:33:20 -04:00
|
|
|
Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
2021-01-15 01:24:03 +02:00
|
|
|
LinkNew: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
2016-11-07 21:58:22 +01:00
|
|
|
NewTemplate: tplHookNew,
|
2015-12-05 13:24:13 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-28 08:08:47 +08:00
|
|
|
if ctx.Data["PageIsOrgSettings"] == true {
|
2023-03-10 15:28:32 +01:00
|
|
|
return &ownerRepoCtx{
|
|
|
|
OwnerID: ctx.ContextUser.ID,
|
2019-03-18 22:33:20 -04:00
|
|
|
Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
2021-01-15 01:24:03 +02:00
|
|
|
LinkNew: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
2016-11-07 21:58:22 +01:00
|
|
|
NewTemplate: tplOrgHookNew,
|
2015-12-05 13:24:13 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-28 08:08:47 +08:00
|
|
|
if ctx.Data["PageIsUserSettings"] == true {
|
2023-03-10 15:28:32 +01:00
|
|
|
return &ownerRepoCtx{
|
|
|
|
OwnerID: ctx.Doer.ID,
|
|
|
|
Link: path.Join(setting.AppSubURL, "/user/settings/hooks"),
|
|
|
|
LinkNew: path.Join(setting.AppSubURL, "/user/settings/hooks"),
|
|
|
|
NewTemplate: tplUserHookNew,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-03-08 22:08:05 +00:00
|
|
|
|
2023-04-28 08:08:47 +08:00
|
|
|
if ctx.Data["PageIsAdmin"] == true {
|
2023-03-10 15:28:32 +01:00
|
|
|
return &ownerRepoCtx{
|
2020-03-08 22:08:05 +00:00
|
|
|
IsAdmin: true,
|
2023-03-10 15:28:32 +01:00
|
|
|
IsSystemWebhook: ctx.Params(":configType") == "system-hooks",
|
2021-01-15 01:24:03 +02:00
|
|
|
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
|
Reenable creating default webhooks. (#24626)
Fixes #24624
This seems to have been broken in
https://github.com/go-gitea/gitea/pull/21563
Previously, this code read
```
// Are we looking at default webhooks?
if ctx.Params(":configType") == "default-hooks" {
return &orgRepoCtx{
IsAdmin: true,
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
LinkNew: path.Join(setting.AppSubURL, "/admin/default-hooks"),
NewTemplate: tplAdminHookNew,
}, nil
}
// Must be system webhooks instead
return &orgRepoCtx{
IsAdmin: true,
IsSystemWebhook: true,
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
LinkNew: path.Join(setting.AppSubURL, "/admin/system-hooks"),
NewTemplate: tplAdminHookNew,
}, nil
```
but was simplified to
```
return &ownerRepoCtx{
IsAdmin: true,
IsSystemWebhook: ctx.Params(":configType") == "system-hooks",
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
LinkNew: path.Join(setting.AppSubURL, "/admin/system-hooks"),
NewTemplate: tplAdminHookNew,
}, nil
```
In other words, combining the `IsSystemWebhook` check into a one-liner
and forgetting that `LinkNew` also depended on it. This meant the
rendered `<form>` always POSTed to `/admin/system-hooks`, even when you
had GETed `/admin/default-hooks/gitea/new`.
2023-05-10 22:10:57 -04:00
|
|
|
LinkNew: path.Join(setting.AppSubURL, "/admin/", ctx.Params(":configType")),
|
2020-03-08 22:08:05 +00:00
|
|
|
NewTemplate: tplAdminHookNew,
|
2019-03-18 22:33:20 -04:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:28:32 +01:00
|
|
|
return nil, errors.New("unable to set OwnerRepo context")
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
|
2024-03-21 14:43:43 +01:00
|
|
|
// WebhookNew render creating webhook page
|
|
|
|
func WebhookNew(ctx *context.Context) {
|
2015-12-05 13:24:13 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
2023-01-01 16:23:15 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
|
2015-12-05 13:24:13 -05:00
|
|
|
|
2023-03-10 15:28:32 +01:00
|
|
|
orCtx, err := getOwnerRepoCtx(ctx)
|
2015-12-05 13:24:13 -05:00
|
|
|
if err != nil {
|
2023-03-10 15:28:32 +01:00
|
|
|
ctx.ServerError("getOwnerRepoCtx", err)
|
2015-12-05 13:24:13 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-08 22:08:05 +00:00
|
|
|
if orCtx.IsAdmin && orCtx.IsSystemWebhook {
|
|
|
|
ctx.Data["PageIsAdminSystemHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminSystemHooksNew"] = true
|
|
|
|
} else if orCtx.IsAdmin {
|
2021-01-15 01:24:03 +02:00
|
|
|
ctx.Data["PageIsAdminDefaultHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminDefaultHooksNew"] = true
|
2019-03-18 22:33:20 -04:00
|
|
|
} else {
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:39:02 +01:00
|
|
|
hookType := ctx.Params(":type")
|
2024-03-22 16:02:48 +01:00
|
|
|
handler := webhook_service.GetWebhookHandler(hookType)
|
|
|
|
if handler == nil {
|
2024-03-21 14:39:02 +01:00
|
|
|
ctx.NotFound("GetWebhookHandler", nil)
|
2015-12-05 13:24:13 -05:00
|
|
|
return
|
|
|
|
}
|
2024-03-21 14:39:02 +01:00
|
|
|
ctx.Data["HookType"] = hookType
|
2024-03-22 16:02:48 +01:00
|
|
|
ctx.Data["WebhookHandler"] = handler
|
2021-01-15 01:24:03 +02:00
|
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
2024-02-15 14:59:48 +01:00
|
|
|
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
|
2024-03-23 21:28:30 +01:00
|
|
|
ctx.Data["WebhookList"] = webhook_service.List()
|
2015-12-05 13:24:13 -05:00
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
// ParseHookEvent convert web form content to webhook.HookEvent
|
2024-04-03 14:22:36 +02:00
|
|
|
func ParseHookEvent(form forms.WebhookCoreForm) *webhook_module.HookEvent {
|
2023-01-01 16:23:15 +01:00
|
|
|
return &webhook_module.HookEvent{
|
2015-12-05 13:24:13 -05:00
|
|
|
PushOnly: form.PushOnly(),
|
|
|
|
SendEverything: form.SendEverything(),
|
|
|
|
ChooseEvents: form.ChooseEvents(),
|
2023-01-01 16:23:15 +01:00
|
|
|
HookEvents: webhook_module.HookEvents{
|
2023-05-25 10:06:27 +08:00
|
|
|
Create: form.Create,
|
|
|
|
Delete: form.Delete,
|
|
|
|
Fork: form.Fork,
|
|
|
|
Issues: form.Issues,
|
|
|
|
IssueAssign: form.IssueAssign,
|
|
|
|
IssueLabel: form.IssueLabel,
|
|
|
|
IssueMilestone: form.IssueMilestone,
|
|
|
|
IssueComment: form.IssueComment,
|
|
|
|
Release: form.Release,
|
|
|
|
Push: form.Push,
|
|
|
|
PullRequest: form.PullRequest,
|
|
|
|
PullRequestAssign: form.PullRequestAssign,
|
|
|
|
PullRequestLabel: form.PullRequestLabel,
|
|
|
|
PullRequestMilestone: form.PullRequestMilestone,
|
|
|
|
PullRequestComment: form.PullRequestComment,
|
|
|
|
PullRequestReview: form.PullRequestReview,
|
|
|
|
PullRequestSync: form.PullRequestSync,
|
|
|
|
PullRequestReviewRequest: form.PullRequestReviewRequest,
|
|
|
|
Wiki: form.Wiki,
|
|
|
|
Repository: form.Repository,
|
|
|
|
Package: form.Package,
|
Actions Failure, Succes, Recover Webhooks (#7508)
Implement Actions Success, Failure and Recover webhooks for Forgejo, Gitea, Gogs, Slack, Discord, DingTalk, Telegram, Microsoft Teams, Feishu / Lark Suite, Matrix, WeCom (Wechat Work), Packagist. Some of these webhooks have not been manually tested.
Implement settings for these new webhooks.
## Checklist
The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).
### Tests
- I added test coverage for Go changes...
- [x] in their respective `*_test.go` for unit tests.
- [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I added test coverage for JavaScript changes...
- [ ] in `web_src/js/*.test.js` if it can be unit tested.
- [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)).
### Documentation
- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.
### Release notes
- [ ] I do not want this change to show in the release notes.
- [x] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.
<!--start release-notes-assistant-->
## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
- [PR](https://codeberg.org/forgejo/forgejo/pulls/7508): <!--number 7508 --><!--line 0 --><!--description QWN0aW9ucyBGYWlsdXJlLCBTdWNjZXMsIFJlY292ZXIgV2ViaG9va3M=-->Actions Failure, Succes, Recover Webhooks<!--description-->
<!--end release-notes-assistant-->
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7508
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: christopher-besch <mail@chris-besch.com>
Co-committed-by: christopher-besch <mail@chris-besch.com>
2025-06-03 14:29:19 +02:00
|
|
|
ActionRunFailure: form.ActionFailure,
|
|
|
|
ActionRunRecover: form.ActionRecover,
|
|
|
|
ActionRunSuccess: form.ActionSuccess,
|
2015-12-05 13:24:13 -05:00
|
|
|
},
|
2019-09-09 08:48:21 +03:00
|
|
|
BranchFilter: form.BranchFilter,
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 13:23:27 +01:00
|
|
|
func WebhookCreate(ctx *context.Context) {
|
2024-03-21 14:39:02 +01:00
|
|
|
hookType := ctx.Params(":type")
|
|
|
|
handler := webhook_service.GetWebhookHandler(hookType)
|
2024-03-21 13:23:27 +01:00
|
|
|
if handler == nil {
|
|
|
|
ctx.NotFound("GetWebhookHandler", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
fields := handler.UnmarshalForm(func(form any) {
|
2024-03-21 13:23:27 +01:00
|
|
|
errs := binding.Bind(ctx.Req, form)
|
2024-03-21 14:15:56 +01:00
|
|
|
middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error checked below in ctx.HasError
|
2024-03-21 13:23:27 +01:00
|
|
|
})
|
|
|
|
|
2017-05-29 09:17:15 +02:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2023-01-01 16:23:15 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
|
2024-03-21 14:39:02 +01:00
|
|
|
ctx.Data["HookType"] = hookType
|
2024-03-22 16:02:48 +01:00
|
|
|
ctx.Data["WebhookHandler"] = handler
|
2017-05-29 09:17:15 +02:00
|
|
|
|
2023-03-10 15:28:32 +01:00
|
|
|
orCtx, err := getOwnerRepoCtx(ctx)
|
2017-05-29 09:17:15 +02:00
|
|
|
if err != nil {
|
2023-03-10 15:28:32 +01:00
|
|
|
ctx.ServerError("getOwnerRepoCtx", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
2021-01-15 01:24:03 +02:00
|
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
2024-03-20 17:39:02 +01:00
|
|
|
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
|
2024-03-23 21:28:30 +01:00
|
|
|
ctx.Data["WebhookList"] = webhook_service.List()
|
2017-05-29 09:17:15 +02:00
|
|
|
|
|
|
|
if ctx.HasError() {
|
2024-03-23 22:43:44 +01:00
|
|
|
// pre-fill the form with the submitted data
|
|
|
|
var w webhook.Webhook
|
|
|
|
w.URL = fields.URL
|
|
|
|
w.ContentType = fields.ContentType
|
|
|
|
w.Secret = fields.Secret
|
2024-04-03 14:22:36 +02:00
|
|
|
w.HookEvent = ParseHookEvent(fields.WebhookCoreForm)
|
|
|
|
w.IsActive = fields.Active
|
2024-03-23 22:43:44 +01:00
|
|
|
w.HTTPMethod = fields.HTTPMethod
|
2024-04-03 14:22:36 +02:00
|
|
|
err := w.SetHeaderAuthorization(fields.AuthorizationHeader)
|
2024-03-23 22:43:44 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SetHeaderAuthorization", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
ctx.Data["HookMetadata"] = fields.Metadata
|
|
|
|
|
2024-03-20 17:39:02 +01:00
|
|
|
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 17:48:23 +02:00
|
|
|
var meta []byte
|
2024-03-21 14:15:56 +01:00
|
|
|
if fields.Metadata != nil {
|
|
|
|
meta, err = json.Marshal(fields.Metadata)
|
2022-08-11 17:48:23 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 22:08:05 +00:00
|
|
|
RepoID: orCtx.RepoID,
|
2024-03-21 14:15:56 +01:00
|
|
|
URL: fields.URL,
|
|
|
|
HTTPMethod: fields.HTTPMethod,
|
|
|
|
ContentType: fields.ContentType,
|
|
|
|
Secret: fields.Secret,
|
2024-04-03 14:22:36 +02:00
|
|
|
HookEvent: ParseHookEvent(fields.WebhookCoreForm),
|
|
|
|
IsActive: fields.Active,
|
2024-03-21 14:39:02 +01:00
|
|
|
Type: hookType,
|
2022-08-11 17:48:23 +02:00
|
|
|
Meta: string(meta),
|
2023-03-10 15:28:32 +01:00
|
|
|
OwnerID: orCtx.OwnerID,
|
2020-03-08 22:08:05 +00:00
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
2024-04-03 14:22:36 +02:00
|
|
|
err = w.SetHeaderAuthorization(fields.AuthorizationHeader)
|
2022-11-03 19:23:20 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SetHeaderAuthorization", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-29 09:17:15 +02:00
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
2022-03-22 23:22:54 +08:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-18 22:33:20 -04:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
|
|
|
|
2024-03-21 13:23:27 +01:00
|
|
|
func WebhookUpdate(ctx *context.Context) {
|
2022-08-23 08:52:35 +02:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
2024-03-21 14:15:56 +01:00
|
|
|
handler := webhook_service.GetWebhookHandler(w.Type)
|
|
|
|
if handler == nil {
|
|
|
|
ctx.NotFound("GetWebhookHandler", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
fields := handler.UnmarshalForm(func(form any) {
|
2024-03-21 14:15:56 +01:00
|
|
|
errs := binding.Bind(ctx.Req, form)
|
|
|
|
middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error checked below in ctx.HasError
|
|
|
|
})
|
|
|
|
|
2024-03-23 22:43:44 +01:00
|
|
|
// pre-fill the form with the submitted data
|
|
|
|
w.URL = fields.URL
|
|
|
|
w.ContentType = fields.ContentType
|
|
|
|
w.Secret = fields.Secret
|
2024-04-03 14:22:36 +02:00
|
|
|
w.HookEvent = ParseHookEvent(fields.WebhookCoreForm)
|
|
|
|
w.IsActive = fields.Active
|
2024-03-23 22:43:44 +01:00
|
|
|
w.HTTPMethod = fields.HTTPMethod
|
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
err := w.SetHeaderAuthorization(fields.AuthorizationHeader)
|
2024-03-23 22:43:44 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SetHeaderAuthorization", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-23 08:52:35 +02:00
|
|
|
if ctx.HasError() {
|
2024-03-23 22:43:44 +01:00
|
|
|
ctx.Data["HookMetadata"] = fields.Metadata
|
2024-03-20 17:39:02 +01:00
|
|
|
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
|
2022-08-23 08:52:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var meta []byte
|
2024-03-21 14:15:56 +01:00
|
|
|
if fields.Metadata != nil {
|
|
|
|
meta, err = json.Marshal(fields.Metadata)
|
2022-08-23 08:52:35 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Meta = string(meta)
|
|
|
|
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2023-10-14 10:37:24 +02:00
|
|
|
} else if err := webhook.UpdateWebhook(ctx, w); err != nil {
|
2022-08-23 08:52:35 +02:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
2023-03-10 15:28:32 +01:00
|
|
|
func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) {
|
|
|
|
orCtx, err := getOwnerRepoCtx(ctx)
|
2015-12-05 13:24:13 -05:00
|
|
|
if err != nil {
|
2023-03-10 15:28:32 +01:00
|
|
|
ctx.ServerError("getOwnerRepoCtx", err)
|
2015-12-05 13:24:13 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
2024-03-14 10:37:15 +09:00
|
|
|
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
|
2024-03-23 21:28:30 +01:00
|
|
|
ctx.Data["WebhookList"] = webhook_service.List()
|
2015-12-05 13:24:13 -05:00
|
|
|
|
2021-11-10 13:13:16 +08:00
|
|
|
var w *webhook.Webhook
|
2016-07-16 01:02:55 +08:00
|
|
|
if orCtx.RepoID > 0 {
|
2023-10-14 10:37:24 +02:00
|
|
|
w, err = webhook.GetWebhookByRepoID(ctx, orCtx.RepoID, ctx.ParamsInt64(":id"))
|
2023-03-10 15:28:32 +01:00
|
|
|
} else if orCtx.OwnerID > 0 {
|
2023-10-14 10:37:24 +02:00
|
|
|
w, err = webhook.GetWebhookByOwnerID(ctx, orCtx.OwnerID, ctx.ParamsInt64(":id"))
|
2021-01-15 01:24:03 +02:00
|
|
|
} else if orCtx.IsAdmin {
|
2023-01-29 02:12:10 +08:00
|
|
|
w, err = webhook.GetSystemOrDefaultWebhook(ctx, ctx.ParamsInt64(":id"))
|
2016-07-16 01:02:55 +08:00
|
|
|
}
|
2021-01-15 01:24:03 +02:00
|
|
|
if err != nil || w == nil {
|
2021-11-10 13:13:16 +08:00
|
|
|
if webhook.IsErrWebhookNotExist(err) {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.NotFound("GetWebhookByID", nil)
|
2015-12-05 13:24:13 -05:00
|
|
|
} else {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("GetWebhookByID", err)
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 01:20:13 +08:00
|
|
|
ctx.Data["HookType"] = w.Type
|
2024-03-20 15:44:01 +01:00
|
|
|
|
|
|
|
if handler := webhook_service.GetWebhookHandler(w.Type); handler != nil {
|
|
|
|
ctx.Data["HookMetadata"] = handler.Metadata(w)
|
2024-03-22 16:02:48 +01:00
|
|
|
ctx.Data["WebhookHandler"] = handler
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
|
2023-10-14 10:37:24 +02:00
|
|
|
ctx.Data["History"], err = w.History(ctx, 1)
|
2015-12-05 13:24:13 -05:00
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("History", err)
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
return orCtx, w
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:43:43 +01:00
|
|
|
// WebhookEdit render editing web hook page
|
|
|
|
func WebhookEdit(ctx *context.Context) {
|
2015-12-05 13:24:13 -05:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
|
2024-03-21 14:43:43 +01:00
|
|
|
// WebhookTest test if web hook is work fine
|
|
|
|
func WebhookTest(ctx *context.Context) {
|
2017-08-29 22:55:24 +08:00
|
|
|
hookID := ctx.ParamsInt64(":id")
|
2023-10-14 10:37:24 +02:00
|
|
|
w, err := webhook.GetWebhookByRepoID(ctx, ctx.Repo.Repository.ID, hookID)
|
2017-08-29 22:55:24 +08:00
|
|
|
if err != nil {
|
2022-10-21 18:21:56 +02:00
|
|
|
ctx.Flash.Error("GetWebhookByRepoID: " + err.Error())
|
2022-03-23 05:54:07 +01:00
|
|
|
ctx.Status(http.StatusInternalServerError)
|
2017-08-29 22:55:24 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-15 05:53:47 -07:00
|
|
|
// Grab latest commit or fake one if it's empty repository.
|
|
|
|
commit := ctx.Repo.Commit
|
|
|
|
if commit == nil {
|
2021-11-24 17:49:20 +08:00
|
|
|
ghost := user_model.NewGhostUser()
|
2024-03-11 05:30:36 +08:00
|
|
|
objectFormat := git.ObjectFormatFromName(ctx.Repo.Repository.ObjectFormatName)
|
2016-08-15 05:53:47 -07:00
|
|
|
commit = &git.Commit{
|
2023-12-17 19:56:08 +08:00
|
|
|
ID: objectFormat.EmptyObjectID(),
|
2016-08-15 05:53:47 -07:00
|
|
|
Author: ghost.NewGitSig(),
|
|
|
|
Committer: ghost.NewGitSig(),
|
|
|
|
CommitMessage: "This is a fake commit",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add context cache as a request level cache (#22294)
To avoid duplicated load of the same data in an HTTP request, we can set
a context cache to do that. i.e. Some pages may load a user from a
database with the same id in different areas on the same page. But the
code is hidden in two different deep logic. How should we share the
user? As a result of this PR, now if both entry functions accept
`context.Context` as the first parameter and we just need to refactor
`GetUserByID` to reuse the user from the context cache. Then it will not
be loaded twice on an HTTP request.
But of course, sometimes we would like to reload an object from the
database, that's why `RemoveContextData` is also exposed.
The core context cache is here. It defines a new context
```go
type cacheContext struct {
ctx context.Context
data map[any]map[any]any
lock sync.RWMutex
}
var cacheContextKey = struct{}{}
func WithCacheContext(ctx context.Context) context.Context {
return context.WithValue(ctx, cacheContextKey, &cacheContext{
ctx: ctx,
data: make(map[any]map[any]any),
})
}
```
Then you can use the below 4 methods to read/write/del the data within
the same context.
```go
func GetContextData(ctx context.Context, tp, key any) any
func SetContextData(ctx context.Context, tp, key, value any)
func RemoveContextData(ctx context.Context, tp, key any)
func GetWithContextCache[T any](ctx context.Context, cacheGroupKey string, cacheTargetID any, f func() (T, error)) (T, error)
```
Then let's take a look at how `system.GetString` implement it.
```go
func GetSetting(ctx context.Context, key string) (string, error) {
return cache.GetWithContextCache(ctx, contextCacheKey, key, func() (string, error) {
return cache.GetString(genSettingCacheKey(key), func() (string, error) {
res, err := GetSettingNoCache(ctx, key)
if err != nil {
return "", err
}
return res.SettingValue, nil
})
})
}
```
First, it will check if context data include the setting object with the
key. If not, it will query from the global cache which may be memory or
a Redis cache. If not, it will get the object from the database. In the
end, if the object gets from the global cache or database, it will be
set into the context cache.
An object stored in the context cache will only be destroyed after the
context disappeared.
2023-02-15 21:37:34 +08:00
|
|
|
apiUser := convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone)
|
2021-06-29 15:34:03 +02:00
|
|
|
|
|
|
|
apiCommit := &api.PayloadCommit{
|
|
|
|
ID: commit.ID.String(),
|
|
|
|
Message: commit.Message(),
|
2021-11-16 18:18:25 +00:00
|
|
|
URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()),
|
2021-06-29 15:34:03 +02:00
|
|
|
Author: &api.PayloadUser{
|
|
|
|
Name: commit.Author.Name,
|
|
|
|
Email: commit.Author.Email,
|
|
|
|
},
|
|
|
|
Committer: &api.PayloadUser{
|
|
|
|
Name: commit.Committer.Name,
|
|
|
|
Email: commit.Committer.Email,
|
2015-12-05 13:24:13 -05:00
|
|
|
},
|
2021-06-29 15:34:03 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 17:18:07 +08:00
|
|
|
commitID := commit.ID.String()
|
2021-06-29 15:34:03 +02:00
|
|
|
p := &api.PushPayload{
|
2022-10-16 18:22:34 +02:00
|
|
|
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
|
|
|
|
Before: commitID,
|
|
|
|
After: commitID,
|
|
|
|
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
|
|
|
|
Commits: []*api.PayloadCommit{apiCommit},
|
|
|
|
TotalCommits: 1,
|
|
|
|
HeadCommit: apiCommit,
|
2023-06-22 21:08:08 +08:00
|
|
|
Repo: convert.ToRepo(ctx, ctx.Repo.Repository, access_model.Permission{AccessMode: perm.AccessModeNone}),
|
2022-10-16 18:22:34 +02:00
|
|
|
Pusher: apiUser,
|
|
|
|
Sender: apiUser,
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
2023-01-01 16:23:15 +01:00
|
|
|
if err := webhook_service.PrepareWebhook(ctx, w, webhook_module.HookEventPush, p); err != nil {
|
2017-08-29 22:55:24 +08:00
|
|
|
ctx.Flash.Error("PrepareWebhook: " + err.Error())
|
2022-03-23 05:54:07 +01:00
|
|
|
ctx.Status(http.StatusInternalServerError)
|
2015-12-05 13:24:13 -05:00
|
|
|
} else {
|
2022-01-05 22:00:20 +01:00
|
|
|
ctx.Flash.Info(ctx.Tr("repo.settings.webhook.delivery.success"))
|
2022-03-23 05:54:07 +01:00
|
|
|
ctx.Status(http.StatusOK)
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:43:43 +01:00
|
|
|
// WebhookReplay replays a webhook
|
|
|
|
func WebhookReplay(ctx *context.Context) {
|
2022-01-05 22:00:20 +01:00
|
|
|
hookTaskUUID := ctx.Params(":uuid")
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-21 18:21:56 +02:00
|
|
|
if err := webhook_service.ReplayHookTask(ctx, w, hookTaskUUID); err != nil {
|
2022-01-05 22:00:20 +01:00
|
|
|
if webhook.IsErrHookTaskNotExist(err) {
|
|
|
|
ctx.NotFound("ReplayHookTask", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("ReplayHookTask", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.webhook.delivery.success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
2024-03-21 14:43:43 +01:00
|
|
|
// WebhookDelete delete a webhook
|
|
|
|
func WebhookDelete(ctx *context.Context) {
|
2023-10-14 10:37:24 +02:00
|
|
|
if err := webhook.DeleteWebhookByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil {
|
2016-07-17 08:33:59 +08:00
|
|
|
ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
|
2015-12-05 13:24:13 -05:00
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:04:01 +08:00
|
|
|
ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/hooks")
|
2015-12-05 13:24:13 -05:00
|
|
|
}
|