Skip to content

Commit

Permalink
feat(github): allow to open PRs as drafts (#4054)
Browse files Browse the repository at this point in the history
This allows to open a pull requests as a draft.

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed May 29, 2023
1 parent 7c6d656 commit f6b9ccb
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type ReleaseNotesGenerator interface {

// PullRequestOpener can open pull requests.
type PullRequestOpener interface {
OpenPullRequest(ctx *context.Context, base, head Repo, title string) error
OpenPullRequest(ctx *context.Context, base, head Repo, title string, draft bool) error
}

// New creates a new client depending on the token type.
Expand Down
5 changes: 4 additions & 1 deletion internal/client/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (c *githubClient) OpenPullRequest(
ctx *context.Context,
base, head Repo,
title string,
draft bool,
) error {
c.checkRateLimit(ctx)
if base.Branch == "" {
Expand All @@ -181,13 +182,15 @@ func (c *githubClient) OpenPullRequest(
}
log := log.
WithField("base", headString(base, Repo{})).
WithField("head", headString(base, head))
WithField("head", headString(base, head)).
WithField("draft", draft)
log.Info("opening pull request")
pr, res, err := c.client.PullRequests.Create(ctx, base.Owner, base.Name, &github.NewPullRequest{
Title: github.String(title),
Base: github.String(base.Branch),
Head: github.String(headString(base, head)),
Body: github.String("Automatically generated by [GoReleaser](https://goreleaser.com)"),
Draft: github.Bool(draft),
})
if err != nil {
if res.StatusCode == 422 {
Expand Down
13 changes: 7 additions & 6 deletions internal/client/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ func TestOpenPullRequestCrossRepo(t *testing.T) {
Name: "something",
Branch: "foo",
}
require.NoError(t, client.OpenPullRequest(ctx, base, head, "some title"))
require.NoError(t, client.OpenPullRequest(ctx, base, head, "some title", false))
}

func TestOpenPullRequestHappyPath(t *testing.T) {
Expand Down Expand Up @@ -488,10 +488,10 @@ func TestOpenPullRequestHappyPath(t *testing.T) {
Branch: "main",
}

require.NoError(t, client.OpenPullRequest(ctx, repo, Repo{}, "some title"))
require.NoError(t, client.OpenPullRequest(ctx, repo, Repo{}, "some title", false))
}

func TestOpenPullRequestNoBaseBranch(t *testing.T) {
func TestOpenPullRequestNoBaseBranchDraft(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

Expand All @@ -502,6 +502,7 @@ func TestOpenPullRequestNoBaseBranch(t *testing.T) {
require.NoError(t, json.Unmarshal(got, &pr))
require.Equal(t, "main", pr.GetBase())
require.Equal(t, "someone:something:foo", pr.GetHead())
require.Equal(t, true, pr.GetDraft())

r, err := os.Open("testdata/github/pull.json")
require.NoError(t, err)
Expand Down Expand Up @@ -540,7 +541,7 @@ func TestOpenPullRequestNoBaseBranch(t *testing.T) {

require.NoError(t, client.OpenPullRequest(ctx, repo, Repo{
Branch: "foo",
}, "some title"))
}, "some title", true))
}

func TestOpenPullRequestPRExists(t *testing.T) {
Expand Down Expand Up @@ -579,7 +580,7 @@ func TestOpenPullRequestPRExists(t *testing.T) {
Branch: "main",
}

require.NoError(t, client.OpenPullRequest(ctx, repo, Repo{}, "some title"))
require.NoError(t, client.OpenPullRequest(ctx, repo, Repo{}, "some title", false))
}

func TestOpenPullRequestBaseEmpty(t *testing.T) {
Expand Down Expand Up @@ -623,7 +624,7 @@ func TestOpenPullRequestBaseEmpty(t *testing.T) {
Branch: "main",
}

require.NoError(t, client.OpenPullRequest(ctx, repo, Repo{}, "some title"))
require.NoError(t, client.OpenPullRequest(ctx, repo, Repo{}, "some title", false))
}

func TestGitHubCreateFileHappyPathCreate(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/client/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Mock struct {
OpenedPullRequest bool
}

func (c *Mock) OpenPullRequest(_ *context.Context, _, _ Repo, _ string) error {
func (c *Mock) OpenPullRequest(_ *context.Context, _, _ Repo, _ string, _ bool) error {
c.OpenedPullRequest = true
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/brew/brew.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func doPublish(ctx *context.Context, formula *artifact.Artifact, cl client.Clien
Name: brew.Tap.PullRequest.Base.Name,
Owner: brew.Tap.PullRequest.Base.Owner,
Branch: brew.Tap.PullRequest.Base.Branch,
}, repo, title)
}, repo, title, brew.Tap.PullRequest.Draft)
}

func doRun(ctx *context.Context, brew config.Homebrew, cl client.ReleaserURLTemplater) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/krew/krew.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func doPublish(ctx *context.Context, manifest *artifact.Artifact, cl client.Clie
Name: cfg.Index.PullRequest.Base.Name,
Owner: cfg.Index.PullRequest.Base.Owner,
Branch: cfg.Index.PullRequest.Base.Branch,
}, repo, title)
}, repo, title, cfg.Index.PullRequest.Draft)
}

