jira-python - how do you update the fixVersions field?
Asked Answered
C

3

6

I'm not sure what I'm doing wrong here, and am hoping someone else has the same problem. I don't get any error, and my json matches what should be correct both on Jira's docs and jira-python questions online. My versions are valid Jira versions. I also have no problem doing this directly through the API, but we are re-writing everything to go through jira-python for cleanliness/ease of use.

This just completely clears the fixVersions field in Jira.

issue=jira.issue("TKT-100")
issue.update(fields={'fixVersions':[{'add': {'name': 'add_me'}},{'remove': {'name': 'remove_me'}}]})

I can add a new version to fixVersions using issue.add_field_value(), but this won't work, because I need to add and remove in one request for the history of the ticket.

issue.add_field_value('fixVersions', {'name': 'add_me'})

Any ideas?

Crossfade answered 14/4, 2015 at 17:43 Comment(1)
I actually figured out that you can do this by first finding all fixVersions in a ticket, throwing all but the one you want to remove into a list, append your new fixVersion, and use the 'set' verb instead of 'add' and 'remove'. Jira doesn't overwrite the other fixVersion values.Crossfade
C
19

Here's a code example of how I got it working for anyone who comes across this later...

    fixVersions = []
    issue = jira.issue('issue_key')
    for version in issue.fields.fixVersions:
        if version.name != 'version_to_remove':
            fixVersions.append({'name': version.name})
    fixVersions.append({'name': 'version_to_add'})
    issue.update(fields={'fixVersions': fixVersions})
Crossfade answered 15/4, 2015 at 17:11 Comment(1)
I actually started with this approach and noticed it was clearing out the existing versions. Only the last version stuck. But using add_field_value() did the trick.Knotweed
D
3

I did it other way:

  1. Create version in the target project.
  2. Update ticket.

    ver = jira.create_version(name='version_name', project='PROJECT_NAME')
    issue = jira.issue('ISSUE_NUM')
    i.update(fields={'fixVersions': [{'name': ver.name}]})}

In my case that worked.

Dorie answered 24/4, 2020 at 12:43 Comment(0)
A
0

A little bit more pythonic version of user797963 solution, may look like that.

def change_fix_version(tickets, remove_versions=[], add_versions=[]):
    fix_versions={version.name for version in ticket.fields.fixVersions}
    fix_versions.difference_update(set(remove_versions))
    fix_versions.update(set(add_versions))
    ticket.update(fields={'fixVersions':fix_versions})

You would call it like that:

 change_fix_versions(jira.issue('my_issue'), remove_versions=['draft'], add_versions=['master', 'release'])
Aylmer answered 24/6, 2020 at 7:7 Comment(1)
you forgot that fix_versions should be a list of dicts. fix_versions=[version.raw for version in ticket.fields.fixVersions]. and then you should assume your usage is like this change_fix_versions(jira.issue('my_issue'), remove_versions=[{'name':'draft'}], add_versions=[{'name':'master'}, {'name':'release'}])'Manmade

© 2022 - 2024 — McMap. All rights reserved.