Skip to content

Commit 355e9a9

Browse files
authored
Add a webhook push test for dev branch (#34421)
1 parent 0902d42 commit 355e9a9

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

tests/integration/repo_webhook_test.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,21 @@ func TestNewWebHookLink(t *testing.T) {
5656
}
5757
}
5858

59-
func testAPICreateWebhookForRepo(t *testing.T, session *TestSession, userName, repoName, url, event string) {
59+
func testAPICreateWebhookForRepo(t *testing.T, session *TestSession, userName, repoName, url, event string, branchFilter ...string) {
6060
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll)
61+
var branchFilterString string
62+
if len(branchFilter) > 0 {
63+
branchFilterString = branchFilter[0]
64+
}
6165
req := NewRequestWithJSON(t, "POST", "/api/v1/repos/"+userName+"/"+repoName+"/hooks", api.CreateHookOption{
6266
Type: "gitea",
6367
Config: api.CreateHookOptionConfig{
6468
"content_type": "json",
6569
"url": url,
6670
},
67-
Events: []string{event},
68-
Active: true,
71+
Events: []string{event},
72+
Active: true,
73+
BranchFilter: branchFilterString,
6974
}).AddTokenAuth(token)
7075
MakeRequest(t, req, http.StatusCreated)
7176
}
@@ -371,6 +376,45 @@ func Test_WebhookPush(t *testing.T) {
371376
})
372377
}
373378

379+
func Test_WebhookPushDevBranch(t *testing.T) {
380+
var payloads []api.PushPayload
381+
var triggeredEvent string
382+
provider := newMockWebhookProvider(func(r *http.Request) {
383+
content, _ := io.ReadAll(r.Body)
384+
var payload api.PushPayload
385+
err := json.Unmarshal(content, &payload)
386+
assert.NoError(t, err)
387+
payloads = append(payloads, payload)
388+
triggeredEvent = "push"
389+
}, http.StatusOK)
390+
defer provider.Close()
391+
392+
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
393+
// 1. create a new webhook with special webhook for repo1
394+
session := loginUser(t, "user2")
395+
396+
// only for dev branch
397+
testAPICreateWebhookForRepo(t, session, "user2", "repo1", provider.URL(), "push", "develop")
398+
399+
// 2. this should not trigger the webhook
400+
testCreateFile(t, session, "user2", "repo1", "master", "test_webhook_push.md", "# a test file for webhook push")
401+
assert.Empty(t, triggeredEvent)
402+
assert.Empty(t, payloads)
403+
404+
// 3. trigger the webhook
405+
testCreateFile(t, session, "user2", "repo1", "develop", "test_webhook_push.md", "# a test file for webhook push")
406+
407+
// 4. validate the webhook is triggered
408+
assert.Equal(t, "push", triggeredEvent)
409+
assert.Len(t, payloads, 1)
410+
assert.Equal(t, "repo1", payloads[0].Repo.Name)
411+
assert.Equal(t, "develop", payloads[0].Branch())
412+
assert.Equal(t, "user2/repo1", payloads[0].Repo.FullName)
413+
assert.Len(t, payloads[0].Commits, 1)
414+
assert.Equal(t, []string{"test_webhook_push.md"}, payloads[0].Commits[0].Added)
415+
})
416+
}
417+
374418
func Test_WebhookIssue(t *testing.T) {
375419
var payloads []api.IssuePayload
376420
var triggeredEvent string

0 commit comments

Comments
 (0)