Apply Command to String-type custom fields with YouTrack Rest API

983 Views Asked by At

and thanks for looking!

I have an instance of YouTrack with several custom fields, some of which are String-type. I'm implementing a module to create a new issue via the YouTrack REST API's PUT request, and then updating its fields with user-submitted values by applying commands. This works great---most of the time.

I know that I can apply multiple commands to an issue at the same time by concatenating them into the query string, like so:

Type Bug Priority Critical add Fix versions 5.1 tag regression

will result in

  • Type: Bug
  • Priority: Critical
  • Fix versions: 5.1

in their respective fields (as well as adding the regression tag). But, if I try to do the same thing with multiple String-type custom fields, then:

Foo something Example Something else Bar P0001

results in

  • Foo: something Example Something else Bar P0001
  • Example:
  • Bar:

The command only applies to the first field, and the rest of the query string is treated like its String value. I can apply the command individually for each field, but is there an easier way to combine these requests?

Thanks again!

2

There are 2 best solutions below

0
On BEST ANSWER

This is an expected result because all string after foo is considered a value of this field, and spaces are also valid symbols for string custom fields.

If you try to apply this command via command window in the UI, you will actually see the same result.

0
On

Such a good question.

I encountered the same issue and have spent an unhealthy amount of time in frustration. Using the command window from the YouTrack UI I noticed it leaves trailing quotations and I was unable to find anything in the documentation which discussed finalizing or identifying the end of a string value. I was also unable to find any mention of setting string field values in the command reference, grammer documentation or examples.

For my solution I am using Python with the requests and urllib modules. - Though I expect you could turn the solution to any language.

The rest API will accept explicit strings in the POST

import requests
import urllib
from collections import OrderedDict

URL = 'http://youtrack.your.address:8000/rest/issue/{issue}/execute?'.format(issue='TEST-1234')

params = OrderedDict({
    'State': 'New',
    'Priority': 'Critical',
    'String Field': '"Message to submit"',
    'Other Details': '"Fold the toilet paper to a point when you are finished."'
})

str_cmd = ' '.join(' '.join([k, v]) for k, v in params.items())
command_url = URL + urllib.urlencode({'command':str_cmd})

result = requests.post(command_url)

# The command result:
# http://youtrack.your.address:8000/rest/issue/TEST-1234/execute?command=Priority+Critical+State+New+String+Field+%22Message+to+submit%22+Other+Details+%22Fold+the+toilet+paper+to+a+point+when+you+are+finished.%22

I'm sad to see this one go unanswered for so long. - Hope this helps!

edit:

After continuing my work, I have concluded that sending all the field updates as a single POST is marginally better for the YouTrack server, but requires more effort than it's worth to:

1) know all fields in the Issues which are string values

2) pre-process all the string values into string literals

3) If you were to send all your field updates as a single request and just one of them was missing, failed to set, or was an unexpected value, then the entire request will fail and you potentially lose all the other information.

I wish the YouTrack documentation had some mention or discussion of these considerations.