From 76dc0b559e073eca268700cc5799c1f00ffdf466 Mon Sep 17 00:00:00 2001 From: gal-legit <99600389+gal-legit@users.noreply.github.com> Date: Mon, 14 Nov 2022 19:49:34 +0200 Subject: [PATCH] feat: output checksums to artifacts info (#3548) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Following #3540, output artifacts' checksums to the artifact info. This addition makes it easier to consume the checksums, especially when running from e.g. GitHub Actions. New tests: 1. Add a check for the checksum in the extra field. 2. Add a test for every checksum algorithm (to see that it doesn't break for any algo's output). 3. Add a case of a binary and an extra file (to see that the logic doesn't break when there's a mix). p.s. While working on that, I noticed that the convention for extra fields is actually to use UpperCamelCase rather than lower. I was mistaken because I looked at the subfields of the "DockerConfig" extra field. I think it's a good idea to fix it quickly, before the next release rolls and it becomes a compatibility issue. I took the liberty to fix it here as an extra commit. Please let me know if you want it to be in a separate PR. --- Tests: ``` go test • refreshing checksums file=binary_bar_checksums.txt • refreshing checksums file=binary_bar_checksums.txt • refreshing checksums file=binary_bar_checksums.txt PASS ok github.com/goreleaser/goreleaser/internal/pipe/checksums 0.184s ``` Co-authored-by: Carlos Alexandro Becker --- internal/pipe/checksums/checksums.go | 18 ++++++++++++++---- internal/pipe/checksums/checksums_test.go | 13 +++++++++++++ internal/pipe/docker/docker.go | 2 +- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/internal/pipe/checksums/checksums.go b/internal/pipe/checksums/checksums.go index b57837667d4..4ec6f8a5745 100644 --- a/internal/pipe/checksums/checksums.go +++ b/internal/pipe/checksums/checksums.go @@ -19,6 +19,10 @@ import ( "github.com/goreleaser/goreleaser/pkg/context" ) +const ( + artifactChecksumExtra = "Checksum" +) + var ( errNoArtifacts = errors.New("there are no artifacts to sign") lock sync.Mutex @@ -137,11 +141,17 @@ func refresh(ctx *context.Context, filepath string) error { return err } -func checksums(algorithm string, artifact *artifact.Artifact) (string, error) { - log.WithField("file", artifact.Name).Debug("checksumming") - sha, err := artifact.Checksum(algorithm) +func checksums(algorithm string, a *artifact.Artifact) (string, error) { + log.WithField("file", a.Name).Debug("checksumming") + sha, err := a.Checksum(algorithm) if err != nil { return "", err } - return fmt.Sprintf("%v %v\n", sha, artifact.Name), nil + + if a.Extra == nil { + a.Extra = make(artifact.Extras) + } + a.Extra[artifactChecksumExtra] = fmt.Sprintf("%s:%s", algorithm, sha) + + return fmt.Sprintf("%v %v\n", sha, a.Name), nil } diff --git a/internal/pipe/checksums/checksums_test.go b/internal/pipe/checksums/checksums_test.go index 6568672ee43..fde1baa0676 100644 --- a/internal/pipe/checksums/checksums_test.go +++ b/internal/pipe/checksums/checksums_test.go @@ -344,6 +344,19 @@ func TestPipeCheckSumsWithExtraFiles(t *testing.T) { require.Contains(t, string(bts), "3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 "+want) } } + + _ = ctx.Artifacts.Visit(func(a *artifact.Artifact) error { + if a.Path != file { + return nil + } + if len(tt.ids) > 0 { + return nil + } + checkSum, err := artifact.Extra[string](*a, artifactChecksumExtra) + require.Nil(t, err) + require.NotEmptyf(t, checkSum, "failed: %v", a.Path) + return nil + }) }) } } diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go index 108bc8ba3a8..82ca2ce20df 100644 --- a/internal/pipe/docker/docker.go +++ b/internal/pipe/docker/docker.go @@ -20,7 +20,7 @@ import ( const ( dockerConfigExtra = "DockerConfig" - dockerDigestExtra = "digest" + dockerDigestExtra = "Digest" useBuildx = "buildx" useDocker = "docker"