Skip to content
Open
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: 4 additions & 0 deletions pkg/jira/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func constructCustomFields(fields map[string]string, configuredFields []IssueTyp
if identifier != strings.ToLower(key) {
continue
}
if parsed, ok := parseCustomFieldJSONContainer(val); ok {
data.Fields.M.customFields[configured.Key] = parsed
continue
}

switch configured.Schema.DataType {
case customFieldFormatOption:
Expand Down
4 changes: 4 additions & 0 deletions pkg/jira/customfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type customFieldTypeStringSet struct {
Set string `json:"set"`
}

type customFieldTypeAnySet struct {
Set any `json:"set"`
}

type customFieldTypeOption struct {
Value string `json:"value"`
}
Expand Down
80 changes: 80 additions & 0 deletions pkg/jira/customfield_json_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package jira

import (
"encoding/json"
"testing"

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

func TestConstructCustomFieldsParsesNestedJSONObjectForCreate(t *testing.T) {
req := CreateRequest{
Project: "TEST",
IssueType: "Task",
Summary: "Test issue",
CustomFields: map[string]string{
"reviewers": `{"accountId":"abc","profile":{"team":{"id":"42"}}}`,
},
}
req.WithCustomFields([]IssueTypeField{
{
Name: "reviewers",
Key: "customfield_10042",
Schema: struct {
DataType string `json:"type"`
Items string `json:"items,omitempty"`
}{
DataType: "user",
},
},
})

body, err := json.Marshal((&Client{}).getRequestData(&req))
assert.NoError(t, err)
assert.JSONEq(t, `{
"update": {},
"fields": {
"project": {"key": "TEST"},
"issuetype": {"name": "Task"},
"summary": "Test issue",
"customfield_10042": {
"accountId": "abc",
"profile": {"team": {"id": "42"}}
}
}
}`, string(body))
}

func TestConstructCustomFieldsParsesNestedJSONObjectForEdit(t *testing.T) {
req := EditRequest{
CustomFields: map[string]string{
"reviewers": `{"accountId":"abc","profile":{"team":{"members":[{"id":"1"},{"id":"2"}]}}}`,
},
}
req.WithCustomFields([]IssueTypeField{
{
Name: "reviewers",
Key: "customfield_10042",
Schema: struct {
DataType string `json:"type"`
Items string `json:"items,omitempty"`
}{
DataType: "user",
},
},
})

body, err := json.Marshal(getRequestDataForEdit(&req))
assert.NoError(t, err)
assert.JSONEq(t, `{
"update": {
"customfield_10042": [{
"set": {
"accountId": "abc",
"profile": {"team": {"members": [{"id":"1"},{"id":"2"}]}}
}
}]
},
"fields": {"parent": {}}
}`, string(body))
}
24 changes: 24 additions & 0 deletions pkg/jira/customfield_parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package jira

import (
"encoding/json"
"strings"
)

func parseCustomFieldJSONContainer(v string) (any, bool) {
trimmed := strings.TrimSpace(v)
if trimmed == "" {
return nil, false
}

if !strings.HasPrefix(trimmed, "{") && !strings.HasPrefix(trimmed, "[") {
return nil, false
}

var out any
if err := json.Unmarshal([]byte(trimmed), &out); err != nil {
return nil, false
}

return out, true
}
4 changes: 4 additions & 0 deletions pkg/jira/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,10 @@ func constructCustomFieldsForEdit(fields map[string]string, configuredFields []I
if identifier != strings.ToLower(key) {
continue
}
if parsed, ok := parseCustomFieldJSONContainer(val); ok {
data.Update.M.customFields[configured.Key] = []customFieldTypeAnySet{{Set: parsed}}
continue
}

switch configured.Schema.DataType {
case customFieldFormatOption:
Expand Down
Loading