1

I can add a label like this

issue.fields.labels.append("MYNEWLABEL")

but I have searched the docs and duckduckgo'd until my hair has gone greyer but I have not figured out how to remove a label. I tried the method I used to remove a component but it didn't work

# doesn't work:
issue.update(update={"labels": [{"remove": {"name": "MYOLDLABEL"}}],},)

this works for components, so I thought something similar would work for labels, but not for me

# works:
issue.update(update={"components": [{"remove": {"name": "MYOLDCOMPONENT"}}],},)

would be ever so happy to hear how to do this, I don't want to have to manually change hundreds of issues (the bulk editor doesnt' work for me in the browser).


FYI, here's the core essence of my script:

#!/usr/bin/env python3

import sys

# https://pypi.org/project/jira/
from jira import JIRA

JIRA_API_TOKEN_ENV_NAME = 'JIRA_API_TOKEN'
JIRA_FQDN = 'https://jira.example.com'
JIRA_JQL = 'labels in (OLDLABEL and labels not in (NEWLABEL)'

def main():
    jira_api_token = os.environ[JIRA_API_TOKEN_ENV_NAME]
    issues = jira.search_issues(JIRA_JQL)
    issue_num = 1
    for issue in issues:
        print(f'=== {issue_num} - {issue.key} ===')
        print(f'project { issue.fields.project.key}')
        print(f'state.name { issue.fields.status.name}')
        was_closed = False
        if issue.fields.status.name == 'Closed':
            was_closed = True

        print(f'labels before { issue.fields.labels }')

        if was_closed:
            print('Was closed, reopening')
            jira.transition_issue(issue, '3')   # transition id 3 - name Reopen Issue

        # works:
        issue.fields.labels.append('NEWLABEL')
        # fails:
        issue.update(update={"labels": [{"remove": {"name": "OLDLABEL"}}],},)

        # works:
        issue.update(fields={"labels": issue.fields.labels})

        if was_closed:
            print('Was closed, reclosing')
            jira.transition_issue(issue, '2')   # transition id 2 - name Close Issue

        issue_num = issue_num + 1
        print('')


1 Answer 1

2

Labels don't have names, so you reference them directly after the add or remove object when updating the labels field:

"update": {
 "labels": [
   {"remove": "Label_ToRemove_1"};
   {"remove": "Label_ToRemove_2"};
   {"add": "Label_ToAdd_1"}
  ]
 }

but I have searched the docs

Really? The Edit Issue endpoint's documentation contains examples that show exactly how to do it.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, that worked. when I said, searched the docs, I meant searched the jira/python docs, I didn't read through the raw jira api definitions. I shouldn't have to! thanks for the code snippet, but not so impressed with the snarky comment. Upvoted anyway.
No problems. I generally advise against the use of those Jira Python libraries, as they are not being well maintained any more and are known to have faults. Use Python's native Requests library instead. Sure, it takes more effort to construct your requests, but it's a safer bet in the long run.
thanks DB, your time is sincerely appreciated. This was one of those jobs that I thought would just take a couple of hours because I found the python library and was able to cargo cult most of what I needed. Shame on me for falling for that old trick played on naive programmers :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.