Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JalonSolov opened this issue Jan 25, 2023 · 9 comments

Comments

@JalonSolov
Copy link

JalonSolov commented Jan 25, 2023

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
Copy link
Author

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

@vladimir-ch
Copy link
Contributor

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.

@JalonSolov
Copy link
Author

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... :-\

@vladimir-ch
Copy link
Contributor

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
Copy link
Contributor

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

@JalonSolov
Copy link
Author

JalonSolov commented Jan 30, 2023

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
  }

@vladimir-ch
Copy link
Contributor

Why not just (untested):

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

@JalonSolov
Copy link
Author

... 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. :-)

@andygrunwald
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants