Skip to content

Commit 727f540

Browse files
fix: handle single history item (#344)
* feat: acr controller crashes when here no history * feat: acr controller crashes when here no history * feat: acr controller crashes when here no history * feat: acr controller crashes when here no history * feat: acr controller crashes when here no history * handle single history item * handle single history item * handle single history item
1 parent 2161a75 commit 727f540

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

acr_controller/service/acr_service.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ func (c *acrService) getRevisions(ctx context.Context, a *application.Applicatio
187187
// in case if sync is already done, we need to use revision from sync result and previous revision from history
188188
if a.Status.Sync.Status == "Synced" && a.Status.OperationState != nil && a.Status.OperationState.SyncResult != nil {
189189
currentRevision := a.Status.OperationState.SyncResult.Revision
190+
// in case if we have only one history record, we need to return empty previous revision, because it is first sync result
191+
if len(a.Status.History) == 1 {
192+
return currentRevision, ""
193+
}
190194
return currentRevision, a.Status.History[len(a.Status.History)-2].Revision
191195
}
192196

acr_controller/service/acr_service_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,68 @@ spec:
6969
targetRevision: HEAD
7070
`
7171

72+
const syncedAppWithSingleHistory = `
73+
apiVersion: argoproj.io/v1alpha1
74+
kind: Application
75+
metadata:
76+
annotations:
77+
argocd.argoproj.io/manifest-generate-paths: .
78+
finalizers:
79+
- resources-finalizer.argocd.argoproj.io
80+
labels:
81+
app.kubernetes.io/instance: guestbook
82+
name: guestbook
83+
namespace: codefresh
84+
operation:
85+
initiatedBy:
86+
automated: true
87+
retry:
88+
limit: 5
89+
sync:
90+
prune: true
91+
revision: c732f4d2ef24c7eeb900e9211ff98f90bb646505
92+
syncOptions:
93+
- CreateNamespace=true
94+
spec:
95+
destination:
96+
namespace: guestbook
97+
server: https://kubernetes.default.svc
98+
project: default
99+
source:
100+
path: apps/guestbook
101+
repoURL: https://github.com/pasha-codefresh/precisely-gitsource.git
102+
targetRevision: HEAD
103+
status:
104+
history:
105+
- deployStartedAt: "2024-06-20T19:35:36Z"
106+
deployedAt: "2024-06-20T19:35:44Z"
107+
id: 3
108+
initiatedBy: {}
109+
revision: 792822850fd2f6db63597533e16dfa27e6757dc5
110+
source:
111+
path: apps/guestbook
112+
repoURL: https://github.com/pasha-codefresh/precisely-gitsource.git
113+
targetRevision: HEAD
114+
operationState:
115+
operation:
116+
sync:
117+
prune: true
118+
revision: c732f4d2ef24c7eeb900e9211ff98f90bb646506
119+
syncOptions:
120+
- CreateNamespace=true
121+
phase: Running
122+
startedAt: "2024-06-20T19:47:34Z"
123+
syncResult:
124+
revision: c732f4d2ef24c7eeb900e9211ff98f90bb646505
125+
source:
126+
path: apps/guestbook
127+
repoURL: https://github.com/pasha-codefresh/precisely-gitsource.git
128+
targetRevision: HEAD
129+
sync:
130+
revision: 00d423763fbf56d2ea452de7b26a0ab20590f521
131+
status: Synced
132+
`
133+
72134
const syncedAppWithHistory = `
73135
apiVersion: argoproj.io/v1alpha1
74136
kind: Application
@@ -175,6 +237,13 @@ func Test_getRevisions(r *testing.T) {
175237
assert.Equal(t, "", previous)
176238
})
177239

240+
r.Run("history list contains only one element, also sync result is here", func(t *testing.T) {
241+
acrService := newTestACRService(&mocks.ApplicationClient{})
242+
current, previous := acrService.getRevisions(context.TODO(), createTestApp(syncedAppWithSingleHistory))
243+
assert.Equal(t, "c732f4d2ef24c7eeb900e9211ff98f90bb646505", current)
244+
assert.Equal(t, "", previous)
245+
})
246+
178247
r.Run("application is synced", func(t *testing.T) {
179248
acrService := newTestACRService(&mocks.ApplicationClient{})
180249
app := createTestApp(syncedAppWithHistory)

util/git/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ func (m *nativeGitClient) ListRevisions(revision string, targetRevision string)
911911
if revision == "" {
912912
return []string{targetRevision}, nil
913913
}
914-
914+
915915
if !IsCommitSHA(revision) || !IsCommitSHA(targetRevision) {
916916
return nil, fmt.Errorf("invalid revision provided, must be SHA")
917917
}

0 commit comments

Comments
 (0)