Skip to content

Commit 1eedd98

Browse files
daviianlafriks
authored andcommitted
Complete push webhooks (#2530)
* implemented missing 'delete' push webhooks moreover created ActionDeleteBranch and ActionDeleteTag * add CommitRepoAction tests for tag/branch creation/deletion * fixed error where push webhook not called if is new branch or tag removed unnecessary code * moved prepare unit test environment into separate method to be used across unit tests * add missing if clause in pushUpdate Signed-off-by: David Schneiderbauer <dschneiderbauer@gmail.com>
1 parent 0d80af6 commit 1eedd98

File tree

8 files changed

+197
-102
lines changed

8 files changed

+197
-102
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
65f1bf27bc3bf70f64657658635e66094edbcb4d

Diff for: models/action.go

+41-16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const (
4646
ActionReopenIssue // 13
4747
ActionClosePullRequest // 14
4848
ActionReopenPullRequest // 15
49+
ActionDeleteTag // 16
50+
ActionDeleteBranch // 17
4951
)
5052

5153
var (
@@ -554,6 +556,12 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
554556
// Check it's tag push or branch.
555557
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
556558
opType = ActionPushTag
559+
if opts.NewCommitID == git.EmptySHA {
560+
opType = ActionDeleteTag
561+
}
562+
opts.Commits = &PushCommits{}
563+
} else if opts.NewCommitID == git.EmptySHA {
564+
opType = ActionDeleteBranch
557565
opts.Commits = &PushCommits{}
558566
} else {
559567
// if not the first commit, set the compare URL.
@@ -599,40 +607,38 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
599607
apiRepo := repo.APIFormat(AccessModeNone)
600608

601609
var shaSum string
610+
var isHookEventPush = false
602611
switch opType {
603612
case ActionCommitRepo: // Push
604-
if err = PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
605-
Ref: opts.RefFullName,
606-
Before: opts.OldCommitID,
607-
After: opts.NewCommitID,
608-
CompareURL: setting.AppURL + opts.Commits.CompareURL,
609-
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
610-
Repo: apiRepo,
611-
Pusher: apiPusher,
612-
Sender: apiPusher,
613-
}); err != nil {
614-
return fmt.Errorf("PrepareWebhooks: %v", err)
615-
}
613+
isHookEventPush = true
616614

617615
if isNewBranch {
618616
gitRepo, err := git.OpenRepository(repo.RepoPath())
619617
if err != nil {
620618
log.Error(4, "OpenRepository[%s]: %v", repo.RepoPath(), err)
621619
}
620+
622621
shaSum, err = gitRepo.GetBranchCommitID(refName)
623622
if err != nil {
624623
log.Error(4, "GetBranchCommitID[%s]: %v", opts.RefFullName, err)
625624
}
626-
return PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
625+
if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
627626
Ref: refName,
628627
Sha: shaSum,
629628
RefType: "branch",
630629
Repo: apiRepo,
631630
Sender: apiPusher,
632-
})
631+
}); err != nil {
632+
return fmt.Errorf("PrepareWebhooks: %v", err)
633+
}
633634
}
634635

636+
case ActionDeleteBranch: // Delete Branch
637+
isHookEventPush = true
638+
635639
case ActionPushTag: // Create
640+
isHookEventPush = true
641+
636642
gitRepo, err := git.OpenRepository(repo.RepoPath())
637643
if err != nil {
638644
log.Error(4, "OpenRepository[%s]: %v", repo.RepoPath(), err)
@@ -641,13 +647,32 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
641647
if err != nil {
642648
log.Error(4, "GetTagCommitID[%s]: %v", opts.RefFullName, err)
643649
}
644-
return PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
650+
if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
645651
Ref: refName,
646652
Sha: shaSum,
647653
RefType: "tag",
648654
Repo: apiRepo,
649655
Sender: apiPusher,
650-
})
656+
}); err != nil {
657+
return fmt.Errorf("PrepareWebhooks: %v", err)
658+
}
659+
case ActionDeleteTag: // Delete Tag
660+
isHookEventPush = true
661+
}
662+
663+
if isHookEventPush {
664+
if err = PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
665+
Ref: opts.RefFullName,
666+
Before: opts.OldCommitID,
667+
After: opts.NewCommitID,
668+
CompareURL: setting.AppURL + opts.Commits.CompareURL,
669+
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
670+
Repo: apiRepo,
671+
Pusher: apiPusher,
672+
Sender: apiPusher,
673+
}); err != nil {
674+
return fmt.Errorf("PrepareWebhooks: %v", err)
675+
}
651676
}
652677

