Skip to content

Commit 8e27c91

Browse files
author
Earl Warren
committed
Merge pull request '[v7.0/forgejo] [BUG] fix webhook creation payload ref' (go-gitea#3070) from bp-v7.0/forgejo-2c85a14-9d29192 into v7.0/forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3070 Reviewed-by: oliverpool <[email protected]>
2 parents 7e5fed7 + a2a833c commit 8e27c91

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

models/webhook/hooktask.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (t *HookTask) simpleMarshalJSON(v any) string {
106106
return string(p)
107107
}
108108

109-
// HookTasks returns a list of hook tasks by given conditions.
109+
// HookTasks returns a list of hook tasks by given conditions, order by ID desc.
110110
func HookTasks(ctx context.Context, hookID int64, page int) ([]*HookTask, error) {
111111
tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
112112
return tasks, db.GetEngine(ctx).

services/webhook/notifier.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -748,10 +748,9 @@ func (m *webhookNotifier) PullRequestReviewRequest(ctx context.Context, doer *us
748748
func (m *webhookNotifier) CreateRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName, refID string) {
749749
apiPusher := convert.ToUser(ctx, pusher, nil)
750750
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeNone})
751-
refName := refFullName.ShortName()
752751

753752
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventCreate, &api.CreatePayload{
754-
Ref: refName, // FIXME: should it be a full ref name?
753+
Ref: refFullName.String(),
755754
Sha: refID,
756755
RefType: refFullName.RefType(),
757756
Repo: apiRepo,
@@ -785,10 +784,9 @@ func (m *webhookNotifier) PullRequestSynchronized(ctx context.Context, doer *use
785784
func (m *webhookNotifier) DeleteRef(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, refFullName git.RefName) {
786785
apiPusher := convert.ToUser(ctx, pusher, nil)
787786
apiRepo := convert.ToRepo(ctx, repo, access_model.Permission{AccessMode: perm.AccessModeOwner})
788-
refName := refFullName.ShortName()
789787

790788
if err := PrepareWebhooks(ctx, EventSource{Repository: repo}, webhook_module.HookEventDelete, &api.DeletePayload{
791-
Ref: refName, // FIXME: should it be a full ref name?
789+
Ref: refFullName.String(),
792790
RefType: refFullName.RefType(),
793791
PusherType: api.PusherTypeUser,
794792
Repo: apiRepo,

tests/integration/pull_merge_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func testPullCleanUp(t *testing.T, session *TestSession, user, repo, pullnum str
8383
return resp
8484
}
8585

86+
// returns the hook tasks, order by ID desc.
8687
func retrieveHookTasks(t *testing.T, hookID int64, activateWebhook bool) []*webhook.HookTask {
8788
t.Helper()
8889
if activateWebhook {

tests/integration/webhook_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"net/http"
8+
"net/url"
9+
"testing"
10+
11+
"code.gitea.io/gitea/models/db"
12+
"code.gitea.io/gitea/models/unittest"
13+
webhook_model "code.gitea.io/gitea/models/webhook"
14+
"code.gitea.io/gitea/modules/json"
15+
webhook_module "code.gitea.io/gitea/modules/webhook"
16+
17+
"github.com/stretchr/testify/assert"
18+
)
19+
20+
func TestWebhookPayloadRef(t *testing.T) {
21+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
22+
w := unittest.AssertExistsAndLoadBean(t, &webhook_model.Webhook{ID: 1})
23+
w.HookEvent = &webhook_module.HookEvent{
24+
SendEverything: true,
25+
}
26+
assert.NoError(t, w.UpdateEvent())
27+
assert.NoError(t, webhook_model.UpdateWebhook(db.DefaultContext, w))
28+
29+
hookTasks := retrieveHookTasks(t, w.ID, true)
30+
hookTasksLenBefore := len(hookTasks)
31+
32+
session := loginUser(t, "user2")
33+
// create new branch
34+
csrf := GetCSRF(t, session, "user2/repo1")
35+
req := NewRequestWithValues(t, "POST", "user2/repo1/branches/_new/branch/master",
36+
map[string]string{
37+
"_csrf": csrf,
38+
"new_branch_name": "arbre",
39+
"create_tag": "false",
40+
},
41+
)
42+
session.MakeRequest(t, req, http.StatusSeeOther)
43+
// delete the created branch
44+
req = NewRequestWithValues(t, "POST", "user2/repo1/branches/delete?name=arbre",
45+
map[string]string{
46+
"_csrf": csrf,
47+
},
48+
)
49+
session.MakeRequest(t, req, http.StatusOK)
50+
51+
// check the newly created hooktasks
52+
hookTasks = retrieveHookTasks(t, w.ID, false)
53+
expected := map[webhook_module.HookEventType]bool{
54+
webhook_module.HookEventCreate: true,
55+
webhook_module.HookEventPush: true, // the branch creation also creates a push event
56+
webhook_module.HookEventDelete: true,
57+
}
58+
for _, hookTask := range hookTasks[:len(hookTasks)-hookTasksLenBefore] {
59+
if !expected[hookTask.EventType] {
60+
t.Errorf("unexpected (or duplicated) event %q", hookTask.EventType)
61+
}
62+
63+
var payload struct {
64+
Ref string `json:"ref"`
65+
}
66+
assert.NoError(t, json.Unmarshal([]byte(hookTask.PayloadContent), &payload))
67+
assert.Equal(t, "refs/heads/arbre", payload.Ref, "unexpected ref for %q event", hookTask.EventType)
68+
delete(expected, hookTask.EventType)
69+
}
70+
assert.Empty(t, expected)
71+
})
72+
}

0 commit comments

Comments
 (0)