go-jira icon indicating copy to clipboard operation
go-jira copied to clipboard

How do I handle a custom field that is an object instead of a simple string?

Open JalonSolov opened this issue 3 years ago • 9 comments

They're constantly adding custom fields to JIRA where I work. One of the latest requires a full object rather than a simple string, as the data. It represents a drop-down field with fixed selections.

Here's what the JSON looks like:

		"customfield_17100": {
			"disabled": false,
			"id": "15700",
			"self": "https://someserver.com/rest/api/2/customFieldOption/15700",
			"value": "Some text value"
		},

Setting a regular customfield doesn't work, since it only allows strings for the values, not objects. If the JSON is pass as a string, it doesn't count as a object.

JalonSolov avatar Jan 25 '23 19:01 JalonSolov

Ah, ok. Looks like I'm out of luck for now, as this is something that is planned for v2 in #477

JalonSolov avatar Jan 26 '23 13:01 JalonSolov

You can use the methods on IssueFields.Unknowns to extract values from custom fields, no matter how nested. See the documentation at https://pkg.go.dev/github.com/trivago/tgo/tcontainer#MarshalMap.Value for how to format the key.

Alternatively, you could use https://github.com/mitchellh/mapstructure to decode IssueFields.Unknowns into a struct that has the custom fields you need. This is very convenient and Go-like.

Or you can process IssueFields.Unknowns "manually" to extract what you need but that's tedious.

vladimir-ch avatar Jan 26 '23 14:01 vladimir-ch

I need to go the other direction... I need to encode the value to send to JIRA. I'll look into modifying IssueFields.Unknowns manually.

It would still be easier than trying to create a giant curl command... :-\

JalonSolov avatar Jan 26 '23 14:01 JalonSolov

Then you should still be able to use IssueFields.Unknowns.Set to set your custom fields to send to Jira https://pkg.go.dev/github.com/trivago/tgo/tcontainer#MarshalMap.Set

vladimir-ch avatar Jan 26 '23 14:01 vladimir-ch

Or you can use https://github.com/mitchellh/mapstructure to "decode" your custom field structs into IssueFields.Unknowns, something like this: https://go.dev/play/p/otekLAkstZK

vladimir-ch avatar Jan 26 '23 15:01 vladimir-ch

I tried this, but apparently go-jira is trying to marshall it again... or something

  customFields := tcontainer.MarshalMap{}
  ...
  data, err := json.Marshal(option)
  if err != nil {
    return fmt.Errorf(err.Error())
  }
  
  customFields["customfield_17100"] = data
  ...
  if len(customFields) > 0 {
    i.Fields.Unknowns = customFields
  }

JalonSolov avatar Jan 30 '23 20:01 JalonSolov

Why not just (untested):

customFields := tcontainer.NewMarshalMap()
customFields["customfield_17100"] = option
i.Fields.Unknowns = customFields

vladimir-ch avatar Jan 31 '23 17:01 vladimir-ch

... you know how sometimes you're just too close, and need to take a step back, just to realize the obvious?

You are correct - that worked. Thank you. :-)

JalonSolov avatar Jan 31 '23 18:01 JalonSolov

Thanks for the good collaboration here :) I will re-open this, because this seem to be a good case for a documentation entry.

andygrunwald avatar Jan 31 '23 20:01 andygrunwald