Skip to content

Commit 8edf3d5

Browse files
lunnyzeripath
authored andcommitted
Move sync mirror actions to mirror service package (#8518)
* move sync mirror actions to mirror service * fix go.mod * fix lint * fix lint
1 parent 1d9a1a0 commit 8edf3d5

File tree

3 files changed

+93
-80
lines changed

3 files changed

+93
-80
lines changed

models/action.go

-74
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package models
77

88
import (
9-
"encoding/json"
109
"fmt"
1110
"html"
1211
"path"
@@ -610,79 +609,6 @@ func MergePullRequestAction(actUser *User, repo *Repository, pull *Issue) error
610609
return mergePullRequestAction(x, actUser, repo, pull)
611610
}
612611

613-
func mirrorSyncAction(e Engine, opType ActionType, repo *Repository, refName string, data []byte) error {
614-
if err := notifyWatchers(e, &Action{
615-
ActUserID: repo.OwnerID,
616-
ActUser: repo.MustOwner(),
617-
OpType: opType,
618-
RepoID: repo.ID,
619-
Repo: repo,
620-
IsPrivate: repo.IsPrivate,
621-
RefName: refName,
622-
Content: string(data),
623-
}); err != nil {
624-
return fmt.Errorf("notifyWatchers: %v", err)
625-
}
626-
627-
defer func() {
628-
go HookQueue.Add(repo.ID)
629-
}()
630-
631-
return nil
632-
}
633-
634-
// MirrorSyncPushActionOptions mirror synchronization action options.
635-
type MirrorSyncPushActionOptions struct {
636-
RefName string
637-
OldCommitID string
638-
NewCommitID string
639-
Commits *PushCommits
640-
}
641-
642-
// MirrorSyncPushAction adds new action for mirror synchronization of pushed commits.
643-
func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) error {
644-
if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
645-
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
646-
}
647-
648-
apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
649-
if err != nil {
650-
return err
651-
}
652-
653-
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
654-
apiPusher := repo.MustOwner().APIFormat()
655-
if err := PrepareWebhooks(repo, HookEventPush, &api.PushPayload{
656-
Ref: opts.RefName,
657-
Before: opts.OldCommitID,
658-
After: opts.NewCommitID,
659-
CompareURL: setting.AppURL + opts.Commits.CompareURL,
660-
Commits: apiCommits,
661-
Repo: repo.APIFormat(AccessModeOwner),
662-
Pusher: apiPusher,
663-
Sender: apiPusher,
664-
}); err != nil {
665-
return fmt.Errorf("PrepareWebhooks: %v", err)
666-
}
667-
668-
data, err := json.Marshal(opts.Commits)
669-
if err != nil {
670-
return err
671-
}
672-
673-
return mirrorSyncAction(x, ActionMirrorSyncPush, repo, opts.RefName, data)
674-
}
675-
676-
// MirrorSyncCreateAction adds new action for mirror synchronization of new reference.
677-
func MirrorSyncCreateAction(repo *Repository, refName string) error {
678-
return mirrorSyncAction(x, ActionMirrorSyncCreate, repo, refName, nil)
679-
}
680-
681-
// MirrorSyncDeleteAction adds new action for mirror synchronization of delete reference.
682-
func MirrorSyncDeleteAction(repo *Repository, refName string) error {
683-
return mirrorSyncAction(x, ActionMirrorSyncDelete, repo, refName, nil)
684-
}
685-
686612
// GetFeedsOptions options for retrieving feeds
687613
type GetFeedsOptions struct {
688614
RequestedUser *User

services/mirror/mirror.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,16 @@ func SyncMirrors() {
329329

330330
// Create reference
331331
if result.oldCommitID == gitShortEmptySha {
332-
if err = models.MirrorSyncCreateAction(m.Repo, result.refName); err != nil {
333-
log.Error("MirrorSyncCreateAction [repo_id: %d]: %v", m.RepoID, err)
332+
if err = SyncCreateAction(m.Repo, result.refName); err != nil {
333+
log.Error("SyncCreateAction [repo_id: %d]: %v", m.RepoID, err)
334334
}
335335
continue
336336
}
337337

338338
// Delete reference
339339
if result.newCommitID == gitShortEmptySha {
340-
if err = models.MirrorSyncDeleteAction(m.Repo, result.refName); err != nil {
341-
log.Error("MirrorSyncDeleteAction [repo_id: %d]: %v", m.RepoID, err)
340+
if err = SyncDeleteAction(m.Repo, result.refName); err != nil {
341+
log.Error("SyncDeleteAction [repo_id: %d]: %v", m.RepoID, err)
342342
}
343343
continue
344344
}
@@ -359,13 +359,13 @@ func SyncMirrors() {
359359
log.Error("CommitsBetweenIDs [repo_id: %d, new_commit_id: %s, old_commit_id: %s]: %v", m.RepoID, newCommitID, oldCommitID, err)
360360
continue
361361
}
362-
if err = models.MirrorSyncPushAction(m.Repo, models.MirrorSyncPushActionOptions{
362+
if err = SyncPushAction(m.Repo, SyncPushActionOptions{
363363
RefName: result.refName,
364364
OldCommitID: oldCommitID,
365365
NewCommitID: newCommitID,
366366
Commits: models.ListToPushCommits(commits),
367367
}); err != nil {
368-
log.Error("MirrorSyncPushAction [repo_id: %d]: %v", m.RepoID, err)
368+
log.Error("SyncPushAction [repo_id: %d]: %v", m.RepoID, err)
369369
continue
370370
}
371371
}

services/mirror/sync.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// Copyright 2019 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package mirror
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
11+
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/setting"
13+
api "code.gitea.io/gitea/modules/structs"
14+
)
15+
16+
func syncAction(opType models.ActionType, repo *models.Repository, refName string, data []byte) error {
17+
if err := models.NotifyWatchers(&models.Action{
18+
ActUserID: repo.OwnerID,
19+
ActUser: repo.MustOwner(),
20+
OpType: opType,
21+
RepoID: repo.ID,
22+
Repo: repo,
23+
IsPrivate: repo.IsPrivate,
24+
RefName: refName,
25+
Content: string(data),
26+
}); err != nil {
27+
return fmt.Errorf("notifyWatchers: %v", err)
28+
}
29+
30+
defer func() {
31+
go models.HookQueue.Add(repo.ID)
32+
}()
33+
34+
return nil
35+
}
36+
37+
// SyncPushActionOptions mirror synchronization action options.
38+
type SyncPushActionOptions struct {
39+
RefName string
40+
OldCommitID string
41+
NewCommitID string
42+
Commits *models.PushCommits
43+
}
44+
45+
// SyncPushAction adds new action for mirror synchronization of pushed commits.
46+
func SyncPushAction(repo *models.Repository, opts SyncPushActionOptions) error {
47+
if len(opts.Commits.Commits) > setting.UI.FeedMaxCommitNum {
48+
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
49+
}
50+
51+
apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
52+
if err != nil {
53+
return err
54+
}
55+
56+
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
57+
apiPusher := repo.MustOwner().APIFormat()
58+
if err := models.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
59+
Ref: opts.RefName,
60+
Before: opts.OldCommitID,
61+
After: opts.NewCommitID,
62+
CompareURL: setting.AppURL + opts.Commits.CompareURL,
63+
Commits: apiCommits,
64+
Repo: repo.APIFormat(models.AccessModeOwner),
65+
Pusher: apiPusher,
66+
Sender: apiPusher,
67+
}); err != nil {
68+
return fmt.Errorf("PrepareWebhooks: %v", err)
69+
}
70+
71+
data, err := json.Marshal(opts.Commits)
72+
if err != nil {
73+
return err
74+
}
75+
76+
return syncAction(models.ActionMirrorSyncPush, repo, opts.RefName, data)
77+
}
78+
79+
// SyncCreateAction adds new action for mirror synchronization of new reference.
80+
func SyncCreateAction(repo *models.Repository, refName string) error {
81+
return syncAction(models.ActionMirrorSyncCreate, repo, refName, nil)
82+
}
83+
84+
// SyncDeleteAction adds new action for mirror synchronization of delete reference.
85+
func SyncDeleteAction(repo *models.Repository, refName string) error {
86+
return syncAction(models.ActionMirrorSyncDelete, repo, refName, nil)
87+
}

0 commit comments

Comments
 (0)