Skip to content

Commit

Permalink
Merge pull request #173 from fujiwara/show-error
Browse files Browse the repository at this point in the history
Add showGHError()
  • Loading branch information
Songmu committed May 15, 2024
2 parents ad28eef + c9b7454 commit 01b6a42
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
14 changes: 10 additions & 4 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ func (tp *tagpr) latestPullRequest(ctx context.Context) (*github.PullRequest, er
if err != nil {
return nil, err
}
pulls, _, err := tp.gh.PullRequests.ListPullRequestsWithCommit(
pulls, resp, err := tp.gh.PullRequests.ListPullRequestsWithCommit(
ctx, tp.owner, tp.repo, commitish, nil)
if err != nil {
showGHError(err, resp)
return nil, err
}
if len(pulls) == 0 {
Expand Down Expand Up @@ -77,13 +78,14 @@ func (tp *tagpr) tagRelease(ctx context.Context, pr *github.PullRequest, currVer
if err != nil {
return nil
}
releases, _, err := tp.gh.Repositories.GenerateReleaseNotes(
releases, resp, err := tp.gh.Repositories.GenerateReleaseNotes(
ctx, tp.owner, tp.repo, &github.GenerateNotesOptions{
TagName: nextTag,
PreviousTagName: previousTag,
TargetCommitish: &targetCommitish,
})
if err != nil {
showGHError(err, resp)
return err
}

Expand All @@ -100,13 +102,17 @@ func (tp *tagpr) tagRelease(ctx context.Context, pr *github.PullRequest, currVer
return nil
}
// Don't use GenerateReleaseNote flag and use pre generated one
_, _, err = tp.gh.Repositories.CreateRelease(
_, resp, err = tp.gh.Repositories.CreateRelease(
ctx, tp.owner, tp.repo, &github.RepositoryRelease{
TagName: &nextTag,
TargetCommitish: &releaseBranch,
Name: &releases.Name,
Body: &releases.Body,
Draft: github.Bool(tp.cfg.ReleaseDraft()),
})
return err
if err != nil {
showGHError(err, resp)
return err
}
return nil
}
29 changes: 20 additions & 9 deletions tagpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,9 @@ func (tp *tagpr) Run(ctx context.Context) error {
if err != nil {
continue
}
issue, _, err := tp.gh.Issues.Get(ctx, tp.owner, tp.repo, prNum)
issue, resp, err := tp.gh.Issues.Get(ctx, tp.owner, tp.repo, prNum)
if err != nil {
showGHError(err, resp)
return err
}
prIssues = append(prIssues, issue)
Expand Down Expand Up @@ -259,12 +260,13 @@ func (tp *tagpr) Run(ctx context.Context) error {
}

head := fmt.Sprintf("%s:%s", tp.owner, rcBranch)
pulls, _, err := tp.gh.PullRequests.List(ctx, tp.owner, tp.repo,
pulls, resp, err := tp.gh.PullRequests.List(ctx, tp.owner, tp.repo,
&github.PullRequestListOptions{
Head: head,
Base: releaseBranch,
})
if err != nil {
showGHError(err, resp)
return err
}

Expand Down Expand Up @@ -461,44 +463,52 @@ OUT:
body = strings.TrimSpace(stuffs[1])
}
if currTagPR == nil {
pr, _, err := tp.gh.PullRequests.Create(ctx, tp.owner, tp.repo, &github.NewPullRequest{
pr, resp, err := tp.gh.PullRequests.Create(ctx, tp.owner, tp.repo, &github.NewPullRequest{
Title: github.String(title),
Body: github.String(body),
Base: &releaseBranch,
Head: github.String(head),
})
if err != nil {
showGHError(err, resp)
return err
}
addingLabels = append(addingLabels, autoLableName)
_, _, err = tp.gh.Issues.AddLabelsToIssue(
_, resp, err = tp.gh.Issues.AddLabelsToIssue(
ctx, tp.owner, tp.repo, *pr.Number, addingLabels)
if err != nil {
showGHError(err, resp)
return err
}
tmpPr, _, err := tp.gh.PullRequests.Get(ctx, tp.owner, tp.repo, *pr.Number)
tmpPr, resp, err := tp.gh.PullRequests.Get(ctx, tp.owner, tp.repo, *pr.Number)
if err == nil {
pr = tmpPr
} else {
showGHError(err, resp)
}
b, _ := json.Marshal(pr)
tp.setOutput("pull_request", string(b))
return nil
}
currTagPR.Title = github.String(title)
currTagPR.Body = github.String(mergeBody(*currTagPR.Body, body))
pr, _, err := tp.gh.PullRequests.Edit(ctx, tp.owner, tp.repo, *currTagPR.Number, currTagPR)
pr, resp, err := tp.gh.PullRequests.Edit(ctx, tp.owner, tp.repo, *currTagPR.Number, currTagPR)
if err != nil {
showGHError(err, resp)
return err
}
if len(addingLabels) > 0 {
_, _, err := tp.gh.Issues.AddLabelsToIssue(
_, resp, err := tp.gh.Issues.AddLabelsToIssue(
ctx, tp.owner, tp.repo, *currTagPR.Number, addingLabels)
if err != nil {
showGHError(err, resp)
return err
}
tmpPr, _, err := tp.gh.PullRequests.Get(ctx, tp.owner, tp.repo, *pr.Number)
tmpPr, resp, err := tp.gh.PullRequests.Get(ctx, tp.owner, tp.repo, *pr.Number)
if err == nil {
pr = tmpPr
} else {
showGHError(err, resp)
}
}
b, _ := json.Marshal(pr)
Expand Down Expand Up @@ -563,8 +573,9 @@ func (tp *tagpr) searchIssues(ctx context.Context, query string) ([]*github.Issu
// Fortunately, we don't need to take care of the page count in response, because
// the default value of per_page is 30 and we can't specify more than 30 commits due to
// the length limit specification of the query string.
issues, _, err := tp.gh.Search.Issues(ctx, query, nil)
issues, resp, err := tp.gh.Search.Issues(ctx, query, nil)
if err != nil {
showGHError(err, resp)
return nil, err
}
return issues.Issues, nil
Expand Down
22 changes: 22 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package tagpr
import (
"fmt"
"os"
"strings"

"github.com/google/go-github/v57/github"
)

func exists(filename string) bool {
Expand All @@ -28,3 +31,22 @@ func setOutput(name, value string) error {
_, err = f.WriteString(fmt.Sprintf("%s=%s\n", name, value))
return err
}

func showGHError(err error, resp *github.Response) {
title := "failed to request GitHub API"
message := err.Error()
if resp != nil {
respInfo := []string{
fmt.Sprintf("status:%d", resp.StatusCode),
}
for name := range resp.Header {
n := strings.ToLower(name)
if strings.HasPrefix(n, "x-ratelimit") || n == "x-github-request-id" || n == "retry-after" {
respInfo = append(respInfo, fmt.Sprintf("%s:%s", n, resp.Header.Get(name)))
}
}
message += " " + strings.Join(respInfo, ", ")
}
// https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#setting-an-error-message
fmt.Printf("::error title=%s::%s\n", title, message)
}

0 comments on commit 01b6a42

Please sign in to comment.