Skip to content

Commit

Permalink
Merge pull request #5 from marcelblijleven/feature/add-filter-option
Browse files Browse the repository at this point in the history
Feature/add filter option
  • Loading branch information
marcelblijleven authored Jan 18, 2022
2 parents 5be3bba + acabd5a commit 6994097
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Aliases:
assignRelease, assignVersion
Flags:
-f, --filter strings The filter flag allows you to ignore issues when assigning a release
-h, --help help for assignRelease
-i, --issues strings The issues you want to assign to release to, can be a single issue or comma separated
-b, --releaseBody string The body of text which contains Jira issues, e.g. a GitHub release body
Expand Down Expand Up @@ -112,6 +113,7 @@ Usage:
jira-helper createAndAssign [flags]
Flags:
-f, --filter strings The filter flag allows you to ignore issues when assigning a release
-h, --help help for createAndAssign
-i, --issues strings The issues you want to assign to release to, can be a single issue or comma separated
-b, --releaseBody string The body of text which contains Jira issues, e.g. a GitHub release body
Expand Down
3 changes: 2 additions & 1 deletion cmd/assignRelease.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ the provided release body.`,
httpClient.Timeout = time.Second * 15
client, err := pkg.NewJiraClient(host, user, token, httpClient)
cobra.CheckErr(err)
cobra.CheckErr(pkg.AssignVersions(body, version, client, issues...))
cobra.CheckErr(pkg.AssignVersions(body, version, client, issues, filter))
},
}

Expand All @@ -49,4 +49,5 @@ func init() {
assignReleaseCmd.Aliases = []string{"assignVersion"}
assignReleaseCmd.Flags().StringVarP(&body, bodyFlagName, bodyShorthand, "", bodyUsage)
assignReleaseCmd.Flags().StringSliceVarP(&issues, issuesFlagName, issuesShorthand, []string{}, issuesUsage)
assignReleaseCmd.Flags().StringSliceVarP(&filter, filterFlagName, filterShorthand, []string{}, filterUsage)
}
3 changes: 2 additions & 1 deletion cmd/createAndAssign.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ today.`,
client, err := pkg.NewJiraClient(host, user, token, httpClient)
cobra.CheckErr(err)
cobra.CheckErr(client.CreateFixVersion(version, project))
cobra.CheckErr(pkg.AssignVersions(body, version, client, issues...))
cobra.CheckErr(pkg.AssignVersions(body, version, client, issues, filter))
},
}

func init() {
rootCmd.AddCommand(createAndAssignCmd)
createAndAssignCmd.Flags().StringVarP(&body, bodyFlagName, bodyShorthand, "", bodyUsage)
createAndAssignCmd.Flags().StringSliceVarP(&issues, issuesFlagName, issuesShorthand, []string{}, issuesUsage)
createAndAssignCmd.Flags().StringSliceVarP(&filter, filterFlagName, filterShorthand, []string{}, filterUsage)
}
5 changes: 5 additions & 0 deletions cmd/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var (
version string
body string
issues []string
filter []string
)

