Skip to content

Commit

Permalink
feat(release): add artifact filter (by id) to release pipe (#1203)
Browse files Browse the repository at this point in the history
* feat(release): add artifact filter (by id) to release pipe

* add `ids: string[]` to release configuration
* add support for filtering artifacts by id within the release pipe

* update docs for release
  • Loading branch information
dmccaffery authored and caarlos0 committed Oct 19, 2019
1 parent a6da05b commit 93259b0
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 11 deletions.
13 changes: 10 additions & 3 deletions internal/pipe/release/release.go
Expand Up @@ -122,16 +122,23 @@ func doPublish(ctx *context.Context, client client.Client) error {
if err != nil {
return err
}
var g = semerrgroup.New(ctx.Parallelism)
for _, artifact := range ctx.Artifacts.Filter(

var filters = []artifact.Filter{
artifact.Or(
artifact.ByType(artifact.UploadableArchive),
artifact.ByType(artifact.UploadableBinary),
artifact.ByType(artifact.Checksum),
artifact.ByType(artifact.Signature),
artifact.ByType(artifact.LinuxPackage),
),
).List() {
}

if len(ctx.Config.Release.IDs) > 0 {
filters = append(filters, artifact.ByIDs(ctx.Config.Release.IDs...))
}

var g = semerrgroup.New(ctx.Parallelism)
for _, artifact := range ctx.Artifacts.Filter(artifact.And(filters...)).List() {
artifact := artifact
g.Go(func() error {
var repeats uint
Expand Down
97 changes: 96 additions & 1 deletion internal/pipe/release/release_test.go
Expand Up @@ -19,13 +19,18 @@ func TestPipeDescription(t *testing.T) {
assert.NotEmpty(t, Pipe{}.String())
}

func TestRunPipe(t *testing.T) {
func TestRunPipeWithoutIDsThenDoesNotFilter(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
assert.NoError(t, err)
debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
assert.NoError(t, err)
filteredtarfile, err := os.Create(filepath.Join(folder, "filtered.tar.gz"))
assert.NoError(t, err)
filtereddebfile, err := os.Create(filepath.Join(folder, "filtered.deb"))
assert.NoError(t, err)

var config = config.Project{
Dist: folder,
Release: config.Release{
Expand All @@ -41,18 +46,108 @@ func TestRunPipe(t *testing.T) {
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
Path: tarfile.Name(),
Extra: map[string]interface{}{
"ID": "foo",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.LinuxPackage,
Name: "bin.deb",
Path: debfile.Name(),
Extra: map[string]interface{}{
"ID": "foo",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "filtered.tar.gz",
Path: filteredtarfile.Name(),
Extra: map[string]interface{}{
"ID": "bar",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.LinuxPackage,
Name: "filtered.deb",
Path: filtereddebfile.Name(),
Extra: map[string]interface{}{
"ID": "bar",
},
})
client := &DummyClient{}
assert.NoError(t, doPublish(ctx, client))
assert.True(t, client.CreatedRelease)
assert.True(t, client.UploadedFile)
assert.Contains(t, client.UploadedFileNames, "bin.deb")
assert.Contains(t, client.UploadedFileNames, "bin.tar.gz")
assert.Contains(t, client.UploadedFileNames, "filtered.deb")
assert.Contains(t, client.UploadedFileNames, "filtered.tar.gz")
}

func TestRunPipeWithIDsThenFilters(t *testing.T) {
folder, err := ioutil.TempDir("", "goreleasertest")
assert.NoError(t, err)
tarfile, err := os.Create(filepath.Join(folder, "bin.tar.gz"))
assert.NoError(t, err)
debfile, err := os.Create(filepath.Join(folder, "bin.deb"))
assert.NoError(t, err)
filteredtarfile, err := os.Create(filepath.Join(folder, "filtered.tar.gz"))
assert.NoError(t, err)
filtereddebfile, err := os.Create(filepath.Join(folder, "filtered.deb"))
assert.NoError(t, err)

var config = config.Project{
Dist: folder,
Release: config.Release{
GitHub: config.Repo{
Owner: "test",
Name: "test",
},
IDs: []string{"foo"},
},
}
var ctx = context.New(config)
ctx.Git = context.GitInfo{CurrentTag: "v1.0.0"}
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "bin.tar.gz",
Path: tarfile.Name(),
Extra: map[string]interface{}{
"ID": "foo",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.LinuxPackage,
Name: "bin.deb",
Path: debfile.Name(),
Extra: map[string]interface{}{
"ID": "foo",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.UploadableArchive,
Name: "filtered.tar.gz",
Path: filteredtarfile.Name(),
Extra: map[string]interface{}{
"ID": "bar",
},
})
ctx.Artifacts.Add(&artifact.Artifact{
Type: artifact.LinuxPackage,
Name: "filtered.deb",
Path: filtereddebfile.Name(),
Extra: map[string]interface{}{
"ID": "bar",
},
})
client := &DummyClient{}
assert.NoError(t, doPublish(ctx, client))
assert.True(t, client.CreatedRelease)
assert.True(t, client.UploadedFile)
assert.Contains(t, client.UploadedFileNames, "bin.deb")
assert.Contains(t, client.UploadedFileNames, "bin.tar.gz")
assert.NotContains(t, client.UploadedFileNames, "filtered.deb")
assert.NotContains(t, client.UploadedFileNames, "filtered.tar.gz")
}

func TestRunPipeReleaseCreationFailed(t *testing.T) {
Expand Down
15 changes: 8 additions & 7 deletions pkg/config/config.go
Expand Up @@ -175,13 +175,14 @@ type Archive struct {

// Release config used for the GitHub/GitLab release
type Release struct {
GitHub Repo `yaml:",omitempty"`
GitLab Repo `yaml:",omitempty"`
Gitea Repo `yaml:",omitempty"`
Draft bool `yaml:",omitempty"`
Disable bool `yaml:",omitempty"`
Prerelease string `yaml:",omitempty"`
NameTemplate string `yaml:"name_template,omitempty"`
GitHub Repo `yaml:",omitempty"`
GitLab Repo `yaml:",omitempty"`
Gitea Repo `yaml:",omitempty"`
Draft bool `yaml:",omitempty"`
Disable bool `yaml:",omitempty"`
Prerelease string `yaml:",omitempty"`
NameTemplate string `yaml:"name_template,omitempty"`
IDs []string `yaml:"ids,omitempty"`
}

// NFPM config
Expand Down
18 changes: 18 additions & 0 deletions www/content/release.md
Expand Up @@ -21,6 +21,12 @@ release:
owner: user
name: repo

# IDs of the archives to use.
# Defaults to all.
ids:
- foo
- bar

# If set to true, will not auto-publish the release.
# Default is false.
draft: true
Expand Down Expand Up @@ -53,6 +59,12 @@ release:
owner: user
name: repo

# IDs of the archives to use.
# Defaults to all.
ids:
- foo
- bar

# You can change the name of the GitLab release.
# Default is `{{.Tag}}`
name_template: "{{.ProjectName}}-v{{.Version}} {{.Env.USER}}"
Expand All @@ -73,6 +85,12 @@ release:
owner: user
name: repo

# IDs of the archives to use.
# Defaults to all.
ids:
- foo
- bar

# You can change the name of the Gitea release.
# Default is `{{.Tag}}`
name_template: "{{.ProjectName}}-v{{.Version}} {{.Env.USER}}"
Expand Down

0 comments on commit 93259b0

Please sign in to comment.