Skip to content

Commit 321c537

Browse files
Deduplicating area in title
1 parent a8356e4 commit 321c537

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

hack/tools/release/notes.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,7 @@ type releaseNoteEntry struct {
435435
prNumber string
436436
}
437437

438+
// modifyEntryTitle removes the specified prefixes from the title.
438439
func modifyEntryTitle(title string, prefixes []string) string {
439440
entryWithoutTag := title
440441
for _, prefix := range prefixes {
@@ -444,6 +445,16 @@ func modifyEntryTitle(title string, prefixes []string) string {
444445
return strings.ToUpper(string(entryWithoutTag[0])) + entryWithoutTag[1:]
445446
}
446447

448+
// trimAreaFromTitle removes the prefixed area from title to avoid duplication.
449+
func trimAreaFromTitle(title, area string) string {
450+
titleWithoutArea := title
451+
pattern := `(?i)^` + regexp.QuoteMeta(area+":")
452+
re := regexp.MustCompile(pattern)
453+
titleWithoutArea = re.ReplaceAllString(titleWithoutArea, "")
454+
titleWithoutArea = strings.TrimSpace(titleWithoutArea)
455+
return titleWithoutArea
456+
}
457+
447458
// generateReleaseNoteEntry processes a commit into a PR line item for the release notes.
448459
func generateReleaseNoteEntry(c *commit) (*releaseNoteEntry, error) {
449460
entry := &releaseNoteEntry{}
@@ -501,6 +512,7 @@ func generateReleaseNoteEntry(c *commit) (*releaseNoteEntry, error) {
501512
}
502513

503514
if *prefixAreaLabel {
515+
entry.title = trimAreaFromTitle(entry.title, area)
504516
entry.title = fmt.Sprintf("- %s: %s", area, entry.title)
505517
} else {
506518
entry.title = fmt.Sprintf("- %s", entry.title)

hack/tools/release/notes_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,45 @@ func Test_trimTitle(t *testing.T) {
6161
})
6262
}
6363
}
64+
65+
func Test_trimAreaFromTitle(t *testing.T) {
66+
tests := []struct {
67+
name string
68+
title string
69+
area string
70+
want string
71+
}{
72+
{
73+
name: "PR title with area",
74+
title: "e2e: improve logging for a detected rollout",
75+
area: "e2e",
76+
want: "improve logging for a detected rollout",
77+
},
78+
{
79+
name: "PR title without area",
80+
title: "improve logging for a detected rollout",
81+
area: "e2e",
82+
want: "improve logging for a detected rollout",
83+
},
84+
{
85+
name: "PR title without area being prefixed",
86+
title: "test/e2e: improve logging for a detected rollout",
87+
area: "e2e",
88+
want: "test/e2e: improve logging for a detected rollout",
89+
},
90+
{
91+
name: "PR title without space between area and title",
92+
title: "e2e:improve logging for a detected rollout",
93+
area: "e2e",
94+
want: "improve logging for a detected rollout",
95+
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
if got := trimAreaFromTitle(tt.title, tt.area); got != tt.want {
101+
t.Errorf("trimAreaFromTitle() = %v, want %v", got, tt.want)
102+
}
103+
})
104+
}
105+
}

0 commit comments

Comments
 (0)