Skip to content

Commit 5ad6ea1

Browse files
committed
chore: externalizes template functions
1 parent 60336c1 commit 5ad6ea1

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

pkg/cmd/generate/cmd.go

+2-16
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,8 @@ func NewCmd() *cobra.Command {
3434
dependencies = simplifyDepsPRs(dependencies)
3535

3636
t, err := template.New("changelog").Funcs(map[string]interface{}{
37-
"withLabels": func(prs []github.PullRequest, labels ...string) []github.PullRequest {
38-
prsWithLabel := make([]github.PullRequest, 0)
39-
for i := range prs {
40-
pr := &prs[i]
41-
if Contains(pr.Labels, labels...) {
42-
prsWithLabel = append(prsWithLabel, *pr)
43-
}
44-
}
45-
return prsWithLabel
46-
},
47-
"combine": func(prs []github.PullRequest, title string) ChangeGroup {
48-
return ChangeGroup{
49-
Title: title,
50-
PullRequests: prs,
51-
}
52-
},
37+
"withLabels": PullRequestWithLabels,
38+
"combine": CombineToChangeGroup,
5339
}).Parse(formats["changelog."+format])
5440
if err != nil {
5541
return err

pkg/cmd/generate/templates.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,25 @@ type ChangeGroup struct {
1313
PullRequests []github.PullRequest
1414
}
1515

16-
// TODO withLabels moved here
16+
// PullRequestWithLabels filters slice of PRs to those matching one of the labels specified
17+
func PullRequestWithLabels(prs []github.PullRequest, labels ...string) []github.PullRequest {
18+
prsWithLabel := make([]github.PullRequest, 0)
19+
for i := range prs {
20+
pr := &prs[i]
21+
if Contains(pr.Labels, labels...) {
22+
prsWithLabel = append(prsWithLabel, *pr)
23+
}
24+
}
25+
return prsWithLabel
26+
}
27+
28+
// CombineToChangeGroup merges group of PRs with corresponding title
29+
func CombineToChangeGroup(prs []github.PullRequest, title string) ChangeGroup {
30+
return ChangeGroup{
31+
Title: title,
32+
PullRequests: prs,
33+
}
34+
}
1735

1836
func Contains(s []string, es ...string) bool {
1937
for _, e := range es {

0 commit comments

Comments
 (0)