Sync mirror after migration

This commit is contained in:
harryzcy 2023-05-06 16:51:09 -04:00
parent 15d16a5607
commit ce51877f70
No known key found for this signature in database
GPG Key ID: E3C2287691E40E35
1 changed files with 60 additions and 6 deletions

View File

@ -16,6 +16,7 @@ import (
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migration"
"code.gitea.io/gitea/modules/notification"
"code.gitea.io/gitea/modules/process"
"code.gitea.io/gitea/modules/proxy"
@ -23,6 +24,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/services/migrations"
)
// gitShortEmptySha Git short empty SHA
@ -196,8 +198,8 @@ func pruneBrokenReferences(ctx context.Context,
return pruneErr
}
// runSync returns true if sync finished without error.
func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bool) {
// runSyncGit returns true if sync git repos finished without error.
func runSyncGit(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bool) {
repoPath := m.Repo.RepoPath()
wikiPath := m.Repo.WikiPath()
timeout := time.Duration(setting.Git.Timeout.Mirror) * time.Second
@ -372,8 +374,6 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
log.Trace("SyncMirrors [repo: %-v Wiki]: git remote update complete", m.Repo)
}
// TODOsync issues, prs, etc.
log.Trace("SyncMirrors [repo: %-v]: invalidating mirror branch caches...", m.Repo)
branches, _, err := git.GetBranchesByPath(ctx, m.Repo.RepoPath(), 0, 0)
if err != nil {
@ -389,6 +389,53 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
return parseRemoteUpdateOutput(output), true
}
// runSyncMisc runs the sync of Issues, Pull Requests, Reviews, Topics, Releases, Labels, and Milestones.
// It returns true if the sync was successful.
func runSyncMisc(ctx context.Context, m *repo_model.Mirror) bool {
repo := m.GetRepository()
remoteURL, remoteErr := git.GetRemoteURL(ctx, repo.RepoPath(), m.GetRemoteName())
if remoteErr != nil {
log.Error("SyncMirrors [repo: %-v]: GetRemoteAddress Error %v", m.Repo, remoteErr)
return false
}
password, ok := remoteURL.User.Password()
if !ok {
password = ""
}
opts := migration.MigrateOptions{
CloneAddr: repo.OriginalURL,
AuthUsername: remoteURL.User.Username(),
AuthPassword: password,
UID: int(repo.OwnerID),
RepoName: repo.Name,
Mirror: true,
LFS: m.LFS,
LFSEndpoint: m.LFSEndpoint,
GitServiceType: repo.OriginalServiceType,
Wiki: repo.HasWiki() && m.SyncWiki,
Issues: m.SyncIssues,
Milestones: m.SyncMilestones,
Labels: m.SyncLabels,
Releases: m.SyncReleases,
Comments: m.SyncComments,
PullRequests: m.SyncPullRequests,
// Topics
// ReleaseAssets
MigrateToRepoID: repo.ID,
MirrorInterval: m.Interval.String(),
}
_, err := migrations.SyncRepository(ctx, repo.Owner, repo.OwnerName, opts, nil, m.UpdatedUnix.AsTime())
if err != nil {
log.Error("SyncMirrors [repo: %-v]: failed to sync repository: %v", m.Repo, err)
return false
}
return true
}
// SyncPullMirror starts the sync of the pull mirror and schedules the next run.
func SyncPullMirror(ctx context.Context, repoID int64) bool {
log.Trace("SyncMirrors [repo_id: %v]", repoID)
@ -411,8 +458,8 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
ctx, _, finished := process.GetManager().AddContext(ctx, fmt.Sprintf("Syncing Mirror %s/%s", m.Repo.OwnerName, m.Repo.Name))
defer finished()
log.Trace("SyncMirrors [repo: %-v]: Running Sync", m.Repo)
results, ok := runSync(ctx, m)
log.Trace("SyncMirrors [repo: %-v]: Running Sync Git", m.Repo)
results, ok := runSyncGit(ctx, m)
if !ok {
if err = repo_model.TouchMirror(ctx, m); err != nil {
log.Error("SyncMirrors [repo: %-v]: failed to TouchMirror: %v", m.Repo, err)
@ -420,6 +467,13 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
return false
}
if ok := runSyncMisc(ctx, m); !ok {
if err = repo_model.TouchMirror(ctx, m); err != nil {
log.Error("SyncMirrors [repo: %-v]: failed to TouchMirror: %v", m.Repo, err)
}
return false
}
log.Trace("SyncMirrors [repo: %-v]: Scheduling next update", m.Repo)
m.ScheduleNextUpdate()
if err = repo_model.UpdateMirror(ctx, m); err != nil {