From c320f338ea6323b658cd626302cc2377d3985f98 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Sat, 16 Sep 2017 15:31:20 -0300 Subject: [PATCH] adding docker images into the release notes --- context/context.go | 10 ++++++++ pipeline/docker/docker.go | 1 + pipeline/release/body.go | 25 +++++++++++++++----- pipeline/release/body_test.go | 32 ++++++++++++++++++++++---- pipeline/release/testdata/release1.txt | 12 ++++++++++ pipeline/release/testdata/release2.txt | 7 ++++++ 6 files changed, 77 insertions(+), 10 deletions(-) create mode 100644 pipeline/release/testdata/release1.txt create mode 100755 pipeline/release/testdata/release2.txt diff --git a/context/context.go b/context/context.go index 83d1f6c44..38cb9c3b2 100644 --- a/context/context.go +++ b/context/context.go @@ -35,6 +35,7 @@ type Context struct { Git GitInfo Binaries map[string]map[string][]Binary Artifacts []string + Dockers []string ReleaseNotes string Version string Validate bool @@ -45,6 +46,7 @@ type Context struct { } var artifactsLock sync.Mutex +var dockersLock sync.Mutex var binariesLock sync.Mutex // AddArtifact adds a file to upload list @@ -56,6 +58,14 @@ func (ctx *Context) AddArtifact(file string) { log.WithField("artifact", file).Info("new release artifact") } +// AddDocker adds a docker image to the docker images list +func (ctx *Context) AddDocker(image string) { + dockersLock.Lock() + defer dockersLock.Unlock() + ctx.Dockers = append(ctx.Dockers, image) + log.WithField("image", image).Info("new docker image") +} + // AddBinary adds a built binary to the current context func (ctx *Context) AddBinary(platform, folder, name, path string) { binariesLock.Lock() diff --git a/pipeline/docker/docker.go b/pipeline/docker/docker.go index 8e1a19732..28a6dd089 100644 --- a/pipeline/docker/docker.go +++ b/pipeline/docker/docker.go @@ -95,6 +95,7 @@ func process(ctx *context.Context, folder string, docker config.Docker, binary c return err } } + ctx.AddDocker(image) return nil } diff --git a/pipeline/release/body.go b/pipeline/release/body.go index d16180fde..fdbedae6d 100644 --- a/pipeline/release/body.go +++ b/pipeline/release/body.go @@ -10,23 +10,36 @@ import ( const bodyTemplate = `{{ .ReleaseNotes }} +{{- if .DockerImages }} + +Docker images: +{{ range $element := .DockerImages }} +- {{ . -}} +{{ end -}} +{{- end }} + --- Automated with [GoReleaser](https://github.com/goreleaser) -Built with {{ .GoVersion }} -` +Built with {{ .GoVersion }}` func describeBody(ctx *context.Context) (bytes.Buffer, error) { - var out bytes.Buffer bts, err := exec.Command("go", "version").CombinedOutput() if err != nil { - return out, err + return bytes.Buffer{}, err } + return describeBodyVersion(ctx, string(bts)) +} + +func describeBodyVersion(ctx *context.Context, version string) (bytes.Buffer, error) { + var out bytes.Buffer var template = template.Must(template.New("release").Parse(bodyTemplate)) - err = template.Execute(&out, struct { + err := template.Execute(&out, struct { ReleaseNotes, GoVersion string + DockerImages []string }{ ReleaseNotes: ctx.ReleaseNotes, - GoVersion: string(bts), + GoVersion: version, + DockerImages: ctx.Dockers, }) return out, err } diff --git a/pipeline/release/body_test.go b/pipeline/release/body_test.go index 109b46e97..c9c172494 100644 --- a/pipeline/release/body_test.go +++ b/pipeline/release/body_test.go @@ -1,6 +1,7 @@ package release import ( + "io/ioutil" "os" "testing" @@ -13,12 +14,35 @@ func TestDescribeBody(t *testing.T) { var changelog = "\nfeature1: description\nfeature2: other description" var ctx = &context.Context{ ReleaseNotes: changelog, + Dockers: []string{ + "goreleaser/goreleaser:0.40.0", + "goreleaser/godownloader:0.1.0", + }, } - out, err := describeBody(ctx) + out, err := describeBodyVersion(ctx, "go version go1.9 darwin/amd64") assert.NoError(err) - assert.Contains(out.String(), changelog) - assert.Contains(out.String(), "Automated with [GoReleaser]") - assert.Contains(out.String(), "Built with go version go1.") + + bts, err := ioutil.ReadFile("testdata/release1.txt") + assert.NoError(err) + ioutil.WriteFile("testdata/release1.txt", out.Bytes(), 0755) + + assert.Equal(string(bts), out.String()) +} + +func TestDescribeBodyNoDockerImages(t *testing.T) { + var assert = assert.New(t) + var changelog = "\nfeature1: description\nfeature2: other description" + var ctx = &context.Context{ + ReleaseNotes: changelog, + } + out, err := describeBodyVersion(ctx, "go version go1.9 darwin/amd64") + assert.NoError(err) + + bts, err := ioutil.ReadFile("testdata/release2.txt") + assert.NoError(err) + ioutil.WriteFile("testdata/release2.txt", out.Bytes(), 0755) + + assert.Equal(string(bts), out.String()) } func TestDontEscapeHTML(t *testing.T) { diff --git a/pipeline/release/testdata/release1.txt b/pipeline/release/testdata/release1.txt new file mode 100644 index 000000000..00f95645a --- /dev/null +++ b/pipeline/release/testdata/release1.txt @@ -0,0 +1,12 @@ + +feature1: description +feature2: other description + +Docker images: + +- goreleaser/goreleaser:0.40.0 +- goreleaser/godownloader:0.1.0 + +--- +Automated with [GoReleaser](https://github.com/goreleaser) +Built with go version go1.9 darwin/amd64 \ No newline at end of file diff --git a/pipeline/release/testdata/release2.txt b/pipeline/release/testdata/release2.txt new file mode 100755 index 000000000..c014ae9c2 --- /dev/null +++ b/pipeline/release/testdata/release2.txt @@ -0,0 +1,7 @@ + +feature1: description +feature2: other description + +--- +Automated with [GoReleaser](https://github.com/goreleaser) +Built with go version go1.9 darwin/amd64 \ No newline at end of file