Skip to content

Commit

Permalink
Fix SPDX format function (#904)
Browse files Browse the repository at this point in the history
Prior, the function prepends `git+` and appends `.git` regardless of whether
the original url has the prefix or suffix already.

Now, it only prepends & appends if they don't exist.

Signed-off-by: Chuang Wang <chuangw@google.com>
  • Loading branch information
chuangw6 authored Aug 23, 2023
1 parent 1425998 commit 4ea5fa2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions pkg/chains/formats/slsa/attest/attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,17 @@ func convertConfigSource(source *v1beta1.RefSource) slsa.ConfigSource {
}

// supports the SPDX format which is recommended by in-toto
// ref: https://spdx.dev/spdx-specification-21-web-version/#h.49x2ik5
// ref: https://spdx.github.io/spdx-spec/v2-draft/package-information/#773-examples
// ref: https://github.com/in-toto/attestation/blob/849867bee97e33678f61cc6bd5da293097f84c25/spec/field_types.md
func SPDXGit(url, revision string) string {
if !strings.HasPrefix(url, artifacts.GitSchemePrefix) {
url = artifacts.GitSchemePrefix + url
}
if !strings.HasSuffix(url, ".git") {
url = url + ".git"
}
if revision == "" {
return artifacts.GitSchemePrefix + url + ".git"
return url
}
return artifacts.GitSchemePrefix + url + fmt.Sprintf("@%s", revision)
return url + fmt.Sprintf("@%s", revision)
}
2 changes: 1 addition & 1 deletion pkg/chains/formats/slsa/internal/material/material_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestTaskMaterials(t *testing.T) {
},
},
{
URI: artifacts.GitSchemePrefix + "https://github.com/GoogleContainerTools/distroless@my-revision",
URI: artifacts.GitSchemePrefix + "https://github.com/GoogleContainerTools/distroless.git@my-revision",
Digest: common.DigestSet{
"sha1": "50c56a48cfb3a5a80fa36ed91c739bdac8381cbe",
},
Expand Down

0 comments on commit 4ea5fa2

Please sign in to comment.