1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-03-11 14:39:28 +02:00

feat: write a dist/metadata.json (#2845)

* feat: write a dist/metadata.json

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: comments

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: lint

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: test

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: lint

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* fix: lint

Signed-off-by: Carlos A Becker <caarlos0@gmail.com>

* test: added one more test
This commit is contained in:
Carlos Alexandro Becker 2022-01-23 16:42:42 -03:00 committed by GitHub
parent 7a2fc167c8
commit aad1e1e3d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 130 additions and 71 deletions

View File

@ -1,28 +0,0 @@
// Package artifacts provides the pipe implementation that creates a artifacts.json file in the dist folder.
package artifacts
import (
"encoding/json"
"os"
"path/filepath"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe implementation.
type Pipe struct{}
func (Pipe) String() string { return "storing artifact list" }
func (Pipe) Skip(ctx *context.Context) bool { return false }
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
bts, err := json.Marshal(ctx.Artifacts.List())
if err != nil {
return err
}
path := filepath.Join(ctx.Config.Dist, "artifacts.json")
log.Log.WithField("file", path).Info("writing")
return os.WriteFile(path, bts, 0o644)
}

View File

@ -1,40 +0,0 @@
package artifacts
import (
"os"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestArtifacts(t *testing.T) {
tmp := t.TempDir()
ctx := context.New(config.Project{
Dist: tmp,
})
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo",
Path: "foo.txt",
Type: artifact.Binary,
Goos: "darwin",
Goarch: "amd64",
Goarm: "7",
Extra: map[string]interface{}{
"foo": "bar",
},
})
require.NoError(t, Pipe{}.Run(ctx))
path := filepath.Join(tmp, "artifacts.json")
golden.RequireEqualJSON(t, golden.RequireReadFile(t, path))
info, err := os.Stat(path)
require.NoError(t, err)
require.Equal(t, "-rw-r--r--", info.Mode().String())
}

View File

@ -0,0 +1,60 @@
// Package metadata provides the pipe implementation that creates a artifacts.json file in the dist folder.
package metadata
import (
"encoding/json"
"os"
"path/filepath"
"time"
"github.com/apex/log"
"github.com/goreleaser/goreleaser/pkg/context"
)
// Pipe implementation.
type Pipe struct{}
func (Pipe) String() string { return "storing release metadata" }
func (Pipe) Skip(ctx *context.Context) bool { return false }
// Run the pipe.
func (Pipe) Run(ctx *context.Context) error {
if err := writeArtifacts(ctx); err != nil {
return err
}
return writeMetadata(ctx)
}
func writeMetadata(ctx *context.Context) error {
return writeJSON(ctx, metadata{
ProjectName: ctx.Config.ProjectName,
Tag: ctx.Git.CurrentTag,
PreviousTag: ctx.Git.PreviousTag,
Version: ctx.Version,
Commit: ctx.Git.Commit,
Date: ctx.Date,
}, "metadata.json")
}
func writeArtifacts(ctx *context.Context) error {
return writeJSON(ctx, ctx.Artifacts.List(), "artifacts.json")
}
func writeJSON(ctx *context.Context, j interface{}, name string) error {
bts, err := json.Marshal(j)
if err != nil {
return err
}
path := filepath.Join(ctx.Config.Dist, name)
log.Log.WithField("file", path).Info("writing")
return os.WriteFile(path, bts, 0o644)
}
type metadata struct {
ProjectName string `json:"project_name"`
Tag string `json:"tag"`
PreviousTag string `json:"previous_tag"`
Version string `json:"version"`
Commit string `json:"commit"`
Date time.Time `json:"date"`
}

View File

@ -0,0 +1,66 @@
package metadata
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/goreleaser/goreleaser/internal/artifact"
"github.com/goreleaser/goreleaser/internal/golden"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/goreleaser/goreleaser/pkg/context"
"github.com/stretchr/testify/require"
)
func TestRunWithError(t *testing.T) {
ctx := context.New(config.Project{
Dist: "testadata/nope",
ProjectName: "foo",
})
require.EqualError(t, Pipe{}.Run(ctx), `open testadata/nope/artifacts.json: no such file or directory`)
}
func TestRun(t *testing.T) {
tmp := t.TempDir()
ctx := context.New(config.Project{
Dist: tmp,
ProjectName: "name",
})
ctx.Version = "1.2.3"
ctx.Git = context.GitInfo{
CurrentTag: "v1.2.3",
PreviousTag: "v1.2.2",
Commit: "aef34a",
}
ctx.Date = time.Date(2022, 0o1, 22, 10, 12, 13, 0, time.UTC)
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo",
Path: "foo.txt",
Type: artifact.Binary,
Goos: "darwin",
Goarch: "amd64",
Goarm: "7",
Extra: map[string]interface{}{
"foo": "bar",
},
})
require.NoError(t, Pipe{}.Run(ctx))
t.Run("artifacts", func(t *testing.T) {
requireEqualJSONFile(t, tmp, "artifacts.json")
})
t.Run("metadata", func(t *testing.T) {
requireEqualJSONFile(t, tmp, "metadata.json")
})
}
func requireEqualJSONFile(tb testing.TB, tmp, s string) {
tb.Helper()
path := filepath.Join(tmp, s)
golden.RequireEqualJSON(tb, golden.RequireReadFile(tb, path))
info, err := os.Stat(path)
require.NoError(tb, err)
require.Equal(tb, "-rw-r--r--", info.Mode().String())
}

View File

@ -0,0 +1 @@
{"project_name":"name","tag":"v1.2.3","previous_tag":"v1.2.2","version":"1.2.3","commit":"aef34a","date":"2022-01-22T10:12:13Z"}

View File

@ -6,7 +6,6 @@ import (
"github.com/goreleaser/goreleaser/internal/pipe/announce"
"github.com/goreleaser/goreleaser/internal/pipe/archive"
"github.com/goreleaser/goreleaser/internal/pipe/artifacts"
"github.com/goreleaser/goreleaser/internal/pipe/aur"
"github.com/goreleaser/goreleaser/internal/pipe/before"
"github.com/goreleaser/goreleaser/internal/pipe/brew"
@ -22,6 +21,7 @@ import (
"github.com/goreleaser/goreleaser/internal/pipe/gofish"
"github.com/goreleaser/goreleaser/internal/pipe/gomod"
"github.com/goreleaser/goreleaser/internal/pipe/krew"
"github.com/goreleaser/goreleaser/internal/pipe/metadata"
"github.com/goreleaser/goreleaser/internal/pipe/nfpm"
"github.com/goreleaser/goreleaser/internal/pipe/prebuild"
"github.com/goreleaser/goreleaser/internal/pipe/publish"
@ -65,7 +65,7 @@ var BuildPipeline = []Piper{
// BuildCmdPipeline is the pipeline run by goreleaser build.
// nolint:gochecknoglobals
var BuildCmdPipeline = append(BuildPipeline, artifacts.Pipe{})
var BuildCmdPipeline = append(BuildPipeline, metadata.Pipe{})
// Pipeline contains all pipe implementations in order.
// nolint: gochecknoglobals
@ -84,7 +84,7 @@ var Pipeline = append(
checksums.Pipe{}, // checksums of the files
sign.Pipe{}, // sign artifacts
docker.Pipe{}, // create and push docker images
artifacts.Pipe{}, // creates an artifacts.json in the dist folder
metadata.Pipe{}, // creates a metadata.json and an artifacts.json files in the dist folder
publish.Pipe{}, // publishes artifacts
announce.Pipe{}, // announce releases
)