@@ -57,6 +57,13 @@ type ArtefactInfo struct {
57
57
Digest string
58
58
}
59
59
60
+ type PackageRefs struct {
61
+ Digest string
62
+ Primary string
63
+ Short string
64
+ SemVer []string
65
+ }
66
+
60
67
func (c * Client ) Fetch (ctx context.Context , ref string , mediaTypes ... MediaType ) ([]* ArtefactInfo , error ) {
61
68
imageIndex , indexManifest , image , err := c .GetIndexOrImage (ctx , ref )
62
69
if err != nil {
@@ -220,18 +227,18 @@ func (c *Client) getImage(ctx context.Context, imageIndex ImageIndex, digest Has
220
227
}
221
228
222
229
// based on https://github.com/fluxcd/pkg/blob/2a323d771e17af02dee2ccbbb9b445b78ab048e5/oci/client/push.go
223
- func (c * Client ) PushArtefact (ctx context.Context , destinationRef , sourceDir string , timestamp * time.Time , sourceAttestations ... attestTypes.Statement ) (string , error ) {
230
+ func (c * Client ) PushArtefact (ctx context.Context , destinationRef , sourceDir string , timestamp * time.Time , sourceAttestations ... attestTypes.Statement ) (* PackageRefs , error ) {
224
231
tmpDir , err := os .MkdirTemp ("" , "bpt-oci-artefact-*" )
225
232
if err != nil {
226
- return "" , err
233
+ return nil , err
227
234
}
228
235
defer os .RemoveAll (tmpDir )
229
236
230
237
tmpFile := filepath .Join (tmpDir , "artefact.tgz" )
231
238
232
239
outputFile , err := os .OpenFile (tmpFile , os .O_RDWR | os .O_CREATE | os .O_EXCL , regularFileMode )
233
240
if err != nil {
234
- return "" , err
241
+ return nil , err
235
242
}
236
243
defer outputFile .Close ()
237
244
@@ -240,25 +247,22 @@ func (c *Client) PushArtefact(ctx context.Context, destinationRef, sourceDir str
240
247
output := io .MultiWriter (outputFile , c .hash )
241
248
242
249
if err := c .BuildArtefact (tmpFile , sourceDir , output ); err != nil {
243
- return "" , err
250
+ return nil , err
244
251
}
245
252
246
253
attestLayer , err := c .BuildAttestations (sourceAttestations )
247
254
if err != nil {
248
- return "" , fmt .Errorf ("failed to serialise attestations: %w" , err )
255
+ return nil , fmt .Errorf ("failed to serialise attestations: %w" , err )
249
256
}
250
257
251
258
repo , err := name .NewRepository (destinationRef )
252
259
if err != nil {
253
- return "" , fmt .Errorf ("invalid URL: %w" , err )
260
+ return nil , fmt .Errorf ("invalid URL: %w" , err )
254
261
}
255
262
hash := hex .EncodeToString (c .hash .Sum (nil ))
256
263
tag := repo .Tag (manifestTypes .ConfigImageTagPrefix + hash )
257
-
258
- tagAliases := append (
259
- SemVerTagsFromAttestations (ctx , tag , sourceAttestations ... ),
260
- tag .Context ().Tag (manifestTypes .ConfigImageTagPrefix + hash [:7 ]),
261
- )
264
+ shortTag := tag .Context ().Tag (manifestTypes .ConfigImageTagPrefix + hash [:7 ])
265
+ semVerTags := SemVerTagsFromAttestations (ctx , tag , sourceAttestations ... )
262
266
263
267
if timestamp == nil {
264
268
timestamp = new (time.Time )
@@ -295,12 +299,12 @@ func (c *Client) PushArtefact(ctx context.Context, destinationRef, sourceDir str
295
299
tarball .WithCompressedCaching ,
296
300
)
297
301
if err != nil {
298
- return "" , fmt .Errorf ("creating artefact content layer failed: %w" , err )
302
+ return nil , fmt .Errorf ("creating artefact content layer failed: %w" , err )
299
303
}
300
304
301
305
config , err = mutate .Append (config , mutate.Addendum {Layer : configLayer })
302
306
if err != nil {
303
- return "" , fmt .Errorf ("appeding content to artifact failed: %w" , err )
307
+ return nil , fmt .Errorf ("appeding content to artifact failed: %w" , err )
304
308
}
305
309
306
310
index = mutate .AppendManifests (index ,
@@ -315,7 +319,7 @@ func (c *Client) PushArtefact(ctx context.Context, destinationRef, sourceDir str
315
319
316
320
summary , err := (attestTypes .Statements )(sourceAttestations ).MarshalSummaryAnnotation ()
317
321
if err != nil {
318
- return "" , err
322
+ return nil , err
319
323
}
320
324
attestAnnotations [AttestationsSummaryAnnotation ] = summary
321
325
@@ -329,7 +333,7 @@ func (c *Client) PushArtefact(ctx context.Context, destinationRef, sourceDir str
329
333
330
334
attest , err = mutate .Append (attest , mutate.Addendum {Layer : attestLayer })
331
335
if err != nil {
332
- return "" , fmt .Errorf ("appeding attestations to artifact failed: %w" , err )
336
+ return nil , fmt .Errorf ("appeding attestations to artifact failed: %w" , err )
333
337
}
334
338
335
339
index = mutate .AppendManifests (index ,
@@ -342,22 +346,33 @@ func (c *Client) PushArtefact(ctx context.Context, destinationRef, sourceDir str
342
346
343
347
digest , err := index .Digest ()
344
348
if err != nil {
345
- return "" , fmt .Errorf ("parsing index digest failed: %w" , err )
349
+ return nil , fmt .Errorf ("parsing index digest failed: %w" , err )
346
350
}
347
351
348
352
if err := remote .WriteIndex (tag , index , c .remoteWithContext (ctx )... ); err != nil {
349
- return "" , fmt .Errorf ("pushing index failed: %w" , err )
353
+ return nil , fmt .Errorf ("pushing index failed: %w" , err )
354
+ }
355
+
356
+ refs := & PackageRefs {
357
+ Digest : digest .String (),
358
+ Primary : tag .String (),
359
+ Short : shortTag .String (),
360
+ SemVer : make ([]string , len (semVerTags )),
350
361
}
351
362
352
- for i := range tagAliases {
353
- if err := remote .Tag (tagAliases [i ], index , c .remoteWithContext (ctx )... ); err != nil {
354
- return "" , fmt .Errorf ("adding alias tagging failed: %w" , err )
363
+ for i , tagAlias := range append (semVerTags , shortTag ) {
364
+ if err := remote .Tag (tagAlias , index , c .remoteWithContext (ctx )... ); err != nil {
365
+ return nil , fmt .Errorf ("adding alias tagging failed: %w" , err )
366
+ }
367
+ if i < len (semVerTags ) {
368
+ refs .SemVer [i ] = tagAlias .String () + "@" + digest .String ()
355
369
}
356
370
}
357
- // TODO: reteurn tag and all of its aliases
358
- return tagAliases [0 ].String () + "@" + digest .String (), err
371
+ return refs , nil
359
372
}
360
373
374
+ func (p * PackageRefs ) String () string { return p .Short + "@" + p .Digest }
375
+
361
376
func SemVerTagsFromAttestations (ctx context.Context , tag name.Tag , sourceAttestations ... attestTypes.Statement ) []name.Tag {
362
377
statements := attestTypes .FilterByPredicateType (manifest .ManifestDirPredicateType , sourceAttestations )
363
378
if len (statements ) != 1 {
0 commit comments