Skip to content

Commit

Permalink
Differentiate between label-filter and release labels
Browse files Browse the repository at this point in the history
Partially reverts the behavior change introduced by
#238.

Filtering is still done in the changelog generation, but now we store
all of the PR labels in the release-state and return to filtering
against all the PR labels using --label-filter.

The newly introduced behavior in #238 is now part of the new
--release-labels flag, which configures the release-labels to use when
generating release notes.

Signed-off-by: Chance Zibolski <[email protected]>
  • Loading branch information
chancez committed Jul 24, 2024
1 parent 22277d6 commit fd6045e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cmd/changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ChangeLogConfig struct {
LastStable string
StateFile string
LabelFilters []string
ReleaseLabels []string
ExcludePRReferences bool
SkipHeader bool
}
Expand Down Expand Up @@ -66,7 +67,8 @@ func Command(ctx context.Context, logger *log.Logger) *cobra.Command {
cmd.Flags().StringVar(&cfg.LastStable, "last-stable", "", "When last stable version is set, it will be used to detect if a bug was already backported or not to that particular branch (e.g.: '1.5', '1.6')")
cmd.Flags().StringVar(&cfg.StateFile, "state-file", "release-state.json", "When set, it will use the already fetched information from a previous run")
cmd.Flags().StringVar(&cfg.RepoName, "repo", "cilium/cilium", "GitHub organization and repository names separated by a slash")
cmd.Flags().StringArrayVar(&cfg.LabelFilters, "label-filter", []string{}, "Filter pull requests by labels. This also defines the order of the release notes.")
cmd.Flags().StringArrayVar(&cfg.LabelFilters, "label-filter", []string{}, "Filter pull requests by labels.")
cmd.Flags().StringArrayVar(&cfg.ReleaseLabels, "release-labels", []string{}, "Specify release labels to consider when generating the changelog. This also defines the order of the release notes.")
cmd.Flags().BoolVar(&cfg.ExcludePRReferences, "exclude-pr-references", false, "If true, do not include references to the PR or PR author")
cmd.Flags().BoolVar(&cfg.SkipHeader, "skip-header", false, "If true, do not print 'Summary of of changes' header")

Expand Down
29 changes: 25 additions & 4 deletions cmd/changelog/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,34 @@ func (cl *ChangeLog) PrintReleaseNotesForWriter(w io.Writer) {
fmt.Fprintln(w, "------------------")
}

listOfPRs := cl.listOfPrs.DeepCopy()
prsWithUpstream := cl.prsWithUpstream.DeepCopy()
var (
listOfPRs = make(types.PullRequests)
prsWithUpstream = make(types.BackportPRs)
)

// Filter the PRs by --label-filter
for id, pr := range cl.listOfPrs.DeepCopy() {
if !filterByLabels(pr.Labels, cl.LabelFilters) {
continue
}
listOfPRs[id] = pr
}

// Filter the Backport PRs by --label-filter
for prNumber, upstreamedPRs := range cl.prsWithUpstream.DeepCopy() {
for upstreamPRNumber, upstreamPR := range upstreamedPRs {
if !filterByLabels(upstreamPR.Labels, cl.LabelFilters) {
continue
}
prsWithUpstream[prNumber][upstreamPRNumber] = upstreamPR
}
}

var releaseNotesOrder []string
if len(cl.LabelFilters) != 0 {
if len(cl.ReleaseLabels) != 0 {
// Only add release notes for release labels specified by --release-labels
for _, label := range defaultReleaseNotesOrder {
if !slices.Contains(cl.LabelFilters, label) {
if !slices.Contains(cl.ReleaseLabels, label) {
continue
}
releaseNotesOrder = append(releaseNotesOrder, label)
Expand Down
15 changes: 15 additions & 0 deletions cmd/changelog/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package changelog

import "slices"

func filterByLabels(labels []string, filters []string) bool {
if len(filters) == 0 {
return true
}
for _, label := range labels {
if slices.Contains(filters, label) {
return true
}
}
return false
}
15 changes: 15 additions & 0 deletions cmd/changelog/labels_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package changelog

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_filterByLabels(t *testing.T) {
assert.True(t, filterByLabels([]string{"label-a", "label-b", "label-c"}, []string{}))
assert.True(t, filterByLabels([]string{"label-a", "label-b", "label-c"}, []string{"label-a"}))
assert.True(t, filterByLabels([]string{"label-a", "label-b", "label-c"}, []string{"label-b"}))
assert.True(t, filterByLabels([]string{"label-a", "label-b", "label-c"}, []string{"label-c"}))
assert.False(t, filterByLabels([]string{"label-a", "label-b", "label-c"}, []string{"label-d"}))
}
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func addFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&cfg.StateFile, "state-file", "release-state.json", "When set, it will use the already fetched information from a previous run")
cmd.Flags().StringVar(&cfg.RepoName, "repo", "cilium/cilium", "GitHub organization and repository names separated by a slash")
cmd.Flags().BoolVar(&cfg.ForceMovePending, "force-move-pending-backports", false, "Force move pending backports to the next version's project")
cmd.Flags().StringArrayVar(&cfg.LabelFilters, "label-filter", []string{}, "Filter pull requests by labels")
cmd.Flags().StringArrayVar(&cfg.LabelFilters, "label-filter", []string{}, "Filter pull requests by labels.")
cmd.Flags().StringArrayVar(&cfg.ReleaseLabels, "release-labels", []string{}, "Specify release labels to consider when generating the changelog. This also defines the order of the release notes.")
cmd.Flags().BoolVar(&cfg.ExcludePRReferences, "exclude-pr-references", false, "If true, do not include references to the PR or PR author")
cmd.Flags().BoolVar(&cfg.SkipHeader, "skip-header", false, "If true, do not print 'Summary of of changes' header")
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/github/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func GeneratePatchRelease(
ReleaseLabel: getReleaseLabel(lbls),
AuthorName: pr.GetUser().GetLogin(),
BackportBranches: getBackportBranches(lbls),
Labels: lbls,
}
nodeIDs[pr.GetNumber()] = pr.GetNodeID()
continue
Expand Down Expand Up @@ -110,6 +111,7 @@ func GeneratePatchRelease(
ReleaseNote: getReleaseNote(upstreamPR.GetTitle(), upstreamPR.GetBody()),
ReleaseLabel: getReleaseLabel(lbls),
AuthorName: upstreamPR.GetUser().GetLogin(),
Labels: lbls,
}
nodeIDs[pr.GetNumber()] = pr.GetNodeID()
nodeIDs[upstreamPR.GetNumber()] = upstreamPR.GetNodeID()
Expand Down
1 change: 1 addition & 0 deletions pkg/types/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PullRequest struct {
// BackportBranches contains all the backport-done labels present in the
// PullRequest.
BackportBranches []string
Labels []string
}

// NodeIDs maps a Pull Request number to its graphql node_id
Expand Down

0 comments on commit fd6045e

Please sign in to comment.