2016-12-07 10:06:28 +05:30
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2016-12-07 10:06:28 +05:30
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2020-12-12 21:03:19 +05:30
|
|
|
"fmt"
|
2018-05-16 19:31:55 +05:30
|
|
|
"net/http"
|
2018-09-10 20:01:08 +05:30
|
|
|
"strings"
|
2018-05-16 19:31:55 +05:30
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2016-12-07 10:06:28 +05:30
|
|
|
"code.gitea.io/gitea/modules/context"
|
2021-07-24 21:33:58 +05:30
|
|
|
"code.gitea.io/gitea/modules/json"
|
2023-01-28 23:42:10 +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"
|
2023-01-01 20:53:15 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2021-11-10 10:43:16 +05:30
|
|
|
webhook_service "code.gitea.io/gitea/services/webhook"
|
2016-12-07 10:06:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// GetOrgHook get an organization's webhook. If there is an error, write to
|
|
|
|
// `ctx` accordingly and return the error
|
2021-11-10 10:43:16 +05:30
|
|
|
func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, error) {
|
|
|
|
w, err := webhook.GetWebhookByOrgID(orgID, hookID)
|
2016-12-07 10:06:28 +05:30
|
|
|
if err != nil {
|
2021-11-10 10:43:16 +05:30
|
|
|
if webhook.IsErrWebhookNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2016-12-07 10:06:28 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetWebhookByOrgID", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoHook get a repo's webhook. If there is an error, write to `ctx`
|
|
|
|
// accordingly and return the error
|
2021-11-10 10:43:16 +05:30
|
|
|
func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhook, error) {
|
|
|
|
w, err := webhook.GetWebhookByRepoID(repoID, hookID)
|
2016-12-07 10:06:28 +05:30
|
|
|
if err != nil {
|
2021-11-10 10:43:16 +05:30
|
|
|
if webhook.IsErrWebhookNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
ctx.NotFound()
|
2016-12-07 10:06:28 +05:30
|
|
|
} else {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckCreateHookOption check if a CreateHookOption form is valid. If invalid,
|
|
|
|
// write the appropriate error to `ctx`. Return whether the form is valid
|
|
|
|
func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
|
2021-11-10 10:43:16 +05:30
|
|
|
if !webhook_service.IsValidHookTaskType(form.Type) {
|
2020-12-12 21:03:19 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Invalid hook type: %s", form.Type))
|
2016-12-07 10:06:28 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, name := range []string{"url", "content_type"} {
|
|
|
|
if _, ok := form.Config[name]; !ok {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: "+name)
|
2016-12-07 10:06:28 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 10:43:16 +05:30
|
|
|
if !webhook.IsValidHookContentType(form.Config["content_type"]) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
|
2016-12-07 10:06:28 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:42:10 +05:30
|
|
|
// AddSystemHook add a system hook
|
|
|
|
func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
|
|
|
hook, ok := addHook(ctx, form, 0, 0)
|
|
|
|
if ok {
|
|
|
|
h, err := webhook_service.ToHook(setting.AppSubURL+"/admin", hook)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusCreated, h)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 10:06:28 +05:30
|
|
|
// AddOrgHook add a hook to an organization. Writes to `ctx` accordingly
|
|
|
|
func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
|
|
|
org := ctx.Org.Organization
|
|
|
|
hook, ok := addHook(ctx, form, org.ID, 0)
|
2022-11-03 23:53:20 +05:30
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiHook, ok := toAPIHook(ctx, org.AsUser().HomeLink(), hook)
|
|
|
|
if !ok {
|
|
|
|
return
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
2022-11-03 23:53:20 +05:30
|
|
|
ctx.JSON(http.StatusCreated, apiHook)
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// AddRepoHook add a hook to a repo. Writes to `ctx` accordingly
|
|
|
|
func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
|
|
|
repo := ctx.Repo
|
|
|
|
hook, ok := addHook(ctx, form, 0, repo.Repository.ID)
|
2022-11-03 23:53:20 +05:30
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
apiHook, ok := toAPIHook(ctx, repo.RepoLink, hook)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusCreated, apiHook)
|
|
|
|
}
|
|
|
|
|
|
|
|
// toAPIHook converts the hook to its API representation.
|
|
|
|
// If there is an error, write to `ctx` accordingly. Return (hook, ok)
|
|
|
|
func toAPIHook(ctx *context.APIContext, repoLink string, hook *webhook.Webhook) (*api.Hook, bool) {
|
2023-01-01 20:53:15 +05:30
|
|
|
apiHook, err := webhook_service.ToHook(repoLink, hook)
|
2022-11-03 23:53:20 +05:30
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "ToHook", err)
|
|
|
|
return nil, false
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
2022-11-03 23:53:20 +05:30
|
|
|
return apiHook, true
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
|
|
|
|
2020-03-06 10:40:48 +05:30
|
|
|
func issuesHook(events []string, event string) bool {
|
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
|
|
|
return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventIssues), true)
|
2020-03-06 10:40:48 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func pullHook(events []string, event string) bool {
|
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
|
|
|
return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventPullRequest), true)
|
2020-03-06 10:40:48 +05:30
|
|
|
}
|
|
|
|
|
2016-12-07 10:06:28 +05:30
|
|
|
// addHook add the hook specified by `form`, `orgID` and `repoID`. If there is
|
|
|
|
// an error, write to `ctx` accordingly. Return (webhook, ok)
|
2021-11-10 10:43:16 +05:30
|
|
|
func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID int64) (*webhook.Webhook, bool) {
|
2016-12-07 10:06:28 +05:30
|
|
|
if len(form.Events) == 0 {
|
|
|
|
form.Events = []string{"push"}
|
|
|
|
}
|
2021-11-10 10:43:16 +05:30
|
|
|
w := &webhook.Webhook{
|
2016-12-07 10:06:28 +05:30
|
|
|
OrgID: orgID,
|
|
|
|
RepoID: repoID,
|
|
|
|
URL: form.Config["url"],
|
2021-11-10 10:43:16 +05:30
|
|
|
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
|
2016-12-07 10:06:28 +05:30
|
|
|
Secret: form.Config["secret"],
|
2019-05-15 17:31:53 +05:30
|
|
|
HTTPMethod: "POST",
|
2023-01-01 20:53:15 +05:30
|
|
|
HookEvent: &webhook_module.HookEvent{
|
2016-12-07 10:06:28 +05:30
|
|
|
ChooseEvents: true,
|
2023-01-01 20:53:15 +05:30
|
|
|
HookEvents: webhook_module.HookEvents{
|
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
|
|
|
Create: util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true),
|
|
|
|
Delete: util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true),
|
|
|
|
Fork: util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true),
|
2020-03-06 10:40:48 +05:30
|
|
|
Issues: issuesHook(form.Events, "issues_only"),
|
2023-01-06 17:19:14 +05:30
|
|
|
IssueAssign: issuesHook(form.Events, string(webhook_module.HookEventIssueAssign)),
|
|
|
|
IssueLabel: issuesHook(form.Events, string(webhook_module.HookEventIssueLabel)),
|
|
|
|
IssueMilestone: issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone)),
|
|
|
|
IssueComment: issuesHook(form.Events, string(webhook_module.HookEventIssueComment)),
|
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
|
|
|
Push: util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true),
|
2020-03-06 10:40:48 +05:30
|
|
|
PullRequest: pullHook(form.Events, "pull_request_only"),
|
2023-01-06 17:19:14 +05:30
|
|
|
PullRequestAssign: pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign)),
|
|
|
|
PullRequestLabel: pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel)),
|
|
|
|
PullRequestMilestone: pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone)),
|
|
|
|
PullRequestComment: pullHook(form.Events, string(webhook_module.HookEventPullRequestComment)),
|
2020-03-06 10:40:48 +05:30
|
|
|
PullRequestReview: pullHook(form.Events, "pull_request_review"),
|
2023-01-06 17:19:14 +05:30
|
|
|
PullRequestSync: pullHook(form.Events, string(webhook_module.HookEventPullRequestSync)),
|
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
|
|
|
Wiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true),
|
|
|
|
Repository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true),
|
|
|
|
Release: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true),
|
2016-12-07 10:06:28 +05:30
|
|
|
},
|
2019-09-09 11:18:21 +05:30
|
|
|
BranchFilter: form.BranchFilter,
|
2016-12-07 10:06:28 +05:30
|
|
|
},
|
2020-12-09 22:50:13 +05:30
|
|
|
IsActive: form.Active,
|
2022-06-20 15:32:49 +05:30
|
|
|
Type: form.Type,
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
2022-11-03 23:53:20 +05:30
|
|
|
err := w.SetHeaderAuthorization(form.AuthorizationHeader)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "SetHeaderAuthorization", err)
|
|
|
|
return nil, false
|
|
|
|
}
|
2023-01-01 20:53:15 +05:30
|
|
|
if w.Type == webhook_module.SLACK {
|
2016-12-07 10:06:28 +05:30
|
|
|
channel, ok := form.Config["channel"]
|
|
|
|
if !ok {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
|
2016-12-07 10:06:28 +05:30
|
|
|
return nil, false
|
|
|
|
}
|
2022-08-11 21:18:23 +05:30
|
|
|
channel = strings.TrimSpace(channel)
|
2018-09-10 20:01:08 +05:30
|
|
|
|
2022-08-11 21:18:23 +05:30
|
|
|
if !webhook_service.IsValidSlackChannel(channel) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusBadRequest, "", "Invalid slack channel name")
|
2018-09-10 20:01:08 +05:30
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
meta, err := json.Marshal(&webhook_service.SlackMeta{
|
2022-08-11 21:18:23 +05:30
|
|
|
Channel: channel,
|
2016-12-07 10:06:28 +05:30
|
|
|
Username: form.Config["username"],
|
|
|
|
IconURL: form.Config["icon_url"],
|
|
|
|
Color: form.Config["color"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
w.Meta = string(meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
return nil, false
|
2022-03-22 20:52:54 +05:30
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateWebhook", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return w, true
|
|
|
|
}
|
|
|
|
|
2023-01-28 23:42:10 +05:30
|
|
|
// EditSystemHook edit system webhook `w` according to `form`. Writes to `ctx` accordingly
|
|
|
|
func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
|
|
|
|
hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !editHook(ctx, form, hook) {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "editHook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updated, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h, err := webhook_service.ToHook(setting.AppURL+"/admin", updated)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, h)
|
|
|
|
}
|
|
|
|
|
2016-12-07 10:06:28 +05:30
|
|
|
// EditOrgHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
|
|
|
|
func EditOrgHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
|
|
|
|
org := ctx.Org.Organization
|
|
|
|
hook, err := GetOrgHook(ctx, org.ID, hookID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !editHook(ctx, form, hook) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updated, err := GetOrgHook(ctx, org.ID, hookID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-03 23:53:20 +05:30
|
|
|
apiHook, ok := toAPIHook(ctx, org.AsUser().HomeLink(), updated)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, apiHook)
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// EditRepoHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
|
|
|
|
func EditRepoHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
|
|
|
|
repo := ctx.Repo
|
|
|
|
hook, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !editHook(ctx, form, hook) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updated, err := GetRepoHook(ctx, repo.Repository.ID, hookID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-03 23:53:20 +05:30
|
|
|
apiHook, ok := toAPIHook(ctx, repo.RepoLink, updated)
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, apiHook)
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// editHook edit the webhook `w` according to `form`. If an error occurs, write
|
|
|
|
// to `ctx` accordingly and return the error. Return whether successful
|
2021-11-10 10:43:16 +05:30
|
|
|
func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webhook) bool {
|
2016-12-07 10:06:28 +05:30
|
|
|
if form.Config != nil {
|
|
|
|
if url, ok := form.Config["url"]; ok {
|
|
|
|
w.URL = url
|
|
|
|
}
|
|
|
|
if ct, ok := form.Config["content_type"]; ok {
|
2021-11-10 10:43:16 +05:30
|
|
|
if !webhook.IsValidHookContentType(ct) {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
|
2016-12-07 10:06:28 +05:30
|
|
|
return false
|
|
|
|
}
|
2021-11-10 10:43:16 +05:30
|
|
|
w.ContentType = webhook.ToHookContentType(ct)
|
2016-12-07 10:06:28 +05:30
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
if w.Type == webhook_module.SLACK {
|
2016-12-07 10:06:28 +05:30
|
|
|
if channel, ok := form.Config["channel"]; ok {
|
2021-11-10 10:43:16 +05:30
|
|
|
meta, err := json.Marshal(&webhook_service.SlackMeta{
|
2016-12-07 10:06:28 +05:30
|
|
|
Channel: channel,
|
|
|
|
Username: form.Config["username"],
|
|
|
|
IconURL: form.Config["icon_url"],
|
|
|
|
Color: form.Config["color"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
w.Meta = string(meta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update events
|
|
|
|
if len(form.Events) == 0 {
|
|
|
|
form.Events = []string{"push"}
|
|
|
|
}
|
|
|
|
w.PushOnly = false
|
|
|
|
w.SendEverything = false
|
|
|
|
w.ChooseEvents = true
|
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
|
|
|
w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true)
|
|
|
|
w.Push = util.SliceContainsString(form.Events, string(webhook_module.HookEventPush), true)
|
|
|
|
w.Create = util.SliceContainsString(form.Events, string(webhook_module.HookEventCreate), true)
|
|
|
|
w.Delete = util.SliceContainsString(form.Events, string(webhook_module.HookEventDelete), true)
|
|
|
|
w.Fork = util.SliceContainsString(form.Events, string(webhook_module.HookEventFork), true)
|
|
|
|
w.Repository = util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true)
|
|
|
|
w.Wiki = util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true)
|
|
|
|
w.Release = util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true)
|
2019-09-09 11:18:21 +05:30
|
|
|
w.BranchFilter = form.BranchFilter
|
2018-05-16 19:31:55 +05:30
|
|
|
|
2022-11-03 23:53:20 +05:30
|
|
|
err := w.SetHeaderAuthorization(form.AuthorizationHeader)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Error(http.StatusInternalServerError, "SetHeaderAuthorization", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:25:00 +05:30
|
|
|
// Issues
|
|
|
|
w.Issues = issuesHook(form.Events, "issues_only")
|
2023-01-06 17:19:14 +05:30
|
|
|
w.IssueAssign = issuesHook(form.Events, string(webhook_module.HookEventIssueAssign))
|
|
|
|
w.IssueLabel = issuesHook(form.Events, string(webhook_module.HookEventIssueLabel))
|
|
|
|
w.IssueMilestone = issuesHook(form.Events, string(webhook_module.HookEventIssueMilestone))
|
|
|
|
w.IssueComment = issuesHook(form.Events, string(webhook_module.HookEventIssueComment))
|
2022-03-29 17:25:00 +05:30
|
|
|
|
|
|
|
// Pull requests
|
|
|
|
w.PullRequest = pullHook(form.Events, "pull_request_only")
|
2023-01-06 17:19:14 +05:30
|
|
|
w.PullRequestAssign = pullHook(form.Events, string(webhook_module.HookEventPullRequestAssign))
|
|
|
|
w.PullRequestLabel = pullHook(form.Events, string(webhook_module.HookEventPullRequestLabel))
|
|
|
|
w.PullRequestMilestone = pullHook(form.Events, string(webhook_module.HookEventPullRequestMilestone))
|
|
|
|
w.PullRequestComment = pullHook(form.Events, string(webhook_module.HookEventPullRequestComment))
|
2022-03-29 17:25:00 +05:30
|
|
|
w.PullRequestReview = pullHook(form.Events, "pull_request_review")
|
2023-01-06 17:19:14 +05:30
|
|
|
w.PullRequestSync = pullHook(form.Events, string(webhook_module.HookEventPullRequestSync))
|
2022-03-29 17:25:00 +05:30
|
|
|
|
2016-12-07 10:06:28 +05:30
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Active != nil {
|
|
|
|
w.IsActive = *form.Active
|
|
|
|
}
|
|
|
|
|
2021-11-10 10:43:16 +05:30
|
|
|
if err := webhook.UpdateWebhook(w); err != nil {
|
2019-12-20 22:37:12 +05:30
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err)
|
2016-12-07 10:06:28 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|