SyncMirrors checks and syncs mirrors. TODO: sync more mirrors at same time.
()
| 384 | // SyncMirrors checks and syncs mirrors. |
| 385 | // TODO: sync more mirrors at same time. |
| 386 | func SyncMirrors() { |
| 387 | ctx := context.Background() |
| 388 | |
| 389 | // Start listening on new sync requests. |
| 390 | for repoID := range MirrorQueue.Queue() { |
| 391 | log.Trace("SyncMirrors [repo_id: %s]", repoID) |
| 392 | MirrorQueue.Remove(repoID) |
| 393 | |
| 394 | m, err := GetMirrorByRepoID(com.StrTo(repoID).MustInt64()) |
| 395 | if err != nil { |
| 396 | log.Error("GetMirrorByRepoID [%v]: %v", repoID, err) |
| 397 | continue |
| 398 | } |
| 399 | |
| 400 | results, ok := m.runSync() |
| 401 | if !ok { |
| 402 | continue |
| 403 | } |
| 404 | |
| 405 | m.ScheduleNextSync() |
| 406 | if err = UpdateMirror(m); err != nil { |
| 407 | log.Error("UpdateMirror [%d]: %v", m.RepoID, err) |
| 408 | continue |
| 409 | } |
| 410 | |
| 411 | // TODO: |
| 412 | // - Create "Mirror Sync" webhook event |
| 413 | // - Create mirror sync (create, push and delete) events and trigger the "mirror sync" webhooks |
| 414 | |
| 415 | if len(results) == 0 { |
| 416 | log.Trace("SyncMirrors [repo_id: %d]: no commits fetched", m.RepoID) |
| 417 | } |
| 418 | |
| 419 | gitRepo, err := git.Open(m.Repo.RepoPath()) |
| 420 | if err != nil { |
| 421 | log.Error("Failed to open repository [repo_id: %d]: %v", m.RepoID, err) |
| 422 | continue |
| 423 | } |
| 424 | |
| 425 | for _, result := range results { |
| 426 | // Discard GitHub pull requests, i.e. refs/pull/* |
| 427 | if strings.HasPrefix(result.refName, "refs/pull/") { |
| 428 | continue |
| 429 | } |
| 430 | |
| 431 | // Delete reference |
| 432 | if result.newCommitID == gitShortEmptyID { |
| 433 | if err = Handle.Actions().MirrorSyncDelete(ctx, m.Repo.MustOwner(), m.Repo, result.refName); err != nil { |
| 434 | log.Error("Failed to create action for mirror sync delete [repo_id: %d]: %v", m.RepoID, err) |
| 435 | } |
| 436 | continue |
| 437 | } |
| 438 | |
| 439 | // New reference |
| 440 | isNewRef := false |
| 441 | if result.oldCommitID == gitShortEmptyID { |
| 442 | if err = Handle.Actions().MirrorSyncCreate(ctx, m.Repo.MustOwner(), m.Repo, result.refName); err != nil { |
| 443 | log.Error("Failed to create action for mirror sync create [repo_id: %d]: %v", m.RepoID, err) |
no test coverage detected