Skip to content

Commit a695640

Browse files
committed
Move mirror sync actions to notification
1 parent 7bc8c6b commit a695640

File tree

8 files changed

+135
-81
lines changed

8 files changed

+135
-81
lines changed

modules/git/utils.go

+13
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ func RefEndName(refStr string) string {
8888
return refStr
8989
}
9090

91+
// SplitRefName splits a full refname to reftype and simple refname
92+
func SplitRefName(refStr string) (string, string) {
93+
if strings.HasPrefix(refStr, BranchPrefix) {
94+
return BranchPrefix, refStr[len(BranchPrefix):]
95+
}
96+
97+
if strings.HasPrefix(refStr, TagPrefix) {
98+
return TagPrefix, refStr[len(TagPrefix):]
99+
}
100+
101+
return "", refStr
102+
}
103+
91104
// ParseBool returns the boolean value represented by the string as per git's git_config_bool
92105
// true will be returned for the result if the string is empty, but valid will be false.
93106
// "true", "yes", "on" are all true, true

modules/notification/action/action.go

+50
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package action
66

77
import (
8+
"encoding/json"
89
"fmt"
910
"path"
1011
"strings"
@@ -206,3 +207,52 @@ func (*actionNotifier) NotifyMergePullRequest(pr *models.PullRequest, doer *mode
206207
log.Error("NotifyWatchers [%d]: %v", pr.ID, err)
207208
}
208209
}
210+
211+
func (a *actionNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
212+
data, err := json.Marshal(commits)
213+
if err != nil {
214+
log.Error("json.Marshal: %v", err)
215+
return
216+
}
217+
218+
if err := models.NotifyWatchers(&models.Action{
219+
ActUserID: repo.OwnerID,
220+
ActUser: repo.MustOwner(),
221+
OpType: models.ActionMirrorSyncPush,
222+
RepoID: repo.ID,
223+
Repo: repo,
224+
IsPrivate: repo.IsPrivate,
225+
RefName: refName,
226+
Content: string(data),
227+
}); err != nil {
228+
log.Error("notifyWatchers: %v", err)
229+
}
230+
}
231+
232+
func (a *actionNotifier) NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
233+
if err := models.NotifyWatchers(&models.Action{
234+
ActUserID: repo.OwnerID,
235+
ActUser: repo.MustOwner(),
236+
OpType: models.ActionMirrorSyncCreate,
237+
RepoID: repo.ID,
238+
Repo: repo,
239+
IsPrivate: repo.IsPrivate,
240+
RefName: refFullName,
241+
}); err != nil {
242+
log.Error("notifyWatchers: %v", err)
243+
}
244+
}
245+
246+
func (a *actionNotifier) NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
247+
if err := models.NotifyWatchers(&models.Action{
248+
ActUserID: repo.OwnerID,
249+
ActUser: repo.MustOwner(),
250+
OpType: models.ActionMirrorSyncCreate,
251+
RepoID: repo.ID,
252+
Repo: repo,
253+
IsPrivate: repo.IsPrivate,
254+
RefName: refFullName,
255+
}); err != nil {
256+
log.Error("notifyWatchers: %v", err)
257+
}
258+
}

modules/notification/base/notifier.go

+4
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,8 @@ type Notifier interface {
4747
NotifyPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits)
4848
NotifyCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string)
4949
NotifyDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string)
50+
51+
NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits)
52+
NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string)
53+
NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string)
5054
}

modules/notification/base/null.go

+12
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,15 @@ func (*NullNotifier) NotifyRenameRepository(doer *models.User, repo *models.Repo
130130
// NotifyTransferRepository places a place holder function
131131
func (*NullNotifier) NotifyTransferRepository(doer *models.User, repo *models.Repository, oldOwnerName string) {
132132
}
133+
134+
// NotifyTransferRepository places a place holder function
135+
func (*NullNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
136+
}
137+
138+
// NotifyTransferRepository places a place holder function
139+
func (*NullNotifier) NotifySyncCreateRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
140+
}
141+
142+
// NotifyTransferRepository places a place holder function
143+
func (*NullNotifier) NotifySyncDeleteRef(doer *models.User, repo *models.Repository, refType, refFullName string) {
144+
}

modules/notification/notification.go

