Merge pull request #442 from compressed/org_hook

Organization-level Webhooks
This commit is contained in:
无闻 2014-09-07 19:30:51 -04:00
commit e79e4b1580
12 changed files with 243 additions and 34 deletions

View File

@ -261,6 +261,13 @@ func runWeb(*cli.Context) {
m.Group("/settings", func(r *macaron.Router) {
r.Get("", org.Settings)
r.Post("", bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost)
r.Get("/hooks", org.SettingsHooks)
r.Get("/hooks/new", repo.WebHooksNew)
r.Post("/hooks/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
r.Post("/hooks/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
r.Get("/hooks/:id", repo.WebHooksEdit)
r.Post("/hooks/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
r.Post("/hooks/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
r.Route("/delete", "GET,POST", org.SettingsDelete)
})

View File

@ -207,7 +207,7 @@ unbind = Unbind
unbind_success = Social account has been unbound.
delete_account = Delete Your Account
delete_prompt = The operation will delete your account permanently, and <strong>CANNOT</strong> be undo!
delete_prompt = The operation will delete your account permanently, and <strong>CANNOT</strong> be undone!
confirm_delete_account = Confirm Deletion
[repo]
@ -316,8 +316,9 @@ settings.update_settings = Update Settings
settings.update_setting_success = Organization setting has been successfully updated.
settings.delete = Delete Organization
settings.delete_account = Delete This Organization
settings.delete_prompt = The operation will delete this organization permanently, and <strong>CANNOT</strong> be undo!
settings.delete_prompt = The operation will delete this organization permanently, and <strong>CANNOT</strong> be undone!
settings.confirm_delete_account = Confirm Deletion
settings.hooks_desc = Add webhooks that will be triggered for <strong>all repositories</strong> under this organization.
members.public = Public
members.public_helper = make private

View File

@ -220,8 +220,20 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
ws, err := GetActiveWebhooksByRepoId(repoId)
if err != nil {
return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error())
} else if len(ws) == 0 {
return errors.New("action.CommitRepoAction(GetActiveWebhooksByRepoId): " + err.Error())
}
// check if repo belongs to org and append additional webhooks
if repo.Owner.IsOrganization() {
// get hooks for org
orgws, err := GetActiveWebhooksByOrgId(repo.OwnerId)
if err != nil {
return errors.New("action.CommitRepoAction(GetActiveWebhooksByOrgId): " + err.Error())
}
ws = append(ws, orgws...)
}
if len(ws) == 0 {
return nil
}

View File

@ -45,6 +45,7 @@ type Webhook struct {
IsActive bool
HookTaskType HookTaskType
Meta string `xorm:"TEXT"` // store hook-specific attributes
OrgId int64
}
// GetEvent handles conversion from Events to HookEvent.
@ -120,6 +121,18 @@ func DeleteWebhook(hookId int64) error {
return err
}
// GetWebhooksByOrgId returns all webhooks for an organization.
func GetWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
err = x.Find(&ws, &Webhook{OrgId: orgId})
return ws, err
}
// GetActiveWebhooksByOrgId returns all active webhooks for an organization.
func GetActiveWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
err = x.Find(&ws, &Webhook{OrgId: orgId, IsActive: true})
return ws, err
}
// ___ ___ __ ___________ __
// / | \ ____ ____ | | _\__ ___/____ _____| | __
// / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /

View File

@ -349,17 +349,8 @@ function initRepo() {
})
}
function initRepoSetting() {
// Options.
// Confirmation of changing repository name.
$('#repo-setting-form').submit(function (e) {
var $reponame = $('#repo_name');
if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
e.preventDefault();
return true;
}
});
// when user changes hook type, hide/show proper divs
function initHookTypeChange() {
// web hook type change
$('select#hook-type').on("change", function () {
hookTypes = ['Gogs','Slack'];
@ -374,6 +365,20 @@ function initRepoSetting() {
}
});
});
}
function initRepoSetting() {
// Options.
// Confirmation of changing repository name.
$('#repo-setting-form').submit(function (e) {
var $reponame = $('#repo_name');
if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
e.preventDefault();
return true;
}
});
initHookTypeChange();
$('#transfer-button').click(function () {
$('#transfer-form').show();
@ -421,6 +426,8 @@ function initOrgSetting() {
return true;
}
});
initHookTypeChange();
}
function initInvite() {

View File

@ -5,6 +5,7 @@
package org
import (
"github.com/Unknwon/com"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/modules/auth"
"github.com/gogits/gogs/modules/base"
@ -15,6 +16,7 @@ import (
const (
SETTINGS_OPTIONS base.TplName = "org/settings/options"
SETTINGS_DELETE base.TplName = "org/settings/delete"
SETTINGS_HOOKS base.TplName = "org/settings/hooks"
)
func Settings(ctx *middleware.Context) {
@ -97,3 +99,29 @@ func SettingsDelete(ctx *middleware.Context) {
ctx.HTML(200, SETTINGS_DELETE)
}
func SettingsHooks(ctx *middleware.Context) {
ctx.Data["Title"] = ctx.Tr("org.settings")
ctx.Data["PageIsSettingsHooks"] = true
// Delete web hook.
remove := com.StrTo(ctx.Query("remove")).MustInt64()
if remove > 0 {
if err := models.DeleteWebhook(remove); err != nil {
ctx.Handle(500, "DeleteWebhook", err)
return
}
ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks")
return
}
ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id)
if err != nil {
ctx.Handle(500, "GetWebhooksByOrgId", err)
return
}
ctx.Data["Webhooks"] = ws
ctx.HTML(200, SETTINGS_HOOKS)
}

View File

@ -6,6 +6,7 @@ package repo
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
@ -26,6 +27,7 @@ const (
COLLABORATION base.TplName = "repo/settings/collaboration"
HOOKS base.TplName = "repo/settings/hooks"
HOOK_NEW base.TplName = "repo/settings/hook_new"
ORG_HOOK_NEW base.TplName = "org/settings/hook_new"
)
func Settings(ctx *middleware.Context) {
@ -284,7 +286,13 @@ func WebHooksNew(ctx *middleware.Context) {
ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
renderHookTypes(ctx)
ctx.HTML(200, HOOK_NEW)
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
ctx.Handle(500, "WebHooksNew(getOrgRepoCtx)", err)
return
}
ctx.HTML(200, orCtx.NewTemplate)
}
func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
@ -293,8 +301,14 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
ctx.Handle(500, "WebHooksNewPost(getOrgRepoCtx)", err)
return
}
if ctx.HasError() {
ctx.HTML(200, HOOK_NEW)
ctx.HTML(200, orCtx.NewTemplate)
return
}
@ -304,7 +318,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
}
w := &models.Webhook{
RepoId: ctx.Repo.Repository.Id,
RepoId: orCtx.RepoId,
Url: form.PayloadUrl,
ContentType: ct,
Secret: form.Secret,
@ -314,6 +328,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
IsActive: form.Active,
HookTaskType: models.GOGS,
Meta: "",
OrgId: orCtx.OrgId,
}
if err := w.UpdateEvent(); err != nil {
@ -325,7 +340,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
ctx.Redirect(orCtx.Link + "/settings/hooks")
}
func WebHooksEdit(ctx *middleware.Context) {
@ -354,16 +369,21 @@ func WebHooksEdit(ctx *middleware.Context) {
case models.SLACK:
{
ctx.Data["SlackHook"] = w.GetSlackHook()
ctx.Data["HookType"] = "slack"
ctx.Data["HookType"] = "Slack"
}
default:
{
ctx.Data["HookType"] = "gogs"
ctx.Data["HookType"] = "Gogs"
}
}
w.GetEvent()
ctx.Data["Webhook"] = w
ctx.HTML(200, HOOK_NEW)
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err)
return
}
ctx.HTML(200, orCtx.NewTemplate)
}
func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
@ -389,8 +409,13 @@ func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
w.GetEvent()
ctx.Data["Webhook"] = w
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
ctx.Handle(500, "WebHooksEditPost(getOrgRepoCtx)", err)
return
}
if ctx.HasError() {
ctx.HTML(200, HOOK_NEW)
ctx.HTML(200, orCtx.NewTemplate)
return
}
@ -415,7 +440,7 @@ func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId))
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
}
func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
@ -424,8 +449,14 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
ctx.Handle(500, "SlackHooksNewPost(getOrgRepoCtx)", err)
return
}
if ctx.HasError() {
ctx.HTML(200, HOOK_NEW)
ctx.HTML(200, orCtx.NewTemplate)
return
}
@ -440,7 +471,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
}
w := &models.Webhook{
RepoId: ctx.Repo.Repository.Id,
RepoId: orCtx.RepoId,
Url: models.GetSlackURL(form.Domain, form.Token),
ContentType: models.JSON,
Secret: "",
@ -450,6 +481,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
IsActive: form.Active,
HookTaskType: models.SLACK,
Meta: string(meta),
OrgId: orCtx.OrgId,
}
if err := w.UpdateEvent(); err != nil {
ctx.Handle(500, "UpdateEvent", err)
@ -460,7 +492,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks")
ctx.Redirect(orCtx.Link + "/settings/hooks")
}
func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
@ -469,9 +501,14 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
ctx.Data["PageIsSettingsHooksEdit"] = true
hookId := com.StrTo(ctx.Params(":id")).MustInt64()
fmt.Println("hookId slack=%d", hookId)
if hookId == 0 {
ctx.Handle(404, "setting.WebHooksEditPost", nil)
ctx.Handle(404, "SlackHooksEditPost(hookId)", nil)
return
}
orCtx, err := getOrgRepoCtx(ctx)
if err != nil {
ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err)
return
}
@ -488,7 +525,7 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
ctx.Data["Webhook"] = w
if ctx.HasError() {
ctx.HTML(200, HOOK_NEW)
ctx.HTML(200, orCtx.NewTemplate)
return
}
meta, err := json.Marshal(&models.Slack{
@ -516,5 +553,33 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId))
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId))
}
type OrgRepoCtx struct {
OrgId int64
RepoId int64
Link string
NewTemplate base.TplName
}
// determines whether this is a repo context or organization context
func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) {
if _, ok := ctx.Data["RepoLink"]; ok {
return &OrgRepoCtx{
OrgId: int64(0),
RepoId: ctx.Repo.Repository.Id,
Link: ctx.Repo.RepoLink,
NewTemplate: HOOK_NEW,
}, nil
} else if _, ok := ctx.Data["OrgLink"]; ok {
return &OrgRepoCtx{
OrgId: ctx.Org.Organization.Id,
RepoId: int64(0),
Link: ctx.Org.OrgLink,
NewTemplate: ORG_HOOK_NEW,
}, nil
} else {
return &OrgRepoCtx{}, errors.New("Unable to set OrgRepo context")
}
}

