Repository: goreleaser/goreleaser
Pull request #1233 (2019-11-08 00:39:19)

feat: filter by id on sign pipe
ads the hability to filter by id on the sign pipe.

with this, the user can have multiple build configs, say, one specifically for macos, then one archive config specifically for it, and finally a sign config for it, signing it with `gon`, so it macOS won't complain when the user tries to run it.

refs https://github.com/goreleaser/goreleaser/issues/1227



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

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

```diff
@@            Coverage Diff             @@
##           master    #1233      +/-   ##
==========================================
+ Coverage   83.65%   83.66%   +<.01%     
==========================================
  Files          58       58              
  Lines        3286     3287       +1     
==========================================
+ Hits         2749     2750       +1     
  Misses        456      456              
  Partials       81       81
```


| [Impacted Files](https://codecov.io/gh/goreleaser/goreleaser/pull/1233?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pkg/config/config.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1233/diff?src=pr&el=tree#diff-cGtnL2NvbmZpZy9jb25maWcuZ28=) | `100% <ø> (ø)` | :arrow_up: |
| [internal/pipe/sign/sign.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1233/diff?src=pr&el=tree#diff-aW50ZXJuYWwvcGlwZS9zaWduL3NpZ24uZ28=) | `94.87% <100%> (+0.06%)` | :arrow_up: |
| [internal/artifact/artifact.go](https://codecov.io/gh/goreleaser/goreleaser/pull/1233/diff?src=pr&el=tree#diff-aW50ZXJuYWwvYXJ0aWZhY3QvYXJ0aWZhY3QuZ28=) | `83.62% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/goreleaser/goreleaser/pull/1233?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/1233?src=pr&el=footer). Last update [3d95590...4940d0a](https://codecov.io/gh/goreleaser/goreleaser/pull/1233?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 c98684e8..f4eaf6cb 100644
--- a/internal/artifact/artifact.go
+++ b/internal/artifact/artifact.go
@@ -211,7 +211,8 @@ func ByIDs(ids ...string) Filter {
 	for _, id := range ids {
 		id := id
 		filters = append(filters, func(a *Artifact) bool {
-			return a.ExtraOr("ID", "") == id
+			// checksum are allways for all artifacts, so return always true.
+			return a.Type == Checksum || a.ExtraOr("ID", "") == id
 		})
 	}
 	return Or(filters...)
diff --git a/internal/pipe/sign/sign.go b/internal/pipe/sign/sign.go
index da797529..ce00ede7 100644
--- a/internal/pipe/sign/sign.go
+++ b/internal/pipe/sign/sign.go
@@ -59,26 +59,29 @@ func (Pipe) Run(ctx *context.Context) error {
 	for i := range ctx.Config.Signs {
 		cfg := ctx.Config.Signs[i]
 		g.Go(func() error {
+			var filters []artifact.Filter
 			switch cfg.Artifacts {
 			case "checksum":
-				var artifacts = ctx.Artifacts.
-					Filter(artifact.ByType(artifact.Checksum)).
-					List()
-				return sign(ctx, cfg, artifacts)
+				filters = append(filters, artifact.ByType(artifact.Checksum))
+				if len(cfg.IDs) > 0 {
+					log.Warn("when artifacts is `checksum`, `ids` has no effect. ignoring")
+				}
 			case "all":
-				var artifacts = ctx.Artifacts.
-					Filter(artifact.Or(
-						artifact.ByType(artifact.UploadableArchive),
-						artifact.ByType(artifact.UploadableBinary),
-						artifact.ByType(artifact.Checksum),
-						artifact.ByType(artifact.LinuxPackage),
-					)).List()
-				return sign(ctx, cfg, artifacts)
+				filters = append(filters, artifact.Or(
+					artifact.ByType(artifact.UploadableArchive),
+					artifact.ByType(artifact.UploadableBinary),
+					artifact.ByType(artifact.Checksum),
+					artifact.ByType(artifact.LinuxPackage),
+				))
+				if len(cfg.IDs) > 0 {
+					filters = append(filters, artifact.ByIDs(cfg.IDs...))
+				}
 			case "none":
 				return pipe.ErrSkipSignEnabled
 			default:
 				return fmt.Errorf("invalid list of artifacts to sign: %s", cfg.Artifacts)
 			}
+			return sign(ctx, cfg, ctx.Artifacts.Filter(artifact.And(filters...)).List())
 		})
 	}
 	return g.Wait()
diff --git a/pkg/config/config.go b/pkg/config/config.go
index 492a4db5..aa607516 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -230,6 +230,7 @@ type Sign struct {
 	Args      []string `yaml:"args,omitempty"`
 	Signature string   `yaml:"signature,omitempty"`
 	Artifacts string   `yaml:"artifacts,omitempty"`
+	IDs       []string `yaml:"ids,omitempty"`
 }
 
 // SnapcraftAppMetadata for the binaries that will be in the snap package
diff --git a/www/content/sign.md b/www/content/sign.md
index b6017b9b..dfdb9b16 100644
--- a/www/content/sign.md
+++ b/www/content/sign.md
@@ -56,4 +56,11 @@ signs:
     #
     # defaults to `none`
     artifacts: all
+
+    # IDs of the artifacts to sign.
+    # Defaults to all.
+    # If `artifacts` is checksum, this fields has no effect.
+    ids:
+      - foo
+      - bar
 ```


Test patch:
diff --git a/internal/artifact/artifact_test.go b/internal/artifact/artifact_test.go
index a6f0e295..cd364c39 100644
--- a/internal/artifact/artifact_test.go
+++ b/internal/artifact/artifact_test.go
@@ -222,15 +222,19 @@ func TestByIDs(t *testing.T) {
 				"ID": "check",
 			},
 		},
+		{
+			Name: "checksum",
+			Type: Checksum,
+		},
 	}
 	var artifacts = New()
 	for _, a := range data {
 		artifacts.Add(a)
 	}
 
-	require.Len(t, artifacts.Filter(ByIDs("check")).items, 1)
-	require.Len(t, artifacts.Filter(ByIDs("foo")).items, 2)
-	require.Len(t, artifacts.Filter(ByIDs("foo", "bar")).items, 3)
+	require.Len(t, artifacts.Filter(ByIDs("check")).items, 2)
+	require.Len(t, artifacts.Filter(ByIDs("foo")).items, 3)
+	require.Len(t, artifacts.Filter(ByIDs("foo", "bar")).items, 4)
 }
 
 func TestByFormats(t *testing.T) {
diff --git a/internal/pipe/sign/sign_test.go b/internal/pipe/sign/sign_test.go
index 63cdd4de..63f68861 100644
--- a/internal/pipe/sign/sign_test.go
+++ b/internal/pipe/sign/sign_test.go
@@ -88,8 +88,8 @@ func TestSignArtifacts(t *testing.T) {
 					},
 				},
 			),
-			signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "linux_amd64/artifact4.sig"},
-			signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "artifact4_1.0.0_linux_amd64.sig"},
+			signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig"},
+			signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig"},
 		},
 		{
 			desc: "sign all artifacts",
@@ -102,8 +102,23 @@ func TestSignArtifacts(t *testing.T) {
 					},
 				},
 			),
-			signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "linux_amd64/artifact4.sig"},
-			signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "artifact4_1.0.0_linux_amd64.sig"},
+			signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig"},
+			signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig"},
+		},
+		{
+			desc: "sign filtered artifacts",
+			ctx: context.New(
+				config.Project{
+					Signs: []config.Sign{
+						{
+							Artifacts: "all",
+							IDs:       []string{"foo"},
+						},
+					},
+				},
+			),
+			signaturePaths: []string{"artifact1.sig", "artifact3.sig", "checksum.sig", "checksum2.sig"},
+			signatureNames: []string{"artifact1.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig"},
 		},
 		{
 			desc: "sign only checksums",
@@ -116,8 +131,23 @@ func TestSignArtifacts(t *testing.T) {
 					},
 				},
 			),
-			signaturePaths: []string{"checksum.sig"},
-			signatureNames: []string{"checksum.sig"},
+			signaturePaths: []string{"checksum.sig", "checksum2.sig"},
+			signatureNames: []string{"checksum.sig", "checksum2.sig"},
+		},
+		{
+			desc: "sign only filtered checksums",
+			ctx: context.New(
+				config.Project{
+					Signs: []config.Sign{
+						{
+							Artifacts: "checksum",
+							IDs:       []string{"foo"},
+						},
+					},
+				},
+			),
+			signaturePaths: []string{"checksum.sig", "checksum2.sig"},
+			signatureNames: []string{"checksum.sig", "checksum2.sig"},
 		},
 		{
 			desc: "sign all artifacts with env",
@@ -141,8 +171,8 @@ func TestSignArtifacts(t *testing.T) {
 					},
 				},
 			),
-			signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "linux_amd64/artifact4.sig"},
-			signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "artifact4_1.0.0_linux_amd64.sig"},
+			signaturePaths: []string{"artifact1.sig", "artifact2.sig", "artifact3.sig", "checksum.sig", "checksum2.sig", "linux_amd64/artifact4.sig"},
+			signatureNames: []string{"artifact1.sig", "artifact2.sig", "artifact3_1.0.0_linux_amd64.sig", "checksum.sig", "checksum2.sig", "artifact4_1.0.0_linux_amd64.sig"},
 		},
 	}
 
@@ -164,7 +194,7 @@ func testSign(t *testing.T, ctx *context.Context, signaturePaths []string, signa
 	ctx.Config.Dist = tmpdir
 
 	// create some fake artifacts
-	var artifacts = []string{"artifact1", "artifact2", "artifact3", "checksum"}
+	var artifacts = []string{"artifact1", "artifact2", "artifact3", "checksum", "checksum2"}
 	err = os.Mkdir(filepath.Join(tmpdir, "linux_amd64"), os.ModePerm)
 	assert.NoError(t, err)
 	for _, f := range artifacts {
@@ -177,26 +207,43 @@ func testSign(t *testing.T, ctx *context.Context, signaturePaths []string, signa
 		Name: "artifact1",
 		Path: filepath.Join(tmpdir, "artifact1"),
 		Type: artifact.UploadableArchive,
+		Extra: map[string]interface{}{
+			"ID": "foo",
+		},
 	})
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Name: "artifact2",
 		Path: filepath.Join(tmpdir, "artifact2"),
 		Type: artifact.UploadableArchive,
+		Extra: map[string]interface{}{
+			"ID": "foo3",
+		},
 	})
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Name: "artifact3_1.0.0_linux_amd64",
 		Path: filepath.Join(tmpdir, "artifact3"),
 		Type: artifact.UploadableBinary,
+		Extra: map[string]interface{}{
+			"ID": "foo",
+		},
 	})
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Name: "checksum",
 		Path: filepath.Join(tmpdir, "checksum"),
 		Type: artifact.Checksum,
 	})
+	ctx.Artifacts.Add(&artifact.Artifact{
+		Name: "checksum2",
+		Path: filepath.Join(tmpdir, "checksum2"),
+		Type: artifact.Checksum,
+	})
 	ctx.Artifacts.Add(&artifact.Artifact{
 		Name: "artifact4_1.0.0_linux_amd64",
 		Path: filepath.Join(tmpdir, "linux_amd64", "artifact4"),
 		Type: artifact.UploadableBinary,
+		Extra: map[string]interface{}{
+			"ID": "foo3",
+		},
 	})
 
 	// configure the pipeline