fix(cache): include SCM identity in source cache key (#8523) · updatecli/updatecli@108339a · GitHub
Skip to content

Commit 108339a

Browse files
authored
fix(cache): include SCM identity in source cache key (#8523)
Two pipelines reusing the same SCMID label (e.g. scmid: source) against different repositories collide in the source cache when their Kind+Spec match, serving the first pipeline's content to the second. Fold the resolved SCM URL and source branch into the cache key. Nil-SCM callers keep the pre-fix hash via json:"scm,omitempty". Credentials live in separate Config fields and are not part of the identity. Fixes: #8522
1 parent 3c36926 commit 108339a

3 files changed

Lines changed: 87 additions & 19 deletions

File tree

‎pkg/core/cache/source.go‎

Lines changed: 23 additions & 11 deletions

‎pkg/core/cache/source_test.go‎

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func TestKey_EmptyKind(t *testing.T) {
164164
}
165165

166166
// Act
167-
key := Key(rc)
167+
key := Key(rc, nil)
168168

169169
// Assert
170170
assert.Equal(t, "", key)
@@ -193,8 +193,8 @@ func TestKey_SameConfigProducesSameKey(t *testing.T) {
193193
}
194194

195195
// Act
196-
key1 := Key(rc)
197-
key2 := Key(rc)
196+
key1 := Key(rc, nil)
197+
key2 := Key(rc, nil)
198198

199199
// Assert
200200
require.NotEmpty(t, key1)
@@ -208,8 +208,8 @@ func TestKey_SameSpecDifferentNamesShareKey(t *testing.T) {
208208
rc1 := resource.ResourceConfig{Kind: "shell", Name: "name-a", Spec: spec}
209209
rc2 := resource.ResourceConfig{Kind: "shell", Name: "name-b", Spec: spec}
210210

211-
key1 := Key(rc1)
212-
key2 := Key(rc2)
211+
key1 := Key(rc1, nil)
212+
key2 := Key(rc2, nil)
213213

214214
require.NotEmpty(t, key1)
215215
assert.Equal(t, key1, key2)
@@ -223,11 +223,58 @@ func TestKey_DifferentSpecsProduceDifferentKeys(t *testing.T) {
223223
rc2 := resource.ResourceConfig{Kind: "shell", Name: "source-b", Spec: shellSpec("echo b")}
224224

225225
// Act
226-
key1 := Key(rc1)
227-
key2 := Key(rc2)
226+
key1 := Key(rc1, nil)
227+
key2 := Key(rc2, nil)
228228

229229
// Assert
230230
require.NotEmpty(t, key1)
231231
require.NotEmpty(t, key2)
232232
assert.NotEqual(t, key1, key2)
233233
}
234+
235+
// TestKey_SameSpecDifferentSCMsProduceDifferentKeys is the regression for
236+
// issue #8522: two pipelines reusing the same SCMID label against different
237+
// repos must not collide in the source cache.
238+
func TestKey_SameSpecDifferentSCMsProduceDifferentKeys(t *testing.T) {
239+
rc := resource.ResourceConfig{Kind: "shell", Name: "source", Spec: shellSpec("cat LICENSE")}
240+
241+
scmA := &SCMIdentity{URL: "https://github.com/example/repo-a.git", Branch: "main"}
242+
scmB := &SCMIdentity{URL: "https://github.com/example/repo-b.git", Branch: "main"}
243+
244+
keyA := Key(rc, scmA)
245+
keyB := Key(rc, scmB)
246+
247+
require.NotEmpty(t, keyA)
248+
require.NotEmpty(t, keyB)
249+
assert.NotEqual(t, keyA, keyB)
250+
}
251+
252+
// TestKey_NilSCMMatchesNilSCMSameSpec guarantees the nil-SCM path still
253+
// dedupes identical Kind+Spec configs (unchanged pre-fix behavior).
254+
func TestKey_NilSCMMatchesNilSCMSameSpec(t *testing.T) {
255+
spec := shellSpec("echo hello")
256+
rc1 := resource.ResourceConfig{Kind: "shell", Name: "name-a", Spec: spec}
257+
rc2 := resource.ResourceConfig{Kind: "shell", Name: "name-b", Spec: spec}
258+
259+
key1 := Key(rc1, nil)
260+
key2 := Key(rc2, nil)
261+
262+
require.NotEmpty(t, key1)
263+
assert.Equal(t, key1, key2)
264+
}
265+
266+
// TestKey_NilVsNonNilSCMProduceDifferentKeys is the third case of the #8522
267+
// regression: identical Kind+Spec with a nil SCM vs a non-nil SCM must not
268+
// collide, since cacheKeyInput.SCM uses json:"scm,omitempty".
269+
func TestKey_NilVsNonNilSCMProduceDifferentKeys(t *testing.T) {
270+
rc := resource.ResourceConfig{Kind: "shell", Name: "source", Spec: shellSpec("cat LICENSE")}
271+
272+
scm := &SCMIdentity{URL: "https://github.com/example/repo-a.git", Branch: "main"}
273+
274+
keyNil := Key(rc, nil)
275+
keySCM := Key(rc, scm)
276+
277+
require.NotEmpty(t, keyNil)
278+
require.NotEmpty(t, keySCM)
279+
assert.NotEqual(t, keyNil, keySCM)
280+
}

‎pkg/core/pipeline/source/main.go‎

Lines changed: 10 additions & 1 deletion

0 commit comments

Comments
 (0)