mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-24 18:56:11 +01:00
1887c95254
At the moment a repository reference is needed for webhooks. With the upcoming package PR we need to send webhooks without a repository reference. For example a package is uploaded to an organization. In theory this enables the usage of webhooks for future user actions. This PR removes the repository id from `HookTask` and changes how the hooks are processed (see `services/webhook/deliver.go`). In a follow up PR I want to remove the usage of the `UniqueQueue´ and replace it with a normal queue because there is no reason to be unique. Co-authored-by: 6543 <6543@obermui.de>
725 lines
21 KiB
Go
725 lines
21 KiB
Go
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
// Copyright 2017 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 repo
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/models/perm"
|
|
user_model "code.gitea.io/gitea/models/user"
|
|
"code.gitea.io/gitea/models/webhook"
|
|
"code.gitea.io/gitea/modules/base"
|
|
"code.gitea.io/gitea/modules/context"
|
|
"code.gitea.io/gitea/modules/convert"
|
|
"code.gitea.io/gitea/modules/git"
|
|
"code.gitea.io/gitea/modules/json"
|
|
"code.gitea.io/gitea/modules/setting"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
"code.gitea.io/gitea/modules/util"
|
|
"code.gitea.io/gitea/modules/web"
|
|
"code.gitea.io/gitea/services/forms"
|
|
webhook_service "code.gitea.io/gitea/services/webhook"
|
|
)
|
|
|
|
const (
|
|
tplHooks base.TplName = "repo/settings/webhook/base"
|
|
tplHookNew base.TplName = "repo/settings/webhook/new"
|
|
tplOrgHookNew base.TplName = "org/settings/hook_new"
|
|
tplAdminHookNew base.TplName = "admin/hook_new"
|
|
)
|
|
|
|
// Webhooks render web hooks list page
|
|
func Webhooks(ctx *context.Context) {
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
|
|
ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks"
|
|
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
|
|
|
|
ws, err := webhook.ListWebhooksByOpts(ctx, &webhook.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
|
|
if err != nil {
|
|
ctx.ServerError("GetWebhooksByRepoID", err)
|
|
return
|
|
}
|
|
ctx.Data["Webhooks"] = ws
|
|
|
|
ctx.HTML(http.StatusOK, tplHooks)
|
|
}
|
|
|
|
type orgRepoCtx struct {
|
|
OrgID int64
|
|
RepoID int64
|
|
IsAdmin bool
|
|
IsSystemWebhook bool
|
|
Link string
|
|
LinkNew string
|
|
NewTemplate base.TplName
|
|
}
|
|
|
|
// getOrgRepoCtx determines whether this is a repo, organization, or admin (both default and system) context.
|
|
func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
|
|
if len(ctx.Repo.RepoLink) > 0 {
|
|
return &orgRepoCtx{
|
|
RepoID: ctx.Repo.Repository.ID,
|
|
Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
|
LinkNew: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
|
NewTemplate: tplHookNew,
|
|
}, nil
|
|
}
|
|
|
|
if len(ctx.Org.OrgLink) > 0 {
|
|
return &orgRepoCtx{
|
|
OrgID: ctx.Org.Organization.ID,
|
|
Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
|
LinkNew: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
|
NewTemplate: tplOrgHookNew,
|
|
}, nil
|
|
}
|
|
|
|
if ctx.Doer.IsAdmin {
|
|
// 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
|
|
}
|
|
|
|
return nil, errors.New("unable to set OrgRepo context")
|
|
}
|
|
|
|
func checkHookType(ctx *context.Context) string {
|
|
hookType := strings.ToLower(ctx.Params(":type"))
|
|
if !util.IsStringInSlice(hookType, setting.Webhook.Types, true) {
|
|
ctx.NotFound("checkHookType", nil)
|
|
return ""
|
|
}
|
|
return hookType
|
|
}
|
|
|
|
// WebhooksNew render creating webhook page
|
|
func WebhooksNew(ctx *context.Context) {
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
if err != nil {
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
return
|
|
}
|
|
|
|
if orCtx.IsAdmin && orCtx.IsSystemWebhook {
|
|
ctx.Data["PageIsAdminSystemHooks"] = true
|
|
ctx.Data["PageIsAdminSystemHooksNew"] = true
|
|
} else if orCtx.IsAdmin {
|
|
ctx.Data["PageIsAdminDefaultHooks"] = true
|
|
ctx.Data["PageIsAdminDefaultHooksNew"] = true
|
|
} else {
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
}
|
|
|
|
hookType := checkHookType(ctx)
|
|
ctx.Data["HookType"] = hookType
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
if hookType == "discord" {
|
|
ctx.Data["DiscordHook"] = map[string]interface{}{
|
|
"Username": "Gitea",
|
|
}
|
|
}
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
|
}
|
|
|
|
// ParseHookEvent convert web form content to webhook.HookEvent
|
|
func ParseHookEvent(form forms.WebhookForm) *webhook.HookEvent {
|
|
return &webhook.HookEvent{
|
|
PushOnly: form.PushOnly(),
|
|
SendEverything: form.SendEverything(),
|
|
ChooseEvents: form.ChooseEvents(),
|
|
HookEvents: webhook.HookEvents{
|
|
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,
|
|
Wiki: form.Wiki,
|
|
Repository: form.Repository,
|
|
Package: form.Package,
|
|
},
|
|
BranchFilter: form.BranchFilter,
|
|
}
|
|
}
|
|
|
|
type webhookParams struct {
|
|
// Type should be imported from webhook package (webhook.XXX)
|
|
Type string
|
|
|
|
URL string
|
|
ContentType webhook.HookContentType
|
|
Secret string
|
|
HTTPMethod string
|
|
WebhookForm forms.WebhookForm
|
|
Meta interface{}
|
|
}
|
|
|
|
func createWebhook(ctx *context.Context, params webhookParams) {
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
ctx.Data["HookType"] = params.Type
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
if err != nil {
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
return
|
|
}
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
|
|
|
if ctx.HasError() {
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
|
return
|
|
}
|
|
|
|
var meta []byte
|
|
if params.Meta != nil {
|
|
meta, err = json.Marshal(params.Meta)
|
|
if err != nil {
|
|
ctx.ServerError("Marshal", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
w := &webhook.Webhook{
|
|
RepoID: orCtx.RepoID,
|
|
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),
|
|
OrgID: orCtx.OrgID,
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
|
}
|
|
if err := w.UpdateEvent(); err != nil {
|
|
ctx.ServerError("UpdateEvent", err)
|
|
return
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
|
ctx.ServerError("CreateWebhook", err)
|
|
return
|
|
}
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
ctx.Redirect(orCtx.Link)
|
|
}
|
|
|
|
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() {
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
|
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)
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
ctx.ServerError("UpdateEvent", err)
|
|
return
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
|
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))
|
|
}
|
|
|
|
// GiteaHooksNewPost response for creating Gitea webhook
|
|
func GiteaHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewWebhookForm)
|
|
|
|
contentType := webhook.ContentTypeJSON
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
contentType = webhook.ContentTypeForm
|
|
}
|
|
|
|
return webhookParams{
|
|
Type: webhook.GITEA,
|
|
URL: form.PayloadURL,
|
|
ContentType: contentType,
|
|
Secret: form.Secret,
|
|
HTTPMethod: form.HTTPMethod,
|
|
WebhookForm: form.WebhookForm,
|
|
}
|
|
}
|
|
|
|
// GogsHooksNewPost response for creating Gogs webhook
|
|
func GogsHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewGogshookForm)
|
|
|
|
contentType := webhook.ContentTypeJSON
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
contentType = webhook.ContentTypeForm
|
|
}
|
|
|
|
return webhookParams{
|
|
Type: webhook.GOGS,
|
|
URL: form.PayloadURL,
|
|
ContentType: contentType,
|
|
Secret: form.Secret,
|
|
WebhookForm: form.WebhookForm,
|
|
}
|
|
}
|
|
|
|
// DiscordHooksNewPost response for creating Discord webhook
|
|
func DiscordHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewDiscordHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.DISCORD,
|
|
URL: form.PayloadURL,
|
|
ContentType: webhook.ContentTypeJSON,
|
|
WebhookForm: form.WebhookForm,
|
|
Meta: &webhook_service.DiscordMeta{
|
|
Username: form.Username,
|
|
IconURL: form.IconURL,
|
|
},
|
|
}
|
|
}
|
|
|
|
// DingtalkHooksNewPost response for creating Dingtalk webhook
|
|
func DingtalkHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewDingtalkHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.DINGTALK,
|
|
URL: form.PayloadURL,
|
|
ContentType: webhook.ContentTypeJSON,
|
|
WebhookForm: form.WebhookForm,
|
|
}
|
|
}
|
|
|
|
// TelegramHooksNewPost response for creating Telegram webhook
|
|
func TelegramHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewTelegramHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.TELEGRAM,
|
|
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID)),
|
|
ContentType: webhook.ContentTypeJSON,
|
|
WebhookForm: form.WebhookForm,
|
|
Meta: &webhook_service.TelegramMeta{
|
|
BotToken: form.BotToken,
|
|
ChatID: form.ChatID,
|
|
},
|
|
}
|
|
}
|
|
|
|
// MatrixHooksNewPost response for creating Matrix webhook
|
|
func MatrixHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewMatrixHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.MATRIX,
|
|
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,
|
|
AccessToken: form.AccessToken,
|
|
MessageType: form.MessageType,
|
|
},
|
|
}
|
|
}
|
|
|
|
// MSTeamsHooksNewPost response for creating MSTeams webhook
|
|
func MSTeamsHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.MSTEAMS,
|
|
URL: form.PayloadURL,
|
|
ContentType: webhook.ContentTypeJSON,
|
|
WebhookForm: form.WebhookForm,
|
|
}
|
|
}
|
|
|
|
// SlackHooksNewPost response for creating Slack webhook
|
|
func SlackHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewSlackHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.SLACK,
|
|
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,
|
|
},
|
|
}
|
|
}
|
|
|
|
// FeishuHooksNewPost response for creating Feishu webhook
|
|
func FeishuHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewFeishuHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.FEISHU,
|
|
URL: form.PayloadURL,
|
|
ContentType: webhook.ContentTypeJSON,
|
|
WebhookForm: form.WebhookForm,
|
|
}
|
|
}
|
|
|
|
// WechatworkHooksNewPost response for creating Wechatwork webhook
|
|
func WechatworkHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewWechatWorkHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.WECHATWORK,
|
|
URL: form.PayloadURL,
|
|
ContentType: webhook.ContentTypeJSON,
|
|
WebhookForm: form.WebhookForm,
|
|
}
|
|
}
|
|
|
|
// PackagistHooksNewPost response for creating Packagist webhook
|
|
func PackagistHooksNewPost(ctx *context.Context) {
|
|
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 {
|
|
form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
|
|
|
|
return webhookParams{
|
|
Type: webhook.PACKAGIST,
|
|
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,
|
|
},
|
|
}
|
|
}
|
|
|
|
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
if err != nil {
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
return nil, nil
|
|
}
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
|
|
|
var w *webhook.Webhook
|
|
if orCtx.RepoID > 0 {
|
|
w, err = webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
|
} else if orCtx.OrgID > 0 {
|
|
w, err = webhook.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
|
|
} else if orCtx.IsAdmin {
|
|
w, err = webhook.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id"))
|
|
}
|
|
if err != nil || w == nil {
|
|
if webhook.IsErrWebhookNotExist(err) {
|
|
ctx.NotFound("GetWebhookByID", nil)
|
|
} else {
|
|
ctx.ServerError("GetWebhookByID", err)
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
ctx.Data["HookType"] = w.Type
|
|
switch w.Type {
|
|
case webhook.SLACK:
|
|
ctx.Data["SlackHook"] = webhook_service.GetSlackHook(w)
|
|
case webhook.DISCORD:
|
|
ctx.Data["DiscordHook"] = webhook_service.GetDiscordHook(w)
|
|
case webhook.TELEGRAM:
|
|
ctx.Data["TelegramHook"] = webhook_service.GetTelegramHook(w)
|
|
case webhook.MATRIX:
|
|
ctx.Data["MatrixHook"] = webhook_service.GetMatrixHook(w)
|
|
case webhook.PACKAGIST:
|
|
ctx.Data["PackagistHook"] = webhook_service.GetPackagistHook(w)
|
|
}
|
|
|
|
ctx.Data["History"], err = w.History(1)
|
|
if err != nil {
|
|
ctx.ServerError("History", err)
|
|
}
|
|
return orCtx, w
|
|
}
|
|
|
|
// WebHooksEdit render editing web hook page
|
|
func WebHooksEdit(ctx *context.Context) {
|
|
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
|
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
|
}
|
|
|
|
// TestWebhook test if web hook is work fine
|
|
func TestWebhook(ctx *context.Context) {
|
|
hookID := ctx.ParamsInt64(":id")
|
|
w, err := webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
|
|
if err != nil {
|
|
ctx.Flash.Error("GetWebhookByRepoID: " + err.Error())
|
|
ctx.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Grab latest commit or fake one if it's empty repository.
|
|
commit := ctx.Repo.Commit
|
|
if commit == nil {
|
|
ghost := user_model.NewGhostUser()
|
|
commit = &git.Commit{
|
|
ID: git.MustIDFromString(git.EmptySHA),
|
|
Author: ghost.NewGitSig(),
|
|
Committer: ghost.NewGitSig(),
|
|
CommitMessage: "This is a fake commit",
|
|
}
|
|
}
|
|
|
|
apiUser := convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone)
|
|
|
|
apiCommit := &api.PayloadCommit{
|
|
ID: commit.ID.String(),
|
|
Message: commit.Message(),
|
|
URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()),
|
|
Author: &api.PayloadUser{
|
|
Name: commit.Author.Name,
|
|
Email: commit.Author.Email,
|
|
},
|
|
Committer: &api.PayloadUser{
|
|
Name: commit.Committer.Name,
|
|
Email: commit.Committer.Email,
|
|
},
|
|
}
|
|
|
|
commitID := commit.ID.String()
|
|
p := &api.PushPayload{
|
|
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,
|
|
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
|
|
Pusher: apiUser,
|
|
Sender: apiUser,
|
|
}
|
|
if err := webhook_service.PrepareWebhook(ctx, w, webhook.HookEventPush, p); err != nil {
|
|
ctx.Flash.Error("PrepareWebhook: " + err.Error())
|
|
ctx.Status(http.StatusInternalServerError)
|
|
} else {
|
|
ctx.Flash.Info(ctx.Tr("repo.settings.webhook.delivery.success"))
|
|
ctx.Status(http.StatusOK)
|
|
}
|
|
}
|
|
|
|
// ReplayWebhook replays a webhook
|
|
func ReplayWebhook(ctx *context.Context) {
|
|
hookTaskUUID := ctx.Params(":uuid")
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
if ctx.Written() {
|
|
return
|
|
}
|
|
|
|
if err := webhook_service.ReplayHookTask(ctx, w, hookTaskUUID); err != nil {
|
|
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))
|
|
}
|
|
|
|
// DeleteWebhook delete a webhook
|
|
func DeleteWebhook(ctx *context.Context) {
|
|
if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil {
|
|
ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
|
|
} else {
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
|
|
}
|
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
|
"redirect": ctx.Repo.RepoLink + "/settings/hooks",
|
|
})
|
|
}
|