Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle single history item #344

Merged
merged 10 commits into from
Oct 16, 2024
4 changes: 4 additions & 0 deletions acr_controller/service/acr_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ func (c *acrService) getRevisions(ctx context.Context, a *application.Applicatio
// in case if sync is already done, we need to use revision from sync result and previous revision from history
if a.Status.Sync.Status == "Synced" && a.Status.OperationState != nil && a.Status.OperationState.SyncResult != nil {
currentRevision := a.Status.OperationState.SyncResult.Revision
// in case if we have only one history record, we need to return empty previous revision, because it is first sync result
if len(a.Status.History) == 1 {
return currentRevision, ""
}
return currentRevision, a.Status.History[len(a.Status.History)-2].Revision
}

Expand Down
69 changes: 69 additions & 0 deletions acr_controller/service/acr_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,68 @@ spec:
targetRevision: HEAD
`

const syncedAppWithSingleHistory = `
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
annotations:
argocd.argoproj.io/manifest-generate-paths: .
finalizers:
- resources-finalizer.argocd.argoproj.io
labels:
app.kubernetes.io/instance: guestbook
name: guestbook
namespace: codefresh
operation:
initiatedBy:
automated: true
retry:
limit: 5
sync:
prune: true
revision: c732f4d2ef24c7eeb900e9211ff98f90bb646505
syncOptions:
- CreateNamespace=true
spec:
destination:
namespace: guestbook
server: https://kubernetes.default.svc
project: default
source:
path: apps/guestbook
repoURL: https://github.com/pasha-codefresh/precisely-gitsource.git
targetRevision: HEAD
status:
history:
- deployStartedAt: "2024-06-20T19:35:36Z"
deployedAt: "2024-06-20T19:35:44Z"
id: 3
initiatedBy: {}
revision: 792822850fd2f6db63597533e16dfa27e6757dc5
source:
path: apps/guestbook
repoURL: https://github.com/pasha-codefresh/precisely-gitsource.git
targetRevision: HEAD
operationState:
operation:
sync:
prune: true
revision: c732f4d2ef24c7eeb900e9211ff98f90bb646506
syncOptions:
- CreateNamespace=true
phase: Running
startedAt: "2024-06-20T19:47:34Z"
syncResult:
revision: c732f4d2ef24c7eeb900e9211ff98f90bb646505
source:
path: apps/guestbook
repoURL: https://github.com/pasha-codefresh/precisely-gitsource.git
targetRevision: HEAD
sync:
revision: 00d423763fbf56d2ea452de7b26a0ab20590f521
status: Synced
`

const syncedAppWithHistory = `
apiVersion: argoproj.io/v1alpha1
kind: Application
Expand Down Expand Up @@ -175,6 +237,13 @@ func Test_getRevisions(r *testing.T) {
assert.Equal(t, "", previous)
})

r.Run("history list contains only one element, also sync result is here", func(t *testing.T) {
acrService := newTestACRService(&mocks.ApplicationClient{})
current, previous := acrService.getRevisions(context.TODO(), createTestApp(syncedAppWithSingleHistory))
assert.Equal(t, "c732f4d2ef24c7eeb900e9211ff98f90bb646505", current)
assert.Equal(t, "", previous)
})

r.Run("application is synced", func(t *testing.T) {
acrService := newTestACRService(&mocks.ApplicationClient{})
app := createTestApp(syncedAppWithHistory)
Expand Down
2 changes: 1 addition & 1 deletion util/git/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,7 @@ func (m *nativeGitClient) ListRevisions(revision string, targetRevision string)
if revision == "" {
return []string{targetRevision}, nil
}

if !IsCommitSHA(revision) || !IsCommitSHA(targetRevision) {
return nil, fmt.Errorf("invalid revision provided, must be SHA")
}
Expand Down
Loading