Skip to content

Commit 0bd387e

Browse files
committed
implemented missing 'delete' push webhooks
moreover created ActionDeleteBranch and ActionDeleteTag Signed-off-by: David Schneiderbauer <[email protected]>
1 parent 6f380a2 commit 0bd387e

File tree

5 files changed

+76
-57
lines changed

5 files changed

+76
-57
lines changed

models/action.go

Lines changed: 34 additions & 12 deletions
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,26 +607,17 @@ 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)
@@ -632,7 +631,12 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
632631
})
633632
}
634633

634+
case ActionDeleteBranch: // Delete Branch
635+
isHookEventPush = true
636+
635637
case ActionPushTag: // Create
638+
isHookEventPush = true
639+
636640
gitRepo, err := git.OpenRepository(repo.RepoPath())
637641
if err != nil {
638642
log.Error(4, "OpenRepository[%s]: %v", repo.RepoPath(), err)
@@ -648,6 +652,24 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
648652
Repo: apiRepo,
649653
Sender: apiPusher,
650654
})
655+
656+
case ActionDeleteTag: // Delete Tag
657+
isHookEventPush = true
658+
}
659+
660+
if isHookEventPush {
661+
if err = PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
662+
Ref: opts.RefFullName,
663+
Before: opts.OldCommitID,
664+
After: opts.NewCommitID,
665+
CompareURL: setting.AppURL + opts.Commits.CompareURL,
666+
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
667+
Repo: apiRepo,
668+
Pusher: apiPusher,
669+
Sender: apiPusher,
670+
}); err != nil {
671+
return fmt.Errorf("PrepareWebhooks: %v", err)
672+
}
651673
}
652674

653675
return nil

models/update.go

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -198,57 +198,46 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
198198
return nil, fmt.Errorf("OpenRepository: %v", err)
199199
}
200200

201-
if isDelRef {
202-
// Tag has been deleted
203-
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
204-
err = pushUpdateDeleteTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):])
205-
if err != nil {
206-
return nil, fmt.Errorf("pushUpdateDeleteTag: %v", err)
207-
}
208-
}
209-
log.GitLogger.Info("Reference '%s' has been deleted from '%s/%s' by %s",
210-
opts.RefFullName, opts.RepoUserName, opts.RepoName, opts.PusherName)
211-
return repo, nil
212-
}
213-
214201
if err = repo.UpdateSize(); err != nil {
215202
log.Error(4, "Failed to update size for repository: %v", err)
216203
}
217204

218-
// Push tags.
205+
var commits = &PushCommits{}
219206
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
220-
pushUpdateAddTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):])
221-
if err := CommitRepoAction(CommitRepoActionOptions{
222-
PusherName: opts.PusherName,
223-
RepoOwnerID: owner.ID,
224-
RepoName: repo.Name,
225-
RefFullName: opts.RefFullName,
226-
OldCommitID: opts.OldCommitID,
227-
NewCommitID: opts.NewCommitID,
228-
Commits: &PushCommits{},
229-
}); err != nil {
230-
return nil, fmt.Errorf("CommitRepoAction (tag): %v", err)
231-
}
232-
return repo, nil
233-
}
234-
235-
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
236-
if err != nil {
237-
return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)
238-
}
239-
240-
// Push new branch.
241-
var l *list.List
242-
if isNewRef {
243-
l, err = newCommit.CommitsBeforeLimit(10)
244-
if err != nil {
245-
return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
207+
// If is tag reference
208+
if isDelRef {
209+
err = pushUpdateDeleteTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):])
210+
if err != nil {
211+
return nil, fmt.Errorf("pushUpdateDeleteTag: %v", err)
212+
}
213+
} else {
214+
err = pushUpdateAddTag(repo, gitRepo, opts.RefFullName[len(git.TagPrefix):])
215+
if err != nil {
216+
return nil, fmt.Errorf("pushUpdateAddTag: %v", err)
217+
}
246218
}
247219
} else {
248-
l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
220+
// If is branch reference
221+
newCommit, err := gitRepo.GetCommit(opts.NewCommitID)
249222
if err != nil {
250-
return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
223+
return nil, fmt.Errorf("gitRepo.GetCommit: %v", err)
251224
}
225+
226+
// Push new branch.
227+
var l *list.List
228+
if isNewRef {
229+
l, err = newCommit.CommitsBeforeLimit(10)
230+
if err != nil {
231+
return nil, fmt.Errorf("newCommit.CommitsBeforeLimit: %v", err)
232+
}
233+
} else {
234+
l, err = newCommit.CommitsBeforeUntil(opts.OldCommitID)
235+
if err != nil {
236+
return nil, fmt.Errorf("newCommit.CommitsBeforeUntil: %v", err)
237+
}
238+
}
239+
240+
commits = ListToPushCommits(l)
252241
}
253242

254243
if err := CommitRepoAction(CommitRepoActionOptions{
@@ -258,9 +247,9 @@ func pushUpdate(opts PushUpdateOptions) (repo *Repository, err error) {
258247
RefFullName: opts.RefFullName,
259248
OldCommitID: opts.OldCommitID,
260249
NewCommitID: opts.NewCommitID,
261-
Commits: ListToPushCommits(l),
250+
Commits: commits,
262251
}); err != nil {
263-
return nil, fmt.Errorf("CommitRepoAction (branch): %v", err)
252+
return nil, fmt.Errorf("CommitRepoAction: %v", err)
264253
}
265254
return repo, nil
266255
}

modules/templates/helper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func ActionIcon(opType models.ActionType) string {
294294
switch opType {
295295
case models.ActionCreateRepo, models.ActionTransferRepo:
296296
return "repo"
297-
case models.ActionCommitRepo, models.ActionPushTag:
297+
case models.ActionCommitRepo, models.ActionPushTag, models.ActionDeleteTag, models.ActionDeleteBranch:
298298
return "git-commit"
299299
case models.ActionCreateIssue:
300300
return "issue-opened"

options/locale/locale_en-US.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,8 @@ comment_issue = `commented on issue <a href="%s/issues/%s">%s#%[2]s</a>`
14361436
merge_pull_request = `merged pull request <a href="%s/pulls/%s">%s#%[2]s</a>`
14371437
transfer_repo = transferred repository <code>%s</code> to <a href="%s">%s</a>
14381438
push_tag = pushed tag <a href="%s/src/%s">%[2]s</a> to <a href="%[1]s">%[3]s</a>
1439+
delete_tag = deleted tag %[2]s from <a href="%[1]s">%[3]s</a>
1440+
delete_branch = deleted branch %[2]s from <a href="%[1]s">%[3]s</a>
14391441
compare_commits = Compare %d commits
14401442
14411443
[tool]

templates/user/dashboard/feeds.tmpl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
{{else if eq .GetOpType 15}}
4444
{{ $index := index .GetIssueInfos 0}}
4545
{{$.i18n.Tr "action.reopen_pull_request" .GetRepoLink $index .ShortRepoPath | Str2html}}
46+
{{else if eq .GetOpType 16}}
47+
{{ $index := index .GetIssueInfos 0}}
48+
{{$.i18n.Tr "action.delete_tag" .GetRepoLink .GetBranch .ShortRepoPath | Str2html}}
49+
{{else if eq .GetOpType 17}}
50+
{{ $index := index .GetIssueInfos 0}}
51+
{{$.i18n.Tr "action.delete_branch" .GetRepoLink .GetBranch .ShortRepoPath | Str2html}}
4652
{{end}}
4753
</p>
4854
{{if eq .GetOpType 5}}

0 commit comments

Comments
 (0)