Repository: goreleaser/goreleaser
Pull request #908 (2019-01-01 16:09:18)

feat: back-reference build artifacts from archive artifact
refs https://github.com/goreleaser/goreleaser/issues/705

codecov-io: # [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/908?src=pr&el=h1) Report
> Merging [#908](https://codecov.io/gh/goreleaser/goreleaser/pull/908?src=pr&el=desc) into [master](https://codecov.io/gh/goreleaser/goreleaser/commit/e1eeba292f9d72977e5ecb2c4302065319ea2e7f?src=pr&el=desc) will **increase** coverage by `0.07%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/goreleaser/goreleaser/pull/908/graphs/tree.svg?width=650&token=3bJvv6b7hN&height=150&src=pr)](https://codecov.io/gh/goreleaser/goreleaser/pull/908?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##           master    #908      +/-   ##
=========================================
+ Coverage   88.12%   88.2%   +0.07%     
=========================================
  Files          48      48              
  Lines        2384    2399      +15     
=========================================
+ Hits         2101    2116      +15     
  Misses        223     223              
  Partials       60      60
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/908?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [internal/tmpl/tmpl.go](https://codecov.io/gh/goreleaser/goreleaser/pull/908/diff?src=pr&el=tree#diff-aW50ZXJuYWwvdG1wbC90bXBsLmdv) | `100% <100%> (ø)` | :arrow_up: |
| [internal/pipe/docker/docker.go](https://codecov.io/gh/goreleaser/goreleaser/pull/908/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9kb2NrZXIvZG9ja2VyLmdv) | `95.18% <100%> (ø)` | :arrow_up: |
| [internal/pipe/archive/archive.go](https://codecov.io/gh/goreleaser/goreleaser/pull/908/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9hcmNoaXZlL2FyY2hpdmUuZ28=) | `95.62% <100%> (+0.38%)` | :arrow_up: |
| [internal/artifact/artifact.go](https://codecov.io/gh/goreleaser/goreleaser/pull/908/diff?src=pr&el=tree#diff-aW50ZXJuYWwvYXJ0aWZhY3QvYXJ0aWZhY3QuZ28=) | `86.74% <100%> (+0.67%)` | :arrow_up: |
| [internal/builders/golang/build.go](https://codecov.io/gh/goreleaser/goreleaser/pull/908/diff?src=pr&el=tree#diff-aW50ZXJuYWwvYnVpbGRlcnMvZ29sYW5nL2J1aWxkLmdv) | `98.29% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/908?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/908?src=pr&el=footer). Last update [e1eeba2...cb72f8c](https://codecov.io/gh/goreleaser/goreleaser/pull/908?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/internal/artifact/artifact.go b/internal/artifact/artifact.go
index d6ba9b01..c042a04f 100644
--- a/internal/artifact/artifact.go
+++ b/internal/artifact/artifact.go
@@ -66,7 +66,16 @@ type Artifact struct {
 	Goarch string
 	Goarm  string
 	Type   Type
-	Extra  map[string]string
+	Extra  map[string]interface{}
+}
+
+// ExtraOr returns the Extra field with the given key or the or value specified
+// if it is nil.
+func (a Artifact) ExtraOr(key string, or interface{}) interface{} {
+	if a.Extra[key] == nil {
+		return or
+	}
+	return a.Extra[key]
 }
 
 // Checksum calculates the SHA256 checksum of the artifact.
diff --git a/internal/builders/golang/build.go b/internal/builders/golang/build.go
index 5e2eb716..f1e490a9 100644
--- a/internal/builders/golang/build.go
+++ b/internal/builders/golang/build.go
@@ -97,7 +97,7 @@ func (*Builder) Build(ctx *context.Context, build config.Build, options api.Opti
 		Goos:   target.os,
 		Goarch: target.arch,
 		Goarm:  target.arm,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": build.Binary,
 			"Ext":    options.Ext,
 		},
diff --git a/internal/pipe/archive/archive.go b/internal/pipe/archive/archive.go
index 2eb48021..2161f53b 100644
--- a/internal/pipe/archive/archive.go
+++ b/internal/pipe/archive/archive.go
@@ -137,6 +137,9 @@ func create(ctx *context.Context, binaries []artifact.Artifact) error {
 		Goos:   binaries[0].Goos,
 		Goarch: binaries[0].Goarch,
 		Goarm:  binaries[0].Goarm,
+		Extra: map[string]interface{}{
+			"Builds": binaries,
+		},
 	})
 	return nil
 }
@@ -161,9 +164,17 @@ func skip(ctx *context.Context, binaries []artifact.Artifact) error {
 		if err != nil {
 			return err
 		}
-		binary.Type = artifact.UploadableBinary
-		binary.Name = name + binary.Extra["Ext"]
-		ctx.Artifacts.Add(binary)
+		ctx.Artifacts.Add(artifact.Artifact{
+			Type:   artifact.UploadableBinary,
+			Name:   name + binary.ExtraOr("Ext", "").(string),
+			Path:   binary.Path,
+			Goos:   binary.Goos,
+			Goarch: binary.Goarch,
+			Goarm:  binary.Goarm,
+			Extra: map[string]interface{}{
+				"Builds": []artifact.Artifact{binary},
+			},
+		})
 	}
 	return nil
 }
diff --git a/internal/pipe/docker/docker.go b/internal/pipe/docker/docker.go
index 7851a991..2d10a922 100644
--- a/internal/pipe/docker/docker.go
+++ b/internal/pipe/docker/docker.go
@@ -105,7 +105,7 @@ func doRun(ctx *context.Context) error {
 					artifact.ByGoarm(docker.Goarm),
 					artifact.ByType(artifact.Binary),
 					func(a artifact.Artifact) bool {
-						return a.Extra["Binary"] == docker.Binary
+						return a.ExtraOr("Binary", "").(string) == docker.Binary
 					},
 				),
 			).List()
diff --git a/internal/tmpl/tmpl.go b/internal/tmpl/tmpl.go
index 5cdddce0..3f479e8d 100644
--- a/internal/tmpl/tmpl.go
+++ b/internal/tmpl/tmpl.go
@@ -64,13 +64,13 @@ func New(ctx *context.Context) *Template {
 // WithArtifact populates fields from the artifact and replacements
 func (t *Template) WithArtifact(a artifact.Artifact, replacements map[string]string) *Template {
 	var bin = a.Extra[binary]
-	if bin == "" {
-		bin = t.fields[projectName].(string)
+	if bin == nil {
+		bin = t.fields[projectName]
 	}
 	t.fields[os] = replace(replacements, a.Goos)
 	t.fields[arch] = replace(replacements, a.Goarch)
 	t.fields[arm] = replace(replacements, a.Goarm)
-	t.fields[binary] = bin
+	t.fields[binary] = bin.(string)
 	t.fields[artifactName] = a.Name
 	return t
 }


Test patch:
diff --git a/internal/artifact/artifact_test.go b/internal/artifact/artifact_test.go
index 3adee819..a4eaec2f 100644
--- a/internal/artifact/artifact_test.go
+++ b/internal/artifact/artifact_test.go
@@ -162,3 +162,13 @@ func TestChecksumFileDoesntExist(t *testing.T) {
 	require.EqualError(t, err, `failed to checksum: open /tmp/adasdasdas/asdasd/asdas: no such file or directory`)
 	require.Empty(t, sum)
 }
+
+func TestExtraOr(t *testing.T) {
+	var a = Artifact{
+		Extra: map[string]interface{}{
+			"Foo": "foo",
+		},
+	}
+	require.Equal(t, "foo", a.ExtraOr("Foo", "bar"))
+	require.Equal(t, "bar", a.ExtraOr("Foobar", "bar"))
+}
diff --git a/internal/builders/golang/build_test.go b/internal/builders/golang/build_test.go
index ed98224e..77af0a36 100644
--- a/internal/builders/golang/build_test.go
+++ b/internal/builders/golang/build_test.go
@@ -1,11 +1,13 @@
 package golang
 
 import (
+	"fmt"
 	"io/ioutil"
 	"path/filepath"
 	"runtime"
 	"strings"
 	"testing"
+	"time"
 
 	"github.com/goreleaser/goreleaser/internal/artifact"
 	"github.com/goreleaser/goreleaser/internal/testlib"
@@ -115,7 +117,7 @@ func TestBuild(t *testing.T) {
 			Goos:   "linux",
 			Goarch: "amd64",
 			Type:   artifact.Binary,
-			Extra: map[string]string{
+			Extra: map[string]interface{}{
 				"Ext":    "",
 				"Binary": "foo",
 			},
@@ -126,7 +128,7 @@ func TestBuild(t *testing.T) {
 			Goos:   "darwin",
 			Goarch: "amd64",
 			Type:   artifact.Binary,
-			Extra: map[string]string{
+			Extra: map[string]interface{}{
 				"Ext":    "",
 				"Binary": "foo",
 			},
@@ -138,7 +140,7 @@ func TestBuild(t *testing.T) {
 			Goarch: "arm",
 			Goarm:  "6",
 			Type:   artifact.Binary,
-			Extra: map[string]string{
+			Extra: map[string]interface{}{
 				"Ext":    "",
 				"Binary": "foo",
 			},
@@ -149,7 +151,7 @@ func TestBuild(t *testing.T) {
 			Goos:   "windows",
 			Goarch: "amd64",
 			Type:   artifact.Binary,
-			Extra: map[string]string{
+			Extra: map[string]interface{}{
 				"Ext":    ".exe",
 				"Binary": "foo",
 			},
@@ -376,9 +378,8 @@ func TestLdFlagsFullTemplate(t *testing.T) {
 	assert.Contains(t, flags, "-X main.version=1.2.3")
 	assert.Contains(t, flags, "-X main.tag=v1.2.3")
 	assert.Contains(t, flags, "-X main.commit=123")
-	// TODO: this will break in 2019
-	assert.Contains(t, flags, "-X main.date=2018")
-	assert.Contains(t, flags, "-X main.time=2018")
+	assert.Contains(t, flags, fmt.Sprintf("-X main.date=%d", time.Now().Year()))
+	assert.Contains(t, flags, fmt.Sprintf("-X main.time=%d", time.Now().Year()))
 	assert.Contains(t, flags, `-X "main.foo=123"`)
 }
 
diff --git a/internal/pipe/archive/archive_test.go b/internal/pipe/archive/archive_test.go
index 475eff1a..71681780 100644
--- a/internal/pipe/archive/archive_test.go
+++ b/internal/pipe/archive/archive_test.go
@@ -59,27 +59,29 @@ func TestRunPipe(t *testing.T) {
 					},
 				},
 			)
-			ctx.Artifacts.Add(artifact.Artifact{
+			var darwinBuild = artifact.Artifact{
 				Goos:   "darwin",
 				Goarch: "amd64",
 				Name:   "mybin",
 				Path:   filepath.Join(dist, "darwinamd64", "mybin"),
 				Type:   artifact.Binary,
-				Extra: map[string]string{
+				Extra: map[string]interface{}{
 					"Binary": "mybin",
 				},
-			})
-			ctx.Artifacts.Add(artifact.Artifact{
+			}
+			var windowsBuild = artifact.Artifact{
 				Goos:   "windows",
 				Goarch: "amd64",
 				Name:   "mybin.exe",
 				Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"),
 				Type:   artifact.Binary,
-				Extra: map[string]string{
+				Extra: map[string]interface{}{
 					"Binary":    "mybin",
 					"Extension": ".exe",
 				},
-			})
+			}
+			ctx.Artifacts.Add(darwinBuild)
+			ctx.Artifacts.Add(windowsBuild)
 			ctx.Version = "0.0.1"
 			ctx.Git.CurrentTag = "v0.0.1"
 			ctx.Config.Archive.Format = format
@@ -91,6 +93,9 @@ func TestRunPipe(t *testing.T) {
 			require.Equal(tt, "foobar_0.0.1_darwin_amd64."+format, darwin.Name)
 			require.Equal(tt, "foobar_0.0.1_windows_amd64.zip", windows.Name)
 
+			require.Equal(t, []artifact.Artifact{darwinBuild}, darwin.Extra["Builds"].([]artifact.Artifact))
+			require.Equal(t, []artifact.Artifact{windowsBuild}, windows.Extra["Builds"].([]artifact.Artifact))
+
 			if format == "tar.gz" {
 				// Check archive contents
 				require.Equal(
@@ -184,7 +189,7 @@ func TestRunPipeBinary(t *testing.T) {
 		Name:   "mybin",
 		Path:   filepath.Join(dist, "darwinamd64", "mybin"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": "mybin",
 		},
 	})
@@ -194,7 +199,7 @@ func TestRunPipeBinary(t *testing.T) {
 		Name:   "mybin.exe",
 		Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": "mybin",
 			"Ext":    ".exe",
 		},
@@ -225,7 +230,7 @@ func TestRunPipeDistRemoved(t *testing.T) {
 		Name:   "mybin.exe",
 		Path:   filepath.Join("/path/to/nope", "windowsamd64", "mybin.exe"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary":    "mybin",
 			"Extension": ".exe",
 		},
@@ -260,7 +265,7 @@ func TestRunPipeInvalidGlob(t *testing.T) {
 		Name:   "mybin",
 		Path:   filepath.Join("dist", "darwinamd64", "mybin"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": "mybin",
 		},
 	})
@@ -291,7 +296,7 @@ func TestRunPipeInvalidNameTemplate(t *testing.T) {
 		Name:   "mybin",
 		Path:   filepath.Join("dist", "darwinamd64", "mybin"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": "mybin",
 		},
 	})
@@ -323,7 +328,7 @@ func TestRunPipeInvalidWrapInDirectoryTemplate(t *testing.T) {
 		Name:   "mybin",
 		Path:   filepath.Join("dist", "darwinamd64", "mybin"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": "mybin",
 		},
 	})
@@ -363,7 +368,7 @@ func TestRunPipeWrap(t *testing.T) {
 		Name:   "mybin",
 		Path:   filepath.Join("dist", "darwinamd64", "mybin"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": "mybin",
 		},
 	})
@@ -487,7 +492,7 @@ func TestBinaryOverride(t *testing.T) {
 				Name:   "mybin",
 				Path:   filepath.Join(dist, "darwinamd64", "mybin"),
 				Type:   artifact.Binary,
-				Extra: map[string]string{
+				Extra: map[string]interface{}{
 					"Binary": "mybin",
 				},
 			})
@@ -497,7 +502,7 @@ func TestBinaryOverride(t *testing.T) {
 				Name:   "mybin.exe",
 				Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"),
 				Type:   artifact.Binary,
-				Extra: map[string]string{
+				Extra: map[string]interface{}{
 					"Binary": "mybin",
 					"Ext":    ".exe",
 				},
@@ -548,7 +553,7 @@ func TestRunPipeSameArchiveFilename(t *testing.T) {
 		Name:   "mybin",
 		Path:   filepath.Join(dist, "darwinamd64", "mybin"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary": "mybin",
 		},
 	})
@@ -558,7 +563,7 @@ func TestRunPipeSameArchiveFilename(t *testing.T) {
 		Name:   "mybin.exe",
 		Path:   filepath.Join(dist, "windowsamd64", "mybin.exe"),
 		Type:   artifact.Binary,
-		Extra: map[string]string{
+		Extra: map[string]interface{}{
 			"Binary":    "mybin",
 			"Extension": ".exe",
 		},
diff --git a/internal/pipe/docker/docker_test.go b/internal/pipe/docker/docker_test.go
index 2af4d0a9..a6c2c425 100644
--- a/internal/pipe/docker/docker_test.go
+++ b/internal/pipe/docker/docker_test.go
@@ -567,7 +567,7 @@ func TestRunPipe(t *testing.T) {
 						Goarch: arch,
 						Goos:   os,
 						Type:   artifact.Binary,
-						Extra: map[string]string{
+						Extra: map[string]interface{}{
 							"Binary": "mybin",
 						},
 					})
diff --git a/internal/tmpl/tmpl_test.go b/internal/tmpl/tmpl_test.go
index f1571acd..f0505cd6 100644
--- a/internal/tmpl/tmpl_test.go
+++ b/internal/tmpl/tmpl_test.go
@@ -44,7 +44,7 @@ func TestWithArtifact(t *testing.T) {
 					Goarch: "amd64",
 					Goos:   "linux",
 					Goarm:  "6",
-					Extra: map[string]string{
+					Extra: map[string]interface{}{
 						"Binary": "binary",
 					},
 				},