-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Implement sync push mirror on commit #19411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 26 commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
ac61c17
Update pushmirror model to allow sync-on-push
harryzcy 04d7bd5
Move mirror push functions to a pushmirror service
harryzcy bd55411
Implement sync-on-push via a mirrorNotifier
harryzcy 8f87595
Fix lint-backend issues
harryzcy eb25993
Add migration for PushMirror struct
harryzcy bcabe51
Improve filtering for push mirrors
harryzcy c6a4f30
Refactor to use modules/mirror
harryzcy 2978942
Update tests after moving code to modules/mirror
harryzcy 6c0e721
Include copyright
harryzcy 0836473
Add 'sync on commit' checkbox in web UI
harryzcy 5a8d2b5
Update checkbox input
harryzcy 88f50e9
Store sync on commit setting
harryzcy c9d6393
Use more generic name SyncOnCommit
harryzcy f2de0a0
Trigger mirror on pull requests
harryzcy 4c157e7
Merge remote-tracking branch 'upstream/main' into sync-on-push
harryzcy 0f745bd
Fix table column name
harryzcy 6f66d44
Update sync_on_commit column to false
harryzcy cb517b2
Make use of mirror queue
harryzcy 3b0bd59
Use boolean keyword FALSE
harryzcy 28925ad
Use False only for postgres
harryzcy 436013e
Remove MergePullRequest event
harryzcy 2bd23e6
Sync with mirror on SyncPushCommits event
harryzcy 9cc7738
Remove unused context
harryzcy 207e0fe
Fill input box with value
harryzcy 08ceff9
Fix a func naming glitch in migration
harryzcy 2cc1297
Apply not null and default to SyncOnCommit
harryzcy 92603fc
Apply suggestions from code review
harryzcy 67fa209
Fix minor errors from code review
harryzcy 740408e
Enable sync on commit by default
harryzcy 2210235
Set SyncOnCommit's default to true in models
harryzcy bc1afc1
Update comment to match function name
harryzcy 6320a5d
Merge branch 'main' into sync-on-push
zeripath 6aab911
Merge branch 'main' into sync-on-push
lunny b7331e9
Apply suggestions from code review
harryzcy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"code.gitea.io/gitea/models/repo" | ||
"code.gitea.io/gitea/modules/timeutil" | ||
"xorm.io/xorm" | ||
) | ||
|
||
func addSyncOnCommitColForPushMirror(x *xorm.Engine) error { | ||
type PushMirror struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
RepoID int64 `xorm:"INDEX"` | ||
Repo *repo.Repository `xorm:"-"` | ||
RemoteName string | ||
|
||
SyncOnCommit bool `xorm:"NOT NULL DEFAULT false"` | ||
Interval time.Duration | ||
CreatedUnix timeutil.TimeStamp `xorm:"created"` | ||
LastUpdateUnix timeutil.TimeStamp `xorm:"INDEX last_update"` | ||
LastError string `xorm:"text"` | ||
} | ||
|
||
if err := x.Sync2(new(PushMirror)); err != nil { | ||
return fmt.Errorf("sync2: %v", err) | ||
} | ||
return nil | ||
harryzcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package mirror | ||
|
||
import ( | ||
"code.gitea.io/gitea/modules/graceful" | ||
"code.gitea.io/gitea/modules/log" | ||
"code.gitea.io/gitea/modules/queue" | ||
"code.gitea.io/gitea/modules/setting" | ||
) | ||
|
||
var mirrorQueue queue.UniqueQueue | ||
|
||
// SyncType type of sync request | ||
type SyncType int | ||
|
||
const ( | ||
// PullMirrorType for pull mirrors | ||
PullMirrorType SyncType = iota | ||
// PushMirrorType for push mirrors | ||
PushMirrorType | ||
) | ||
|
||
// SyncRequest for the mirror queue | ||
type SyncRequest struct { | ||
Type SyncType | ||
ReferenceID int64 // RepoID for pull mirror, MirrorID fro push mirror | ||
harryzcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// StartSyncMirrors starts a go routine to sync the mirrors | ||
func StartSyncMirrors(queueHandle func(data ...queue.Data) []queue.Data) { | ||
if !setting.Mirror.Enabled { | ||
return | ||
} | ||
mirrorQueue = queue.CreateUniqueQueue("mirror", queueHandle, new(SyncRequest)) | ||
|
||
go graceful.GetManager().RunWithShutdownFns(mirrorQueue.Run) | ||
} | ||
|
||
// AddPullMirrorToQueue adds repoID to mirror queue | ||
func AddPullMirrorToQueue(repoID int64) { | ||
if !setting.Mirror.Enabled { | ||
return | ||
} | ||
go func() { | ||
err := PushToQueue(PullMirrorType, repoID) | ||
if err != nil { | ||
log.Error("Unable to push sync request for to the queue for pull mirror repo[%d]: Error: %v", repoID, err) | ||
return | ||
} | ||
}() | ||
} | ||
|
||
// AddPushMirrorToQueue adds the push mirror to the queue | ||
func AddPushMirrorToQueue(mirrorID int64) { | ||
if !setting.Mirror.Enabled { | ||
return | ||
} | ||
go func() { | ||
err := PushToQueue(PushMirrorType, mirrorID) | ||
if err != nil { | ||
log.Error("Unable to push sync request to the queue for pull mirror repo[%d]: Error: %v", mirrorID, err) | ||
} | ||
}() | ||
} | ||
|
||
harryzcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// PushToQueue adds the sync request to the queue | ||
func PushToQueue(mirrorType SyncType, referenceID int64) error { | ||
return mirrorQueue.Push(&SyncRequest{ | ||
Type: mirrorType, | ||
ReferenceID: referenceID, | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package mirror | ||
|
||
import ( | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
user_model "code.gitea.io/gitea/models/user" | ||
harryzcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"code.gitea.io/gitea/modules/log" | ||
mirror_module "code.gitea.io/gitea/modules/mirror" | ||
"code.gitea.io/gitea/modules/notification/base" | ||
"code.gitea.io/gitea/modules/repository" | ||
) | ||
|
||
type mirrorNotifier struct { | ||
base.NullNotifier | ||
} | ||
|
||
var _ base.Notifier = &mirrorNotifier{} | ||
|
||
// NewNotifier create a new mirrorNotifier notifier | ||
func NewNotifier() base.Notifier { | ||
return &mirrorNotifier{} | ||
} | ||
|
||
func (m *mirrorNotifier) NotifyPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { | ||
syncPushMirrorWithSyncOnCommit(repo.ID) | ||
} | ||
|
||
func (m *mirrorNotifier) NotifySyncPushCommits(pusher *user_model.User, repo *repo_model.Repository, opts *repository.PushUpdateOptions, commits *repository.PushCommits) { | ||
harryzcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
syncPushMirrorWithSyncOnCommit(repo.ID) | ||
} | ||
|
||
lunny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func syncPushMirrorWithSyncOnCommit(repoID int64) { | ||
syncOnCommit := true | ||
pushMirrors, err := repo_model.GetPushMirrorsByRepoIDWithSyncOnCommit(repoID, syncOnCommit) | ||
harryzcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Error("repo_model.GetPushMirrorsByRepoIDWithSyncOnCommit failed: %v", err) | ||
harryzcy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
|
||
for _, mirror := range pushMirrors { | ||
mirror_module.AddPushMirrorToQueue(mirror.ID) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.