diff --git a/internal/api/sm.go b/internal/api/sm.go index fa4b6ac..8f0d458 100644 --- a/internal/api/sm.go +++ b/internal/api/sm.go @@ -52,12 +52,12 @@ func (s *SMService) GetServiceDesks(ctx context.Context) ([]*ServiceDesk, error) // RequestType represents a request type in a service desk. type RequestType struct { - ID string `json:"id"` - Name string `json:"name"` - Description string `json:"description"` - ServiceDesk *struct { - ID string `json:"id"` - } `json:"serviceDeskId,omitempty"` + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description"` + IssueTypeID string `json:"issueTypeId"` + ServiceDeskID string `json:"serviceDeskId"` + PortalID string `json:"portalId"` } // RequestTypePage represents a paginated list of request types. diff --git a/internal/api/sm_test.go b/internal/api/sm_test.go index 328e823..beffb68 100644 --- a/internal/api/sm_test.go +++ b/internal/api/sm_test.go @@ -64,15 +64,18 @@ func TestGetServiceDesks(t *testing.T) { } func TestGetRequestTypes(t *testing.T) { + // Shaped like the real servicedeskapi response: the id fields are plain strings. server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(RequestTypePage{ - Size: 2, Start: 0, Limit: 50, IsLastPage: true, - Values: []*RequestType{ - {ID: "10", Name: "Incident", Description: "Report an incident"}, - {ID: "26", Name: "Service Delivery", Description: "DL ticket creation"}, - }, - }) + _, _ = w.Write([]byte(`{ + "size": 2, "start": 0, "limit": 50, "isLastPage": true, + "values": [ + {"id": "10", "name": "Incident", "description": "Report an incident", + "issueTypeId": "10035", "serviceDeskId": "1", "portalId": "1", "groupIds": ["50"]}, + {"id": "26", "name": "Service Delivery", "description": "DL ticket creation", + "issueTypeId": "10036", "serviceDeskId": "1", "portalId": "1", "groupIds": []} + ] + }`)) })) defer server.Close() @@ -90,6 +93,11 @@ func TestGetRequestTypes(t *testing.T) { if result.Values[1].Name != "Service Delivery" { t.Errorf("second type name = %q, want Service Delivery", result.Values[1].Name) } + first := result.Values[0] + if first.IssueTypeID != "10035" || first.ServiceDeskID != "1" || first.PortalID != "1" { + t.Errorf("first type ids = issueType %q, serviceDesk %q, portal %q; want 10035, 1, 1", + first.IssueTypeID, first.ServiceDeskID, first.PortalID) + } } func TestGetRequestTypeFields(t *testing.T) { diff --git a/internal/cmd/issue/field_util.go b/internal/cmd/issue/field_util.go index b742344..f963373 100644 --- a/internal/cmd/issue/field_util.go +++ b/internal/cmd/issue/field_util.go @@ -153,6 +153,10 @@ func ParseCustomField(ctx context.Context, jira *api.JiraService, raw string) (s // based on the field's schema. func coerceFieldValue(field *api.Field, value string) interface{} { if field != nil && field.Schema != nil { + // Jira silently ignores a Service Management request type id sent as a number. + if field.Schema.Type == "sd-customerrequesttype" { + return value + } customType := field.Schema.Custom if strings.Contains(customType, "select") || strings.Contains(customType, "radiobuttons") { return map[string]string{"value": value} diff --git a/internal/cmd/issue/field_util_test.go b/internal/cmd/issue/field_util_test.go index 06af858..32fdf11 100644 --- a/internal/cmd/issue/field_util_test.go +++ b/internal/cmd/issue/field_util_test.go @@ -212,6 +212,24 @@ func TestCoerceFieldValue_SelectStillWorks(t *testing.T) { } } +// TestCoerceFieldValue_RequestTypeStaysString covers the Service Management +// Request Type field: Jira silently ignores its id when it is sent as a number. +func TestCoerceFieldValue_RequestTypeStaysString(t *testing.T) { + field := &api.Field{ + ID: "customfield_10010", + Name: "Request Type", + Schema: &api.FieldSchema{ + Type: "sd-customerrequesttype", + Custom: "com.atlassian.servicedesk:vp-origin", + }, + } + + got := coerceFieldValue(field, "3") + if got != "3" { + t.Errorf("got %#v (%T), want the string \"3\"", got, got) + } +} + // TestCoerceFieldValue_RadioStillWorks verifies radiobutton fields are not regressed. func TestCoerceFieldValue_RadioStillWorks(t *testing.T) { field := &api.Field{ diff --git a/internal/cmd/sm/requesttype.go b/internal/cmd/sm/requesttype.go index ec2d1b9..f7ab584 100644 --- a/internal/cmd/sm/requesttype.go +++ b/internal/cmd/sm/requesttype.go @@ -74,18 +74,22 @@ func runRequestTypeList(opts *RequestTypeListOptions) error { return output.JSON(opts.IO.Out, types) } - headers := []string{"ID", "Name", "Description"} + headers := []string{"ID", "Name", "Issue Type ID", "Description"} + output.SimpleTable(opts.IO.Out, headers, requestTypeRows(types)) + return nil +} + +// requestTypeRows builds the table rows for request types. +func requestTypeRows(types []*api.RequestType) [][]string { rows := make([][]string, 0, len(types)) for _, t := range types { desc := t.Description if len(desc) > 60 { desc = desc[:57] + "..." } - rows = append(rows, []string{t.ID, t.Name, desc}) + rows = append(rows, []string{t.ID, t.Name, t.IssueTypeID, desc}) } - - output.SimpleTable(opts.IO.Out, headers, rows) - return nil + return rows } // ── request-type fields ──────────────────────────────────────────── diff --git a/internal/cmd/sm/requesttype_test.go b/internal/cmd/sm/requesttype_test.go new file mode 100644 index 0000000..2c179a9 --- /dev/null +++ b/internal/cmd/sm/requesttype_test.go @@ -0,0 +1,19 @@ +package sm + +import ( + "reflect" + "testing" + + "github.com/enthus-appdev/atl-cli/internal/api" +) + +func TestRequestTypeRows_IncludesIssueType(t *testing.T) { + rows := requestTypeRows([]*api.RequestType{ + {ID: "3", Name: "Änderungs-Ticket", IssueTypeID: "10035", Description: "Change request"}, + }) + + want := [][]string{{"3", "Änderungs-Ticket", "10035", "Change request"}} + if !reflect.DeepEqual(rows, want) { + t.Errorf("rows = %v, want %v", rows, want) + } +}