Skip to content

Commit

Permalink
fix: validate if revision already exist before recalculation (#340)
Browse files Browse the repository at this point in the history
  • Loading branch information
pasha-codefresh authored Sep 30, 2024
1 parent c7f9d1b commit 9958541
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
12 changes: 7 additions & 5 deletions acr_controller/service/acr_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"context"
"encoding/json"
"errors"
"sync"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -41,11 +42,12 @@ func getChangeRevisionFromRevisions(revisions []string) string {
}

func getChangeRevision(app *application.Application) string {
if changeRevision := getChangeRevisionFromRevisions(app.Operation.Sync.ChangeRevisions); changeRevision != "" {
return changeRevision
}
if app.Status.OperationState != nil && app.Status.OperationState.Operation.Sync != nil {
if changeRevision := getChangeRevisionFromRevisions(app.Status.OperationState.Operation.Sync.ChangeRevisions); changeRevision != "" {
changeRevision := app.Status.OperationState.Operation.Sync.ChangeRevision
if changeRevision != "" {
return changeRevision
}
if changeRevision = getChangeRevisionFromRevisions(app.Status.OperationState.Operation.Sync.ChangeRevisions); changeRevision != "" {
return changeRevision
}
}
Expand All @@ -67,7 +69,7 @@ func (c *acrService) ChangeRevision(ctx context.Context, a *application.Applicat

if getChangeRevision(app) != "" {
log.Infof("Change revision already calculated for application %s", app.Name)
return nil
return errors.New("change revision already calculated")
}

revision, err := c.calculateRevision(ctx, app)
Expand Down
20 changes: 20 additions & 0 deletions acr_controller/service/acr_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,24 @@ func Test_ChangeRevision(r *testing.T) {

assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision)
})

r.Run("Change revision already exists", func(t *testing.T) {
client := &mocks.ApplicationClient{}
client.On("GetChangeRevision", mock.Anything, mock.Anything).Return(&appclient.ChangeRevisionResponse{
Revision: pointer.String("new-revision"),
}, nil)
acrService := newTestACRService(client)
app := createTestApp(syncedAppWithHistory)

err := acrService.ChangeRevision(context.TODO(), app)
require.NoError(t, err)

app, err = acrService.applicationClientset.ArgoprojV1alpha1().Applications(app.Namespace).Get(context.TODO(), app.Name, metav1.GetOptions{})
require.NoError(t, err)

assert.Equal(t, "new-revision", app.Status.OperationState.Operation.Sync.ChangeRevision)

err = acrService.ChangeRevision(context.TODO(), app)
require.Error(t, err, "change revision already calculated")
})
}
2 changes: 1 addition & 1 deletion changelog/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
### Features
- feat: monorepo controller v1.0.0
- fix: change revision controller should verify that revision already exists

0 comments on commit 9958541

Please sign in to comment.