Skip to content

Commit 8ea121a

Browse files
committed
fix(referrer): preserve backward compat and fix order-dependent tests
- Return nil pagination opts when proto request has no pagination field, preserving backward compatibility (all references returned for old clients). - Merge CLI pagination hint into a single log message without trailing \n. - Make test assertions order-independent since DESC ordering changes reference order non-deterministically when timestamps are equal. Signed-off-by: Miguel Martinez Trivino <miguel@chainloop.dev>
1 parent 898fcd8 commit 8ea121a

3 files changed

Lines changed: 26 additions & 18 deletions

File tree

app/cli/cmd/referrer_discover.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ func newReferrerDiscoverCmd() *cobra.Command {
5757
}
5858

5959
if next := res.NextCursor; next != "" {
60-
logger.Info().Msg("Pagination options \n")
61-
logger.Info().Msgf("--next %s\n", next)
60+
logger.Info().Msgf("To fetch the next page, use: --next %s", next)
6261
}
6362

6463
return nil

app/controlplane/internal/service/referrer.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,13 @@ func (s *ReferrerService) DiscoverPublicShared(ctx context.Context, req *pb.Disc
105105
}, nil
106106
}
107107

108-
const defaultReferrerPageSize = 20
109-
110108
// referrerPaginationOptsFromProto converts the proto pagination request to cursor options.
111-
// It always returns non-nil options, defaulting to limit=20 when no pagination is provided.
109+
// Returns nil when the request has no pagination, preserving backward compatibility (all references returned).
112110
func referrerPaginationOptsFromProto(p *pb.CursorPaginationRequest) (*pagination.CursorOptions, error) {
113-
limit := int(p.GetLimit())
114-
if limit == 0 {
115-
limit = defaultReferrerPageSize
111+
if p == nil {
112+
return nil, nil
116113
}
117-
return pagination.NewCursor(p.GetCursor(), limit)
114+
return pagination.NewCursor(p.GetCursor(), int(p.GetLimit()))
118115
}
119116

120117
func bizReferrerToPb(r *biz.StoredReferrer) *pb.ReferrerItem {

app/controlplane/pkg/biz/referrer_integration_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ func (s *referrerIntegrationTestSuite) TestExtractAndPersistsDependentAttestatio
143143
s.NoError(err)
144144
// It has a commit and an attestation
145145
require.Len(s.T(), got.References, 2)
146-
s.Equal(wantDependentAtt.String(), got.References[0].Digest)
146+
digests := []string{got.References[0].Digest, got.References[1].Digest}
147+
s.Contains(digests, wantDependentAtt.String())
147148
})
148149
}
149150

@@ -266,10 +267,19 @@ func (s *referrerIntegrationTestSuite) TestExtractAndPersists() {
266267
// it has all the references
267268
require.Len(t, got.References, 6)
268269

269-
for i, want := range []*biz.Referrer{
270-
wantReferrerArtifact, wantReferrerContainerImage, wantReferrerCommit, wantReferrerOpenVEX, wantReferrerSarif, wantReferrerSBOM} {
271-
gotR := got.References[i]
272-
s.Equal(want, gotR.Referrer)
270+
wantRefs := []*biz.Referrer{
271+
wantReferrerArtifact, wantReferrerContainerImage, wantReferrerCommit, wantReferrerOpenVEX, wantReferrerSarif, wantReferrerSBOM,
272+
}
273+
for _, want := range wantRefs {
274+
found := false
275+
for _, gotR := range got.References {
276+
if gotR.Referrer.Digest == want.Digest {
277+
s.Equal(want, gotR.Referrer)
278+
found = true
279+
break
280+
}
281+
}
282+
s.True(found, "expected referrer with digest %s not found", want.Digest)
273283
}
274284
s.Equal([]uuid.UUID{s.org1UUID}, got.OrgIDs)
275285
s.Equal([]uuid.UUID{s.workflow1.ID}, got.WorkflowIDs)
@@ -387,10 +397,12 @@ func (s *referrerIntegrationTestSuite) TestExtractAndPersists() {
387397
s.NoError(err)
388398
// it should be referenced by two attestations since it's subject of both
389399
require.Len(t, got.References, 2)
390-
s.Equal("ATTESTATION", got.References[0].Kind)
391-
s.Equal("sha256:2e9bf8e13acd112eff355787b2b72eb8af4ee51fc22c7e65611939f2225e1dc5", got.References[0].Digest)
392-
s.Equal("ATTESTATION", got.References[1].Kind)
393-
s.Equal("sha256:5f4d1baadaf3e439f769f11c7ba0c5f77dad27d00689144d1311b48e65818bbd", got.References[1].Digest)
400+
gotDigests := []string{got.References[0].Digest, got.References[1].Digest}
401+
for _, ref := range got.References {
402+
s.Equal("ATTESTATION", ref.Kind)
403+
}
404+
s.Contains(gotDigests, "sha256:2e9bf8e13acd112eff355787b2b72eb8af4ee51fc22c7e65611939f2225e1dc5")
405+
s.Contains(gotDigests, "sha256:5f4d1baadaf3e439f769f11c7ba0c5f77dad27d00689144d1311b48e65818bbd")
394406
})
395407

396408
s.T().Run("if all associated workflows are private, the referrer is private", func(t *testing.T) {

0 commit comments

Comments
 (0)