Skip to content

Commit 0d0b481

Browse files
authored
*: Misc. updates (#6)
Signed-off-by: timflannagan <[email protected]>
1 parent 19022a2 commit 0d0b481

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

internal/generator/generator.go

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"regexp"
7+
"sort"
78
"strings"
89

910
"github.com/google/go-github/v68/github"
@@ -77,7 +78,7 @@ func (g *Generator) getReferencedPRs(ctx context.Context, _ []*github.Commit) ([
7778
var prs []*github.PullRequest
7879

7980
// Search for PRs referenced in commit messages
80-
query := fmt.Sprintf("repo:%s/%s is:pr is:merged", g.owner, g.repo)
81+
query := fmt.Sprintf(`repo:%s/%s is:pr is:merged label:"release-note","release-note-needed"`, g.owner, g.repo)
8182
result, _, err := g.client.Search.Issues(ctx, query, &github.SearchOptions{
8283
TextMatch: true,
8384
ListOptions: github.ListOptions{
@@ -110,14 +111,41 @@ func (g *Generator) generateChangelog(prs []*github.PullRequest) string {
110111
kind := g.getPRKind(pr)
111112
buckets[kind] = append(buckets[kind], pr)
112113
}
113-
114-
// Print each bucket
115-
for kind, header := range kindHeaders {
116-
if prs, ok := buckets[kind]; ok && len(prs) > 0 {
117-
changelog.WriteString(fmt.Sprintf("\n## %s\n\n", header))
118-
for _, pr := range prs {
119-
changelog.WriteString(fmt.Sprintf("- %s (#%d)\n", *pr.Title, *pr.Number))
114+
// sort the buckets by kind to ensure deterministic output
115+
kinds := make([]string, 0, len(kindHeaders))
116+
for kind := range kindHeaders {
117+
kinds = append(kinds, kind)
118+
}
119+
sort.Strings(kinds)
120+
121+
// build the changelog
122+
for _, kind := range kinds {
123+
header := kindHeaders[kind]
124+
prs := buckets[kind]
125+
if len(prs) == 0 {
126+
continue
127+
}
128+
changelog.WriteString(fmt.Sprintf("\n## %s\n\n", header))
129+
for _, pr := range prs {
130+
body := pr.GetBody()
131+
if body == "" {
132+
continue
120133
}
134+
// attempt to extract a release-note from the PR body
135+
match := releaseNoteRE.FindStringSubmatch(body)
136+
if len(match) < 2 {
137+
// skip PRs that have the release-note label but no release-note body.
138+
// TODO(tim): this shouldn't be possible with the labeler being a required
139+
// check, but we'll check for it anyway in case users have manually added
140+
// the label to a PR.
141+
continue
142+
}
143+
note := match[1]
144+
if note == "" {
145+
// TODO(tim): we should probably log this as an error as this is unexpected
146+
continue
147+
}
148+
changelog.WriteString(fmt.Sprintf("- %s (#%d)\n", note, pr.GetNumber()))
121149
}
122150
}
123151

@@ -129,9 +157,9 @@ func (g *Generator) getPRKind(pr *github.PullRequest) string {
129157
// Check labels first
130158
for _, label := range pr.Labels {
131159
switch *label.Name {
132-
case "kind/new-feature":
160+
case "kind/feature", "kind/new_feature":
133161
return "new_feature"
134-
case "kind/bug":
162+
case "kind/fix", "kind/bug_fix":
135163
return "bug_fix"
136164
case "kind/breaking_change":
137165
return "breaking_change"
@@ -142,20 +170,5 @@ func (g *Generator) getPRKind(pr *github.PullRequest) string {
142170
}
143171
}
144172

145-
// Fall back to title-based detection
146-
title := strings.ToLower(*pr.Title)
147-
switch {
148-
case strings.Contains(title, "feat") || strings.Contains(title, "feature"):
149-
return "new_feature"
150-
case strings.Contains(title, "fix") || strings.Contains(title, "bug"):
151-
return "bug_fix"
152-
case strings.Contains(title, "break") || strings.Contains(title, "breaking"):
153-
return "breaking_change"
154-
case strings.Contains(title, "doc") || strings.Contains(title, "docs"):
155-
return "documentation"
156-
case strings.Contains(title, "perf") || strings.Contains(title, "performance"):
157-
return "performance"
158-
default:
159-
return "other"
160-
}
173+
return "other"
161174
}

internal/generator/generator_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ func TestGenerateChangelog(t *testing.T) {
4545
pullRequests: []*github.PullRequest{{
4646
Number: github.Ptr(42),
4747
Title: github.Ptr("Add new feature"),
48-
Body: github.Ptr("```release-note\\nMy note for PR42\\n```"),
48+
Body: github.Ptr("```release-note\nMy note for PR42\n```"),
4949
Labels: []*github.Label{{
50-
Name: github.Ptr("kind/new-feature"),
50+
Name: github.Ptr("kind/feature"),
5151
}},
5252
}},
5353
expectedChangelog: `
5454
## 🚀 Features
5555
56-
- Add new feature (#42)
56+
- My note for PR42 (#42)
5757
`,
5858
expectError: false,
5959
},
@@ -96,40 +96,40 @@ func TestGenerateChangelog(t *testing.T) {
9696
{
9797
Number: github.Ptr(42),
9898
Title: github.Ptr("Add new feature"),
99-
Body: github.Ptr("```release-note\\ASDF\\n```"),
99+
Body: github.Ptr("```release-note\nImplement new feature\n```"),
100100
Labels: []*github.Label{{
101-
Name: github.Ptr("kind/"),
101+
Name: github.Ptr("kind/feature"),
102102
}},
103103
},
104104
{
105105
Number: github.Ptr(43),
106106
Title: github.Ptr("Fix bug"),
107-
Body: github.Ptr("```release-note\\nFixed a bug\\n```"),
107+
Body: github.Ptr("```release-note\nFixed a bug\n```"),
108108
Labels: []*github.Label{{
109-
Name: github.Ptr("kind/bug"),
109+
Name: github.Ptr("kind/fix"),
110110
}},
111111
},
112112
{
113113
Number: github.Ptr(44),
114114
Title: github.Ptr("Remove old feature"),
115-
Body: github.Ptr("```release-note\\nRemoved old feature\\n```"),
115+
Body: github.Ptr("```release-note\nRemoved old feature\n```"),
116116
Labels: []*github.Label{{
117117
Name: github.Ptr("kind/breaking_change"),
118118
}},
119119
},
120120
},
121121
expectedChangelog: `
122-
## 🚀 Features
122+
## 💥 Breaking Changes
123123
124-
- Add new feature (#42)
124+
- Removed old feature (#44)
125125
126126
## 🐛 Bug Fixes
127127
128-
- Fix bug (#43)
128+
- Fixed a bug (#43)
129129
130-
## 💥 Breaking Changes
130+
## 🚀 Features
131131
132-
- Remove old feature (#44)
132+
- Implement new feature (#42)
133133
`,
134134
expectError: false,
135135
},

0 commit comments

Comments
 (0)