Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pull request templates #4105

Merged
merged 2 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 38 additions & 9 deletions internal/client/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ func headString(base, head Repo) string {
}, ":")
}

func (c *githubClient) getPRTemplate(ctx *context.Context, repo Repo) (string, error) {
content, _, _, err := c.client.Repositories.GetContents(
ctx, repo.Owner, repo.Name,
".github/PULL_REQUEST_TEMPLATE.md",
&github.RepositoryContentGetOptions{
Ref: repo.Branch,
},
)
if err != nil {
return "", err
}
return content.GetContent()
}

const prFooter = "###### Automated with [GoReleaser](https://goreleaser.com)"

func (c *githubClient) OpenPullRequest(
ctx *context.Context,
base, head Repo,
Expand All @@ -180,26 +196,38 @@ func (c *githubClient) OpenPullRequest(
}
base.Branch = def
}
tpl, err := c.getPRTemplate(ctx, base)
if err != nil {
log.WithError(err).Debug("no pull request template found...")
}
if len(tpl) > 0 {
log.Info("got a pr template")
}
log := log.
WithField("base", headString(base, Repo{})).
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),
})
pr, res, err := c.client.PullRequests.Create(
ctx,
firstNonEmpty(base.Owner, head.Owner),
firstNonEmpty(base.Name, head.Name),
&github.NewPullRequest{
Title: github.String(title),
Base: github.String(base.Branch),
Head: github.String(headString(base, head)),
Body: github.String(strings.Join([]string{tpl, prFooter}, "\n")),
Draft: github.Bool(draft),
},
)
if err != nil {
if res.StatusCode == 422 {
log.Warn("PR already exists, doing nothing...")
log.WithError(err).Warn("pull request validation failed")
return nil
}
return fmt.Errorf("could not create pull request: %w", err)
}
log.WithField("url", pr.GetHTMLURL()).Info("opened")
log.WithField("url", pr.GetHTMLURL()).Info("pull request created")
return nil
}

Expand Down Expand Up @@ -240,6 +268,7 @@ func (c *githubClient) CreateFile(
log.
WithField("repository", repo.String()).
WithField("branch", repo.Branch).
WithField("file", path).
Info("pushing")

if defBranch != branch && branch != "" {
Expand Down
39 changes: 39 additions & 0 deletions internal/client/github_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -405,17 +406,30 @@ func TestCloseMilestone(t *testing.T) {
require.NoError(t, client.CloseMilestone(ctx, repo, "v1.13.0"))
}

const testPRTemplate = "fake template\n- [ ] mark this\n---"

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

if r.URL.Path == "/repos/someone/something/contents/.github/PULL_REQUEST_TEMPLATE.md" {
content := github.RepositoryContent{
Encoding: github.String("base64"),
Content: github.String(base64.StdEncoding.EncodeToString([]byte(testPRTemplate))),
}
bts, _ := json.Marshal(content)
_, _ = w.Write(bts)
return
}

if r.URL.Path == "/repos/someone/something/pulls" {
got, err := io.ReadAll(r.Body)
require.NoError(t, err)
var pr github.NewPullRequest
require.NoError(t, json.Unmarshal(got, &pr))
require.Equal(t, "main", pr.GetBase())
require.Equal(t, "someoneelse:something:foo", pr.GetHead())
require.Equal(t, testPRTemplate+"\n"+prFooter, pr.GetBody())
r, err := os.Open("testdata/github/pull.json")
require.NoError(t, err)
_, err = io.Copy(w, r)
Expand Down Expand Up @@ -457,6 +471,16 @@ func TestOpenPullRequestHappyPath(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

if r.URL.Path == "/repos/someone/something/contents/.github/PULL_REQUEST_TEMPLATE.md" {
content := github.RepositoryContent{
Encoding: github.String("base64"),
Content: github.String(base64.StdEncoding.EncodeToString([]byte(testPRTemplate))),
}
bts, _ := json.Marshal(content)
_, _ = w.Write(bts)
return
}

if r.URL.Path == "/repos/someone/something/pulls" {
r, err := os.Open("testdata/github/pull.json")
require.NoError(t, err)
Expand Down Expand Up @@ -495,6 +519,11 @@ func TestOpenPullRequestNoBaseBranchDraft(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

if r.URL.Path == "/repos/someone/something/contents/.github/PULL_REQUEST_TEMPLATE.md" {
w.WriteHeader(http.StatusNotFound)
return
}

if r.URL.Path == "/repos/someone/something/pulls" {
got, err := io.ReadAll(r.Body)
require.NoError(t, err)
Expand Down Expand Up @@ -548,6 +577,11 @@ func TestOpenPullRequestPRExists(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

if r.URL.Path == "/repos/someone/something/contents/.github/PULL_REQUEST_TEMPLATE.md" {
w.WriteHeader(http.StatusNotFound)
return
}

if r.URL.Path == "/repos/someone/something/pulls" {
w.WriteHeader(http.StatusUnprocessableEntity)
r, err := os.Open("testdata/github/pull.json")
Expand Down Expand Up @@ -587,6 +621,11 @@ func TestOpenPullRequestBaseEmpty(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

if r.URL.Path == "/repos/someone/something/contents/.github/PULL_REQUEST_TEMPLATE.md" {
w.WriteHeader(http.StatusNotFound)
return
}

if r.URL.Path == "/repos/someone/something/pulls" {
r, err := os.Open("testdata/github/pull.json")
require.NoError(t, err)
Expand Down