+21
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,24 @@ func NotifyDeleteRef(pusher *models.User, repo *models.Repository, refType, refF
227227
notifier.NotifyDeleteRef(pusher, repo, refType, refFullName)
228228
}
229229
}
230+
231+
// NotifySyncPushCommits notifies commits pushed to notifiers
232+
func NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
233+
for _, notifier := range notifiers {
234+
notifier.NotifySyncPushCommits(pusher, repo, refName, oldCommitID, newCommitID, commits)
235+
}
236+
}
237+
238+
// NotifySyncCreateRef notifies branch or tag creation to notifiers
239+
func NotifySyncCreateRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
240+
for _, notifier := range notifiers {
241+
notifier.NotifySyncCreateRef(pusher, repo, refType, refFullName)
242+
}
243+
}
244+
245+
// NotifySyncDeleteRef notifies branch or tag deletion to notifiers
246+
func NotifySyncDeleteRef(pusher *models.User, repo *models.Repository, refType, refFullName string) {
247+
for _, notifier := range notifiers {
248+
notifier.NotifySyncDeleteRef(pusher, repo, refType, refFullName)
249+
}
250+
}

modules/notification/webhook/webhook.go

+22
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,25 @@ func (m *webhookNotifier) NotifyUpdateRelease(doer *models.User, rel *models.Rel
695695
func (m *webhookNotifier) NotifyDeleteRelease(doer *models.User, rel *models.Release) {
696696
sendReleaseHook(doer, rel, api.HookReleaseDeleted)
697697
}
698+
699+
func (m *webhookNotifier) NotifySyncPushCommits(pusher *models.User, repo *models.Repository, refName, oldCommitID, newCommitID string, commits *models.PushCommits) {
700+
apiPusher := pusher.APIFormat()
701+
apiCommits, err := commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
702+
if err != nil {
703+
log.Error("commits.ToAPIPayloadCommits failed: %v", err)
704+
return
705+
}
706+
707+
if err := webhook_module.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
708+
Ref: refName,
709+
Before: oldCommitID,
710+
After: newCommitID,
711+
CompareURL: setting.AppURL + commits.CompareURL,
712+
Commits: apiCommits,
713+
Repo: repo.APIFormat(models.AccessModeOwner),
714+
Pusher: apiPusher,
715+
Sender: apiPusher,
716+
}); err != nil {
717+
log.Error("PrepareWebhooks: %v", err)
718+
}
719+
}

services/mirror/mirror.go

+13-14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"code.gitea.io/gitea/modules/cache"
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/log"
17+
"code.gitea.io/gitea/modules/notification"
1718
"code.gitea.io/gitea/modules/process"
1819
"code.gitea.io/gitea/modules/setting"
1920
"code.gitea.io/gitea/modules/sync"
@@ -336,19 +337,17 @@ func syncMirror(repoID string) {
336337
continue
337338
}
338339

340+
tp, _ := git.SplitRefName(result.refName)
341+
339342
// Create reference
340343
if result.oldCommitID == gitShortEmptySha {
341-
if err = SyncCreateAction(m.Repo, result.refName); err != nil {
342-
log.Error("SyncCreateAction [repo_id: %d]: %v", m.RepoID, err)
343-
}
344+
notification.NotifySyncCreateRef(m.Repo.MustOwner(), m.Repo, tp, result.refName)
344345
continue
345346
}
346347

347348
// Delete reference
348349
if result.newCommitID == gitShortEmptySha {
349-
if err = SyncDeleteAction(m.Repo, result.refName); err != nil {
350-
log.Error("SyncDeleteAction [repo_id: %d]: %v", m.RepoID, err)
351-
}
350+
notification.NotifySyncDeleteRef(m.Repo.MustOwner(), m.Repo, tp, result.refName)
352351
continue
353352
}
354353

@@ -368,15 +367,15 @@ func syncMirror(repoID string) {
368367
log.Error("CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err)
369368
continue
370369
}
371-
if err = SyncPushAction(m.Repo, SyncPushActionOptions{
372-
RefName: result.refName,
373-
OldCommitID: oldCommitID,
374-
NewCommitID: newCommitID,
375-
Commits: models.ListToPushCommits(commits),
376-
}); err != nil {
377-
log.Error("SyncPushAction [repo_id: %d]: %v", m.RepoID, err)
378-
continue
370+
371+
theCommits := models.ListToPushCommits(commits)
372+
if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum {
373+
theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum]
379374
}
375+
376+
theCommits.CompareURL = m.Repo.ComposeCompareURL(oldCommitID, newCommitID)
377+
378+
notification.NotifySyncPushCommits(m.Repo.MustOwner(), m.Repo, result.refName, oldCommitID, newCommitID, models.ListToPushCommits(commits))
380379
}
381380

382381
// Get latest commit date and update to current repository updated time

services/mirror/sync.go

-67
This file was deleted.

0 commit comments

Comments
 (0)