View File

@ -0,0 +1,37 @@
{{template "ng/base/head" .}}
{{template "ng/base/header" .}}
{{template "org/base/header" .}}
<div id="setting-wrapper" class="main-wrapper">
<div id="org-setting" class="container clear">
{{template "org/settings/nav" .}}
<div class="grid-4-5 left">
<div class="setting-content">
{{template "ng/base/alert" .}}
<div id="setting-content">
<div id="repo-hooks-panel" class="panel panel-radius">
<div class="panel-header">
<strong>{{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}}</strong>
</div>
{{template "repo/settings/hook_types" .}}
{{template "repo/settings/hook_gogs" .}}
{{template "repo/settings/hook_slack" .}}
</div>
</div>
{{if .PageIsSettingsHooksEdit}}
<br>
<div id="setting-content">
<div id="repo-hooks-history-panel" class="panel panel-radius">
<div class="panel-header">
<strong>{{.i18n.Tr "repo.settings.recent_deliveries"}}</strong>
</div>
<ul class="panel-body setting-list">
<li>Coming soon!</li>
</ul>
</div>
</div>
{{end}}
</div>
</div>
</div>
</div>
{{template "ng/base/footer" .}}

View File

@ -0,0 +1,38 @@
{{template "ng/base/head" .}}
{{template "ng/base/header" .}}
{{template "org/base/header" .}}
<div id="setting-wrapper" class="main-wrapper">
<div id="org-setting" class="container clear">
{{template "org/settings/nav" .}}
<div class="grid-4-5 left">
<div class="setting-content">
{{template "ng/base/alert" .}}
<div id="setting-content">
<div id="repo-hooks-panel" class="panel panel-radius">
<div class="panel-header">
<a class="btn btn-small btn-black btn-header btn-radius right" href="{{.OrgLink}}/settings/hooks/new">{{.i18n.Tr "repo.settings.add_webhook"}}</a>
<strong>{{.i18n.Tr "repo.settings.hooks"}}</strong>
</div>
<ul class="panel-body setting-list">
<li>{{.i18n.Tr "org.settings.hooks_desc" | Str2html}}</li>
{{range .Webhooks}}
<li>
{{if .IsActive}}
<span class="left text-success"><i class="octicon octicon-check"></i></span>
{{else}}
<span class="left text-grey"><i class="octicon octicon-primitive-dot"></i></span>
{{end}}
<a class="link" href="{{$.OrgLink}}/settings/hooks/{{.Id}}">{{.Url}}</a>
<a href="{{$.OrgLink}}/settings/hooks?remove={{.Id}}" class="text-red right"><i class="fa fa-times"></i></a>
<a href="{{$.OrgLink}}/settings/hooks/{{.Id}}" class="text-blue right"><i class="fa fa-pencil"></i></a>
</li>
{{end}}
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{{template "ng/base/footer" .}}

