diff --git a/acr_controller/service/acr_service.go b/acr_controller/service/acr_service.go index 1bf941b854cd3..e35dfc5766a65 100644 --- a/acr_controller/service/acr_service.go +++ b/acr_controller/service/acr_service.go @@ -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 } diff --git a/acr_controller/service/acr_service_test.go b/acr_controller/service/acr_service_test.go index cac465f51871a..a2554d793693d 100644 --- a/acr_controller/service/acr_service_test.go +++ b/acr_controller/service/acr_service_test.go @@ -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 @@ -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) diff --git a/util/git/client.go b/util/git/client.go index 4122886d894fd..317628579c5a2 100644 --- a/util/git/client.go +++ b/util/git/client.go @@ -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") }