From 5430f503e04e8ec43bc2f6121a8d3893e8c04a07 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 31 Dec 2022 14:27:55 +1300 Subject: [PATCH] sync/reorder: add error cases, remove debug messaging --- sync.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/sync.go b/sync.go index e3b9d0b..51a0586 100644 --- a/sync.go +++ b/sync.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "log" "sort" "teafolio/gitea" @@ -9,10 +10,10 @@ import ( ) // rearrangeOrder applies rearrangements from the OverrideOrder configuration. -func (this *Application) rearrangeOrder(repos []gitea.Repo) []gitea.Repo { +func (this *Application) rearrangeOrder(repos []gitea.Repo) ([]gitea.Repo, error) { if len(this.cfg.OverrideOrder) == 0 { - return repos // nothing to do + return repos, nil // nothing to do } // Collect pre-existing positions for repos @@ -35,8 +36,6 @@ func (this *Application) rearrangeOrder(repos []gitea.Repo) []gitea.Repo { return insertAt[i].pos < insertAt[j].pos }) - log.Printf("insertAt=%#v\n", insertAt) - // Walking-insertion loop ret := make([]gitea.Repo, 0, len(repos)) nextRepo := 0 @@ -44,7 +43,6 @@ func (this *Application) rearrangeOrder(repos []gitea.Repo) []gitea.Repo { for { if nextOverride < len(insertAt) && insertAt[nextOverride].pos == len(ret) { // We have an override to insert at this position - log.Printf("OVERRIDE insert of %q", insertAt[nextOverride].repoName) ret = append(ret, reposByName[insertAt[nextOverride].repoName]) nextOverride++ @@ -52,12 +50,10 @@ func (this *Application) rearrangeOrder(repos []gitea.Repo) []gitea.Repo { // There are still other repos to insert generally if _, ok := this.cfg.OverrideOrder[repos[nextRepo].Name]; ok { // This repo has an overridden position. Don't insert it generally here - log.Printf("skipping natural insert of %q", repos[nextRepo].Name) nextRepo++ } else { // This repo does not have an overriden position. Here is fine - log.Printf("OK natural insert of %q", repos[nextRepo].Name) ret = append(ret, repos[nextRepo]) nextRepo++ } @@ -70,7 +66,11 @@ func (this *Application) rearrangeOrder(repos []gitea.Repo) []gitea.Repo { } } - return ret + if len(ret) != len(repos) { + return nil, fmt.Errorf("Failed to apply OverrideOrder (got %d of %d repositories)", len(ret), len(repos)) + } + + return ret, nil } func (this *Application) sync(ctx context.Context) (bool, error) { @@ -144,7 +144,12 @@ func (this *Application) sync(ctx context.Context) (bool, error) { sort.Slice(repos, func(i, j int) bool { return repos[i].GiteaCreated.After(repos[j].GiteaCreated) }) - repos = this.rearrangeOrder(repos) + reordered, err := this.rearrangeOrder(repos) + if err != nil { + log.Printf("Reorder failure: %s", err.Error()) + } else { + repos = reordered + } // Commit our changes for the other threads to look at this.reposMut.Lock()