653678
return nil

Diff for: models/action_test.go

+103-41
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"strings"
66
"testing"
77

8+
"code.gitea.io/git"
89
"code.gitea.io/gitea/modules/setting"
910

1011
"github.com/stretchr/testify/assert"
@@ -202,55 +203,116 @@ func TestUpdateIssuesCommit(t *testing.T) {
202203
CheckConsistencyFor(t, &Action{})
203204
}
204205

205-
func TestCommitRepoAction(t *testing.T) {
206-
assert.NoError(t, PrepareTestDatabase())
207-
208-
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
209-
repo := AssertExistsAndLoadBean(t, &Repository{ID: 2, OwnerID: user.ID}).(*Repository)
210-
repo.Owner = user
206+
func testCorrectRepoAction(t *testing.T, opts CommitRepoActionOptions, actionBean *Action) {
207+
AssertNotExistsBean(t, actionBean)
208+
assert.NoError(t, CommitRepoAction(opts))
209+
AssertExistsAndLoadBean(t, actionBean)
210+
CheckConsistencyFor(t, &Action{})
211+
}
211212

212-
pushCommits := NewPushCommits()
213-
pushCommits.Commits = []*PushCommit{
213+
func TestCommitRepoAction(t *testing.T) {
214+
samples := []struct {
215+
userID int64
216+
repositoryID int64
217+
commitRepoActionOptions CommitRepoActionOptions
218+
action Action
219+
}{
214220
{
215-
Sha1: "abcdef1",
216-
CommitterEmail: "user2@example.com",
217-
CommitterName: "User Two",
218-
AuthorEmail: "user4@example.com",
219-
AuthorName: "User Four",
220-
Message: "message1",
221+
userID: 2,
222+
repositoryID: 2,
223+
commitRepoActionOptions: CommitRepoActionOptions{
224+
RefFullName: "refName",
225+
OldCommitID: "oldCommitID",
226+
NewCommitID: "newCommitID",
227+
Commits: &PushCommits{
228+
avatars: make(map[string]string),
229+
Commits: []*PushCommit{
230+
{
231+
Sha1: "abcdef1",
232+
CommitterEmail: "user2@example.com",
233+
CommitterName: "User Two",
234+
AuthorEmail: "user4@example.com",
235+
AuthorName: "User Four",
236+
Message: "message1",
237+
},
238+
{
239+
Sha1: "abcdef2",
240+
CommitterEmail: "user2@example.com",
241+
CommitterName: "User Two",
242+
AuthorEmail: "user2@example.com",
243+
AuthorName: "User Two",
244+
Message: "message2",
245+
},
246+
},
247+
Len: 2,
248+
},
249+
},
250+
action: Action{
251+
OpType: ActionCommitRepo,
252+
RefName: "refName",
253+
},
221254
},
222255
{
223-
Sha1: "abcdef2",
224-
CommitterEmail: "user2@example.com",
225-
CommitterName: "User Two",
226-
AuthorEmail: "user2@example.com",
227-
AuthorName: "User Two",
228-
Message: "message2",
256+
userID: 2,
257+
repositoryID: 1,
258+
commitRepoActionOptions: CommitRepoActionOptions{
259+
RefFullName: git.TagPrefix + "v1.1",
260+
OldCommitID: git.EmptySHA,
261+
NewCommitID: "newCommitID",
262+
Commits: &PushCommits{},
263+
},
264+
action: Action{
265+
OpType: ActionPushTag,
266+
RefName: "v1.1",
267+
},
268+
},
269+
{
270+
userID: 2,
271+
repositoryID: 1,
272+
commitRepoActionOptions: CommitRepoActionOptions{
273+
RefFullName: git.TagPrefix + "v1.1",
274+
OldCommitID: "oldCommitID",
275+
NewCommitID: git.EmptySHA,
276+
Commits: &PushCommits{},
277+
},
278+
action: Action{
279+
OpType: ActionDeleteTag,
280+
RefName: "v1.1",
281+
},
282+
},
283+
{
284+
userID: 2,
285+
repositoryID: 1,
286+
commitRepoActionOptions: CommitRepoActionOptions{
287+
RefFullName: git.BranchPrefix + "feature/1",
288+
OldCommitID: "oldCommitID",
289+
NewCommitID: git.EmptySHA,
290+
Commits: &PushCommits{},
291+
},
292+
action: Action{
293+
OpType: ActionDeleteBranch,
294+
RefName: "feature/1",
295+
},
229296
},
230297
}
231-
pushCommits.Len = len(pushCommits.Commits)
232298

233-
actionBean := &Action{
234-
OpType: ActionCommitRepo,
235-
ActUserID: user.ID,
236-
ActUser: user,
237-
RepoID: repo.ID,
238-
Repo: repo,
239-
RefName: "refName",
240-
IsPrivate: repo.IsPrivate,
299+
for _, s := range samples {
300+
prepareTestEnv(t)
301+
302+
user := AssertExistsAndLoadBean(t, &User{ID: s.userID}).(*User)
303+
repo := AssertExistsAndLoadBean(t, &Repository{ID: s.repositoryID, OwnerID: user.ID}).(*Repository)
304+
repo.Owner = user
305+
306+
s.commitRepoActionOptions.PusherName = user.Name
307+
s.commitRepoActionOptions.RepoOwnerID = user.ID
308+
s.commitRepoActionOptions.RepoName = repo.Name
309+
310+
s.action.ActUserID = user.ID
311+
s.action.RepoID = repo.ID
312+
s.action.IsPrivate = repo.IsPrivate
313+
314+
testCorrectRepoAction(t, s.commitRepoActionOptions, &s.action)
241315
}
242-
AssertNotExistsBean(t, actionBean)
243-
assert.NoError(t, CommitRepoAction(CommitRepoActionOptions{
244-
PusherName: user.Name,
245-
RepoOwnerID: user.ID,
246-
RepoName: repo.Name,
247-
RefFullName: "refName",
248-
OldCommitID: "oldCommitID",
249-
NewCommitID: "newCommitID",
250-
Commits: pushCommits,
251-
}))
252-
AssertExistsAndLoadBean(t, actionBean)
253-
CheckConsistencyFor(t, &Action{})
254316
}
255317

256318
func TestTransferRepoAction(t *testing.T) {

Diff for: models/unit_tests.go

+10
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
package models
66

77
import (
8+
"os"
89
"testing"
910

11+
"code.gitea.io/gitea/modules/setting"
12+
13+
"github.com/Unknwon/com"
1014
"github.com/go-xorm/core"
1115
"github.com/go-xorm/xorm"
1216
"github.com/stretchr/testify/assert"
@@ -38,6 +42,12 @@ func PrepareTestDatabase() error {
3842
return LoadFixtures()
3943
}
4044

45+
func prepareTestEnv(t testing.TB) {
46+
assert.NoError(t, PrepareTestDatabase())
47+
assert.NoError(t, os.RemoveAll(setting.RepoRootPath))
48+
assert.NoError(t, com.CopyDir("../integrations/gitea-repositories-meta", setting.RepoRootPath))
49+
}
50+
4151
type testCond struct {
4252
query interface{}
4353
args []interface{}

0 commit comments

Comments
 (0)