From 9b67b06f77fb7dd4fae584239215434eab9a977b Mon Sep 17 00:00:00 2001 From: na-ga <537006+na-ga@users.noreply.github.com> Date: Tue, 29 Mar 2022 18:56:32 +0900 Subject: [PATCH 1/3] fix: behavior with usePullRequestMetadata option --- release.go | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/release.go b/release.go index 98d818a..27a624a 100644 --- a/release.go +++ b/release.go @@ -235,7 +235,8 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] } gen, limit := cfg.ReleaseNoteGenerator, 1000 - prs := make(map[int]*github.PullRequest, limit) + shaPRs := make(map[string]*github.PullRequest, limit) + numPRs := make(map[int]*github.PullRequest, limit) if gen.UsePullRequestMetadata { opts := &ListPullRequestOptions{ State: PullRequestStateClosed, @@ -248,11 +249,34 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] return nil, err } for i := range v { + sha := *v[i].MergeCommitSHA + shaPRs[sha] = v[i] number := *v[i].Number - prs[number] = v[i] + numPRs[number] = v[i] } } + getPullRequest := func(commit Commit) (*github.PullRequest, error) { + if !commit.IsMerge() { + return nil, nil + } + if pr, ok := shaPRs[commit.Hash]; ok { + return pr, nil + } + prNumber, ok := commit.PullRequestNumber() + if !ok { + return nil, nil + } + if pr, ok := numPRs[prNumber]; ok { + return pr, nil + } + pr, err := ghClient.getPullRequest(ctx, event.Owner, event.Repo, prNumber) + if err != nil { + return nil, err + } + return pr, nil + } + out := make([]ReleaseCommit, 0, len(commits)) for _, commit := range commits { @@ -273,22 +297,15 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] } if gen.UsePullRequestMetadata { - prNumber, ok := commit.PullRequestNumber() - if !ok { - continue - } - c.PullRequestNumber = prNumber - - var err error - pr, ok := prs[prNumber] - if !ok { - pr, err = ghClient.getPullRequest(ctx, event.Owner, event.Repo, prNumber) - } + pr, err := getPullRequest(commit) if err != nil { return nil, err } - c.PullRequestOwner = pr.GetUser().GetLogin() - c.ReleaseNote = extractReleaseNote(pr.GetTitle(), pr.GetBody(), gen.UseReleaseNoteBlock) + if pr != nil { + c.PullRequestNumber = pr.GetNumber() + c.PullRequestOwner = pr.GetUser().GetLogin() + c.ReleaseNote = extractReleaseNote(pr.GetTitle(), pr.GetBody(), gen.UseReleaseNoteBlock) + } } out = append(out, c) From 8dde46a44c7a73ee4bdb3836f265df359c3ed5d0 Mon Sep 17 00:00:00 2001 From: na-ga <537006+na-ga@users.noreply.github.com> Date: Wed, 30 Mar 2022 11:33:26 +0900 Subject: [PATCH 2/3] refactor: remove pr map by pr number --- release.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/release.go b/release.go index 27a624a..bb51a91 100644 --- a/release.go +++ b/release.go @@ -235,8 +235,7 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] } gen, limit := cfg.ReleaseNoteGenerator, 1000 - shaPRs := make(map[string]*github.PullRequest, limit) - numPRs := make(map[int]*github.PullRequest, limit) + prs := make(map[string]*github.PullRequest, limit) if gen.UsePullRequestMetadata { opts := &ListPullRequestOptions{ State: PullRequestStateClosed, @@ -250,9 +249,7 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] } for i := range v { sha := *v[i].MergeCommitSHA - shaPRs[sha] = v[i] - number := *v[i].Number - numPRs[number] = v[i] + prs[sha] = v[i] } } @@ -260,16 +257,13 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] if !commit.IsMerge() { return nil, nil } - if pr, ok := shaPRs[commit.Hash]; ok { + if pr, ok := prs[commit.Hash]; ok { return pr, nil } prNumber, ok := commit.PullRequestNumber() if !ok { return nil, nil } - if pr, ok := numPRs[prNumber]; ok { - return pr, nil - } pr, err := ghClient.getPullRequest(ctx, event.Owner, event.Repo, prNumber) if err != nil { return nil, err From 4b07ef43d76af058e4c932188c2cff2a0dfe13bb Mon Sep 17 00:00:00 2001 From: na-ga <537006+na-ga@users.noreply.github.com> Date: Wed, 30 Mar 2022 12:04:33 +0900 Subject: [PATCH 3/3] fix: error handling when get pull request --- release.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/release.go b/release.go index bb51a91..ef02079 100644 --- a/release.go +++ b/release.go @@ -18,6 +18,7 @@ import ( "context" "encoding/json" "fmt" + "log" "regexp" "strings" @@ -266,7 +267,7 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] } pr, err := ghClient.getPullRequest(ctx, event.Owner, event.Repo, prNumber) if err != nil { - return nil, err + return nil, fmt.Errorf("failed to get pull request by number %d: %v", prNumber, err) } return pr, nil } @@ -293,7 +294,8 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits [] if gen.UsePullRequestMetadata { pr, err := getPullRequest(commit) if err != nil { - return nil, err + // only error logging, ignore error + log.Printf("Failed to get pull request: %v\n", err) } if pr != nil { c.PullRequestNumber = pr.GetNumber()