mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
559 lines
14 KiB
559 lines
14 KiB
9 years ago
|
// Copyright 2015 The Gogs 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 (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/Unknwon/com"
|
||
|
|
||
9 years ago
|
git "github.com/gogits/git-module"
|
||
9 years ago
|
api "github.com/gogits/go-gogs-client"
|
||
|
|
||
|
"github.com/gogits/gogs/models"
|
||
8 years ago
|
"github.com/gogits/gogs/models/errors"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/form"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
9 years ago
|
)
|
||
|
|
||
|
const (
|
||
8 years ago
|
WEBHOOKS = "repo/settings/webhook/base"
|
||
|
WEBHOOK_NEW = "repo/settings/webhook/new"
|
||
|
ORG_WEBHOOK_NEW = "org/settings/webhook_new"
|
||
9 years ago
|
)
|
||
|
|
||
8 years ago
|
func Webhooks(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings.hooks")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["BaseLink"] = c.Repo.RepoLink
|
||
|
c.Data["Description"] = c.Tr("repo.settings.hooks_desc", "https://github.com/gogits/go-gogs-client/wiki/Repositories-Webhooks")
|
||
|
c.Data["Types"] = setting.Webhook.Types
|
||
|
|
||
|
ws, err := models.GetWebhooksByRepoID(c.Repo.Repository.ID)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "GetWebhooksByRepoID", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Webhooks"] = ws
|
||
9 years ago
|
|
||
8 years ago
|
c.HTML(200, WEBHOOKS)
|
||
9 years ago
|
}
|
||
|
|
||
|
type OrgRepoCtx struct {
|
||
|
OrgID int64
|
||
|
RepoID int64
|
||
|
Link string
|
||
8 years ago
|
NewTemplate string
|
||
9 years ago
|
}
|
||
|
|
||
|
// getOrgRepoCtx determines whether this is a repo context or organization context.
|
||
8 years ago
|
func getOrgRepoCtx(c *context.Context) (*OrgRepoCtx, error) {
|
||
|
if len(c.Repo.RepoLink) > 0 {
|
||
|
c.Data["PageIsRepositoryContext"] = true
|
||
9 years ago
|
return &OrgRepoCtx{
|
||
8 years ago
|
RepoID: c.Repo.Repository.ID,
|
||
|
Link: c.Repo.RepoLink,
|
||
8 years ago
|
NewTemplate: WEBHOOK_NEW,
|
||
9 years ago
|
}, nil
|
||
|
}
|
||
|
|
||
8 years ago
|
if len(c.Org.OrgLink) > 0 {
|
||
|
c.Data["PageIsOrganizationContext"] = true
|
||
9 years ago
|
return &OrgRepoCtx{
|
||
8 years ago
|
OrgID: c.Org.Organization.ID,
|
||
|
Link: c.Org.OrgLink,
|
||
8 years ago
|
NewTemplate: ORG_WEBHOOK_NEW,
|
||
9 years ago
|
}, nil
|
||
|
}
|
||
|
|
||
|
return nil, errors.New("Unable to set OrgRepo context")
|
||
|
}
|
||
|
|
||
8 years ago
|
func checkHookType(c *context.Context) string {
|
||
|
hookType := strings.ToLower(c.Params(":type"))
|
||
9 years ago
|
if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
|
||
8 years ago
|
c.Handle(404, "checkHookType", nil)
|
||
9 years ago
|
return ""
|
||
|
}
|
||
|
return hookType
|
||
|
}
|
||
|
|
||
8 years ago
|
func WebhooksNew(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings.add_webhook")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksNew"] = true
|
||
|
c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
||
9 years ago
|
|
||
8 years ago
|
orCtx, err := getOrgRepoCtx(c)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "getOrgRepoCtx", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Data["HookType"] = checkHookType(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["BaseLink"] = orCtx.Link
|
||
9 years ago
|
|
||
8 years ago
|
c.HTML(200, orCtx.NewTemplate)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func ParseHookEvent(f form.Webhook) *models.HookEvent {
|
||
9 years ago
|
return &models.HookEvent{
|
||
8 years ago
|
PushOnly: f.PushOnly(),
|
||
|
SendEverything: f.SendEverything(),
|
||
|
ChooseEvents: f.ChooseEvents(),
|
||
9 years ago
|
HookEvents: models.HookEvents{
|
||
8 years ago
|
Create: f.Create,
|
||
|
Delete: f.Delete,
|
||
|
Fork: f.Fork,
|
||
|
Push: f.Push,
|
||
|
Issues: f.Issues,
|
||
|
IssueComment: f.IssueComment,
|
||
|
PullRequest: f.PullRequest,
|
||
8 years ago
|
Release: f.Release,
|
||
9 years ago
|
},
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func WebHooksNewPost(c *context.Context, f form.NewWebhook) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings.add_webhook")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksNew"] = true
|
||
|
c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
||
|
c.Data["HookType"] = "gogs"
|
||
9 years ago
|
|
||
8 years ago
|
orCtx, err := getOrgRepoCtx(c)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "getOrgRepoCtx", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["BaseLink"] = orCtx.Link
|
||
9 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, orCtx.NewTemplate)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
contentType := models.JSON
|
||
8 years ago
|
if models.HookContentType(f.ContentType) == models.FORM {
|
||
9 years ago
|
contentType = models.FORM
|
||
|
}
|
||
|
|
||
|
w := &models.Webhook{
|
||
|
RepoID: orCtx.RepoID,
|
||
8 years ago
|
URL: f.PayloadURL,
|
||
9 years ago
|
ContentType: contentType,
|
||
8 years ago
|
Secret: f.Secret,
|
||
|
HookEvent: ParseHookEvent(f.Webhook),
|
||
|
IsActive: f.Active,
|
||
9 years ago
|
HookTaskType: models.GOGS,
|
||
|
OrgID: orCtx.OrgID,
|
||
|
}
|
||
|
if err := w.UpdateEvent(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateEvent", err)
|
||
9 years ago
|
return
|
||
|
} else if err := models.CreateWebhook(w); err != nil {
|
||
8 years ago
|
c.Handle(500, "CreateWebhook", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
|
||
|
c.Redirect(orCtx.Link + "/settings/hooks")
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func SlackHooksNewPost(c *context.Context, f form.NewSlackHook) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksNew"] = true
|
||
|
c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
||
9 years ago
|
|
||
8 years ago
|
orCtx, err := getOrgRepoCtx(c)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "getOrgRepoCtx", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, orCtx.NewTemplate)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
meta, err := json.Marshal(&models.SlackMeta{
|
||
8 years ago
|
Channel: f.Channel,
|
||
|
Username: f.Username,
|
||
|
IconURL: f.IconURL,
|
||
|
Color: f.Color,
|
||
9 years ago
|
})
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "Marshal", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
w := &models.Webhook{
|
||
|
RepoID: orCtx.RepoID,
|
||
8 years ago
|
URL: f.PayloadURL,
|
||
9 years ago
|
ContentType: models.JSON,
|
||
8 years ago
|
HookEvent: ParseHookEvent(f.Webhook),
|
||
|
IsActive: f.Active,
|
||
9 years ago
|
HookTaskType: models.SLACK,
|
||
|
Meta: string(meta),
|
||
|
OrgID: orCtx.OrgID,
|
||
|
}
|
||
|
if err := w.UpdateEvent(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateEvent", err)
|
||
9 years ago
|
return
|
||
|
} else if err := models.CreateWebhook(w); err != nil {
|
||
8 years ago
|
c.Handle(500, "CreateWebhook", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
|
||
|
c.Redirect(orCtx.Link + "/settings/hooks")
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
// FIXME: merge logic to Slack
|
||
8 years ago
|
func DiscordHooksNewPost(c *context.Context, f form.NewDiscordHook) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksNew"] = true
|
||
|
c.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
||
8 years ago
|
|
||
8 years ago
|
orCtx, err := getOrgRepoCtx(c)
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "getOrgRepoCtx", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, orCtx.NewTemplate)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
meta, err := json.Marshal(&models.SlackMeta{
|
||
8 years ago
|
Username: f.Username,
|
||
|
IconURL: f.IconURL,
|
||
|
Color: f.Color,
|
||
8 years ago
|
})
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "Marshal", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
w := &models.Webhook{
|
||
|
RepoID: orCtx.RepoID,
|
||
8 years ago
|
URL: f.PayloadURL,
|
||
8 years ago
|
ContentType: models.JSON,
|
||
8 years ago
|
HookEvent: ParseHookEvent(f.Webhook),
|
||
|
IsActive: f.Active,
|
||
8 years ago
|
HookTaskType: models.DISCORD,
|
||
|
Meta: string(meta),
|
||
|
OrgID: orCtx.OrgID,
|
||
|
}
|
||
|
if err := w.UpdateEvent(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateEvent", err)
|
||
8 years ago
|
return
|
||
|
} else if err := models.CreateWebhook(w); err != nil {
|
||
8 years ago
|
c.Handle(500, "CreateWebhook", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.settings.add_hook_success"))
|
||
|
c.Redirect(orCtx.Link + "/settings/hooks")
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func checkWebhook(c *context.Context) (*OrgRepoCtx, *models.Webhook) {
|
||
|
c.Data["RequireHighlightJS"] = true
|
||
9 years ago
|
|
||
8 years ago
|
orCtx, err := getOrgRepoCtx(c)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "getOrgRepoCtx", err)
|
||
9 years ago
|
return nil, nil
|
||
|
}
|
||
8 years ago
|
c.Data["BaseLink"] = orCtx.Link
|
||
9 years ago
|
|
||
9 years ago
|
var w *models.Webhook
|
||
|
if orCtx.RepoID > 0 {
|
||
8 years ago
|
w, err = models.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
||
9 years ago
|
} else {
|
||
8 years ago
|
w, err = models.GetWebhookByOrgID(c.Org.Organization.ID, c.ParamsInt64(":id"))
|
||
9 years ago
|
}
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetWebhookOfRepoByID/GetWebhookByOrgID", errors.IsWebhookNotExist, err)
|
||
9 years ago
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
switch w.HookTaskType {
|
||
|
case models.SLACK:
|
||
8 years ago
|
c.Data["SlackHook"] = w.GetSlackHook()
|
||
|
c.Data["HookType"] = "slack"
|
||
8 years ago
|
case models.DISCORD:
|
||
8 years ago
|
c.Data["SlackHook"] = w.GetSlackHook()
|
||
|
c.Data["HookType"] = "discord"
|
||
9 years ago
|
default:
|
||
8 years ago
|
c.Data["HookType"] = "gogs"
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.Data["History"], err = w.History(1)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "History", err)
|
||
9 years ago
|
}
|
||
|
return orCtx, w
|
||
|
}
|
||
|
|
||
8 years ago
|
func WebHooksEdit(c *context.Context) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings.update_webhook")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksEdit"] = true
|
||
9 years ago
|
|
||
8 years ago
|
orCtx, w := checkWebhook(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Webhook"] = w
|
||
9 years ago
|
|
||
8 years ago
|
c.HTML(200, orCtx.NewTemplate)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func WebHooksEditPost(c *context.Context, f form.NewWebhook) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings.update_webhook")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksEdit"] = true
|
||
9 years ago
|
|
||
8 years ago
|
orCtx, w := checkWebhook(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Webhook"] = w
|
||
9 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, orCtx.NewTemplate)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
contentType := models.JSON
|
||
8 years ago
|
if models.HookContentType(f.ContentType) == models.FORM {
|
||
9 years ago
|
contentType = models.FORM
|
||
|
}
|
||
|
|
||
8 years ago
|
w.URL = f.PayloadURL
|
||
9 years ago
|
w.ContentType = contentType
|
||
8 years ago
|
w.Secret = f.Secret
|
||
|
w.HookEvent = ParseHookEvent(f.Webhook)
|
||
|
w.IsActive = f.Active
|
||
9 years ago
|
if err := w.UpdateEvent(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateEvent", err)
|
||
9 years ago
|
return
|
||
|
} else if err := models.UpdateWebhook(w); err != nil {
|
||
8 years ago
|
c.Handle(500, "WebHooksEditPost", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
|
||
|
c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func SlackHooksEditPost(c *context.Context, f form.NewSlackHook) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksEdit"] = true
|
||
9 years ago
|
|
||
8 years ago
|
orCtx, w := checkWebhook(c)
|
||
|
if c.Written() {
|
||
9 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Webhook"] = w
|
||
9 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, orCtx.NewTemplate)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
|
meta, err := json.Marshal(&models.SlackMeta{
|
||
8 years ago
|
Channel: f.Channel,
|
||
|
Username: f.Username,
|
||
|
IconURL: f.IconURL,
|
||
|
Color: f.Color,
|
||
9 years ago
|
})
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "Marshal", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
w.URL = f.PayloadURL
|
||
8 years ago
|
w.Meta = string(meta)
|
||
8 years ago
|
w.HookEvent = ParseHookEvent(f.Webhook)
|
||
|
w.IsActive = f.Active
|
||
8 years ago
|
if err := w.UpdateEvent(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateEvent", err)
|
||
8 years ago
|
return
|
||
|
} else if err := models.UpdateWebhook(w); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateWebhook", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
|
||
|
c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
|
||
8 years ago
|
}
|
||
|
|
||
|
// FIXME: merge logic to Slack
|
||
8 years ago
|
func DiscordHooksEditPost(c *context.Context, f form.NewDiscordHook) {
|
||
|
c.Data["Title"] = c.Tr("repo.settings")
|
||
|
c.Data["PageIsSettingsHooks"] = true
|
||
|
c.Data["PageIsSettingsHooksEdit"] = true
|
||
8 years ago
|
|
||
8 years ago
|
orCtx, w := checkWebhook(c)
|
||
|
if c.Written() {
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
c.Data["Webhook"] = w
|
||
8 years ago
|
|
||
8 years ago
|
if c.HasError() {
|
||
|
c.HTML(200, orCtx.NewTemplate)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
meta, err := json.Marshal(&models.SlackMeta{
|
||
8 years ago
|
Username: f.Username,
|
||
|
IconURL: f.IconURL,
|
||
|
Color: f.Color,
|
||
8 years ago
|
})
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "Marshal", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
w.URL = f.PayloadURL
|
||
9 years ago
|
w.Meta = string(meta)
|
||
8 years ago
|
w.HookEvent = ParseHookEvent(f.Webhook)
|
||
|
w.IsActive = f.Active
|
||
9 years ago
|
if err := w.UpdateEvent(); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateEvent", err)
|
||
9 years ago
|
return
|
||
|
} else if err := models.UpdateWebhook(w); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateWebhook", err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.settings.update_hook_success"))
|
||
|
c.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func TestWebhook(c *context.Context) {
|
||
8 years ago
|
var authorUsername, committerUsername string
|
||
|
|
||
8 years ago
|
// Grab latest commit or fake one if it's empty repository.
|
||
8 years ago
|
commit := c.Repo.Commit
|
||
8 years ago
|
if commit == nil {
|
||
|
ghost := models.NewGhostUser()
|
||
|
commit = &git.Commit{
|
||
|
ID: git.MustIDFromString(git.EMPTY_SHA),
|
||
|
Author: ghost.NewGitSig(),
|
||
|
Committer: ghost.NewGitSig(),
|
||
|
CommitMessage: "This is a fake commit",
|
||
|
}
|
||
8 years ago
|
authorUsername = ghost.Name
|
||
|
committerUsername = ghost.Name
|
||
|
} else {
|
||
|
// Try to match email with a real user.
|
||
|
author, err := models.GetUserByEmail(commit.Author.Email)
|
||
|
if err == nil {
|
||
|
authorUsername = author.Name
|
||
8 years ago
|
} else if !errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Handle(500, "GetUserByEmail.(author)", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
committer, err := models.GetUserByEmail(commit.Committer.Email)
|
||
|
if err == nil {
|
||
|
committerUsername = committer.Name
|
||
8 years ago
|
} else if !errors.IsUserNotExist(err) {
|
||
8 years ago
|
c.Handle(500, "GetUserByEmail.(committer)", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
fileStatus, err := commit.FileStatus()
|
||
|
if err != nil {
|
||
8 years ago
|
c.Handle(500, "FileStatus", err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
apiUser := c.User.APIFormat()
|
||
9 years ago
|
p := &api.PushPayload{
|
||
8 years ago
|
Ref: git.BRANCH_PREFIX + c.Repo.Repository.DefaultBranch,
|
||
8 years ago
|
Before: commit.ID.String(),
|
||
|
After: commit.ID.String(),
|
||
9 years ago
|
Commits: []*api.PayloadCommit{
|
||
|
{
|
||
8 years ago
|
ID: commit.ID.String(),
|
||
|
Message: commit.Message(),
|
||
8 years ago
|
URL: c.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
|
||
8 years ago
|
Author: &api.PayloadUser{
|
||
8 years ago
|
Name: commit.Author.Name,
|
||
|
Email: commit.Author.Email,
|
||
|
UserName: authorUsername,
|
||
9 years ago
|
},
|
||
8 years ago
|
Committer: &api.PayloadUser{
|
||
8 years ago
|
Name: commit.Committer.Name,
|
||
|
Email: commit.Committer.Email,
|
||
|
UserName: committerUsername,
|
||
9 years ago
|
},
|
||
8 years ago
|
Added: fileStatus.Added,
|
||
|
Removed: fileStatus.Removed,
|
||
|
Modified: fileStatus.Modified,
|
||
9 years ago
|
},
|
||
|
},
|
||
8 years ago
|
Repo: c.Repo.Repository.APIFormat(nil),
|
||
8 years ago
|
Pusher: apiUser,
|
||
|
Sender: apiUser,
|
||
9 years ago
|
}
|
||
8 years ago
|
if err := models.TestWebhook(c.Repo.Repository, models.HOOK_EVENT_PUSH, p, c.ParamsInt64("id")); err != nil {
|
||
|
c.Handle(500, "TestWebhook", err)
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Info(c.Tr("repo.settings.webhook.test_delivery_success"))
|
||
|
c.Status(200)
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func RedeliveryWebhook(c *context.Context) {
|
||
|
webhook, err := models.GetWebhookOfRepoByID(c.Repo.Repository.ID, c.ParamsInt64(":id"))
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetWebhookOfRepoByID/GetWebhookByOrgID", errors.IsWebhookNotExist, err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
hookTask, err := models.GetHookTaskOfWebhookByUUID(webhook.ID, c.Query("uuid"))
|
||
8 years ago
|
if err != nil {
|
||
8 years ago
|
c.NotFoundOrServerError("GetHookTaskOfWebhookByUUID/GetWebhookByOrgID", errors.IsHookTaskNotExist, err)
|
||
8 years ago
|
return
|
||
|
}
|
||
|
|
||
|
hookTask.IsDelivered = false
|
||
|
if err = models.UpdateHookTask(hookTask); err != nil {
|
||
8 years ago
|
c.Handle(500, "UpdateHookTask", err)
|
||
8 years ago
|
} else {
|
||
8 years ago
|
go models.HookQueue.Add(c.Repo.Repository.ID)
|
||
|
c.Flash.Info(c.Tr("repo.settings.webhook.redelivery_success", hookTask.UUID))
|
||
|
c.Status(200)
|
||
8 years ago
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
func DeleteWebhook(c *context.Context) {
|
||
|
if err := models.DeleteWebhookOfRepoByID(c.Repo.Repository.ID, c.QueryInt64("id")); err != nil {
|
||
|
c.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
|
||
9 years ago
|
} else {
|
||
8 years ago
|
c.Flash.Success(c.Tr("repo.settings.webhook_deletion_success"))
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
c.JSON(200, map[string]interface{}{
|
||
|
"redirect": c.Repo.RepoLink + "/settings/hooks",
|
||
9 years ago
|
})
|
||
|
}
|