Skip to content

Commit e79e4b1

Browse files
committed
Merge pull request gogs#442 from compressed/org_hook
Organization-level Webhooks
2 parents 40bc130 + f7be61c commit e79e4b1

File tree

12 files changed

+243
-34
lines changed

12 files changed

+243
-34
lines changed

cmd/web.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ func runWeb(*cli.Context) {
261261
m.Group("/settings", func(r *macaron.Router) {
262262
r.Get("", org.Settings)
263263
r.Post("", bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost)
264+
r.Get("/hooks", org.SettingsHooks)
265+
r.Get("/hooks/new", repo.WebHooksNew)
266+
r.Post("/hooks/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
267+
r.Post("/hooks/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
268+
r.Get("/hooks/:id", repo.WebHooksEdit)
269+
r.Post("/hooks/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
270+
r.Post("/hooks/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
264271
r.Route("/delete", "GET,POST", org.SettingsDelete)
265272
})
266273

conf/locale/locale_en-US.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ unbind = Unbind
207207
unbind_success = Social account has been unbound.
208208
209209
delete_account = Delete Your Account
210-
delete_prompt = The operation will delete your account permanently, and <strong>CANNOT</strong> be undo!
210+
delete_prompt = The operation will delete your account permanently, and <strong>CANNOT</strong> be undone!
211211
confirm_delete_account = Confirm Deletion
212212
213213
[repo]
@@ -316,8 +316,9 @@ settings.update_settings = Update Settings
316316
settings.update_setting_success = Organization setting has been successfully updated.
317317
settings.delete = Delete Organization
318318
settings.delete_account = Delete This Organization
319-
settings.delete_prompt = The operation will delete this organization permanently, and <strong>CANNOT</strong> be undo!
319+
settings.delete_prompt = The operation will delete this organization permanently, and <strong>CANNOT</strong> be undone!
320320
settings.confirm_delete_account = Confirm Deletion
321+
settings.hooks_desc = Add webhooks that will be triggered for <strong>all repositories</strong> under this organization.
321322
322323
members.public = Public
323324
members.public_helper = make private

models/action.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,20 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string,
220220

221221
ws, err := GetActiveWebhooksByRepoId(repoId)
222222
if err != nil {
223-
return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error())
224-
} else if len(ws) == 0 {
223+
return errors.New("action.CommitRepoAction(GetActiveWebhooksByRepoId): " + err.Error())
224+
}
225+
226+
// check if repo belongs to org and append additional webhooks
227+
if repo.Owner.IsOrganization() {
228+
// get hooks for org
229+
orgws, err := GetActiveWebhooksByOrgId(repo.OwnerId)
230+
if err != nil {
231+
return errors.New("action.CommitRepoAction(GetActiveWebhooksByOrgId): " + err.Error())
232+
}
233+
ws = append(ws, orgws...)
234+
}
235+
236+
if len(ws) == 0 {
225237
return nil
226238
}
227239

models/webhook.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Webhook struct {
4545
IsActive bool
4646
HookTaskType HookTaskType
4747
Meta string `xorm:"TEXT"` // store hook-specific attributes
48+
OrgId int64
4849
}
4950

5051
// GetEvent handles conversion from Events to HookEvent.
@@ -120,6 +121,18 @@ func DeleteWebhook(hookId int64) error {
120121
return err
121122
}
122123

124+
// GetWebhooksByOrgId returns all webhooks for an organization.
125+
func GetWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
126+
err = x.Find(&ws, &Webhook{OrgId: orgId})
127+
return ws, err
128+
}
129+
130+
// GetActiveWebhooksByOrgId returns all active webhooks for an organization.
131+
func GetActiveWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) {
132+
err = x.Find(&ws, &Webhook{OrgId: orgId, IsActive: true})
133+
return ws, err
134+
}
135+
123136
// ___ ___ __ ___________ __
124137
// / | \ ____ ____ | | _\__ ___/____ _____| | __
125138
// / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /

public/ng/js/gogs.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,8 @@ function initRepo() {
349349
})
350350
}
351351

352-
function initRepoSetting() {
353-
// Options.
354-
// Confirmation of changing repository name.
355-
$('#repo-setting-form').submit(function (e) {
356-
var $reponame = $('#repo_name');
357-
if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
358-
e.preventDefault();
359-
return true;
360-
}
361-
});
362-
352+
// when user changes hook type, hide/show proper divs
353+
function initHookTypeChange() {
363354
// web hook type change
364355
$('select#hook-type').on("change", function () {
365356
hookTypes = ['Gogs','Slack'];
@@ -374,6 +365,20 @@ function initRepoSetting() {
374365
}
375366
});
376367
});
368+
}
369+
370+
function initRepoSetting() {
371+
// Options.
372+
// Confirmation of changing repository name.
373+
$('#repo-setting-form').submit(function (e) {
374+
var $reponame = $('#repo_name');
375+
if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) {
376+
e.preventDefault();
377+
return true;
378+
}
379+
});
380+
381+
initHookTypeChange();
377382

378383
$('#transfer-button').click(function () {
379384
$('#transfer-form').show();
@@ -421,6 +426,8 @@ function initOrgSetting() {
421426
return true;
422427
}
423428
});
429+
430+
initHookTypeChange();
424431
}
425432

426433
function initInvite() {

routers/org/setting.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package org
66

77
import (
8+
"github.com/Unknwon/com"
89
"github.com/gogits/gogs/models"
910
"github.com/gogits/gogs/modules/auth"
1011
"github.com/gogits/gogs/modules/base"
@@ -15,6 +16,7 @@ import (
1516
const (
1617
SETTINGS_OPTIONS base.TplName = "org/settings/options"
1718
SETTINGS_DELETE base.TplName = "org/settings/delete"
19+
SETTINGS_HOOKS base.TplName = "org/settings/hooks"
1820
)
1921

2022
func Settings(ctx *middleware.Context) {
@@ -97,3 +99,29 @@ func SettingsDelete(ctx *middleware.Context) {
9799

98100
ctx.HTML(200, SETTINGS_DELETE)
99101
}
102+
103+
func SettingsHooks(ctx *middleware.Context) {
104+
ctx.Data["Title"] = ctx.Tr("org.settings")
105+
ctx.Data["PageIsSettingsHooks"] = true
106+
107+
// Delete web hook.
108+
remove := com.StrTo(ctx.Query("remove")).MustInt64()
109+
if remove > 0 {
110+
if err := models.DeleteWebhook(remove); err != nil {
111+
ctx.Handle(500, "DeleteWebhook", err)
112+
return
113+
}
114+
ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success"))
115+
ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks")
116+
return
117+
}
118+
119+
ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id)
120+
if err != nil {
121+
ctx.Handle(500, "GetWebhooksByOrgId", err)
122+
return
123+
}
124+
125+
ctx.Data["Webhooks"] = ws
126+
ctx.HTML(200, SETTINGS_HOOKS)
127+
}

0 commit comments

Comments
 (0)