@@ -44,18 +44,26 @@ var (
44
44
exampleManifest , _ = json .Marshal (artifactspec.Manifest {
45
45
MediaType : artifactspec .MediaTypeArtifactManifest ,
46
46
ArtifactType : "example/content" })
47
- exampleManifestDescriptor = artifactspec .Descriptor {
47
+ exampleManifestDescriptor = ocispec .Descriptor {
48
48
MediaType : artifactspec .MediaTypeArtifactManifest ,
49
49
Digest : digest .Digest (digest .FromBytes (exampleManifest )),
50
50
Size : int64 (len (exampleManifest ))}
51
+ exampleManifestDescriptorArtifactspec = artifactspec.Descriptor {
52
+ MediaType : exampleManifestDescriptor .MediaType ,
53
+ Digest : exampleManifestDescriptor .Digest ,
54
+ Size : exampleManifestDescriptor .Size }
51
55
exampleSignatureManifest , _ = json .Marshal (artifactspec.Manifest {
52
56
MediaType : artifactspec .MediaTypeArtifactManifest ,
53
57
ArtifactType : "example/signature" ,
54
- Subject : & exampleManifestDescriptor })
58
+ Subject : & exampleManifestDescriptorArtifactspec })
55
59
exampleSignatureManifestDescriptor = ocispec.Descriptor {
56
60
MediaType : artifactspec .MediaTypeArtifactManifest ,
57
61
Digest : digest .FromBytes (exampleSignatureManifest ),
58
62
Size : int64 (len (exampleSignatureManifest ))}
63
+ exampleSignatureManifestDescriptorArtifactspec = artifactspec.Descriptor {
64
+ MediaType : exampleSignatureManifestDescriptor .MediaType ,
65
+ Digest : exampleSignatureManifestDescriptor .Digest ,
66
+ Size : exampleSignatureManifestDescriptor .Size }
59
67
)
60
68
61
69
func pushBlob (ctx context.Context , mediaType string , blob []byte , target oras.Target ) (desc ocispec.Descriptor , err error ) {
@@ -122,11 +130,33 @@ func TestMain(m *testing.M) {
122
130
w .Header ().Set ("Docker-Content-Digest" , string (exampleSignatureManifestDescriptor .Digest ))
123
131
w .Header ().Set ("Content-Length" , strconv .Itoa (len (exampleSignatureManifest )))
124
132
w .Write (exampleSignatureManifest )
125
- case strings .Contains (p , "/manifests/" + string (exampleManifestDescriptor .Digest )):
133
+ case strings .Contains (p , "/manifests/latest" ) && m == "PUT" :
134
+ w .WriteHeader (http .StatusCreated )
135
+ case strings .Contains (p , "/manifests/" + string (exampleManifestDescriptor .Digest )),
136
+ strings .Contains (p , "/manifests/latest" ) && m == "HEAD" :
126
137
w .Header ().Set ("Content-Type" , artifactspec .MediaTypeArtifactManifest )
127
138
w .Header ().Set ("Docker-Content-Digest" , string (exampleManifestDescriptor .Digest ))
128
139
w .Header ().Set ("Content-Length" , strconv .Itoa (len (exampleManifest )))
129
- w .Write (exampleManifest )
140
+ if m == "GET" {
141
+ w .Write (exampleManifest )
142
+ }
143
+ case strings .Contains (p , "/artifacts/referrers" ):
144
+ w .Header ().Set ("ORAS-Api-Version" , "oras/1.0" )
145
+ q := r .URL .Query ()
146
+ var referrers []artifactspec.Descriptor
147
+ if q .Get ("digest" ) == exampleManifestDescriptor .Digest .String () {
148
+ referrers = []artifactspec.Descriptor {exampleSignatureManifestDescriptorArtifactspec }
149
+ } else if q .Get ("digest" ) == exampleSignatureManifestDescriptor .Digest .String () {
150
+ referrers = []artifactspec.Descriptor {}
151
+ }
152
+ result := struct {
153
+ Referrers []artifactspec.Descriptor `json:"referrers"`
154
+ }{
155
+ Referrers : referrers ,
156
+ }
157
+ if err := json .NewEncoder (w ).Encode (result ); err != nil {
158
+ panic (err )
159
+ }
130
160
case strings .Contains (p , "/manifests/" ) && (m == "HEAD" || m == "GET" ):
131
161
w .Header ().Set ("Content-Type" , ocispec .MediaTypeImageManifest )
132
162
w .Header ().Set ("Docker-Content-Digest" , string (manifestDesc .Digest ))
@@ -311,3 +341,26 @@ func Example_copyArtifactManifestRemoteToLocal() {
311
341
// Output:
312
342
// true
313
343
}
344
+
345
+ // Example_extendedCopyArtifactAndReferrersRemoteToLocal gives an example of
346
+ // copying an artifact along with its referrers from a remote repository to local.
347
+ func Example_extendedCopyArtifactAndReferrersRemoteToLocal () {
348
+ src , err := remote .NewRepository (fmt .Sprintf ("%s/source" , remoteHost ))
349
+ if err != nil {
350
+ panic (err )
351
+ }
352
+ dst := memory .New ()
353
+ ctx := context .Background ()
354
+
355
+ tagName := "latest"
356
+ // ExtendedCopy will copy the artifact tagged by "latest" along with all of its
357
+ // referrers from src to dst.
358
+ desc , err := oras .ExtendedCopy (ctx , src , tagName , dst , tagName , oras .DefaultExtendedCopyOptions )
359
+ if err != nil {
360
+ panic (err )
361
+ }
362
+
363
+ fmt .Println (desc .Digest )
364
+ // Output:
365
+ // sha256:1f3e679d4fc05dca20a699ae5af5fb2b7d481d5694aff929165d1c8b0f4c8598
366
+ }
0 commit comments