mirror of
https://github.com/gogs/gogs.git
synced 2025-12-23 16:50:02 +01:00
webhook: only trigger specific webhook for test delivery (#3030)
This commit is contained in:
@@ -3,7 +3,7 @@ Gogs [](http
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
##### Current tip version: [`.VERSION`](templates/.VERSION) (see [Releases](https://github.com/gogits/gogs/releases) for binary versions ~~or submit a task on [alpha stage automated binary building system](https://build.gogs.io/)~~)
|
##### Current tip version: [`.VERSION`](templates/.VERSION) (see [Releases](https://github.com/gogits/gogs/releases) for binary versions)
|
||||||
|
|
||||||
| Web | UI | Preview |
|
| Web | UI | Preview |
|
||||||
|:-------------:|:-------:|:-------:|
|
|:-------------:|:-------:|:-------:|
|
||||||
|
|||||||
@@ -197,8 +197,8 @@ func getWebhook(bean *Webhook) (*Webhook, error) {
|
|||||||
return bean, nil
|
return bean, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetWebhookByRepoID returns webhook of repository by given ID.
|
// GetWebhookOfRepoByID returns webhook of repository by given ID.
|
||||||
func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
|
func GetWebhookOfRepoByID(repoID, id int64) (*Webhook, error) {
|
||||||
return getWebhook(&Webhook{
|
return getWebhook(&Webhook{
|
||||||
ID: id,
|
ID: id,
|
||||||
RepoID: repoID,
|
RepoID: repoID,
|
||||||
@@ -433,29 +433,14 @@ func UpdateHookTask(t *HookTask) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrepareWebhooks adds new webhooks to task queue for given payload.
|
// prepareWebhooks adds list of webhooks to task queue.
|
||||||
func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
|
func prepareWebhooks(repo *Repository, event HookEventType, p api.Payloader, webhooks []*Webhook) (err error) {
|
||||||
ws, err := GetActiveWebhooksByRepoID(repo.ID)
|
if len(webhooks) == 0 {
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("GetActiveWebhooksByRepoID: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if repo belongs to org and append additional webhooks
|
|
||||||
if repo.MustOwner().IsOrganization() {
|
|
||||||
// get hooks for org
|
|
||||||
orgws, err := GetActiveWebhooksByOrgID(repo.OwnerID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("GetActiveWebhooksByOrgID: %v", err)
|
|
||||||
}
|
|
||||||
ws = append(ws, orgws...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(ws) == 0 {
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var payloader api.Payloader
|
var payloader api.Payloader
|
||||||
for _, w := range ws {
|
for _, w := range webhooks {
|
||||||
switch event {
|
switch event {
|
||||||
case HOOK_EVENT_CREATE:
|
case HOOK_EVENT_CREATE:
|
||||||
if !w.HasCreateEvent() {
|
if !w.HasCreateEvent() {
|
||||||
@@ -504,6 +489,34 @@ func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) err
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrepareWebhooks adds all active webhooks to task queue.
|
||||||
|
func PrepareWebhooks(repo *Repository, event HookEventType, p api.Payloader) error {
|
||||||
|
webhooks, err := GetActiveWebhooksByRepoID(repo.ID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("GetActiveWebhooksByRepoID [%d]: %v", repo.ID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if repo belongs to org and append additional webhooks
|
||||||
|
if repo.MustOwner().IsOrganization() {
|
||||||
|
// get hooks for org
|
||||||
|
orgws, err := GetActiveWebhooksByOrgID(repo.OwnerID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("GetActiveWebhooksByOrgID [%d]: %v", repo.OwnerID, err)
|
||||||
|
}
|
||||||
|
webhooks = append(webhooks, orgws...)
|
||||||
|
}
|
||||||
|
return prepareWebhooks(repo, event, p, webhooks)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestWebhook adds the test webhook matches the ID to task queue.
|
||||||
|
func TestWebhook(repo *Repository, event HookEventType, p api.Payloader, webhookID int64) error {
|
||||||
|
webhook, err := GetWebhookOfRepoByID(repo.ID, webhookID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("GetWebhookOfRepoByID [repo_id: %d, id: %d]: %v", repo.ID, webhookID, err)
|
||||||
|
}
|
||||||
|
return prepareWebhooks(repo, event, p, []*Webhook{webhook})
|
||||||
|
}
|
||||||
|
|
||||||
func (t *HookTask) deliver() {
|
func (t *HookTask) deliver() {
|
||||||
t.IsDelivered = true
|
t.IsDelivered = true
|
||||||
|
|
||||||
@@ -541,7 +554,7 @@ func (t *HookTask) deliver() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update webhook last delivery status.
|
// Update webhook last delivery status.
|
||||||
w, err := GetWebhookByRepoID(t.RepoID, t.HookID)
|
w, err := GetWebhookOfRepoByID(t.RepoID, t.HookID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(5, "GetWebhookByID: %v", err)
|
log.Error(5, "GetWebhookByID: %v", err)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
|
|||||||
|
|
||||||
// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
|
// https://github.com/gogits/go-gogs-client/wiki/Repositories#edit-a-hook
|
||||||
func EditHook(ctx *context.APIContext, form api.EditHookOption) {
|
func EditHook(ctx *context.APIContext, form api.EditHookOption) {
|
||||||
w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
w, err := models.GetWebhookOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if models.IsErrWebhookNotExist(err) {
|
if models.IsErrWebhookNotExist(err) {
|
||||||
ctx.Status(404)
|
ctx.Status(404)
|
||||||
|
|||||||
@@ -273,7 +273,7 @@ func checkWebhook(ctx *context.Context) (*OrgRepoCtx, *models.Webhook) {
|
|||||||
|
|
||||||
var w *models.Webhook
|
var w *models.Webhook
|
||||||
if orCtx.RepoID > 0 {
|
if orCtx.RepoID > 0 {
|
||||||
w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
w, err = models.GetWebhookOfRepoByID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
||||||
} else {
|
} else {
|
||||||
w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
|
w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
|
||||||
}
|
}
|
||||||
@@ -504,8 +504,8 @@ func TestWebhook(ctx *context.Context) {
|
|||||||
Pusher: apiUser,
|
Pusher: apiUser,
|
||||||
Sender: apiUser,
|
Sender: apiUser,
|
||||||
}
|
}
|
||||||
if err := models.PrepareWebhooks(ctx.Repo.Repository, models.HOOK_EVENT_PUSH, p); err != nil {
|
if err := models.TestWebhook(ctx.Repo.Repository, models.HOOK_EVENT_PUSH, p, ctx.QueryInt64("id")); err != nil {
|
||||||
ctx.Flash.Error("PrepareWebhooks: " + err.Error())
|
ctx.Flash.Error("TestWebhook: " + err.Error())
|
||||||
ctx.Status(500)
|
ctx.Status(500)
|
||||||
} else {
|
} else {
|
||||||
go models.HookQueue.Add(ctx.Repo.Repository.ID)
|
go models.HookQueue.Add(ctx.Repo.Repository.ID)
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
{{if .IsRepositoryAdmin}}
|
{{if .IsRepositoryAdmin}}
|
||||||
<div class="ui right">
|
<div class="ui right">
|
||||||
<button class="ui teal tiny button poping up" id="test-delivery" data-content=
|
<button class="ui teal tiny button poping up" id="test-delivery" data-content=
|
||||||
"{{.i18n.Tr "repo.settings.webhook.test_delivery_desc"}}" data-variation="inverted tiny" data-link="{{.Link}}/test" data-redirect="{{.Link}}">{{.i18n.Tr "repo.settings.webhook.test_delivery"}}</button>
|
"{{.i18n.Tr "repo.settings.webhook.test_delivery_desc"}}" data-variation="inverted tiny" data-link="{{.Link}}/test?id={{.Webhook.ID}}" data-redirect="{{.Link}}">{{.i18n.Tr "repo.settings.webhook.test_delivery"}}</button>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
</h4>
|
</h4>
|
||||||
|
|||||||
Reference in New Issue
Block a user