Skip to content

Commit

Permalink
feat: allow to pr krew plugin manifests (#3915)
Browse files Browse the repository at this point in the history
following up on #3903

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Apr 7, 2023
1 parent 01b14f9 commit 5805c5d
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
17 changes: 16 additions & 1 deletion internal/pipe/krew/krew.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,22 @@ func doPublish(ctx *context.Context, manifest *artifact.Artifact, cl client.Clie
return err
}

return cl.CreateFile(ctx, author, repo, content, gpath, msg)
if !cfg.Index.PullRequest.Enabled {
return cl.CreateFile(ctx, author, repo, content, gpath, msg)
}

log.Info("brews.pull_request enabled, creating a PR")
pcl, ok := cl.(client.PullRequestOpener)
if !ok {
return fmt.Errorf("client does not support pull requests")
}

if err := cl.CreateFile(ctx, author, repo, content, gpath, msg); err != nil {
return err
}

title := fmt.Sprintf("Updated %s to %s", ctx.Config.ProjectName, ctx.Version)
return pcl.OpenPullRequest(ctx, repo, cfg.Index.PullRequest.Base, title)
}

func buildManifestPath(folder, filename string) string {
Expand Down
53 changes: 53 additions & 0 deletions internal/pipe/krew/krew_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,59 @@ func TestFullPipe(t *testing.T) {
}
}

func TestRunPipePullRequest(t *testing.T) {
folder := t.TempDir()
ctx := testctx.NewWithCfg(
config.Project{
Dist: folder,
ProjectName: "foo",
Krews: []config.Krew{
{
Name: "foo",
Homepage: "https://goreleaser.com",
ShortDescription: "test",
Description: "Fake desc",
Index: config.RepoRef{
Owner: "foo",
Name: "bar",
Branch: "update-{{.Version}}",
PullRequest: config.PullRequest{
Enabled: true,
},
},
},
},
},
testctx.WithVersion("1.2.1"),
testctx.WithCurrentTag("v1.2.1"),
)
path := filepath.Join(folder, "dist/foo_darwin_all/foo")
ctx.Artifacts.Add(&artifact.Artifact{
Name: "foo_macos.tar.gz",
Path: path,
Goos: "darwin",
Goarch: "all",
Type: artifact.UploadableArchive,
Extra: map[string]interface{}{
artifact.ExtraID: "foo",
artifact.ExtraFormat: "tar.gz",
artifact.ExtraBinaries: []string{"foo"},
},
})

require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
f, err := os.Create(path)
require.NoError(t, err)
require.NoError(t, f.Close())

client := client.NewMock()
require.NoError(t, runAll(ctx, client))
require.NoError(t, publishAll(ctx, client))
require.True(t, client.CreatedFile)
require.True(t, client.OpenedPullRequest)
golden.RequireEqualYaml(t, []byte(client.Content))
}

func TestRunPipeUniversalBinary(t *testing.T) {
folder := t.TempDir()

Expand Down
24 changes: 24 additions & 0 deletions internal/pipe/krew/testdata/TestRunPipePullRequest.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
name: foo
spec:
version: v1.2.1
platforms:
- bin: foo
uri: https://dummyhost/download/v1.2.1/foo_macos.tar.gz
sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
selector:
matchLabels:
os: darwin
arch: amd64
- bin: foo
uri: https://dummyhost/download/v1.2.1/foo_macos.tar.gz
sha256: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
selector:
matchLabels:
os: darwin
arch: arm64
shortDescription: test
homepage: https://goreleaser.com
description: Fake desc
14 changes: 14 additions & 0 deletions www/docs/customization/krew.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ krews:
# provided to GoReleaser
token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"

# Sets up pull request creation instead of just pushing to the given branch.
# Make sure the 'branch' property is different from base before enabling
# it.
#
# Since: v1.17
pull_request:
# Whether to enable it or not.
enabled: true

# Base branch of the PR.
#
# Default: default repository branch.
base: main

# URL which is determined by the given Token (github or
# gitlab)
# Default:
Expand Down

0 comments on commit 5805c5d

Please sign in to comment.