View File

@ -5,6 +5,7 @@
<div class="panel-body">
<ul class="menu menu-vertical switching-list grid-1-5 left">
<li {{if .PageIsSettingsOptions}}class="current"{{end}}><a href="/org/{{.Org.LowerName}}/settings">{{.i18n.Tr "org.settings.options"}}</a></li>
<li {{if .PageIsSettingsHooks}}class="current"{{end}}><a href="/org/{{.Org.LowerName}}/settings/hooks">{{.i18n.Tr "repo.settings.hooks"}}</a></li>
<li {{if .PageIsSettingsDelete}}class="current"{{end}}><a href="/org/{{.Org.LowerName}}/settings/delete">{{.i18n.Tr "org.settings.delete"}}</a></li>
</ul>
</div>

View File

@ -1,5 +1,5 @@
<div id="gogs" class="{{if (and .PageIsSettingsHooksEdit (not (eq .HookType "Gogs")))}}hidden{{end}}">
<form class="form form-align panel-body repo-setting-form" id="repo-setting-form-gogs" action="{{.RepoLink}}/settings/hooks/gogs/{{if .PageIsSettingsHooksNew}}new{{else}}{{.Webhook.Id}}{{end}}" method="post">
<form class="form form-align panel-body repo-setting-form" id="repo-setting-form-gogs" action="{{if .RepoLink}}{{.RepoLink}}{{else if .OrgLink}}{{.OrgLink}}{{end}}/settings/hooks/gogs/{{if .PageIsSettingsHooksNew}}new{{else}}{{.Webhook.Id}}{{end}}" method="post">
{{.CsrfTokenHtml}}
<input type="hidden" name="hook_type" value="gogs">
<div class="text-center panel-desc">{{.i18n.Tr "repo.settings.add_webhook_desc" | Str2html}}</div>

View File

@ -1,5 +1,5 @@
<div id="slack" class="{{if or .PageIsSettingsHooksNew (and .PageIsSettingsHooksEdit (not (eq .HookType "Slack")))}}hidden{{end}}">
<form class="form form-align panel-body repo-setting-form" id="repo-setting-form-slack" action="{{.RepoLink}}/settings/hooks/slack/{{if .PageIsSettingsHooksNew}}new{{else}}{{.Webhook.Id}}{{end}}" method="post">
<form class="form form-align panel-body repo-setting-form" id="repo-setting-form-slack" action="{{if .RepoLink}}{{.RepoLink}}{{else if .OrgLink}}{{.OrgLink}}{{end}}/settings/hooks/slack/{{if .PageIsSettingsHooksNew}}new{{else}}{{.Webhook.Id}}{{end}}" method="post">
{{.CsrfTokenHtml}}
<input type="hidden" name="hook_type" value="slack">
<div class="text-center panel-desc">{{.i18n.Tr "repo.settings.add_slack_hook_desc" | Str2html}}</div>