const (
Expand Down Expand Up @@ -38,4 +39,8 @@ const (
issuesFlagName = "issues"
issuesShorthand = "i"
issuesUsage = "The issues you want to assign to release to, can be a single issue or comma separated"

filterFlagName = "filter"
filterShorthand = "f"
filterUsage = "The filter flag allows you to ignore issues when assigning a release"
)
32 changes: 31 additions & 1 deletion pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,35 @@ func removeDuplicates(items []string) []string {
return filtered
}

func filterSlice(main []string, filter []string) []string {
var filtered []string

if len(filter) == 0 {
return main
}

for _, i := range main {
include := true

for _, f := range filter {
if i == f {
include = false
break
}
}

if include {
filtered = append(filtered, i)
}
}

if filtered == nil {
return []string{}
}

return filtered
}

// extractIssuesFromText gathers all issue numbers from the provided text
func extractIssuesFromText(text string) []string {
r := regexp.MustCompile("[A-Z]+-[0-9]+")
Expand All @@ -71,9 +100,10 @@ func extractIssuesFromText(text string) []string {

// AssignVersions extracts the issues from the provided release body and calls the AssignVersion endpoint of the
// jira client.
func AssignVersions(releaseBody, version string, client *JiraClient, issues ...string) error {
func AssignVersions(releaseBody, version string, client *JiraClient, issues []string, filter []string) error {
issues = append(issues, extractIssuesFromText(releaseBody)...)
issues = removeDuplicates(issues)
issues = filterSlice(issues, filter)

for _, issue := range issues {
if err := client.AssignVersion(issue, version); err != nil {
Expand Down
63 changes: 57 additions & 6 deletions pkg/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ For changes in this version, see the changelog
Merge commit that triggered this release: feat: marcel introduces c0ffee (MB-1337)`

err = AssignVersions(releaseBody, "My first version", jiraClient)
err = AssignVersions(releaseBody, "My first version", jiraClient, nil, nil)
assert.NoError(t, err)
assert.Equal(t, 1, mockClient.CalledTimes)
assert.Equal(t, "{\"update\":{\"fixVersions\":[{\"add\":{\"name\":\"My first version\"}}]}}", mockClient.CalledWith[0])
Expand All @@ -99,7 +99,7 @@ For changes in this version, see the changelog
Merge commit that triggered this release: feat: marcel introduces c0ffee (MB-1337, MB-1338)`

err = AssignVersions(releaseBody, "My first version", jiraClient)
err = AssignVersions(releaseBody, "My first version", jiraClient, nil, nil)
assert.NoError(t, err)
assert.Equal(t, 2, mockClient.CalledTimes)
assert.Equal(t, []string{
Expand All @@ -116,7 +116,7 @@ func TestAssignVersions_singleIssue(t *testing.T) {
log.Fatalln(err)
}

err = AssignVersions("", "My first version", jiraClient, "MB-1234")
err = AssignVersions("", "My first version", jiraClient, []string{"MB-1234"}, nil)
assert.NoError(t, err)
assert.Equal(t, 1, mockClient.CalledTimes)
assert.Equal(t, []string{
Expand All @@ -132,7 +132,7 @@ func TestAssignVersions_duplicateIssue(t *testing.T) {
log.Fatalln(err)
}

err = AssignVersions("", "My first version", jiraClient, "MB-1234", "MB-1234")
err = AssignVersions("", "My first version", jiraClient, []string{"MB-1234", "MB-1234"}, nil)
assert.NoError(t, err)
assert.Equal(t, 1, mockClient.CalledTimes)
assert.Equal(t, []string{
Expand All @@ -153,7 +153,7 @@ For changes in this version, see the changelog
Merge commit that triggered this release: feat: marcel introduces c0ffee (MB-1337, MB-1338)`

err = AssignVersions(releaseBody, "My first version", jiraClient, "MB-1339", "MB-1340")
err = AssignVersions(releaseBody, "My first version", jiraClient, []string{"MB-1339", "MB-1340"}, nil)
assert.NoError(t, err)
assert.Equal(t, 4, mockClient.CalledTimes)
assert.Equal(t, []string{
Expand All @@ -177,7 +177,7 @@ For changes in this version, see the changelog
Merge commit that triggered this release: feat: marcel introduces c0ffee (MB-1337, MB-1338)`

err = AssignVersions(releaseBody, "My first version", jiraClient, "MB-1337", "MB-1338")
err = AssignVersions(releaseBody, "My first version", jiraClient, []string{"MB-1337", "MB-1338"}, nil)
assert.NoError(t, err)
assert.Equal(t, 2, mockClient.CalledTimes)
assert.Equal(t, []string{
Expand All @@ -199,3 +199,54 @@ func Test_handleJiraError_unreadableResponse(t *testing.T) {
err := handleJiraError(res)
assert.EqualError(t, err, "request unsuccessful (Bad request), could not read response: invalid character 'e' in literal true (expecting 'r')")
}

func Test_filterSlice(t *testing.T) {
type args struct {
main []string
filter []string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "empty filter",
args: args{
main: []string{"a", "b", "c"},
filter: []string{},
},
want: []string{"a", "b", "c"},
},
{
name: "nil filter",
args: args{
main: []string{"a", "b", "c"},
filter: nil,
},
want: []string{"a", "b", "c"},
},
{
name: "filter some",
args: args{
main: []string{"a", "b", "c"},
filter: []string{"b"},
},
want: []string{"a", "c"},
},
{
name: "filter all",
args: args{
main: []string{"a", "b", "c"},
filter: []string{"a", "b", "c"},
},
want: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := filterSlice(tt.args.main, tt.args.filter)
assert.Equal(t, tt.want, got, "filterSlice() = %v, want %v", got, tt.want)
})
}
}

0 comments on commit 6994097

Please sign in to comment.