@@ -56,16 +56,21 @@ func TestNewWebHookLink(t *testing.T) {
56
56
}
57
57
}
58
58
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 ) {
60
60
token := getTokenForLoggedInUser (t , session , auth_model .AccessTokenScopeAll )
61
+ var branchFilterString string
62
+ if len (branchFilter ) > 0 {
63
+ branchFilterString = branchFilter [0 ]
64
+ }
61
65
req := NewRequestWithJSON (t , "POST" , "/api/v1/repos/" + userName + "/" + repoName + "/hooks" , api.CreateHookOption {
62
66
Type : "gitea" ,
63
67
Config : api.CreateHookOptionConfig {
64
68
"content_type" : "json" ,
65
69
"url" : url ,
66
70
},
67
- Events : []string {event },
68
- Active : true ,
71
+ Events : []string {event },
72
+ Active : true ,
73
+ BranchFilter : branchFilterString ,
69
74
}).AddTokenAuth (token )
70
75
MakeRequest (t , req , http .StatusCreated )
71
76
}
@@ -371,6 +376,45 @@ func Test_WebhookPush(t *testing.T) {
371
376
})
372
377
}
373
378
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
+
374
418
func Test_WebhookIssue (t * testing.T ) {
375
419
var payloads []api.IssuePayload
376
420
var triggeredEvent string
0 commit comments