From 53aafd7ee1014375b05af627805631ebfc0cbf6a Mon Sep 17 00:00:00 2001 From: silverwind Date: Fri, 13 Nov 2026 08:43:13 +0000 Subject: [PATCH] fix(actions): include previous values in `edited` payloads (#40179) Actions `edited` payloads omitted `changes`, so workflows could not see previous values. Issue/PR title and body edits now carry `changes.title`/`changes.body`, base branch changes add `changes.base.ref`/`changes.base.sha` (and `changes.ref` now holds the old branch, not the new one), release edits carry `changes.name`/`changes.body`. _Written by Claude (Opus 5.5)._ --------- Co-authored-by: Claude (Opus 5.5) --- modules/structs/hook.go | 16 +++-- routers/api/v1/repo/pull.go | 2 - routers/web/repo/pull.go | 2 - services/actions/notifier.go | 80 ++++------------------- services/actions/notifier_helper.go | 3 +- services/convert/release.go | 14 ++++ services/feed/notifier.go | 10 +-- services/feed/notifier_test.go | 10 +++ services/mailer/notify.go | 2 +- services/notify/notifier.go | 6 +- services/notify/notify.go | 12 ++-- services/notify/null.go | 6 +- services/pull/pull.go | 13 +++- services/release/release.go | 47 +++++++------ services/release/release_test.go | 4 +- services/webhook/notifier.go | 20 +++--- tests/integration/actions_trigger_test.go | 19 ++++++ tests/integration/repo_webhook_test.go | 16 +++++ 18 files changed, 151 insertions(+), 131 deletions(-) diff --git a/modules/structs/hook.go b/modules/structs/hook.go index 84f492b45c0..7833f779022 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -333,7 +333,8 @@ type ReleasePayload struct { // The action performed on the release (published, updated, deleted) Action HookReleaseAction `json:"action"` // The release that was acted upon - Release *Release `json:"release"` + Release *Release `json:"release"` + Changes *ChangesPayload `json:"changes,omitempty"` // The repository containing the release Repository *Repository `json:"repository"` // The user who performed the action @@ -460,6 +461,11 @@ type ChangesFromPayload struct { From string `json:"from"` } +type ChangesBasePayload struct { + Ref ChangesFromPayload `json:"ref"` + Sha ChangesFromPayload `json:"sha"` +} + type ChangesOwnerPayload struct { From struct { User *User `json:"user,omitempty"` @@ -474,9 +480,11 @@ type ChangesPayload struct { // Changes made to the body/description Body *ChangesFromPayload `json:"body,omitempty"` // Changes made to the reference - Ref *ChangesFromPayload `json:"ref,omitempty"` - // Changes made to the repository name - Name *ChangesFromPayload `json:"name,omitempty"` + Ref *ChangesFromPayload `json:"ref,omitempty"` + Base *ChangesBasePayload `json:"base,omitempty"` + // Changes made to the repository or release name + Name *ChangesFromPayload `json:"name,omitempty"` + TagName *ChangesFromPayload `json:"tag_name,omitempty"` // Changes made to the repository owner Owner *ChangesOwnerPayload `json:"owner,omitempty"` // Changes made to the labels added diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go index d653cf580c3..63a75fdf41a 100644 --- a/routers/api/v1/repo/pull.go +++ b/routers/api/v1/repo/pull.go @@ -38,7 +38,6 @@ import ( "gitea.dev/services/gitdiff" issue_service "gitea.dev/services/issue" "gitea.dev/services/notifications" - notify_service "gitea.dev/services/notify" pull_service "gitea.dev/services/pull" repo_service "gitea.dev/services/repository" ) @@ -745,7 +744,6 @@ func EditPullRequest(ctx *context.APIContext) { ctx.APIErrorInternal(err) return } - notify_service.PullRequestChangeTargetBranch(ctx, ctx.Doer, pr, form.Base) } // update allow edits diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go index 6ab98717b17..9d508677591 100644 --- a/routers/web/repo/pull.go +++ b/routers/web/repo/pull.go @@ -1565,8 +1565,6 @@ func UpdatePullRequestTarget(ctx *context.Context) { } return } - notify_service.PullRequestChangeTargetBranch(ctx, ctx.Doer, pr, targetBranch) - ctx.JSON(http.StatusOK, map[string]any{ "base_branch": pr.BaseBranch, }) diff --git a/services/actions/notifier.go b/services/actions/notifier.go index ae7b6ea59f5..cbfba737615 100644 --- a/services/actions/notifier.go +++ b/services/actions/notifier.go @@ -62,50 +62,16 @@ func (n *actionsNotifier) NewIssue(ctx context.Context, issue *issues_model.Issu // IssueChangeContent notifies change content of issue func (n *actionsNotifier) IssueChangeContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldContent string) { ctx = withMethod(ctx, "IssueChangeContent") - n.notifyIssueChangeWithTitleOrContent(ctx, doer, issue) + notifyIssueEdited(ctx, doer, issue, &api.ChangesPayload{Body: &api.ChangesFromPayload{From: oldContent}}) } func (n *actionsNotifier) IssueChangeTitle(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, oldTitle string) { ctx = withMethod(ctx, "IssueChangeTitle") - n.notifyIssueChangeWithTitleOrContent(ctx, doer, issue) + notifyIssueEdited(ctx, doer, issue, &api.ChangesPayload{Title: &api.ChangesFromPayload{From: oldTitle}}) } -func (n *actionsNotifier) notifyIssueChangeWithTitleOrContent(ctx context.Context, doer *user_model.User, issue *issues_model.Issue) { - var err error - if err = issue.LoadRepo(ctx); err != nil { - log.Error("LoadRepo: %v", err) - return - } - - permission, _ := access_model.GetIndividualUserRepoPermission(ctx, issue.Repo, issue.Poster) - if issue.IsPull { - if err = issue.LoadPullRequest(ctx); err != nil { - log.Error("loadPullRequest: %v", err) - return - } - newNotifyInputFromIssue(issue, webhook_module.HookEventPullRequest). - WithDoer(doer). - WithPayload(&api.PullRequestPayload{ - Action: api.HookIssueEdited, - Index: issue.Index, - PullRequest: convert.ToPayloadPullRequest(ctx, issue.PullRequest, nil), - Repository: convert.ToRepo(ctx, issue.Repo, access_model.Permission{AccessMode: perm_model.AccessModeNone}), - Sender: convert.ToUser(ctx, doer, nil), - }). - WithPullRequest(issue.PullRequest). - Notify(ctx) - return - } - newNotifyInputFromIssue(issue, webhook_module.HookEventIssues). - WithDoer(doer). - WithPayload(&api.IssuePayload{ - Action: api.HookIssueEdited, - Index: issue.Index, - Issue: convert.ToAPIIssue(ctx, doer, issue), - Repository: convert.ToRepo(ctx, issue.Repo, permission), - Sender: convert.ToUser(ctx, doer, nil), - }). - Notify(ctx) +func notifyIssueEdited(ctx context.Context, doer *user_model.User, issue *issues_model.Issue, changes *api.ChangesPayload) { + notifyIssueChange(ctx, doer, issue, util.Iif(issue.IsPull, webhook_module.HookEventPullRequest, webhook_module.HookEventIssues), api.HookIssueEdited, changes) } // IssueChangeStatus notifies close or reopen issue to notifiers @@ -649,14 +615,14 @@ func (n *actionsNotifier) SyncDeleteRef(ctx context.Context, pusher *user_model. n.DeleteRef(ctx, pusher, repo, refFullName) } -func (n *actionsNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) { +func (n *actionsNotifier) NewRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { ctx = withMethod(ctx, "NewRelease") - notifyRelease(ctx, rel.Publisher, rel, api.HookReleasePublished) + notifyRelease(ctx, doer, rel, api.HookReleasePublished, nil) } -func (n *actionsNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { +func (n *actionsNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel, oldRelease *repo_model.Release) { ctx = withMethod(ctx, "UpdateRelease") - notifyRelease(ctx, doer, rel, api.HookReleaseUpdated) + notifyRelease(ctx, doer, rel, api.HookReleaseUpdated, convert.ToReleaseChanges(rel, oldRelease)) } func (n *actionsNotifier) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { @@ -665,7 +631,7 @@ func (n *actionsNotifier) DeleteRelease(ctx context.Context, doer *user_model.Us return } ctx = withMethod(ctx, "DeleteRelease") - notifyRelease(ctx, doer, rel, api.HookReleaseDeleted) + notifyRelease(ctx, doer, rel, api.HookReleaseDeleted, nil) } func (n *actionsNotifier) PackageCreate(ctx context.Context, doer *user_model.User, pd *packages_model.PackageDescriptor) { @@ -710,35 +676,17 @@ func (n *actionsNotifier) PullRequestSynchronized(ctx context.Context, doer *use Notify(ctx) } -func (n *actionsNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) { +func (n *actionsNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch, oldSHA string) { ctx = withMethod(ctx, "PullRequestChangeTargetBranch") if err := pr.LoadIssue(ctx); err != nil { log.Error("pr.LoadIssue: %v", err) return } - - if err := pr.Issue.LoadRepo(ctx); err != nil { - log.Error("pr.Issue.LoadRepo: %v", err) - return - } - - permission, _ := access_model.GetIndividualUserRepoPermission(ctx, pr.Issue.Repo, pr.Issue.Poster) - newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequest). - WithPayload(&api.PullRequestPayload{ - Action: api.HookIssueEdited, - Index: pr.Issue.Index, - Changes: &api.ChangesPayload{ - Ref: &api.ChangesFromPayload{ - From: oldBranch, - }, - }, - PullRequest: convert.ToPayloadPullRequest(ctx, pr, nil), - Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission), - Sender: convert.ToUser(ctx, doer, nil), - }). - WithPullRequest(pr). - Notify(ctx) + notifyIssueEdited(ctx, doer, pr.Issue, &api.ChangesPayload{ + Ref: &api.ChangesFromPayload{From: oldBranch}, + Base: &api.ChangesBasePayload{Ref: api.ChangesFromPayload{From: oldBranch}, Sha: api.ChangesFromPayload{From: oldSHA}}, + }) } func (n *actionsNotifier) NewWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) { diff --git a/services/actions/notifier_helper.go b/services/actions/notifier_helper.go index ab9c67af81d..84fd41f8c01 100644 --- a/services/actions/notifier_helper.go +++ b/services/actions/notifier_helper.go @@ -514,7 +514,7 @@ func newNotifyInputFromIssue(issue *issues_model.Issue, event webhook_module.Hoo return newNotifyInput(issue.Repo, issue.Poster, event) } -func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction) { +func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction, changes *api.ChangesPayload) { if err := rel.LoadAttributes(ctx); err != nil { log.Error("LoadAttributes: %v", err) return @@ -527,6 +527,7 @@ func notifyRelease(ctx context.Context, doer *user_model.User, rel *repo_model.R WithPayload(&api.ReleasePayload{ Action: action, Release: convert.ToAPIRelease(ctx, rel.Repo, rel), + Changes: changes, Repository: convert.ToRepo(ctx, rel.Repo, permission), Sender: convert.ToUser(ctx, doer, nil), }). diff --git a/services/convert/release.go b/services/convert/release.go index cda14e10579..8216a440763 100644 --- a/services/convert/release.go +++ b/services/convert/release.go @@ -32,3 +32,17 @@ func ToAPIRelease(ctx context.Context, repo *repo_model.Repository, r *repo_mode Attachments: ToAPIAttachments(ctx, repo, r.Attachments), } } + +func ToReleaseChanges(rel, oldRelease *repo_model.Release) *api.ChangesPayload { + changes := &api.ChangesPayload{} + if oldRelease.Title != rel.Title { + changes.Name = &api.ChangesFromPayload{From: oldRelease.Title} + } + if oldRelease.Note != rel.Note { + changes.Body = &api.ChangesFromPayload{From: oldRelease.Note} + } + if oldRelease.TagName != rel.TagName { + changes.TagName = &api.ChangesFromPayload{From: oldRelease.TagName} + } + return changes +} diff --git a/services/feed/notifier.go b/services/feed/notifier.go index 2e81090942a..737230e57c5 100644 --- a/services/feed/notifier.go +++ b/services/feed/notifier.go @@ -453,14 +453,14 @@ func (a *actionNotifier) SyncDeleteRef(ctx context.Context, doer *user_model.Use } } -func (a *actionNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) { - if err := rel.LoadAttributes(ctx); err != nil { - log.Error("LoadAttributes: %v", err) +func (a *actionNotifier) NewRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { + if err := rel.LoadRepo(ctx); err != nil { + log.Error("LoadRepo: %v", err) return } if err := NotifyWatchers(ctx, &activities_model.Action{ - ActUserID: rel.PublisherID, - ActUser: rel.Publisher, + ActUserID: doer.ID, + ActUser: doer, OpType: activities_model.ActionPublishRelease, RepoID: rel.RepoID, Repo: rel.Repo, diff --git a/services/feed/notifier_test.go b/services/feed/notifier_test.go index b0f4c40ccea..60d3295209f 100644 --- a/services/feed/notifier_test.go +++ b/services/feed/notifier_test.go @@ -53,6 +53,16 @@ func TestRenameRepoAction(t *testing.T) { unittest.CheckConsistencyFor(t, &activities_model.Action{}) } +func TestNewReleaseActionCreditsDoer(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) + rel := unittest.AssertExistsAndLoadBean(t, &repo_model.Release{ID: 1}) + NewNotifier().NewRelease(t.Context(), doer, rel) + + unittest.AssertExistsAndLoadBean(t, &activities_model.Action{OpType: activities_model.ActionPublishRelease, ActUserID: doer.ID, RepoID: rel.RepoID}) +} + func TestPullRequestReviewActionExcerpts(t *testing.T) { assert.NoError(t, unittest.PrepareTestDatabase()) diff --git a/services/mailer/notify.go b/services/mailer/notify.go index 0ec7ab0142b..8eb0b5f0c75 100644 --- a/services/mailer/notify.go +++ b/services/mailer/notify.go @@ -188,7 +188,7 @@ func (m *mailNotifier) PullReviewDismiss(ctx context.Context, doer *user_model.U } } -func (m *mailNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) { +func (m *mailNotifier) NewRelease(ctx context.Context, _doer *user_model.User, rel *repo_model.Release) { if err := rel.LoadAttributes(ctx); err != nil { log.Error("LoadAttributes: %v", err) return diff --git a/services/notify/notifier.go b/services/notify/notifier.go index ddb8cd6b351..979f64de6e5 100644 --- a/services/notify/notifier.go +++ b/services/notify/notifier.go @@ -50,7 +50,7 @@ type Notifier interface { PullRequestSynchronized(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, before, after string) PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User) PullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, mentions []*user_model.User) - PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) + PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch, oldSHA string) PullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment) PullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment) @@ -63,8 +63,8 @@ type Notifier interface { EditWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page, comment string) DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, page string) - NewRelease(ctx context.Context, rel *repo_model.Release) - UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) + NewRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) + UpdateRelease(ctx context.Context, doer *user_model.User, rel, oldRelease *repo_model.Release) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) PushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) diff --git a/services/notify/notify.go b/services/notify/notify.go index c79551f966e..adde6278d9e 100644 --- a/services/notify/notify.go +++ b/services/notify/notify.go @@ -182,11 +182,11 @@ func PullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, c } // PullRequestChangeTargetBranch notifies when a pull request's target branch was changed -func PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) { +func PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch, oldSHA string) { defer recoverPanic() for _, notifier := range notifiers { - notifier.PullRequestChangeTargetBranch(ctx, doer, pr, oldBranch) + notifier.PullRequestChangeTargetBranch(ctx, doer, pr, oldBranch, oldSHA) } } @@ -235,7 +235,7 @@ func DeleteComment(ctx context.Context, doer *user_model.User, c *issues_model.C } // NewRelease notifies new release to notifiers -func NewRelease(ctx context.Context, rel *repo_model.Release) { +func NewRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { defer recoverPanic() if err := rel.LoadAttributes(ctx); err != nil { @@ -243,16 +243,16 @@ func NewRelease(ctx context.Context, rel *repo_model.Release) { return } for _, notifier := range notifiers { - notifier.NewRelease(ctx, rel) + notifier.NewRelease(ctx, doer, rel) } } // UpdateRelease notifies update release to notifiers -func UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { +func UpdateRelease(ctx context.Context, doer *user_model.User, rel, oldRelease *repo_model.Release) { defer recoverPanic() for _, notifier := range notifiers { - notifier.UpdateRelease(ctx, doer, rel) + notifier.UpdateRelease(ctx, doer, rel, oldRelease) } } diff --git a/services/notify/null.go b/services/notify/null.go index 7704202ef0b..b7b4a7d7d33 100644 --- a/services/notify/null.go +++ b/services/notify/null.go @@ -68,7 +68,7 @@ func (*NullNotifier) PullRequestSynchronized(ctx context.Context, doer *user_mod } // PullRequestChangeTargetBranch places a place holder function -func (*NullNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) { +func (*NullNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch, oldSHA string) { } // PullRequestPushCommits notifies when push commits to pull request's head branch @@ -100,11 +100,11 @@ func (*NullNotifier) DeleteWikiPage(ctx context.Context, doer *user_model.User, } // NewRelease places a place holder function -func (*NullNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) { +func (*NullNotifier) NewRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { } // UpdateRelease places a place holder function -func (*NullNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { +func (*NullNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel, oldRelease *repo_model.Release) { } // DeleteRelease places a place holder function diff --git a/services/pull/pull.go b/services/pull/pull.go index f7aed4f5f30..7b007d117a9 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -301,6 +301,11 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer return err } + oldSHAs, err := git_model.FindBranchesByRepoAndBranchName(ctx, map[int64]string{pr.BaseRepoID: pr.BaseBranch}) + if err != nil { + return err + } + // Set new target branch oldBranch := pr.BaseBranch pr.BaseBranch = targetBranch @@ -317,7 +322,7 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer } // add first push codes comment - return db.WithTx(ctx, func(ctx context.Context) error { + err = db.WithTx(ctx, func(ctx context.Context) error { // The UPDATE acquires the transaction lock, if the UPDATE succeeds, it should have updated one row (the "base_branch" is changed) // If no row is updated, it means the PR has been merged or closed in the meantime updated, err := pr.UpdateColsIfNotMerged(ctx, "merge_base", "status", "conflicted_files", "changed_protected_files", "base_branch") @@ -364,6 +369,12 @@ func ChangeTargetBranch(ctx context.Context, pr *issues_model.PullRequest, doer _, _, err = CreatePushPullComment(ctx, doer, pr, git.BranchPrefix+pr.BaseBranch, pr.GetGitHeadRefName(), false) return err }) + if err != nil { + return err + } + releaser() + notify_service.PullRequestChangeTargetBranch(ctx, doer, pr, oldBranch, oldSHAs[pr.BaseRepoID]) + return nil } type TestPullRequestOptions struct { diff --git a/services/release/release.go b/services/release/release.go index e87a99f7496..fb547aec0b2 100644 --- a/services/release/release.go +++ b/services/release/release.go @@ -68,37 +68,36 @@ func (err ErrProtectedTagName) Unwrap() error { return util.ErrPermissionDenied } -func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Release, msg string) (bool, error) { +func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Release, msg string) error { err := rel.LoadAttributes(ctx) if err != nil { - return false, err + return err } err = rel.Repo.MustNotBeArchived() if err != nil { - return false, err + return err } - var created bool // Only actual create when a release is published. if !rel.IsDraft { if !git.IsTagExist(ctx, rel.Repo, rel.TagName) { if err := rel.LoadAttributes(ctx); err != nil { log.Error("LoadAttributes: %v", err) - return false, err + return err } protectedTags, err := git_model.GetProtectedTags(ctx, rel.Repo.ID) if err != nil { - return false, fmt.Errorf("GetProtectedTags: %w", err) + return fmt.Errorf("GetProtectedTags: %w", err) } isAllowed, err := git_model.IsUserAllowedToControlTag(ctx, protectedTags, rel.TagName, rel.PublisherID) if err != nil { - return false, err + return err } if !isAllowed { - return false, ErrProtectedTagName{ + return ErrProtectedTagName{ TagName: rel.TagName, } } @@ -106,27 +105,26 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel target := util.IfZero(rel.Target, rel.Repo.DefaultBranch) commit, err := gitRepo.GetCommit(ctx, target) if err != nil { - return false, err + return err } if len(msg) > 0 { if err = gitRepo.CreateAnnotatedTag(ctx, rel.TagName, msg, commit.ID.String()); err != nil { if strings.Contains(err.Error(), "is not a valid tag name") { - return false, ErrInvalidTagName{ + return ErrInvalidTagName{ TagName: rel.TagName, } } - return false, err + return err } } else if err = gitRepo.CreateTag(ctx, rel.TagName, commit.ID.String()); err != nil { if strings.Contains(err.Error(), "is not a valid tag name") { - return false, ErrInvalidTagName{ + return ErrInvalidTagName{ TagName: rel.TagName, } } - return false, err + return err } - created = true rel.LowerTagName = strings.ToLower(rel.TagName) objectFormat := git.ObjectFormatFromName(rel.Repo.ObjectFormatName) @@ -149,14 +147,14 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel } commit, err := gitRepo.GetTagCommit(ctx, rel.TagName) if err != nil { - return false, fmt.Errorf("GetTagCommit: %w", err) + return fmt.Errorf("GetTagCommit: %w", err) } rel.Sha1 = commit.ID.String() rel.CreatedUnix = timeutil.TimeStamp(commit.Committer.When.Unix()) // dated by its commit, so an old commit does not become the latest release rel.NumCommits, err = git.CommitsCountOfCommit(ctx, rel.Repo, commit.ID.String()) if err != nil { - return false, fmt.Errorf("CommitsCount: %w", err) + return fmt.Errorf("CommitsCount: %w", err) } if rel.PublisherID <= 0 { @@ -168,7 +166,7 @@ func createTag(ctx context.Context, gitRepo *git.Repository, rel *repo_model.Rel } else { rel.CreatedUnix = timeutil.TimeStampNow() } - return created, nil + return nil } // UpdateLatestRelease follows GitHub's make_latest, except that omitting it lets a newly published release become the latest only if it is the newest by date @@ -207,7 +205,7 @@ func CreateRelease(ctx context.Context, gitRepo *git.Repository, rel *repo_model } } - if _, err = createTag(ctx, gitRepo, rel, msg); err != nil { + if err = createTag(ctx, gitRepo, rel, msg); err != nil { return err } @@ -226,7 +224,7 @@ func CreateRelease(ctx context.Context, gitRepo *git.Repository, rel *repo_model } if !rel.IsDraft { - notify_service.NewRelease(ctx, rel) + notify_service.NewRelease(ctx, rel.Publisher, rel) } return nil @@ -280,7 +278,7 @@ func CreateNewTag(ctx context.Context, doer *user_model.User, repo *repo_model.R IsTag: true, } - if _, err = createTag(ctx, gitRepo, rel, msg); err != nil { + if err = createTag(ctx, gitRepo, rel, msg); err != nil { return err } @@ -297,8 +295,7 @@ func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repo if rel.ID == 0 { return errors.New("UpdateRelease only accepts an exist release") } - isTagCreated, err := createTag(ctx, gitRepo, rel, "") - if err != nil { + if err := createTag(ctx, gitRepo, rel, ""); err != nil { return err } rel.LowerTagName = strings.ToLower(rel.TagName) @@ -384,11 +381,11 @@ func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repo } if !rel.IsDraft { - if !isTagCreated && !isConvertedFromTag { - notify_service.UpdateRelease(ctx, doer, rel) + if !isConvertedFromTag && !oldRelease.IsDraft { + notify_service.UpdateRelease(ctx, doer, rel, oldRelease) return nil } - notify_service.NewRelease(ctx, rel) + notify_service.NewRelease(ctx, doer, rel) } return nil } diff --git a/services/release/release_test.go b/services/release/release_test.go index 999b3eb0ff1..a8391b62966 100644 --- a/services/release/release_test.go +++ b/services/release/release_test.go @@ -308,7 +308,7 @@ func TestRelease_createTag(t *testing.T) { IsPrerelease: testCase.isPrerelease, IsTag: false, } - _, err = createTag(t.Context(), gitRepo, release, "") + err = createTag(t.Context(), gitRepo, release, "") assert.NoError(t, err) if !testCase.isDraft && !testCase.isPrerelease { assert.NotEmpty(t, release.CreatedUnix) @@ -321,7 +321,7 @@ func TestRelease_createTag(t *testing.T) { if !testCase.isDraft { release.Note = "Changed note" } - _, err = createTag(t.Context(), gitRepo, release, "") + err = createTag(t.Context(), gitRepo, release, "") assert.NoError(t, err) if testCase.isDraft { assert.Less(t, int64(releaseCreatedUnix), int64(release.CreatedUnix)) diff --git a/services/webhook/notifier.go b/services/webhook/notifier.go index db0f5bb8ec9..e581c8f10c6 100644 --- a/services/webhook/notifier.go +++ b/services/webhook/notifier.go @@ -741,7 +741,7 @@ func (*webhookNotifier) MergePullRequest(ctx context.Context, doer *user_model.U } } -func (m *webhookNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) { +func (m *webhookNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch, oldSHA string) { if err := pr.LoadIssue(ctx); err != nil { log.Error("LoadIssue: %v", err) return @@ -754,9 +754,8 @@ func (m *webhookNotifier) PullRequestChangeTargetBranch(ctx context.Context, doe Action: api.HookIssueEdited, Index: issue.Index, Changes: &api.ChangesPayload{ - Ref: &api.ChangesFromPayload{ - From: oldBranch, - }, + Ref: &api.ChangesFromPayload{From: oldBranch}, + Base: &api.ChangesBasePayload{Ref: api.ChangesFromPayload{From: oldBranch}, Sha: api.ChangesFromPayload{From: oldSHA}}, }, PullRequest: convert.ToPayloadPullRequest(ctx, pr, doer), Repository: convert.ToRepo(ctx, issue.Repo, mode), @@ -887,7 +886,7 @@ func (m *webhookNotifier) DeleteRef(ctx context.Context, pusher *user_model.User } } -func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction) { +func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model.Release, action api.HookReleaseAction, changes *api.ChangesPayload) { if err := rel.LoadAttributes(ctx); err != nil { log.Error("LoadAttributes: %v", err) return @@ -897,6 +896,7 @@ func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model if err := PrepareWebhooks(ctx, EventSource{Repository: rel.Repo}, webhook_module.HookEventRelease, &api.ReleasePayload{ Action: action, Release: convert.ToAPIRelease(ctx, rel.Repo, rel), + Changes: changes, Repository: convert.ToRepo(ctx, rel.Repo, permission), Sender: convert.ToUser(ctx, doer, nil), }); err != nil { @@ -904,16 +904,16 @@ func sendReleaseHook(ctx context.Context, doer *user_model.User, rel *repo_model } } -func (m *webhookNotifier) NewRelease(ctx context.Context, rel *repo_model.Release) { - sendReleaseHook(ctx, rel.Publisher, rel, api.HookReleasePublished) +func (m *webhookNotifier) NewRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { + sendReleaseHook(ctx, doer, rel, api.HookReleasePublished, nil) } -func (m *webhookNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { - sendReleaseHook(ctx, doer, rel, api.HookReleaseUpdated) +func (m *webhookNotifier) UpdateRelease(ctx context.Context, doer *user_model.User, rel, oldRelease *repo_model.Release) { + sendReleaseHook(ctx, doer, rel, api.HookReleaseUpdated, convert.ToReleaseChanges(rel, oldRelease)) } func (m *webhookNotifier) DeleteRelease(ctx context.Context, doer *user_model.User, rel *repo_model.Release) { - sendReleaseHook(ctx, doer, rel, api.HookReleaseDeleted) + sendReleaseHook(ctx, doer, rel, api.HookReleaseDeleted, nil) } func (m *webhookNotifier) SyncPushCommits(ctx context.Context, pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { diff --git a/tests/integration/actions_trigger_test.go b/tests/integration/actions_trigger_test.go index 6d4e8f25064..5c0633d2b53 100644 --- a/tests/integration/actions_trigger_test.go +++ b/tests/integration/actions_trigger_test.go @@ -1098,6 +1098,7 @@ jobs: pullIssue := &issues_model.Issue{ RepoID: repo.ID, Title: "A test PR", + Content: "original", PosterID: user2.ID, Poster: user2, IsPull: true, @@ -1121,9 +1122,19 @@ jobs: checkCommitStatusAndInsertFakeStatus(t, repo, sha) // edited + latestChanges := func() *api.ChangesPayload { + payload, err := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRun{RepoID: repo.ID}, unittest.OrderBy("id DESC")).GetPullRequestEventPayload() + require.NoError(t, err) + return payload.Changes + } err = issue_service.ChangeContent(t.Context(), pullIssue, user2, "test", 0) assert.NoError(t, err) checkCommitStatusAndInsertFakeStatus(t, repo, sha) + assert.Equal(t, &api.ChangesPayload{Body: &api.ChangesFromPayload{From: "original"}}, latestChanges()) + + require.NoError(t, issue_service.ChangeTitle(t.Context(), pullIssue, user2, "A renamed PR")) + checkCommitStatusAndInsertFakeStatus(t, repo, sha) + assert.Equal(t, &api.ChangesPayload{Title: &api.ChangesFromPayload{From: "A test PR"}}, latestChanges()) // closed err = issue_service.CloseIssue(t.Context(), pullIssue, user2, "") @@ -1228,6 +1239,14 @@ jobs: _, err = issue_service.ReviewRequest(t.Context(), pullIssue, user2, nil, user4, false) assert.NoError(t, err) checkCommitStatusAndInsertFakeStatus(t, repo, sha) + + testCreateBranch(t, ctx.Session, repo.OwnerName, repo.Name, "commit/"+addWorkflow.Commit.Parents[0].SHA, "other-base", http.StatusSeeOther) + require.NoError(t, pull_service.ChangeTargetBranch(t.Context(), pullRequest, user2, "other-base")) + checkCommitStatusAndInsertFakeStatus(t, repo, sha) + assert.Equal(t, &api.ChangesPayload{ + Ref: &api.ChangesFromPayload{From: "main"}, + Base: &api.ChangesBasePayload{Ref: api.ChangesFromPayload{From: "main"}, Sha: api.ChangesFromPayload{From: addWorkflow.Commit.SHA}}, + }, latestChanges()) }) } diff --git a/tests/integration/repo_webhook_test.go b/tests/integration/repo_webhook_test.go index 7667965e723..db0c51a2113 100644 --- a/tests/integration/repo_webhook_test.go +++ b/tests/integration/repo_webhook_test.go @@ -348,6 +348,22 @@ func Test_WebhookRelease(t *testing.T) { assert.Equal(t, "v0.0.99", payloads[0].Release.TagName) assert.False(t, payloads[0].Release.IsDraft) assert.False(t, payloads[0].Release.IsPrerelease) + assert.Nil(t, payloads[0].Changes) + + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeAll) + editRelease := func(opts *api.EditReleaseOption) { + MakeRequest(t, NewRequestWithJSON(t, "PATCH", fmt.Sprintf("/api/v1/repos/user2/repo1/releases/%d", payloads[0].Release.ID), opts).AddTokenAuth(token), http.StatusOK) + } + editRelease(&api.EditReleaseOption{Title: "renamed", TagName: "v0.0.100"}) + assert.Len(t, payloads, 2) + assert.Equal(t, api.HookReleaseUpdated, payloads[1].Action) + assert.Equal(t, &api.ChangesPayload{Name: &api.ChangesFromPayload{From: "v0.0.99"}, TagName: &api.ChangesFromPayload{From: "v0.0.99"}}, payloads[1].Changes) + + editRelease(&api.EditReleaseOption{IsDraft: new(true)}) + editRelease(&api.EditReleaseOption{IsDraft: new(false)}) + assert.Len(t, payloads, 3) + assert.Equal(t, api.HookReleasePublished, payloads[2].Action) + assert.Nil(t, payloads[2].Changes) }) }