-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mock image-details-by-digest for no-stale-base-images
- Loading branch information
1 parent
1393a15
commit 23cf1ac
Showing
2 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package mocks | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/atomist-skills/go-skill" | ||
"github.com/atomist-skills/go-skill/policy/data" | ||
"github.com/atomist-skills/go-skill/policy/types" | ||
) | ||
|
||
const ( | ||
ImageDetailsQueryName = "image-details-by-digest" | ||
|
||
baseImagesByDigestQuery = ` | ||
query ($context: Context!, $digest: String!) { | ||
baseImagesByDigest(context: $context, digest: $digest) { | ||
images { | ||
digest | ||
tags { | ||
current | ||
name | ||
} | ||
repository { | ||
hostName | ||
repoName | ||
} | ||
} | ||
} | ||
}` | ||
) | ||
|
||
type ( | ||
GqlImagePlatform struct { | ||
Architecture string `json:"architecture"` | ||
Os string `json:"os"` | ||
Variant string `json:"variant,omitempty"` | ||
} | ||
|
||
Repository struct { | ||
HostName string `json:"hostName" edn:"hostName"` | ||
RepoName string `json:"repoName" edn:"repoName"` | ||
} | ||
|
||
Tag struct { | ||
Name string `json:"name" edn:"name"` | ||
Current bool `json:"current" edn:"current"` | ||
} | ||
|
||
BaseImage struct { | ||
Digest string `json:"digest" edn:"digest"` | ||
Repository Repository `json:"repository" edn:"repository"` | ||
Tags []Tag `json:"tags" edn:"tags"` | ||
} | ||
|
||
ImageDetailsByDigest struct { | ||
Digest string `json:"digest" edn:"digest"` | ||
BaseImage *BaseImage `json:"baseImage" edn:"baseImage"` | ||
BaseImageTag *Tag `json:"baseImageTag" edn:"baseImageTag"` | ||
} | ||
|
||
ImageDetailsByDigestResponse struct { | ||
ImageDetailsByDigest *ImageDetailsByDigest `json:"imageDetailsByDigest" edn:"imageDetailsByDigest"` | ||
} | ||
|
||
BaseImagesByDigest struct { | ||
Images []BaseImage `json:"images" edn:"images"` | ||
} | ||
|
||
BaseImagesByDigestResponse struct { | ||
BaseImagesByDigest *BaseImagesByDigest `json:"baseImagesByDigest" edn:"baseImagesByDigest"` | ||
} | ||
) | ||
|
||
func MockBaseImageDetails(ctx context.Context, req skill.RequestContext, sb *types.SBOM) (ImageDetailsByDigestResponse, error) { | ||
ds, err := data.NewSyncGraphqlDataSource(ctx, req) | ||
if err != nil { | ||
return ImageDetailsByDigestResponse{}, err | ||
} | ||
|
||
baseImageDigest := sb.Source.Provenance.BaseImage.Digest | ||
|
||
var queryResponse BaseImagesByDigestResponse | ||
|
||
queryVariables := map[string]interface{}{"digest": baseImageDigest} | ||
_, err = ds.Query(ctx, "base-images-by-digest", baseImagesByDigestQuery, queryVariables, &queryResponse) | ||
if err != nil { | ||
return ImageDetailsByDigestResponse{}, err | ||
} | ||
|
||
if len(queryResponse.BaseImagesByDigest.Images) == 0 { | ||
return ImageDetailsByDigestResponse{}, fmt.Errorf("no base images found for digest %s", baseImageDigest) | ||
} | ||
|
||
baseImage := queryResponse.BaseImagesByDigest.Images[0] | ||
baseImageTag := findBaseImageTag(baseImage, sb.Source.Provenance.BaseImage.Tag) | ||
|
||
mockResponse := ImageDetailsByDigestResponse{ | ||
ImageDetailsByDigest: &ImageDetailsByDigest{ | ||
Digest: sb.Source.Image.Digest, | ||
BaseImage: &baseImage, | ||
BaseImageTag: baseImageTag, | ||
}, | ||
} | ||
|
||
return mockResponse, nil | ||
} | ||
|
||
func findBaseImageTag(baseImage BaseImage, tag string) *Tag { | ||
for _, t := range baseImage.Tags { | ||
if t.Name == tag { | ||
return &t | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters