From b67a023bec82c8c02f85a8d31d6bc520e5ecbf3f Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Mon, 13 Jan 2020 09:09:13 +0200 Subject: [PATCH] Send tag create and push webhook when release created on UI (#8671) (#9702) * 'update' * Send push tag event when release created * send tag create event while release created in UI * update to go v1.13 * fix gofmt error * update #8671 move release tag created hook to modules/notification/webhook due to #8802 refactoring * use NotifyCreateRef and NotifyPushCommits instead of NotifyNewReleaseTag * move tag notification to correct place Co-authored-by: Benno --- services/release/release.go | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/services/release/release.go b/services/release/release.go index b4f814a77..da7359e3f 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" ) @@ -37,6 +38,54 @@ func createTag(gitRepo *git.Repository, rel *models.Release) error { return err } rel.LowerTagName = strings.ToLower(rel.TagName) + // Prepare Notify + if err := rel.LoadAttributes(); err != nil { + log.Error("LoadAttributes: %v", err) + return err + } + + refName := git.TagPrefix + rel.TagName + apiPusher := rel.Publisher.APIFormat() + apiRepo := rel.Repo.APIFormat(models.AccessModeNone) + apiCommits, err := models.NewPushCommits().ToAPIPayloadCommits(rel.Repo.RepoPath(), rel.Repo.HTMLURL()) + if err != nil { + log.Error("commits.ToAPIPayloadCommits failed: %v", err) + return err + } + + if err := models.PrepareWebhooks(rel.Repo, models.HookEventPush, &api.PushPayload{ + Ref: refName, + Before: git.EmptySHA, + After: commit.ID.String(), + CompareURL: setting.AppURL + models.NewPushCommits().CompareURL, + Commits: apiCommits, + Repo: apiRepo, + Pusher: apiPusher, + Sender: apiPusher, + }); err != nil { + log.Error("PrepareWebhooks: %v", err) + } + + gitRepo, err := git.OpenRepository(rel.Repo.RepoPath()) + if err != nil { + log.Error("OpenRepository[%s]: %v", rel.Repo.RepoPath(), err) + } + shaSum, err := gitRepo.GetTagCommitID(refName) + if err != nil { + gitRepo.Close() + log.Error("GetTagCommitID[%s]: %v", refName, err) + } + gitRepo.Close() + + if err = models.PrepareWebhooks(rel.Repo, models.HookEventCreate, &api.CreatePayload{ + Ref: git.RefEndName(refName), + Sha: shaSum, + RefType: "tag", + Repo: apiRepo, + Sender: apiPusher, + }); err != nil { + return fmt.Errorf("PrepareWebhooks: %v", err) + } } commit, err := gitRepo.GetTagCommit(rel.TagName) if err != nil {