Skip to content
Permalink
Browse files
fix: allow to template scoop/brew/krew repo ref (#3521)
closes #3508

- adds template support for krew and scoop repo refs
- template branch on reporef on brew as well

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 2, 2022
1 parent 7544f7a commit 1a69d44d6d37021530b1d0f49d5e0fb1aa1f7299
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 126 deletions.
@@ -4,10 +4,33 @@ import (
"github.com/goreleaser/goreleaser/pkg/config"
)

// RepoFromRef converts a config.RepoRef into a Repo.
func RepoFromRef(ref config.RepoRef) Repo {
return Repo{
Owner: ref.Owner,
Name: ref.Name,
Branch: ref.Branch,
}
}

// TemplateRef templates a config.RepoFromRef
func TemplateRef(apply func(s string) (string, error), ref config.RepoRef) (config.RepoRef, error) {
name, err := apply(ref.Name)
if err != nil {
return ref, err
}
owner, err := apply(ref.Owner)
if err != nil {
return ref, err
}
branch, err := apply(ref.Branch)
if err != nil {
return ref, err
}
return config.RepoRef{
Owner: owner,
Name: name,
Token: ref.Token,
Branch: branch,
}, nil
}
@@ -1,6 +1,7 @@
package client

import (
"fmt"
"testing"

"github.com/goreleaser/goreleaser/pkg/config"
@@ -25,3 +26,50 @@ func TestRepoFromRef(t *testing.T) {
require.Equal(t, name, repo.Name)
require.Equal(t, branch, repo.Branch)
}

func TestTemplateRef(t *testing.T) {
expected := config.RepoRef{
Owner: "owner",
Name: "name",
Branch: "branch",
Token: "token",
}
t.Run("success", func(t *testing.T) {
ref, err := TemplateRef(func(s string) (string, error) {
if s == "token" {
return "", fmt.Errorf("nope")
}
return s, nil
}, expected)
require.NoError(t, err)
require.Equal(t, expected, ref)
})

t.Run("fail owner", func(t *testing.T) {
_, err := TemplateRef(func(s string) (string, error) {
if s == "token" || s == "owner" {
return "", fmt.Errorf("nope")
}
return s, nil
}, expected)
require.Error(t, err)
})
t.Run("fail name", func(t *testing.T) {
_, err := TemplateRef(func(s string) (string, error) {
if s == "token" || s == "name" {
return "", fmt.Errorf("nope")
}
return s, nil
}, expected)
require.Error(t, err)
})
t.Run("fail branch", func(t *testing.T) {
_, err := TemplateRef(func(s string) (string, error) {
if s == "token" || s == "branch" {
return "", fmt.Errorf("nope")
}
return s, nil
}, expected)
require.Error(t, err)
})
}
@@ -196,17 +196,11 @@ func doRun(ctx *context.Context, brew config.Homebrew, cl client.Client) error {
}
brew.Name = name

tapOwner, err := tmpl.New(ctx).Apply(brew.Tap.Owner)
ref, err := client.TemplateRef(tmpl.New(ctx).Apply, brew.Tap)
if err != nil {
return err
}
brew.Tap.Owner = tapOwner

tapName, err := tmpl.New(ctx).Apply(brew.Tap.Name)
if err != nil {
return err
}
brew.Tap.Name = tapName
brew.Tap = ref

skipUpload, err := tmpl.New(ctx).Apply(brew.SkipUpload)
if err != nil {
@@ -301,6 +301,11 @@ func doPublish(ctx *context.Context, manifest *artifact.Artifact, cl client.Clie
return pipe.Skip("prerelease detected with 'auto' upload, skipping krew publish")
}

ref, err := client.TemplateRef(tmpl.New(ctx).Apply, cfg.Index)
if err != nil {
return err
}
cfg.Index = ref
repo := client.RepoFromRef(cfg.Index)

gpath := buildManifestPath(manifestsFolder, manifest.Name)
@@ -159,6 +159,12 @@ func doPublish(ctx *context.Context, cl client.Client) error {
return err
}

ref, err := client.TemplateRef(tmpl.New(ctx).Apply, scoop.Bucket)
if err != nil {
return err
}
scoop.Bucket = ref

repo := client.RepoFromRef(scoop.Bucket)
return cl.CreateFile(
ctx,

0 comments on commit 1a69d44

Please sign in to comment.