diff --git a/internal/engine/goliac_reconciliator.go b/internal/engine/goliac_reconciliator.go index 7b4226e..f06082a 100644 --- a/internal/engine/goliac_reconciliator.go +++ b/internal/engine/goliac_reconciliator.go @@ -66,9 +66,7 @@ func (r *GoliacReconciliatorImpl) Reconciliate(ctx context.Context, local Goliac } } - r.Commit(ctx, dryrun) - - return nil + return r.Commit(ctx, dryrun) } /* @@ -751,9 +749,10 @@ func (r *GoliacReconciliatorImpl) Rollback(ctx context.Context, dryrun bool, err r.executor.Rollback(dryrun, err) } } -func (r *GoliacReconciliatorImpl) Commit(ctx context.Context, dryrun bool) { +func (r *GoliacReconciliatorImpl) Commit(ctx context.Context, dryrun bool) error { logrus.WithFields(map[string]interface{}{"dryrun": dryrun}).Debugf("reconciliation commit") if r.executor != nil { - r.executor.Commit(dryrun) + return r.executor.Commit(dryrun) } + return nil } diff --git a/internal/engine/goliac_reconciliator_test.go b/internal/engine/goliac_reconciliator_test.go index d06d1a0..5e6fbd3 100644 --- a/internal/engine/goliac_reconciliator_test.go +++ b/internal/engine/goliac_reconciliator_test.go @@ -227,7 +227,8 @@ func (r *ReconciliatorListenerRecorder) Begin(dryrun bool) { } func (r *ReconciliatorListenerRecorder) Rollback(dryrun bool, err error) { } -func (r *ReconciliatorListenerRecorder) Commit(dryrun bool) { +func (r *ReconciliatorListenerRecorder) Commit(dryrun bool) error { + return nil } func TestReconciliation(t *testing.T) { diff --git a/internal/engine/reconciliator_executor.go b/internal/engine/reconciliator_executor.go index a609203..6a8a07f 100644 --- a/internal/engine/reconciliator_executor.go +++ b/internal/engine/reconciliator_executor.go @@ -25,5 +25,5 @@ type ReconciliatorExecutor interface { Begin(dryrun bool) Rollback(dryrun bool, err error) - Commit(dryrun bool) + Commit(dryrun bool) error } diff --git a/internal/engine/remote.go b/internal/engine/remote.go index aad45b7..5b33419 100644 --- a/internal/engine/remote.go +++ b/internal/engine/remote.go @@ -1764,5 +1764,6 @@ func (g *GoliacRemoteImpl) Begin(dryrun bool) { } func (g *GoliacRemoteImpl) Rollback(dryrun bool, err error) { } -func (g *GoliacRemoteImpl) Commit(dryrun bool) { +func (g *GoliacRemoteImpl) Commit(dryrun bool) error { + return nil } diff --git a/internal/github_batch_executor.go b/internal/github_batch_executor.go index 3c9223f..8d8342e 100644 --- a/internal/github_batch_executor.go +++ b/internal/github_batch_executor.go @@ -1,8 +1,9 @@ package internal import ( + "fmt" + "github.com/Alayacare/goliac/internal/engine" - "github.com/sirupsen/logrus" ) /** @@ -209,15 +210,15 @@ func (g *GithubBatchExecutor) Begin(dryrun bool) { func (g *GithubBatchExecutor) Rollback(dryrun bool, err error) { g.commands = make([]GithubCommand, 0) } -func (g *GithubBatchExecutor) Commit(dryrun bool) { +func (g *GithubBatchExecutor) Commit(dryrun bool) error { if len(g.commands) > g.maxChangesets { - logrus.Errorf("More than %d changesets to apply (total of %d), this is suspicious. Aborting", g.maxChangesets, len(g.commands)) - return + return fmt.Errorf("More than %d changesets to apply (total of %d), this is suspicious. Aborting", g.maxChangesets, len(g.commands)) } for _, c := range g.commands { c.Apply() } g.commands = make([]GithubCommand, 0) + return nil } type GithubCommandAddUserToOrg struct { diff --git a/internal/goliac.go b/internal/goliac.go index ef1ce97..b98f856 100644 --- a/internal/goliac.go +++ b/internal/goliac.go @@ -226,6 +226,7 @@ func (g *GoliacImpl) applyToGithub(dryrun bool, teamreponame string, branch stri } } else { // we have 1 or more commits to apply + var lastErr error for _, commit := range commits { if err := g.local.CheckoutCommit(commit); err == nil { errs, _ := g.local.LoadAndValidate() @@ -241,9 +242,14 @@ func (g *GoliacImpl) applyToGithub(dryrun bool, teamreponame string, branch stri ctx := context.WithValue(context.TODO(), engine.KeyAuthor, fmt.Sprintf("%s <%s>", commit.Author.Name, commit.Author.Email)) err = reconciliator.Reconciliate(ctx, g.local, g.remote, teamreponame, dryrun, reposToArchive) if err != nil { - return fmt.Errorf("Error when reconciliating: %v", err) + // we keep the last error and continue + // to see if the next commit can be applied without error + // (like if we reached the max changesets, but the next commit will fix it) + lastErr = fmt.Errorf("error when reconciliating: %v", err) + } else { + lastErr = nil } - if !dryrun { + if !dryrun && err == nil { accessToken, err := g.githubClient.GetAccessToken() if err != nil { return err @@ -254,6 +260,9 @@ func (g *GoliacImpl) applyToGithub(dryrun bool, teamreponame string, branch stri logrus.Errorf("Not able to checkout commit %s", commit.Hash.String()) } } + if lastErr != nil { + return lastErr + } } accessToken, err := g.githubClient.GetAccessToken() if err != nil { diff --git a/internal/goliac_server.go b/internal/goliac_server.go index 582ed50..518c159 100644 --- a/internal/goliac_server.go +++ b/internal/goliac_server.go @@ -502,8 +502,10 @@ func (g *GoliacServerImpl) Serve() { } else { now := time.Now() g.lastSyncTime = &now + previousError := g.lastSyncError g.lastSyncError = err - if err != nil { + // log the error only if it's a new one + if err != nil && (previousError == nil || err.Error() != previousError.Error()) { logrus.Error(err) } g.syncInterval = config.Config.ServerApplyInterval