pycontribs / jira

Python Jira library. Development chat available on https://matrix.to/#/#pycontribs:matrix.org
https://jira.readthedocs.io
BSD 2-Clause "Simplified" License
1.92k stars 859 forks source link

issue.add_field_value fails to add a version string to the collection #1724

Open swordfish0x0 opened 10 months ago

swordfish0x0 commented 10 months ago

Bug summary

using Jira python library 3.5.2

I'm attempting to add a version to the versions list and this fails.

The code can successsfully get the issue ticket... issue = jira.issue(TICKET_ID)

get the versions.. x = issue.get_field('versions')

Now...if I call issue.add_field_value('versions', ADD_THIS_VERSION_STR)

This call fails with a thrown error message

response text = {"errorMessages":[],"errors":{"versions":"Could not find valid 'id' or 'name' in version object."}}

The docs and comments say takes a string and a string see https://jira.readthedocs.io/api.html#jira.resources.Issue.add_field_value

says: " add_field_value(field: str, value: str) Add a value to a field that supports multiple values, without resetting the existing values. This should work with: labels, multiple checkbox lists, multiple select"

Is there an existing issue for this?

Jira Instance type

Jira Server or Data Center (Self-hosted)

Jira instance version

8.20.19

jira-python version

3.5.2

Python Interpreter version

3.9

Which operating systems have you used?

Reproduction steps

from jira import JIRA
...
issue = jira.issue(TICKET_ID)
..
get the versions..
x = issue.get_field('versions')
...
Now...if I call
This call fails
issue.add_field_value('versions', ADD_THIS_VERSION_STR)

Throws error message:
response text = {"errorMessages":[],"errors":{"versions":"Could not find valid 'id' or 'name' in version object."}}

Stack trace

File "C:\Program Files\Python39\lib\site-packages\jira\resources.py", line 712, in add_field_value
    super().update(fields={"update": {field: [{"add": value}]}})
  File "C:\Program Files\Python39\lib\site-packages\jira\resources.py", line 324, in update
    r = self._session.put(self.self + querystring, data=json.dumps(data))
  File "C:\Program Files\Python39\lib\site-packages\requests\sessions.py", line 647, in put
    return self.request("PUT", url, data=data, **kwargs)
  File "C:\Program Files\Python39\lib\site-packages\jira\resilientsession.py", line 246, in request
    elif raise_on_error(response, **processed_kwargs):
  File "C:\Program Files\Python39\lib\site-packages\jira\resilientsession.py", line 71, in raise_on_error
    raise JIRAError(

Expected behaviour

the issue.add_field_value should work and add the value to the list or docs should be updated to explain correctly how to use this api.

Additional Context

No response

nickbroon commented 1 month ago

The type hints appear to be incorrect, it does not always take a string. (in the case of the labels field a plain string work) But for more complex fields a dictionary may be required, for example the following works to add a fixVersion, and the use of dictionary with name appears to be required:

version = "1.2.3"
issue.add_field_value('fixVersions', {'name': version})

The type hint on the function should probably be replaced with Any instead of string.