Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[actions-gh-release] Add labels option in commitCategories #5327

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions tool/actions-gh-release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type ReleaseCommitMatcherConfig struct {
ParentOfMergeCommit bool `json:"parentOfMergeCommit,omitempty"`
Prefixes []string `json:"prefixes,omitempty"`
Contains []string `json:"contains,omitempty"`
Labels []string `json:"labels,omitempty"`
}

func (c ReleaseCommitMatcherConfig) Empty() bool {
Expand All @@ -92,6 +93,17 @@ func (c ReleaseCommitMatcherConfig) Match(commit Commit, mergeCommit *Commit) bo
return false
}

func (c ReleaseCommitMatcherConfig) MatchLabels(labels []*github.Label) bool {
for _, cl := range c.Labels {
for _, l := range labels {
if l.GetName() == cl {
return true
}
}
}
return false
}

func (c *ReleaseConfig) Validate() error {
if c.Tag == "" {
return fmt.Errorf("tag must be specified")
Expand Down Expand Up @@ -279,6 +291,14 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits []
return pr, nil
}

// Whether commitCategory is decided by PR's labels or not.
useLabels := false
for _, ctg := range cfg.CommitCategories {
if len(ctg.Labels) > 0 {
useLabels = true
break
}
}
out := make([]ReleaseCommit, 0, len(commits))
for _, commit := range commits {

Expand All @@ -298,17 +318,22 @@ func buildReleaseCommits(ctx context.Context, ghClient *githubClient, commits []
CategoryName: determineCommitCategory(commit, mergeCommits[commit.Hash], cfg.CommitCategories),
}

if gen.UsePullRequestMetadata {
if gen.UsePullRequestMetadata || useLabels {
pr, err := getPullRequest(commit)
if err != nil {
// only error logging, ignore error
log.Printf("Failed to get pull request: %v\n", err)
}
if pr != nil {

if pr != nil && gen.UsePullRequestMetadata {
c.PullRequestNumber = pr.GetNumber()
c.PullRequestOwner = pr.GetUser().GetLogin()
c.ReleaseNote = extractReleaseNote(pr.GetTitle(), pr.GetBody(), gen.UseReleaseNoteBlock)
}

if pr != nil && useLabels {
c.CategoryName = determineCommitCategoryOfPR(pr, cfg.CommitCategories)
}
}

out = append(out, c)
Expand Down Expand Up @@ -343,6 +368,15 @@ func determineCommitCategory(commit Commit, mergeCommit *Commit, categories []Re
return ""
}

func determineCommitCategoryOfPR(pr *github.PullRequest, categories []ReleaseCommitCategoryConfig) string {
for _, c := range categories {
if c.ReleaseCommitMatcherConfig.MatchLabels(pr.Labels) {
return c.ID
}
}
return ""
}

func renderReleaseNote(p ReleaseProposal, cfg ReleaseConfig) []byte {
var b strings.Builder
b.WriteString(fmt.Sprintf("## Release %s with changes since %s\n\n", p.Tag, p.PreTag))
Expand Down
74 changes: 74 additions & 0 deletions tool/actions-gh-release/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"testing"

"github.com/google/go-github/v39/github"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -306,6 +307,79 @@ func TestBuildReleaseCommits(t *testing.T) {
}
}

func TestBuildReleaseCommitsForCategoryLabel(t *testing.T) {
t.Parallel()

cfgs := []ReleaseCommitCategoryConfig{
{
ID: "breaking-change",
Title: "Breaking Changes",
ReleaseCommitMatcherConfig: ReleaseCommitMatcherConfig{
Labels: []string{"change-category/breaking-change"},
},
},
{
ID: "notable-change",
Title: "Notable Changes",
ReleaseCommitMatcherConfig: ReleaseCommitMatcherConfig{
Labels: []string{"change-category/notable-change"},
},
},
{
ID: "internal-change",
Title: "Internal Changes",
ReleaseCommitMatcherConfig: ReleaseCommitMatcherConfig{},
},
}

testcases := []struct {
title string
labels []*github.Label
categories []ReleaseCommitCategoryConfig
expectedCategory string
}{
{
title: "no categories config provided",
labels: []*github.Label{},
categories: []ReleaseCommitCategoryConfig{},
expectedCategory: "",
},
{
title: "breaking-change",
labels: []*github.Label{{Name: github.String("change-category/breaking-change")}},
categories: cfgs,
expectedCategory: "breaking-change",
},
{
title: "not match any category",
labels: []*github.Label{{Name: github.String("foo")}},
categories: cfgs,
expectedCategory: "",
},
{
title: "matching multiple labels results in the first one defined in config",
labels: []*github.Label{
{Name: github.String("change-category/notable-change")},
{Name: github.String("change-category/breaking-change")},
},
categories: cfgs,
expectedCategory: "breaking-change",
},
}

for _, tc := range testcases {
t.Run(tc.title, func(t *testing.T) {
t.Parallel()

pr := &github.PullRequest{
Labels: tc.labels,
}
got := determineCommitCategoryOfPR(pr, tc.categories)
assert.Equal(t, tc.expectedCategory, got)
})
}
}

func TestRenderReleaseNote(t *testing.T) {
testcases := []struct {
name string
Expand Down
Loading