2015-12-05 23:54:13 +05:30
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2017-05-29 12:47:15 +05:30
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2023-07-02 06:29:32 +05:30
|
|
|
package setting
|
2015-12-05 23:54:13 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-04-05 21:00:52 +05:30
|
|
|
"net/http"
|
2021-11-16 23:48:25 +05:30
|
|
|
"net/url"
|
2019-03-19 08:03:20 +05:30
|
|
|
"path"
|
2015-12-05 23:54:13 +05:30
|
|
|
"strings"
|
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2023-06-22 18:38:08 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-10 10:43:16 +05:30
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
2019-03-27 15:03:00 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2016-11-10 21:54:48 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 15:51:34 +05:30
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-12-25 15:29:32 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 21:06:53 +05:30
|
|
|
"code.gitea.io/gitea/modules/web"
|
2023-01-01 20:53:15 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2024-02-27 12:42:22 +05:30
|
|
|
"code.gitea.io/gitea/services/context"
|
2022-12-29 08:27:15 +05:30
|
|
|
"code.gitea.io/gitea/services/convert"
|
2021-04-07 01:14:05 +05:30
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-11-10 10:43:16 +05:30
|
|
|
webhook_service "code.gitea.io/gitea/services/webhook"
|
2015-12-05 23:54:13 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-19 08:03:20 +05:30
|
|
|
tplHooks base.TplName = "repo/settings/webhook/base"
|
|
|
|
tplHookNew base.TplName = "repo/settings/webhook/new"
|
|
|
|
tplOrgHookNew base.TplName = "org/settings/hook_new"
|
2023-03-10 19:58:32 +05:30
|
|
|
tplUserHookNew base.TplName = "user/settings/hook_new"
|
2019-03-19 08:03:20 +05:30
|
|
|
tplAdminHookNew base.TplName = "admin/hook_new"
|
2015-12-05 23:54:13 +05:30
|
|
|
)
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// Webhooks render web hooks list page
|
2016-03-11 22:26:52 +05:30
|
|
|
func Webhooks(ctx *context.Context) {
|
2015-12-05 23:54:13 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
2019-03-19 08:03:20 +05:30
|
|
|
ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
|
2021-01-15 04:54:03 +05:30
|
|
|
ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks"
|
2023-01-14 14:37:01 +05:30
|
|
|
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://forgejo.org/docs/latest/user/webhooks/")
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2023-11-24 09:19:41 +05:30
|
|
|
ws, err := db.Find[webhook.Webhook](ctx, webhook.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
|
2015-12-05 23:54:13 +05:30
|
|
|
if err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("GetWebhooksByRepoID", err)
|
2015-12-05 23:54:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhooks"] = ws
|
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, tplHooks)
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2023-03-10 19:58:32 +05:30
|
|
|
type ownerRepoCtx struct {
|
|
|
|
OwnerID int64
|
2020-03-09 03:38:05 +05:30
|
|
|
RepoID int64
|
|
|
|
IsAdmin bool
|
|
|
|
IsSystemWebhook bool
|
|
|
|
Link string
|
2021-01-15 04:54:03 +05:30
|
|
|
LinkNew string
|
2020-03-09 03:38:05 +05:30
|
|
|
NewTemplate base.TplName
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2023-03-10 19:58:32 +05:30
|
|
|
// 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 05:38:47 +05:30
|
|
|
if ctx.Data["PageIsRepoSettings"] == true {
|
2023-03-10 19:58:32 +05:30
|
|
|
return &ownerRepoCtx{
|
2015-12-05 23:54:13 +05:30
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2019-03-19 08:03:20 +05:30
|
|
|
Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
2021-01-15 04:54:03 +05:30
|
|
|
LinkNew: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
2016-11-08 02:28:22 +05:30
|
|
|
NewTemplate: tplHookNew,
|
2015-12-05 23:54:13 +05:30
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-28 05:38:47 +05:30
|
|
|
if ctx.Data["PageIsOrgSettings"] == true {
|
2023-03-10 19:58:32 +05:30
|
|
|
return &ownerRepoCtx{
|
|
|
|
OwnerID: ctx.ContextUser.ID,
|
2019-03-19 08:03:20 +05:30
|
|
|
Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
2021-01-15 04:54:03 +05:30
|
|
|
LinkNew: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
2016-11-08 02:28:22 +05:30
|
|
|
NewTemplate: tplOrgHookNew,
|
2015-12-05 23:54:13 +05:30
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-28 05:38:47 +05:30
|
|
|
if ctx.Data["PageIsUserSettings"] == true {
|
2023-03-10 19:58:32 +05:30
|
|
|
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-09 03:38:05 +05:30
|
|
|
|
2023-04-28 05:38:47 +05:30
|
|
|
if ctx.Data["PageIsAdmin"] == true {
|
2023-03-10 19:58:32 +05:30
|
|
|
return &ownerRepoCtx{
|
2020-03-09 03:38:05 +05:30
|
|
|
IsAdmin: true,
|
2023-03-10 19:58:32 +05:30
|
|
|
IsSystemWebhook: ctx.Params(":configType") == "system-hooks",
|
2021-01-15 04:54:03 +05:30
|
|
|
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-11 07:40:57 +05:30
|
|
|
LinkNew: path.Join(setting.AppSubURL, "/admin/", ctx.Params(":configType")),
|
2020-03-09 03:38:05 +05:30
|
|
|
NewTemplate: tplAdminHookNew,
|
2019-03-19 08:03:20 +05:30
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-10 19:58:32 +05:30
|
|
|
return nil, errors.New("unable to set OwnerRepo context")
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2016-03-11 22:26:52 +05:30
|
|
|
func checkHookType(ctx *context.Context) string {
|
2015-12-05 23:54:13 +05:30
|
|
|
hookType := strings.ToLower(ctx.Params(":type"))
|
Improve utils of slices (#22379)
- Move the file `compare.go` and `slice.go` to `slice.go`.
- Fix `ExistsInSlice`, it's buggy
- It uses `sort.Search`, so it assumes that the input slice is sorted.
- It passes `func(i int) bool { return slice[i] == target })` to
`sort.Search`, that's incorrect, check the doc of `sort.Search`.
- Conbine `IsInt64InSlice(int64, []int64)` and `ExistsInSlice(string,
[]string)` to `SliceContains[T]([]T, T)`.
- Conbine `IsSliceInt64Eq([]int64, []int64)` and `IsEqualSlice([]string,
[]string)` to `SliceSortedEqual[T]([]T, T)`.
- Add `SliceEqual[T]([]T, T)` as a distinction from
`SliceSortedEqual[T]([]T, T)`.
- Redesign `RemoveIDFromList([]int64, int64) ([]int64, bool)` to
`SliceRemoveAll[T]([]T, T) []T`.
- Add `SliceContainsFunc[T]([]T, func(T) bool)` and
`SliceRemoveAllFunc[T]([]T, func(T) bool)` for general use.
- Add comments to explain why not `golang.org/x/exp/slices`.
- Add unit tests.
2023-01-11 11:01:16 +05:30
|
|
|
if !util.SliceContainsString(setting.Webhook.Types, hookType, true) {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.NotFound("checkHookType", nil)
|
2015-12-05 23:54:13 +05:30
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return hookType
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// WebhooksNew render creating webhook page
|
2016-03-11 22:26:52 +05:30
|
|
|
func WebhooksNew(ctx *context.Context) {
|
2015-12-05 23:54:13 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
2023-01-01 20:53:15 +05:30
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2023-03-10 19:58:32 +05:30
|
|
|
orCtx, err := getOwnerRepoCtx(ctx)
|
2015-12-05 23:54:13 +05:30
|
|
|
if err != nil {
|
2023-03-10 19:58:32 +05:30
|
|
|
ctx.ServerError("getOwnerRepoCtx", err)
|
2015-12-05 23:54:13 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-09 03:38:05 +05:30
|
|
|
if orCtx.IsAdmin && orCtx.IsSystemWebhook {
|
|
|
|
ctx.Data["PageIsAdminSystemHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminSystemHooksNew"] = true
|
|
|
|
} else if orCtx.IsAdmin {
|
2021-01-15 04:54:03 +05:30
|
|
|
ctx.Data["PageIsAdminDefaultHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminDefaultHooksNew"] = true
|
2019-03-19 08:03:20 +05:30
|
|
|
} else {
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
}
|
|
|
|
|
2017-08-28 10:36:45 +05:30
|
|
|
hookType := checkHookType(ctx)
|
|
|
|
ctx.Data["HookType"] = hookType
|
2015-12-05 23:54:13 +05:30
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2017-08-28 10:36:45 +05:30
|
|
|
if hookType == "discord" {
|
2023-07-05 00:06:08 +05:30
|
|
|
ctx.Data["DiscordHook"] = map[string]any{
|
2017-08-28 10:36:45 +05:30
|
|
|
"Username": "Gitea",
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 04:54:03 +05:30
|
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
2024-02-15 19:29:48 +05:30
|
|
|
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2021-04-05 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
// ParseHookEvent convert web form content to webhook.HookEvent
|
2023-01-01 20:53:15 +05:30
|
|
|
func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent {
|
|
|
|
return &webhook_module.HookEvent{
|
2015-12-05 23:54:13 +05:30
|
|
|
PushOnly: form.PushOnly(),
|
|
|
|
SendEverything: form.SendEverything(),
|
|
|
|
ChooseEvents: form.ChooseEvents(),
|
2023-01-01 20:53:15 +05:30
|
|
|
HookEvents: webhook_module.HookEvents{
|
2023-05-25 07:36:27 +05:30
|
|
|
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,
|
2015-12-05 23:54:13 +05:30
|
|
|
},
|
2019-09-09 11:18:21 +05:30
|
|
|
BranchFilter: form.BranchFilter,
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
type webhookParams struct {
|
|
|
|
// Type should be imported from webhook package (webhook.XXX)
|
|
|
|
Type string
|
|
|
|
|
2022-08-11 21:18:23 +05:30
|
|
|
URL string
|
|
|
|
ContentType webhook.HookContentType
|
|
|
|
Secret string
|
|
|
|
HTTPMethod string
|
|
|
|
WebhookForm forms.WebhookForm
|
2023-07-05 00:06:08 +05:30
|
|
|
Meta any
|
2022-08-11 21:18:23 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
func createWebhook(ctx *context.Context, params webhookParams) {
|
2017-05-29 12:47:15 +05:30
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2023-01-01 20:53:15 +05:30
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
|
2022-08-11 21:18:23 +05:30
|
|
|
ctx.Data["HookType"] = params.Type
|
2017-05-29 12:47:15 +05:30
|
|
|
|
2023-03-10 19:58:32 +05:30
|
|
|
orCtx, err := getOwnerRepoCtx(ctx)
|
2017-05-29 12:47:15 +05:30
|
|
|
if err != nil {
|
2023-03-10 19:58:32 +05:30
|
|
|
ctx.ServerError("getOwnerRepoCtx", err)
|
2017-05-29 12:47:15 +05:30
|
|
|
return
|
|
|
|
}
|
2021-01-15 04:54:03 +05:30
|
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
2024-03-20 22:09:02 +05:30
|
|
|
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
|
2017-05-29 12:47:15 +05:30
|
|
|
|
|
|
|
if ctx.HasError() {
|
2024-03-20 22:09:02 +05:30
|
|
|
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
|
2017-05-29 12:47:15 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:18:23 +05:30
|
|
|
var meta []byte
|
|
|
|
if params.Meta != nil {
|
|
|
|
meta, err = json.Marshal(params.Meta)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-29 12:47:15 +05:30
|
|
|
}
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
w := &webhook.Webhook{
|
2020-03-09 03:38:05 +05:30
|
|
|
RepoID: orCtx.RepoID,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: params.URL,
|
|
|
|
HTTPMethod: params.HTTPMethod,
|
|
|
|
ContentType: params.ContentType,
|
|
|
|
Secret: params.Secret,
|
|
|
|
HookEvent: ParseHookEvent(params.WebhookForm),
|
|
|
|
IsActive: params.WebhookForm.Active,
|
|
|
|
Type: params.Type,
|
|
|
|
Meta: string(meta),
|
2023-03-10 19:58:32 +05:30
|
|
|
OwnerID: orCtx.OwnerID,
|
2020-03-09 03:38:05 +05:30
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-05-29 12:47:15 +05:30
|
|
|
}
|
2022-11-03 23:53:20 +05:30
|
|
|
err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SetHeaderAuthorization", err)
|
|
|
|
return
|
|
|
|
}
|
2017-05-29 12:47:15 +05:30
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-05-29 12:47:15 +05:30
|
|
|
return
|
2022-03-22 20:52:54 +05:30
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-05-29 12:47:15 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-19 08:03:20 +05:30
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-05-29 12:47:15 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
func editWebhook(ctx *context.Context, params webhookParams) {
|
|
|
|
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
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2024-03-20 22:09:02 +05:30
|
|
|
ctx.HTML(http.StatusUnprocessableEntity, orCtx.NewTemplate)
|
2022-08-23 12:22:35 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var meta []byte
|
|
|
|
var err error
|
|
|
|
if params.Meta != nil {
|
|
|
|
meta, err = json.Marshal(params.Meta)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = params.URL
|
|
|
|
w.ContentType = params.ContentType
|
|
|
|
w.Secret = params.Secret
|
|
|
|
w.HookEvent = ParseHookEvent(params.WebhookForm)
|
|
|
|
w.IsActive = params.WebhookForm.Active
|
|
|
|
w.HTTPMethod = params.HTTPMethod
|
|
|
|
w.Meta = string(meta)
|
|
|
|
|
2022-11-03 23:53:20 +05:30
|
|
|
err = w.SetHeaderAuthorization(params.WebhookForm.AuthorizationHeader)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("SetHeaderAuthorization", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2023-10-14 14:07:24 +05:30
|
|
|
} else if err := webhook.UpdateWebhook(ctx, w); err != nil {
|
2022-08-23 12:22:35 +05:30
|
|
|
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-01-14 14:37:01 +05:30
|
|
|
// ForgejoHooksNewPost response for creating Forgejo webhook
|
|
|
|
func ForgejoHooksNewPost(ctx *context.Context) {
|
|
|
|
createWebhook(ctx, forgejoHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// ForgejoHooksEditPost response for editing Forgejo webhook
|
|
|
|
func ForgejoHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, forgejoHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func forgejoHookParams(ctx *context.Context) webhookParams {
|
|
|
|
form := web.GetForm(ctx).(*forms.NewWebhookForm)
|
|
|
|
|
|
|
|
contentType := webhook.ContentTypeJSON
|
|
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
|
|
contentType = webhook.ContentTypeForm
|
|
|
|
}
|
|
|
|
|
|
|
|
return webhookParams{
|
|
|
|
Type: webhook_module.FORGEJO,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: contentType,
|
|
|
|
Secret: form.Secret,
|
|
|
|
HTTPMethod: form.HTTPMethod,
|
|
|
|
WebhookForm: form.WebhookForm,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-11 21:18:23 +05:30
|
|
|
// GiteaHooksNewPost response for creating Gitea webhook
|
|
|
|
func GiteaHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, giteaHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GiteaHooksEditPost response for editing Gitea webhook
|
|
|
|
func GiteaHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, giteaHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func giteaHookParams(ctx *context.Context) webhookParams {
|
2022-08-11 21:18:23 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewWebhookForm)
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2022-08-11 21:18:23 +05:30
|
|
|
contentType := webhook.ContentTypeJSON
|
|
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
|
|
contentType = webhook.ContentTypeForm
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.GITEA,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: contentType,
|
|
|
|
Secret: form.Secret,
|
|
|
|
HTTPMethod: form.HTTPMethod,
|
|
|
|
WebhookForm: form.WebhookForm,
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2022-08-11 21:18:23 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// GogsHooksNewPost response for creating Gogs webhook
|
2022-08-11 21:18:23 +05:30
|
|
|
func GogsHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, gogsHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GogsHooksEditPost response for editing Gogs webhook
|
|
|
|
func GogsHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, gogsHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func gogsHookParams(ctx *context.Context) webhookParams {
|
2022-08-11 21:18:23 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewGogshookForm)
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
contentType := webhook.ContentTypeJSON
|
|
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
|
|
contentType = webhook.ContentTypeForm
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.GOGS,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: contentType,
|
|
|
|
Secret: form.Secret,
|
|
|
|
WebhookForm: form.WebhookForm,
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// DiscordHooksNewPost response for creating Discord webhook
|
2021-01-26 21:06:53 +05:30
|
|
|
func DiscordHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, discordHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DiscordHooksEditPost response for editing Discord webhook
|
|
|
|
func DiscordHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, discordHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func discordHookParams(ctx *context.Context) webhookParams {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewDiscordHookForm)
|
2017-08-28 10:36:45 +05:30
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.DISCORD,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
|
|
|
Meta: &webhook_service.DiscordMeta{
|
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
},
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2017-08-28 10:36:45 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// DingtalkHooksNewPost response for creating Dingtalk webhook
|
2021-01-26 21:06:53 +05:30
|
|
|
func DingtalkHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, dingtalkHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DingtalkHooksEditPost response for editing Dingtalk webhook
|
|
|
|
func DingtalkHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, dingtalkHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func dingtalkHookParams(ctx *context.Context) webhookParams {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewDingtalkHookForm)
|
2017-11-21 09:56:43 +05:30
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.DINGTALK,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2017-11-21 09:56:43 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// TelegramHooksNewPost response for creating Telegram webhook
|
2021-01-26 21:06:53 +05:30
|
|
|
func TelegramHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, telegramHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TelegramHooksEditPost response for editing Telegram webhook
|
|
|
|
func TelegramHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, telegramHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func telegramHookParams(ctx *context.Context) webhookParams {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewTelegramHookForm)
|
2019-04-19 08:15:02 +05:30
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.TELEGRAM,
|
2023-08-13 19:30:06 +05:30
|
|
|
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&message_thread_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID), url.QueryEscape(form.ThreadID)),
|
2022-08-11 21:18:23 +05:30
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
|
|
|
Meta: &webhook_service.TelegramMeta{
|
|
|
|
BotToken: form.BotToken,
|
|
|
|
ChatID: form.ChatID,
|
2023-08-13 19:30:06 +05:30
|
|
|
ThreadID: form.ThreadID,
|
2022-08-11 21:18:23 +05:30
|
|
|
},
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2019-04-19 08:15:02 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// MatrixHooksNewPost response for creating Matrix webhook
|
2021-01-26 21:06:53 +05:30
|
|
|
func MatrixHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, matrixHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatrixHooksEditPost response for editing Matrix webhook
|
|
|
|
func MatrixHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, matrixHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func matrixHookParams(ctx *context.Context) webhookParams {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewMatrixHookForm)
|
2020-03-28 18:39:55 +05:30
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.MATRIX,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID)),
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
HTTPMethod: http.MethodPut,
|
|
|
|
WebhookForm: form.WebhookForm,
|
|
|
|
Meta: &webhook_service.MatrixMeta{
|
|
|
|
HomeserverURL: form.HomeserverURL,
|
|
|
|
Room: form.RoomID,
|
|
|
|
MessageType: form.MessageType,
|
|
|
|
},
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2020-03-28 18:39:55 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// MSTeamsHooksNewPost response for creating MSTeams webhook
|
2021-01-26 21:06:53 +05:30
|
|
|
func MSTeamsHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, mSTeamsHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// MSTeamsHooksEditPost response for editing MSTeams webhook
|
|
|
|
func MSTeamsHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, mSTeamsHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func mSTeamsHookParams(ctx *context.Context) webhookParams {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm)
|
2019-04-19 19:48:06 +05:30
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.MSTEAMS,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2019-04-19 19:48:06 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// SlackHooksNewPost response for creating Slack webhook
|
2021-01-26 21:06:53 +05:30
|
|
|
func SlackHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, slackHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// SlackHooksEditPost response for editing Slack webhook
|
|
|
|
func SlackHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, slackHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func slackHookParams(ctx *context.Context) webhookParams {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewSlackHookForm)
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.SLACK,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
|
|
|
Meta: &webhook_service.SlackMeta{
|
|
|
|
Channel: strings.TrimSpace(form.Channel),
|
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
Color: form.Color,
|
|
|
|
},
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// FeishuHooksNewPost response for creating Feishu webhook
|
2021-01-26 21:06:53 +05:30
|
|
|
func FeishuHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, feishuHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FeishuHooksEditPost response for editing Feishu webhook
|
|
|
|
func FeishuHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, feishuHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func feishuHookParams(ctx *context.Context) webhookParams {
|
2021-04-07 01:14:05 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewFeishuHookForm)
|
2020-02-12 14:18:28 +05:30
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.FEISHU,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2020-02-12 14:18:28 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// WechatworkHooksNewPost response for creating Wechatwork webhook
|
2021-07-23 10:11:27 +05:30
|
|
|
func WechatworkHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, wechatworkHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// WechatworkHooksEditPost response for editing Wechatwork webhook
|
|
|
|
func WechatworkHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, wechatworkHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func wechatworkHookParams(ctx *context.Context) webhookParams {
|
2021-07-23 10:11:27 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewWechatWorkHookForm)
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.WECHATWORK,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2021-07-23 10:11:27 +05:30
|
|
|
}
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
// PackagistHooksNewPost response for creating Packagist webhook
|
2022-01-23 19:16:30 +05:30
|
|
|
func PackagistHooksNewPost(ctx *context.Context) {
|
2022-08-23 12:22:35 +05:30
|
|
|
createWebhook(ctx, packagistHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PackagistHooksEditPost response for editing Packagist webhook
|
|
|
|
func PackagistHooksEditPost(ctx *context.Context) {
|
|
|
|
editWebhook(ctx, packagistHookParams(ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
func packagistHookParams(ctx *context.Context) webhookParams {
|
2022-01-23 19:16:30 +05:30
|
|
|
form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
|
|
|
|
|
2022-08-23 12:22:35 +05:30
|
|
|
return webhookParams{
|
2023-01-01 20:53:15 +05:30
|
|
|
Type: webhook_module.PACKAGIST,
|
2022-08-11 21:18:23 +05:30
|
|
|
URL: fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)),
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
WebhookForm: form.WebhookForm,
|
|
|
|
Meta: &webhook_service.PackagistMeta{
|
|
|
|
Username: form.Username,
|
|
|
|
APIToken: form.APIToken,
|
|
|
|
PackageURL: form.PackageURL,
|
|
|
|
},
|
2022-08-23 12:22:35 +05:30
|
|
|
}
|
2022-01-23 19:16:30 +05:30
|
|
|
}
|
|
|
|
|
2023-03-10 19:58:32 +05:30
|
|
|
func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) {
|
|
|
|
orCtx, err := getOwnerRepoCtx(ctx)
|
2015-12-05 23:54:13 +05:30
|
|
|
if err != nil {
|
2023-03-10 19:58:32 +05:30
|
|
|
ctx.ServerError("getOwnerRepoCtx", err)
|
2015-12-05 23:54:13 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
2024-03-14 07:07:15 +05:30
|
|
|
ctx.Data["BaseLinkNew"] = orCtx.LinkNew
|
2015-12-05 23:54:13 +05:30
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
var w *webhook.Webhook
|
2016-07-15 22:32:55 +05:30
|
|
|
if orCtx.RepoID > 0 {
|
2023-10-14 14:07:24 +05:30
|
|
|
w, err = webhook.GetWebhookByRepoID(ctx, orCtx.RepoID, ctx.ParamsInt64(":id"))
|
2023-03-10 19:58:32 +05:30
|
|
|
} else if orCtx.OwnerID > 0 {
|
2023-10-14 14:07:24 +05:30
|
|
|
w, err = webhook.GetWebhookByOwnerID(ctx, orCtx.OwnerID, ctx.ParamsInt64(":id"))
|
2021-01-15 04:54:03 +05:30
|
|
|
} else if orCtx.IsAdmin {
|
2023-01-28 23:42:10 +05:30
|
|
|
w, err = webhook.GetSystemOrDefaultWebhook(ctx, ctx.ParamsInt64(":id"))
|
2016-07-15 22:32:55 +05:30
|
|
|
}
|
2021-01-15 04:54:03 +05:30
|
|
|
if err != nil || w == nil {
|
2021-11-10 10:43:16 +05:30
|
|
|
if webhook.IsErrWebhookNotExist(err) {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.NotFound("GetWebhookByID", nil)
|
2015-12-05 23:54:13 +05:30
|
|
|
} else {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("GetWebhookByID", err)
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 22:50:13 +05:30
|
|
|
ctx.Data["HookType"] = w.Type
|
|
|
|
switch w.Type {
|
2023-01-01 20:53:15 +05:30
|
|
|
case webhook_module.SLACK:
|
2021-11-10 10:43:16 +05:30
|
|
|
ctx.Data["SlackHook"] = webhook_service.GetSlackHook(w)
|
2023-01-01 20:53:15 +05:30
|
|
|
case webhook_module.DISCORD:
|
2021-11-10 10:43:16 +05:30
|
|
|
ctx.Data["DiscordHook"] = webhook_service.GetDiscordHook(w)
|
2023-01-01 20:53:15 +05:30
|
|
|
case webhook_module.TELEGRAM:
|
2021-11-10 10:43:16 +05:30
|
|
|
ctx.Data["TelegramHook"] = webhook_service.GetTelegramHook(w)
|
2023-01-01 20:53:15 +05:30
|
|
|
case webhook_module.MATRIX:
|
2021-11-10 10:43:16 +05:30
|
|
|
ctx.Data["MatrixHook"] = webhook_service.GetMatrixHook(w)
|
2023-01-01 20:53:15 +05:30
|
|
|
case webhook_module.PACKAGIST:
|
2022-01-23 19:16:30 +05:30
|
|
|
ctx.Data["PackagistHook"] = webhook_service.GetPackagistHook(w)
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2023-10-14 14:07:24 +05:30
|
|
|
ctx.Data["History"], err = w.History(ctx, 1)
|
2015-12-05 23:54:13 +05:30
|
|
|
if err != nil {
|
2018-01-11 03:04:17 +05:30
|
|
|
ctx.ServerError("History", err)
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
return orCtx, w
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// WebHooksEdit render editing web hook page
|
2016-03-11 22:26:52 +05:30
|
|
|
func WebHooksEdit(ctx *context.Context) {
|
2015-12-05 23:54:13 +05:30
|
|
|
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 21:00:52 +05:30
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// TestWebhook test if web hook is work fine
|
2016-03-11 22:26:52 +05:30
|
|
|
func TestWebhook(ctx *context.Context) {
|
2017-08-29 20:25:24 +05:30
|
|
|
hookID := ctx.ParamsInt64(":id")
|
2023-10-14 14:07:24 +05:30
|
|
|
w, err := webhook.GetWebhookByRepoID(ctx, ctx.Repo.Repository.ID, hookID)
|
2017-08-29 20:25:24 +05:30
|
|
|
if err != nil {
|
2022-10-21 21:51:56 +05:30
|
|
|
ctx.Flash.Error("GetWebhookByRepoID: " + err.Error())
|
2022-03-23 10:24:07 +05:30
|
|
|
ctx.Status(http.StatusInternalServerError)
|
2017-08-29 20:25:24 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-15 18:23:47 +05:30
|
|
|
// Grab latest commit or fake one if it's empty repository.
|
|
|
|
commit := ctx.Repo.Commit
|
|
|
|
if commit == nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
ghost := user_model.NewGhostUser()
|
2024-03-11 03:00:36 +05:30
|
|
|
objectFormat := git.ObjectFormatFromName(ctx.Repo.Repository.ObjectFormatName)
|
2016-08-15 18:23:47 +05:30
|
|
|
commit = &git.Commit{
|
2023-12-17 17:26:08 +05:30
|
|
|
ID: objectFormat.EmptyObjectID(),
|
2016-08-15 18:23:47 +05:30
|
|
|
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 19:07:34 +05:30
|
|
|
apiUser := convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone)
|
2021-06-29 19:04:03 +05:30
|
|
|
|
|
|
|
apiCommit := &api.PayloadCommit{
|
|
|
|
ID: commit.ID.String(),
|
|
|
|
Message: commit.Message(),
|
2021-11-16 23:48:25 +05:30
|
|
|
URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()),
|
2021-06-29 19:04:03 +05:30
|
|
|
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 23:54:13 +05:30
|
|
|
},
|
2021-06-29 19:04:03 +05:30
|
|
|
}
|
|
|
|
|
2022-09-04 14:48:07 +05:30
|
|
|
commitID := commit.ID.String()
|
2021-06-29 19:04:03 +05:30
|
|
|
p := &api.PushPayload{
|
2022-10-16 21:52:34 +05:30
|
|
|
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 18:38:08 +05:30
|
|
|
Repo: convert.ToRepo(ctx, ctx.Repo.Repository, access_model.Permission{AccessMode: perm.AccessModeNone}),
|
2022-10-16 21:52:34 +05:30
|
|
|
Pusher: apiUser,
|
|
|
|
Sender: apiUser,
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
if err := webhook_service.PrepareWebhook(ctx, w, webhook_module.HookEventPush, p); err != nil {
|
2017-08-29 20:25:24 +05:30
|
|
|
ctx.Flash.Error("PrepareWebhook: " + err.Error())
|
2022-03-23 10:24:07 +05:30
|
|
|
ctx.Status(http.StatusInternalServerError)
|
2015-12-05 23:54:13 +05:30
|
|
|
} else {
|
2022-01-06 02:30:20 +05:30
|
|
|
ctx.Flash.Info(ctx.Tr("repo.settings.webhook.delivery.success"))
|
2022-03-23 10:24:07 +05:30
|
|
|
ctx.Status(http.StatusOK)
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-06 02:30:20 +05:30
|
|
|
// ReplayWebhook replays a webhook
|
|
|
|
func ReplayWebhook(ctx *context.Context) {
|
|
|
|
hookTaskUUID := ctx.Params(":uuid")
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-21 21:51:56 +05:30
|
|
|
if err := webhook_service.ReplayHookTask(ctx, w, hookTaskUUID); err != nil {
|
2022-01-06 02:30:20 +05:30
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
// DeleteWebhook delete a webhook
|
2016-03-11 22:26:52 +05:30
|
|
|
func DeleteWebhook(ctx *context.Context) {
|
2023-10-14 14:07:24 +05:30
|
|
|
if err := webhook.DeleteWebhookByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil {
|
2016-07-17 06:03:59 +05:30
|
|
|
ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
|
2015-12-05 23:54:13 +05:30
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
|
|
|
|
}
|
|
|
|
|
2023-07-26 11:34:01 +05:30
|
|
|
ctx.JSONRedirect(ctx.Repo.RepoLink + "/settings/hooks")
|
2015-12-05 23:54:13 +05:30
|
|
|
}
|