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?

user797963
  • 2,907
  • 9
  • 47
  • 88
  • 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. – user797963 Apr 14 '15 at 18:29

3 Answers3

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})
user797963
  • 2,907
  • 9
  • 47
  • 88
  • 2
    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. – sonofanickel Mar 28 '19 at 18:06
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.

bvrch
  • 53
  • 5
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'])
user1235183
  • 3,002
  • 1
  • 27
  • 66
  • 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'}])'` – studioj Jun 04 '21 at 23:05