diff --git a/tag.go b/tag.go index 22b921c..f2519eb 100644 --- a/tag.go +++ b/tag.go @@ -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 { @@ -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 } @@ -100,7 +102,7 @@ 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, @@ -108,5 +110,9 @@ func (tp *tagpr) tagRelease(ctx context.Context, pr *github.PullRequest, currVer Body: &releases.Body, Draft: github.Bool(tp.cfg.ReleaseDraft()), }) - return err + if err != nil { + showGHError(err, resp) + return err + } + return nil } diff --git a/tagpr.go b/tagpr.go index 925cd9f..57644c9 100644 --- a/tagpr.go +++ b/tagpr.go @@ -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) @@ -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 } @@ -461,24 +463,28 @@ 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)) @@ -486,19 +492,23 @@ OUT: } 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) @@ -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 diff --git a/util.go b/util.go index fd80cc6..3624e2b 100644 --- a/util.go +++ b/util.go @@ -3,6 +3,9 @@ package tagpr import ( "fmt" "os" + "strings" + + "github.com/google/go-github/v57/github" ) func exists(filename string) bool { @@ -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) +}