Skip to content
Merged
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
4 changes: 2 additions & 2 deletions internal/cmd/issue/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func create(cmd *cobra.Command, _ []string) {
}
cr.ForProjectType(projectType)

if strings.EqualFold(params.issueType, jira.IssueTypeSubTask) {
cr.SubtaskField = cmdutil.GetSubtaskHandle(cc.issueTypes)
if handle := cmdutil.GetSubtaskHandle(params.issueType, cc.issueTypes); handle != "" {
cr.SubtaskField = handle
}

resp, err := client.CreateV2(&cr)
Expand Down
29 changes: 24 additions & 5 deletions internal/cmdutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,33 @@ func NormalizeJiraError(msg string) string {
// GetSubtaskHandle fetches actual subtask handle.
// This value can either be handle or name based
// on the used jira version.
func GetSubtaskHandle(issueTypes []*jira.IssueType) string {
func GetSubtaskHandle(issueType string, issueTypes []*jira.IssueType) string {
get := func(it *jira.IssueType) string {
if it.Handle != "" {
return it.Handle
}
return it.Name
}
fallback := ""

for _, it := range issueTypes {
if it.Subtask {
if it.Handle != "" {
return it.Handle
// Exact matches return immediately
if strings.EqualFold(issueType, it.Name) {
return get(it)
}

// Store the first subtask type as backup
if fallback == "" {
fallback = get(it)
}
return it.Name
}
}
return jira.IssueTypeSubTask

// Set default for fallback if none found
if strings.EqualFold(issueType, jira.IssueTypeSubTask) && fallback == "" {
fallback = jira.IssueTypeSubTask
}

return fallback
}
43 changes: 36 additions & 7 deletions internal/cmdutil/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,10 @@ func TestGetSubtaskHandle(t *testing.T) {
t.Parallel()

cases := []struct {
name string
input []*jira.IssueType
expected string
name string
input []*jira.IssueType
inputType string
expected string
}{
{
name: "should get default issue type handle for sub-task",
Expand All @@ -186,7 +187,8 @@ func TestGetSubtaskHandle(t *testing.T) {
Subtask: false,
},
},
expected: "Sub-task",
inputType: "Sub-task",
expected: "Sub-task",
},
{
name: "should get valid sub-task handle",
Expand All @@ -204,7 +206,8 @@ func TestGetSubtaskHandle(t *testing.T) {
Subtask: true,
},
},
expected: "Sub-Task",
inputType: "Sub-task",
expected: "Sub-Task",
},
{
name: "should get sub-task name as handle",
Expand All @@ -221,7 +224,33 @@ func TestGetSubtaskHandle(t *testing.T) {
Subtask: true,
},
},
expected: "Subtask",
inputType: "Sub-task",
expected: "Subtask",
},
{
name: "exact matches for a custom sub-task should take precedence",
input: []*jira.IssueType{
{
ID: "123",
Name: "Story",
Handle: "story",
Subtask: false,
},
{
ID: "234",
Name: "Sub-Task",
Handle: "Sub-Task",
Subtask: true,
},
{
ID: "567",
Name: "Custom Sub-Task",
Handle: "Custom Sub-Task",
Subtask: true,
},
},
inputType: "Custom Sub-Task",
expected: "Custom Sub-Task",
},
}

Expand All @@ -231,7 +260,7 @@ func TestGetSubtaskHandle(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

assert.Equal(t, tc.expected, GetSubtaskHandle(tc.input))
assert.Equal(t, tc.expected, GetSubtaskHandle(tc.inputType, tc.input))
})
}
}