func buildManifestPath(folder, filename string) string {
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func doPublish(ctx *context.Context, prefetcher shaPrefetcher, cl client.Client,
Name: nix.Repository.PullRequest.Base.Name,
Owner: nix.Repository.PullRequest.Base.Owner,
Branch: nix.Repository.PullRequest.Base.Branch,
}, repo, title)
}, repo, title, nix.Repository.PullRequest.Draft)
}

func doBuildPkg(ctx *context.Context, data templateData) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/pipe/scoop/scoop.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func doPublish(ctx *context.Context, manifest *artifact.Artifact, cl client.Clie
Name: scoop.Bucket.PullRequest.Base.Name,
Owner: scoop.Bucket.PullRequest.Base.Owner,
Branch: scoop.Bucket.PullRequest.Base.Branch,
}, repo, title)
}, repo, title, scoop.Bucket.PullRequest.Draft)
}

// Manifest represents a scoop.sh App Manifest.
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func (a PullRequestBase) JSONSchema() *jsonschema.Schema {
type PullRequest struct {
Enabled bool `yaml:"enabled,omitempty" json:"enabled,omitempty"`
Base PullRequestBase `yaml:"base,omitempty" json:"base,omitempty"`
Draft bool `yaml:"draft,omitempty" json:"draft,omitempty"`
}

// HomebrewDependency represents Homebrew dependency.
Expand Down
5 changes: 4 additions & 1 deletion www/docs/cookbooks/set-a-custom-git-tag.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Setting a custom git tag

You can force the [build tag](/customization/build/#define-build-tag) and [previous changelog tag](/customization/release/#define-previous-tag) using environment variables.
You can force the
[build tag](/customization/build/#define-build-tag) and
[previous changelog tag](/customization/release/#define-previous-tag)
using environment variables.
This can be useful in cases where one git commit is referenced by multiple git tags.

Example usage:
Expand Down
8 changes: 4 additions & 4 deletions www/docs/customization/homebrew.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ brews:
# Whether to enable it or not.
enabled: true

# Base branch of the PR.
# If base is a string, the PR will be opened into the same repository.
# Whether to open the PR as a draft or not.
#
# Default: default repository branch.
base: main
# Default: false
# Since: v1.19
draft: true

# Base can also be another repository, in which case the owner and name
# above will be used as HEAD, allowing cross-repository pull requests.
Expand Down
8 changes: 4 additions & 4 deletions www/docs/customization/krew.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ krews:
# Whether to enable it or not.
enabled: true

# Base branch of the PR.
# If base is a string, the PR will be opened into the same repository.
# Whether to open the PR as a draft or not.
#
# Default: default repository branch.
base: main
# Default: false
# Since: v1.19
draft: true

# Base can also be another repository, in which case the owner and name
# above will be used as HEAD, allowing cross-repository pull requests.
Expand Down
8 changes: 4 additions & 4 deletions www/docs/customization/nix.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ nix:
# Whether to enable it or not.
enabled: true

# Base branch of the PR.
# If base is a string, the PR will be opened into the same repository.
# Whether to open the PR as a draft or not.
#
# Default: default repository branch.
base: main
# Default: false
# Since: v1.19
draft: true

# Base can also be another repository, in which case the owner and name
# above will be used as HEAD, allowing cross-repository pull requests.
Expand Down
8 changes: 4 additions & 4 deletions www/docs/customization/scoop.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ scoops:
# Whether to enable it or not.
enabled: true

# Base branch of the PR.
# If base is a string, the PR will be opened into the same repository.
# Whether to open the PR as a draft or not.
#
# Default: default repository branch.
base: main
# Default: false
# Since: v1.19
draft: true

# Base can also be another repository, in which case the owner and name
# above will be used as HEAD, allowing cross-repository pull requests.
Expand Down

0 comments on commit f6b9ccb

Please sign in to comment.