summaryrefslogtreecommitdiffstats
path: root/models/action.go
diff options
context:
space:
mode:
authorDavid Schneiderbauer <daviian@users.noreply.github.com>2017-09-21 09:43:26 +0200
committerLauris BH <lauris@nix.lv>2017-09-21 10:43:26 +0300
commit1eedd983ea0f387b6e72264b1cd195a4d3ea1253 (patch)
treed4bbb070df7df0d6f38e2f20a8baf52598623a6a /models/action.go
parent0d80af649a50c4b9e5e4ba764399872fc92f70f2 (diff)
downloadgitea-1eedd983ea0f387b6e72264b1cd195a4d3ea1253.tar.gz
gitea-1eedd983ea0f387b6e72264b1cd195a4d3ea1253.zip
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>
Diffstat (limited to 'models/action.go')
-rw-r--r--models/action.go57
1 files changed, 41 insertions, 16 deletions
diff --git a/models/action.go b/models/action.go
index e687336229..27cfdc865c 100644
--- a/models/action.go
+++ b/models/action.go
@@ -46,6 +46,8 @@ const (
ActionReopenIssue // 13
ActionClosePullRequest // 14
ActionReopenPullRequest // 15
+ ActionDeleteTag // 16
+ ActionDeleteBranch // 17
)
var (
@@ -554,6 +556,12 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
// Check it's tag push or branch.
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
opType = ActionPushTag
+ if opts.NewCommitID == git.EmptySHA {
+ opType = ActionDeleteTag
+ }
+ opts.Commits = &PushCommits{}
+ } else if opts.NewCommitID == git.EmptySHA {
+ opType = ActionDeleteBranch
opts.Commits = &PushCommits{}
} else {
// if not the first commit, set the compare URL.
@@ -599,40 +607,38 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
apiRepo := repo.APIFormat(AccessModeNone)
var shaSum string
+ var isHookEventPush = false
switch opType {
case ActionCommitRepo: // Push
- if err = PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
- Ref: opts.RefFullName,
- Before: opts.OldCommitID,
- After: opts.NewCommitID,
- CompareURL: setting.AppURL + opts.Commits.CompareURL,
- Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
- Repo: apiRepo,
- Pusher: apiPusher,
- Sender: apiPusher,
- }); err != nil {
- return fmt.Errorf("PrepareWebhooks: %v", err)
- }
+ isHookEventPush = true
if isNewBranch {
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
log.Error(4, "OpenRepository[%s]: %v", repo.RepoPath(), err)
}
+
shaSum, err = gitRepo.GetBranchCommitID(refName)
if err != nil {
log.Error(4, "GetBranchCommitID[%s]: %v", opts.RefFullName, err)
}
- return PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
+ if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
Ref: refName,
Sha: shaSum,
RefType: "branch",
Repo: apiRepo,
Sender: apiPusher,
- })
+ }); err != nil {
+ return fmt.Errorf("PrepareWebhooks: %v", err)
+ }
}
+ case ActionDeleteBranch: // Delete Branch
+ isHookEventPush = true
+
case ActionPushTag: // Create
+ isHookEventPush = true
+
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
log.Error(4, "OpenRepository[%s]: %v", repo.RepoPath(), err)
@@ -641,13 +647,32 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
if err != nil {
log.Error(4, "GetTagCommitID[%s]: %v", opts.RefFullName, err)
}
- return PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
+ if err = PrepareWebhooks(repo, HookEventCreate, &api.CreatePayload{
Ref: refName,
Sha: shaSum,
RefType: "tag",
Repo: apiRepo,
Sender: apiPusher,
- })
+ }); err != nil {
+ return fmt.Errorf("PrepareWebhooks: %v", err)
+ }
+ case ActionDeleteTag: // Delete Tag
+ isHookEventPush = true
+ }
+
+ if isHookEventPush {
+ if err = PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
+ Ref: opts.RefFullName,
+ Before: opts.OldCommitID,
+ After: opts.NewCommitID,
+ CompareURL: setting.AppURL + opts.Commits.CompareURL,
+ Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
+ Repo: apiRepo,
+ Pusher: apiPusher,
+ Sender: apiPusher,
+ }); err != nil {
+ return fmt.Errorf("PrepareWebhooks: %v", err)
+ }
}
return nil