2016-12-07 05:36:28 +01:00
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
2020-12-12 16:33:19 +01:00
|
|
|
"fmt"
|
2018-05-16 16:01:55 +02:00
|
|
|
"net/http"
|
2018-09-10 16:31:08 +02:00
|
|
|
"strings"
|
2018-05-16 16:01:55 +02:00
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2016-12-07 05:36:28 +01:00
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-11-10 05:41:51 +01:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2021-07-24 18:03:58 +02:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-05-11 12:21:34 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-12-25 10:59:32 +01:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-11-10 06:13:16 +01:00
|
|
|
webhook_service "code.gitea.io/gitea/services/webhook"
|
2016-12-07 05:36:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetOrgHook get an organization's webhook. If there is an error, write to
|
|
|
|
// `ctx` accordingly and return the error
|
2021-11-10 06:13:16 +01:00
|
|
|
func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, error) {
|
|
|
|
w, err := webhook.GetWebhookByOrgID(orgID, hookID)
|
2016-12-07 05:36:28 +01:00
|
|
|
if err != nil {
|
2021-11-10 06:13:16 +01:00
|
|
|
if webhook.IsErrWebhookNotExist(err) {
|
2019-03-19 03:29:43 +01:00
|
|
|
ctx.NotFound()
|
2016-12-07 05:36:28 +01:00
|
|
|
} else {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetWebhookByOrgID", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
|
|
|
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 06:13:16 +01:00
|
|
|
func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhook, error) {
|
|
|
|
w, err := webhook.GetWebhookByRepoID(repoID, hookID)
|
2016-12-07 05:36:28 +01:00
|
|
|
if err != nil {
|
2021-11-10 06:13:16 +01:00
|
|
|
if webhook.IsErrWebhookNotExist(err) {
|
2019-03-19 03:29:43 +01:00
|
|
|
ctx.NotFound()
|
2016-12-07 05:36:28 +01:00
|
|
|
} else {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "GetWebhookByID", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
|
|
|
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 06:13:16 +01:00
|
|
|
if !webhook_service.IsValidHookTaskType(form.Type) {
|
2020-12-12 16:33:19 +01:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Invalid hook type: %s", form.Type))
|
2016-12-07 05:36:28 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, name := range []string{"url", "content_type"} {
|
|
|
|
if _, ok := form.Config[name]; !ok {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: "+name)
|
2016-12-07 05:36:28 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 06:13:16 +01:00
|
|
|
if !webhook.IsValidHookContentType(form.Config["content_type"]) {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
|
2016-12-07 05:36:28 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
if ok {
|
2021-11-19 12:41:40 +01:00
|
|
|
ctx.JSON(http.StatusCreated, convert.ToHook(org.AsUser().HomeLink(), hook))
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
if ok {
|
2017-11-20 08:00:53 +01:00
|
|
|
ctx.JSON(http.StatusCreated, convert.ToHook(repo.RepoLink, hook))
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-06 06:10:48 +01:00
|
|
|
func issuesHook(events []string, event string) bool {
|
2021-11-10 06:13:16 +01:00
|
|
|
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(webhook.HookEventIssues), events, true)
|
2020-03-06 06:10:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func pullHook(events []string, event string) bool {
|
2021-11-10 06:13:16 +01:00
|
|
|
return util.IsStringInSlice(event, events, true) || util.IsStringInSlice(string(webhook.HookEventPullRequest), events, true)
|
2020-03-06 06:10:48 +01:00
|
|
|
}
|
|
|
|
|
2016-12-07 05:36:28 +01:00
|
|
|
// 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 06:13:16 +01:00
|
|
|
func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID int64) (*webhook.Webhook, bool) {
|
2016-12-07 05:36:28 +01:00
|
|
|
if len(form.Events) == 0 {
|
|
|
|
form.Events = []string{"push"}
|
|
|
|
}
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2016-12-07 05:36:28 +01:00
|
|
|
OrgID: orgID,
|
|
|
|
RepoID: repoID,
|
|
|
|
URL: form.Config["url"],
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
|
2016-12-07 05:36:28 +01:00
|
|
|
Secret: form.Config["secret"],
|
2019-05-15 14:01:53 +02:00
|
|
|
HTTPMethod: "POST",
|
2021-11-10 06:13:16 +01:00
|
|
|
HookEvent: &webhook.HookEvent{
|
2016-12-07 05:36:28 +01:00
|
|
|
ChooseEvents: true,
|
2021-11-10 06:13:16 +01:00
|
|
|
HookEvents: webhook.HookEvents{
|
|
|
|
Create: util.IsStringInSlice(string(webhook.HookEventCreate), form.Events, true),
|
|
|
|
Delete: util.IsStringInSlice(string(webhook.HookEventDelete), form.Events, true),
|
|
|
|
Fork: util.IsStringInSlice(string(webhook.HookEventFork), form.Events, true),
|
2020-03-06 06:10:48 +01:00
|
|
|
Issues: issuesHook(form.Events, "issues_only"),
|
2021-11-10 06:13:16 +01:00
|
|
|
IssueAssign: issuesHook(form.Events, string(webhook.HookEventIssueAssign)),
|
|
|
|
IssueLabel: issuesHook(form.Events, string(webhook.HookEventIssueLabel)),
|
|
|
|
IssueMilestone: issuesHook(form.Events, string(webhook.HookEventIssueMilestone)),
|
|
|
|
IssueComment: issuesHook(form.Events, string(webhook.HookEventIssueComment)),
|
|
|
|
Push: util.IsStringInSlice(string(webhook.HookEventPush), form.Events, true),
|
2020-03-06 06:10:48 +01:00
|
|
|
PullRequest: pullHook(form.Events, "pull_request_only"),
|
2021-11-10 06:13:16 +01:00
|
|
|
PullRequestAssign: pullHook(form.Events, string(webhook.HookEventPullRequestAssign)),
|
|
|
|
PullRequestLabel: pullHook(form.Events, string(webhook.HookEventPullRequestLabel)),
|
|
|
|
PullRequestMilestone: pullHook(form.Events, string(webhook.HookEventPullRequestMilestone)),
|
|
|
|
PullRequestComment: pullHook(form.Events, string(webhook.HookEventPullRequestComment)),
|
2020-03-06 06:10:48 +01:00
|
|
|
PullRequestReview: pullHook(form.Events, "pull_request_review"),
|
2021-11-10 06:13:16 +01:00
|
|
|
PullRequestSync: pullHook(form.Events, string(webhook.HookEventPullRequestSync)),
|
2022-09-04 21:54:23 +02:00
|
|
|
Wiki: util.IsStringInSlice(string(webhook.HookEventWiki), form.Events, true),
|
2021-11-10 06:13:16 +01:00
|
|
|
Repository: util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true),
|
|
|
|
Release: util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true),
|
2016-12-07 05:36:28 +01:00
|
|
|
},
|
2019-09-09 07:48:21 +02:00
|
|
|
BranchFilter: form.BranchFilter,
|
2016-12-07 05:36:28 +01:00
|
|
|
},
|
2020-12-09 18:20:13 +01:00
|
|
|
IsActive: form.Active,
|
2022-06-20 12:02:49 +02:00
|
|
|
Type: form.Type,
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
2021-11-10 06:13:16 +01:00
|
|
|
if w.Type == webhook.SLACK {
|
2016-12-07 05:36:28 +01:00
|
|
|
channel, ok := form.Config["channel"]
|
|
|
|
if !ok {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
|
2016-12-07 05:36:28 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
2022-08-11 17:48:23 +02:00
|
|
|
channel = strings.TrimSpace(channel)
|
2018-09-10 16:31:08 +02:00
|
|
|
|
2022-08-11 17:48:23 +02:00
|
|
|
if !webhook_service.IsValidSlackChannel(channel) {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusBadRequest, "", "Invalid slack channel name")
|
2018-09-10 16:31:08 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.SlackMeta{
|
2022-08-11 17:48:23 +02:00
|
|
|
Channel: channel,
|
2016-12-07 05:36:28 +01:00
|
|
|
Username: form.Config["username"],
|
|
|
|
IconURL: form.Config["icon_url"],
|
|
|
|
Color: form.Config["color"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
w.Meta = string(meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
return nil, false
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateWebhook", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return w, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2021-11-19 12:41:40 +01:00
|
|
|
ctx.JSON(http.StatusOK, convert.ToHook(org.AsUser().HomeLink(), updated))
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.JSON(http.StatusOK, convert.ToHook(repo.RepoLink, updated))
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 06:13:16 +01:00
|
|
|
func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webhook) bool {
|
2016-12-07 05:36:28 +01:00
|
|
|
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 06:13:16 +01:00
|
|
|
if !webhook.IsValidHookContentType(ct) {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
|
2016-12-07 05:36:28 +01:00
|
|
|
return false
|
|
|
|
}
|
2021-11-10 06:13:16 +01:00
|
|
|
w.ContentType = webhook.ToHookContentType(ct)
|
2016-12-07 05:36:28 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
if w.Type == webhook.SLACK {
|
2016-12-07 05:36:28 +01:00
|
|
|
if channel, ok := form.Config["channel"]; ok {
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.SlackMeta{
|
2016-12-07 05:36:28 +01:00
|
|
|
Channel: channel,
|
|
|
|
Username: form.Config["username"],
|
|
|
|
IconURL: form.Config["icon_url"],
|
|
|
|
Color: form.Config["color"],
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "slack: JSON marshal failed", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
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
|
2021-11-10 06:13:16 +01:00
|
|
|
w.Create = util.IsStringInSlice(string(webhook.HookEventCreate), form.Events, true)
|
|
|
|
w.Push = util.IsStringInSlice(string(webhook.HookEventPush), form.Events, true)
|
|
|
|
w.Create = util.IsStringInSlice(string(webhook.HookEventCreate), form.Events, true)
|
|
|
|
w.Delete = util.IsStringInSlice(string(webhook.HookEventDelete), form.Events, true)
|
|
|
|
w.Fork = util.IsStringInSlice(string(webhook.HookEventFork), form.Events, true)
|
|
|
|
w.Repository = util.IsStringInSlice(string(webhook.HookEventRepository), form.Events, true)
|
2022-09-04 21:54:23 +02:00
|
|
|
w.Wiki = util.IsStringInSlice(string(webhook.HookEventWiki), form.Events, true)
|
2021-11-10 06:13:16 +01:00
|
|
|
w.Release = util.IsStringInSlice(string(webhook.HookEventRelease), form.Events, true)
|
2019-09-09 07:48:21 +02:00
|
|
|
w.BranchFilter = form.BranchFilter
|
2018-05-16 16:01:55 +02:00
|
|
|
|
2022-03-29 13:55:00 +02:00
|
|
|
// Issues
|
|
|
|
w.Issues = issuesHook(form.Events, "issues_only")
|
|
|
|
w.IssueAssign = issuesHook(form.Events, string(webhook.HookEventIssueAssign))
|
|
|
|
w.IssueLabel = issuesHook(form.Events, string(webhook.HookEventIssueLabel))
|
|
|
|
w.IssueMilestone = issuesHook(form.Events, string(webhook.HookEventIssueMilestone))
|
|
|
|
w.IssueComment = issuesHook(form.Events, string(webhook.HookEventIssueComment))
|
|
|
|
|
|
|
|
// Pull requests
|
|
|
|
w.PullRequest = pullHook(form.Events, "pull_request_only")
|
|
|
|
w.PullRequestAssign = pullHook(form.Events, string(webhook.HookEventPullRequestAssign))
|
|
|
|
w.PullRequestLabel = pullHook(form.Events, string(webhook.HookEventPullRequestLabel))
|
|
|
|
w.PullRequestMilestone = pullHook(form.Events, string(webhook.HookEventPullRequestMilestone))
|
|
|
|
w.PullRequestComment = pullHook(form.Events, string(webhook.HookEventPullRequestComment))
|
|
|
|
w.PullRequestReview = pullHook(form.Events, "pull_request_review")
|
|
|
|
w.PullRequestSync = pullHook(form.Events, string(webhook.HookEventPullRequestSync))
|
|
|
|
|
2016-12-07 05:36:28 +01:00
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if form.Active != nil {
|
|
|
|
w.IsActive = *form.Active
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
if err := webhook.UpdateWebhook(w); err != nil {
|
2019-12-20 18:07:12 +01:00
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateWebhook", err)
|
2016-12-07 05:36:28 +01:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|