Skip to content

Commit 022d2d8

Browse files
lunnyguillep2k
andauthored
Move push commits events to notification (#8783)
* Move push commits events to notification * Update modules/notification/base/null.go Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com>
1 parent fe7a6d9 commit 022d2d8

File tree

6 files changed

+41
-36
lines changed

6 files changed

+41
-36
lines changed

modules/notification/base/notifier.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ type Notifier interface {
4040
NotifyNewRelease(rel *models.Release)
4141
NotifyUpdateRelease(doer *models.User, rel *models.Release)
4242
NotifyDeleteRelease(doer *models.User, rel *models.Release)
43+
44+
NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits)
4345
}

modules/notification/base/null.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,7 @@ func (*NullNotifier) NotifyCreateRepository(doer *models.User, u *models.User, r
106106
// NotifyMigrateRepository places a place holder function
107107
func (*NullNotifier) NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Repository) {
108108
}
109+
110+
// NotifyPushCommits notifies commits pushed to notifiers
111+
func (*NullNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
112+
}

modules/notification/notification.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,10 @@ func NotifyMigrateRepository(doer *models.User, u *models.User, repo *models.Rep
183183
notifier.NotifyMigrateRepository(doer, u, repo)
184184
}
185185
}
186+
187+
// NotifyPushCommits notifies commits pushed to notifiers
188+
func NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
189+
for _, notifier := range notifiers {
190+
notifier.NotifyPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
191+
}
192+
}

modules/notification/webhook/webhook.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/gitea/models"
99
"code.gitea.io/gitea/modules/log"
1010
"code.gitea.io/gitea/modules/notification/base"
11+
"code.gitea.io/gitea/modules/setting"
1112
api "code.gitea.io/gitea/modules/structs"
1213
webhook_module "code.gitea.io/gitea/modules/webhook"
1314
)
@@ -461,3 +462,25 @@ func (m *webhookNotifier) NotifyIssueChangeMilestone(doer *models.User, issue *m
461462
log.Error("PrepareWebhooks [is_pull: %v]: %v", issue.IsPull, err)
462463
}
463464
}
465+
466+
func (m *webhookNotifier) NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
467+
apiPusher := pusher.APIFormat()
468+
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
469+
if err != nil {
470+
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
471+
return
472+
}
473+
474+
if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
475+
Ref: refName,
476+
Before: oldCommitID,
477+
After: newCommitID,
478+
CompareURL: setting.AppURL + commits.CompareURL,
479+
Commits: apiCommits,
480+
Repo: repo.APIFormat(models.AccessModeOwner),
481+
Pusher: apiPusher,
482+
Sender: apiPusher,
483+
}); err != nil {
484+
log.Error("PrepareWebhooks: %v", err)
485+
}
486+
}

modules/repofiles/action.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/modules/git"
1414
"code.gitea.io/gitea/modules/log"
15+
"code.gitea.io/gitea/modules/notification"
1516
"code.gitea.io/gitea/modules/setting"
1617
api "code.gitea.io/gitea/modules/structs"
1718
"code.gitea.io/gitea/modules/webhook"
@@ -190,22 +191,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
190191
}
191192

192193
if isHookEventPush {
193-
commits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
194-
if err != nil {
195-
return err
196-
}
197-
if err = webhook.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
198-
Ref: opts.RefFullName,
199-
Before: opts.OldCommitID,
200-
After: opts.NewCommitID,
201-
CompareURL: setting.AppURL + opts.Commits.CompareURL,
202-
Commits: commits,
203-
Repo: apiRepo,
204-
Pusher: apiPusher,
205-
Sender: apiPusher,
206-
}); err != nil {
207-
return fmt.Errorf("PrepareWebhooks: %v", err)
208-
}
194+
notification.NotifyPushCommits(pusher, repo, opts.RefFullName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
209195
}
210196

211197
return nil

services/mirror/sync.go

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"fmt"
1010

1111
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/notification"
1213
"code.gitea.io/gitea/modules/setting"
13-
api "code.gitea.io/gitea/modules/structs"
14-
"code.gitea.io/gitea/modules/webhook"
1514
)
1615

1716
func syncAction(opType models.ActionType, repo *models.Repository, refName string, data []byte) error {
@@ -45,25 +44,9 @@ func SyncPushAction(repo *models.Repository, opts SyncPushActionOptions) error {
4544
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
4645
}
4746

48-
apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
49-
if err != nil {
50-
return err
51-
}
52-
5347
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
54-
apiPusher := repo.MustOwner().APIFormat()
55-
if err := webhook.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
56-
Ref: opts.RefName,
57-
Before: opts.OldCommitID,
58-
After: opts.NewCommitID,
59-
CompareURL: setting.AppURL + opts.Commits.CompareURL,
60-
Commits: apiCommits,
61-
Repo: repo.APIFormat(models.AccessModeOwner),
62-
Pusher: apiPusher,
63-
Sender: apiPusher,
64-
}); err != nil {
65-
return fmt.Errorf("PrepareWebhooks: %v", err)
66-
}
48+
49+
notification.NotifyPushCommits(repo.MustOwner(), repo, opts.RefName, opts.OldCommitID, opts.NewCommitID, opts.Commits)
6750

6851
data, err := json.Marshal(opts.Commits)
6952
if err != nil {

0 commit comments

Comments
 (0)