Skip to content

Commit

Permalink
Merge pull request #68 from AlayaCare/report_maxchangesets_error
Browse files Browse the repository at this point in the history
report on max changes error
  • Loading branch information
nzin-alayacare authored Oct 11, 2024
2 parents 43df7da + a54b8e7 commit 0e05b50
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 15 deletions.
9 changes: 4 additions & 5 deletions internal/engine/goliac_reconciliator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ func (r *GoliacReconciliatorImpl) Reconciliate(ctx context.Context, local Goliac
}
}

r.Commit(ctx, dryrun)

return nil
return r.Commit(ctx, dryrun)
}

/*
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion internal/engine/goliac_reconciliator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/reconciliator_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ type ReconciliatorExecutor interface {

Begin(dryrun bool)
Rollback(dryrun bool, err error)
Commit(dryrun bool)
Commit(dryrun bool) error
}
3 changes: 2 additions & 1 deletion internal/engine/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions internal/github_batch_executor.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package internal

import (
"fmt"

"github.com/Alayacare/goliac/internal/engine"
"github.com/sirupsen/logrus"
)

/**
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions internal/goliac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion internal/goliac_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0e05b50

Please sign in to comment.