Skip to content

Commit 1181097

Browse files
committed
Improve log messages, commets and variable names
1 parent cb9ad7c commit 1181097

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

hack/tools/release/notes/list.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ import (
3232
type githubFromToPRLister struct {
3333
client *githubClient
3434
fromRef, toRef ref
35-
branch string
35+
// branch is optional. It helps optimize the PR query by restricting
36+
// the results to PRs merged in the selected branch and in main
37+
branch string
3638
}
3739

3840
func newGithubFromToPRLister(repo string, fromRef, toRef ref, branch string) *githubFromToPRLister {
@@ -44,8 +46,7 @@ func newGithubFromToPRLister(repo string, fromRef, toRef ref, branch string) *gi
4446
}
4547
}
4648

47-
// listPRs queries the PRs merged in a branch after
48-
// the `fromRef` reference and before `toRef` (included).
49+
// listPRs returns the PRs merged between `fromRef` and `toRef` (included).
4950
func (l *githubFromToPRLister) listPRs() ([]pr, error) {
5051
log.Printf("Computing diff between %s and %s", l.fromRef, l.toRef)
5152
diff, err := l.client.getDiffAllCommits(l.fromRef.value, l.toRef.value)
@@ -83,19 +84,19 @@ func (l *githubFromToPRLister) listPRs() ([]pr, error) {
8384
// any PRs entries not belonging to the computed diff
8485
toDate := toCommit.Committer.Date.Add(1 * time.Minute)
8586

86-
log.Printf("Listing PRs from branch %s from %s to %s", l.branch, fromDate, toDate)
87+
log.Printf("Listing PRs from %s to %s", fromDate, toDate)
8788
gPRs, err := l.client.listMergedPRs(fromDate, toDate, l.branch, "main")
8889
if err != nil {
8990
return nil, err
9091
}
9192

9293
log.Printf("Found %d PRs in github", len(gPRs))
9394

94-
prIDs := buildSetOfPrIDs(diff.Commits)
95+
selectedPRNumbers := buildSetOfPRNumbers(diff.Commits)
9596

9697
prs := make([]pr, 0, len(gPRs))
9798
for _, p := range gPRs {
98-
if _, ok := prIDs[fmt.Sprintf("%d", p.Number)]; !ok {
99+
if _, ok := selectedPRNumbers[fmt.Sprintf("%d", p.Number)]; !ok {
99100
continue
100101
}
101102
labels := make([]string, 0, len(p.Labels))
@@ -111,24 +112,24 @@ func (l *githubFromToPRLister) listPRs() ([]pr, error) {
111112

112113
log.Printf("%d PRs match the commits from the git diff", len(prs))
113114

114-
if len(prs) != len(prIDs) {
115-
return nil, errors.Errorf("expected %d PRs from commit list but only found %d", len(prIDs), len(prs))
115+
if len(prs) != len(selectedPRNumbers) {
116+
return nil, errors.Errorf("expected %d PRs from commit list but only found %d", len(selectedPRNumbers), len(prs))
116117
}
117118

118119
return prs, nil
119120
}
120121

121122
var mergeCommitMessage = regexp.MustCompile(`(?m)^Merge pull request #(\d+) .*$`)
122123

123-
func buildSetOfPrIDs(commits []githubCommitNode) map[string]struct{} {
124-
prIDs := make(map[string]struct{})
124+
func buildSetOfPRNumbers(commits []githubCommitNode) map[string]struct{} {
125+
prNumbers := make(map[string]struct{})
125126
for _, commit := range commits {
126127
match := mergeCommitMessage.FindStringSubmatch(commit.Commit.Message)
127128
if len(match) != 2 {
128129
continue
129130
}
130-
prIDs[match[1]] = struct{}{}
131+
prNumbers[match[1]] = struct{}{}
131132
}
132133

133-
return prIDs
134+
return prNumbers
134135
}

hack/tools/release/notes/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func readCmdConfig() *notesCmdConfig {
6161
config := &notesCmdConfig{}
6262

6363
flag.StringVar(&config.repo, "repository", "kubernetes-sigs/cluster-api", "The repo to run the tool from.")
64-
flag.StringVar(&config.fromRef, "from", "", "The tag or commit to start from. If not set, it will be calculated from release.")
64+
flag.StringVar(&config.fromRef, "from", "", "The tag or commit to start from. It must be formatted as heads/<branch name> for branches and tags/<tag name> for tags. If not set, it will be calculated from release.")
6565
flag.StringVar(&config.toRef, "to", "", "The ref (tag, branch or commit to stop at. It must be formatted as heads/<branch name> for branches and tags/<tag name> for tags. If not set, it will default to branch.")
6666
flag.StringVar(&config.branch, "branch", "", "The branch to generate the notes from. If not set, it will be calculated from release.")
6767
flag.StringVar(&config.newTag, "release", "", "The tag for the enw release.")
@@ -187,6 +187,8 @@ func computeConfigDefaults(config *notesCmdConfig) error {
187187
return nil
188188
}
189189

190+
// defaultBranchForNewTag calculates the branch to cut a release
191+
// based on the new release tag.
190192
func defaultBranchForNewTag(newTag semver.Version) string {
191193
if newTag.Patch == 0 {
192194
if len(newTag.Pre) == 0 {

hack/tools/release/notes/ref.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,11 @@ import (
2525
"github.com/pkg/errors"
2626
)
2727

28+
// ref represents a git reference.
2829
type ref struct {
29-
reType, value string
30+
// reType is the ref type: tags for a tag, head for a branch, commit for a commit.
31+
reType string
32+
value string
3033
}
3134

3235
func (r ref) String() string {

hack/tools/release/notes/release_notes_integration_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ func TestReleaseNotesIntegration(t *testing.T) {
4444
expected: "test/golden/v1.3.10.md",
4545
},
4646
{
47-
// The release notes command computes everything from last tag
48-
// to HEAD. Hence if we use the head of release-1.5, this test will
49-
// become invalid every time we backport some PR to release branch release-1.5.
50-
// Here we cheat a little by poiting to worktree to v1.5.0, which is the
51-
// release that this test is simulating, so it should not exist yet. But
52-
// it represents accurately the HEAD of release-1.5 when we released v1.5.0.
47+
// By default when using the `--release` options, notes command computes
48+
// everything from last tag to HEAD. Hence if we use the head of release-1.5,
49+
// this test will become invalid every time we backport some PR to release
50+
// branch release-1.5.
51+
// Instead, to simulate the v1.5.0 release, we manually set the `--to` flag,
52+
// which sets the upper boundary for the PR search.
5353
name: "new minor",
5454
args: []string{"--from", "tags/v1.4.0", "--to", "tags/v1.5.0", "--branch", "release-1.5"},
5555
expected: "test/golden/v1.5.0.md",

0 commit comments

Comments
 (0)