-
-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow custom field values to be specified as JSON #661
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @octachrome, Thank you for working on this.
I think we can get rid of json:
prefix (unless I am missing something). Let me know what you think! Thanks again!
pkg/jira/create.go
Outdated
if strings.HasPrefix(key, "json:") { | ||
key = key[5:] | ||
rawJson = true | ||
} | ||
for _, configured := range configuredFields { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we still need to go through configured field so why not just add a type called json
that accepts the raw json data?
For instance the configuration can be:
issue:
fields:
custom:
- name: platform
key: customfield_10032
schema:
datatype: json
And we can pass it as is
Now, we don't need to pass json:
tag?
jira issue create --custom='platform=[{"value":"Linux"}]'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a good idea to have a json
type that can be used in the config file. But is there an easy way to update the config file to set the type of custom fields? I think the Jira users on my team will find it hard to locate this file and make the right change by hand. Another inconvenience is that if a user runs jira init
(for example if their password/access token changes), then the config file will be reset.
Looking at my config file, all the custom fields for which JSON is needed have type any
. What do you think about using JSON by default for all fields of type any
?
Another idea is to add an option to the jira init
command to mark certain fields as needing JSON, e.g.:
jira init --json-custom-fields "first-field,second-field,etc"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But is there an easy way to update the config file to set the type of custom fields?
Not at the moment unfortunately. There is a plan to have something like gh config but I don't have any timeline for it yet (open for PRs :)).
I think the Jira users on my team will find it hard to locate this file and make the right change by hand.
Unless I am missing something, even with the json:
tag you will still need to configure the field in config right? since we are looping through the configured fields in line#236?
for _, configured := range configuredFields {
Looking at my config file, all the custom fields for which JSON is needed have type any. What do you think about using JSON by default for all fields of type any?
Another idea is to add an option to the jira init command to mark certain fields as needing JSON, e.g.:
These are all valid ideas. The inconsistencies in Jira could make things a bit tricky so I am not sure if this will be the same case for all Jira installations.
I would suggest, let's start with simple and working implementation and we can make it better iteratively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you will still need to configure the field in config right
Running jira init
populates the config file with all custom fields, even the ones with unknown (any
) type, so no manual config is necessary.
The current default behaviour for any
type fields is to just send a string. I would rather not change that default behaviour in case it breaks something for an existing user (unless you know that this never happens in practice).
So I propose to add json
as a new custom field type and add a flag to jira init
to make it easy to configure these fields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! Let's start by adding json
as a new custom field type. We can talk about how we can make the config easily configurable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is my second attempt.
- Added
json
as a custom type, which can be used in the config file to force a field to use JSON when setting/editing. - JSON is used by default for unknown field types, such as
user
orversion
. - Factored out the code which constructs the
customFieldTypeXxx
objects so that it can be called recursively for arrays of any type, including JSON. - Replaced the type-specific structs used to edit custom fields on issues, such as
customFieldTypeNumberSet
andcustomFieldTypeStringSet
with generic structs that can be used to set/add/remove items on any field type.
I tested this on our Jira installation with custom fields of type option
, user
, version
, date
, number
, string
, any
and array of string
. This worked nicely, and I could specify user
and version
types as JSON from the command line with no manual changes to the config file needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ankitpokhrel Have you had a chance to review my update?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @octachrome, sorry for the late response. Would you be able to provide some examples on how to construct valid requests for different types of custom fields? Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ankitpokhrel, if you mean CLI examples, see below (the syntax is the same for the issue create
command):
# Field of type "version"
jira issue edit --no-input --custom 'found-in-version={"name": "MyProduct 1.0"}' XYZ-1234
# Field of type "user"
jira issue edit --no-input --custom 'assigned-developer={"name": "BobFossil"}' XYZ-1234
I could add these to the readme, or it might be better to add them to the article here: #346.
…custom json:name=val flag syntax
Would it be possible for the entire custom argument to be json? |
The disadvantage is that we would need to parse the JSON, separate out the fields, then reassemble the individual JSON pieces to send to Jira, which is awkward. What would be the benefit of this approach? |
There are two main advantages in my opinion.
With this said, I fully acknowledge that the proposed change may not be possible with the proposed PR, hence the question. Thanks for entertaining it! |
I don't understand how anything except the value can be JSON. How can the field be JSON? Can you give an example?
I agree this is the main disadvantage, and your suggestion does overcome that with a nicer syntax than what I originally proposed.
Please give examples of what you mean by "free-form data" and "truly custom fields". I am using the Jira CLI to automate actions in shell scripts, and I expect other people are too. Assembling a single JSON object to describe all my custom field values is not easy with a shell script, keeping track of where commas are needed, etc. An extension of your suggestion is to allow the option to be passed more than once. So both of the following would be equivalent.
I would also use a different option name to avoid confusion with |
any update on this one? this will make things much easier when transitioning issues, updating custom fields etc. @ankitpokhrel @octachrome |
Is there any plan to merge this at any point? |
Sorry I got distracted with other priorities for a while now. I'll check if this can be merged soon. |
This PR adds the ability to update custom fields whose type is not directly supported by
jira-cli
. This is achieved by specifying the JSON representation of the field value, e.g.:If the field value starts with
json:
, the remaining string is interpreted as raw JSON. Note that when updating an issue, theset
oradd
operation must also be provided, since this can be different for each custom field.The implementation uses a new custom field struct type,
customFieldTypeJson
, and an implementation ofMarshallJSON
for that type which just returns the JSON stored in the struct.After I implemented this I found #593 suggesting the same idea.