Skip to content

Commit

Permalink
chore: externalizes template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak committed Feb 12, 2021
1 parent 60336c1 commit 5ad6ea1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
18 changes: 2 additions & 16 deletions pkg/cmd/generate/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,8 @@ func NewCmd() *cobra.Command {
dependencies = simplifyDepsPRs(dependencies)

t, err := template.New("changelog").Funcs(map[string]interface{}{
"withLabels": func(prs []github.PullRequest, labels ...string) []github.PullRequest {
prsWithLabel := make([]github.PullRequest, 0)
for i := range prs {
pr := &prs[i]
if Contains(pr.Labels, labels...) {
prsWithLabel = append(prsWithLabel, *pr)
}
}
return prsWithLabel
},
"combine": func(prs []github.PullRequest, title string) ChangeGroup {
return ChangeGroup{
Title: title,
PullRequests: prs,
}
},
"withLabels": PullRequestWithLabels,
"combine": CombineToChangeGroup,
}).Parse(formats["changelog."+format])
if err != nil {
return err
Expand Down
20 changes: 19 additions & 1 deletion pkg/cmd/generate/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,25 @@ type ChangeGroup struct {
PullRequests []github.PullRequest
}

// TODO withLabels moved here
// PullRequestWithLabels filters slice of PRs to those matching one of the labels specified
func PullRequestWithLabels(prs []github.PullRequest, labels ...string) []github.PullRequest {
prsWithLabel := make([]github.PullRequest, 0)
for i := range prs {
pr := &prs[i]
if Contains(pr.Labels, labels...) {
prsWithLabel = append(prsWithLabel, *pr)
}
}
return prsWithLabel
}

// CombineToChangeGroup merges group of PRs with corresponding title
func CombineToChangeGroup(prs []github.PullRequest, title string) ChangeGroup {
return ChangeGroup{
Title: title,
PullRequests: prs,
}
}

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

0 comments on commit 5ad6ea1

Please sign in to comment.