3

I am using the python API for Jira. I would like to get a list of all the labels that are being used in a project. I know that issue.fields.labels will get me just the labels of an issue, but I am interested in looping through all the labels used in a project. Found this to list all components in a project components = jira.project_components(projectID)

Am looking for something similar, but for labels...

PS376
  • 539
  • 8
  • 13
  • Looks like the Jira Python library does not provide this, but you might be able to [use the REST API](https://stackoverflow.com/a/63496223/4288506). – Marcono1234 Aug 19 '20 at 23:21

2 Answers2

3

Using jira-python library for python3

For a whole project, you would write a loop to iterate through the issues in the project, then append the labels to a list.

from jira import JIRA
host = "http://<site>.atlassian.net"
jira = JIRA(host,basic_auth=(<user>, <password>))

projectlist = jira.search_issues('project = "Project Name"')
alllabels = []
for ticket in projectlist:
    issue = jira.issue(ticket)
    labels = issue.fields.labels
    for i in labels:
        alllabels.append(i)

If you wanted unique labels only you could use a set instead of a list for alllables.

gnfrazier
  • 51
  • 7
  • 2
    Sad that we must resort to this. Thanks – Matt May 03 '19 at 01:55
  • Optimization hints: Use a `set` instead of `list` to have unique entries (`alllabels = set()`). Also, update the set with `alllabels.update(labels)` instead of iterating through all retrieved `labels`. – j4x Jun 15 '20 at 12:40
2

Labels are a field that is shared across all issues potentially, but I don't think there is a REST API to get the list of all labels. So you'd either have to write a JIRA add-on to provide such a resource, or retrieve all the issues in question and iterate over them. You can simplify things by excluding issues that have no label

JQL: project = MYPROJ and labels is not empty

And restrict the fields that are returned from the search using the "fields" parameter for search_issues

mdoar
  • 6,758
  • 1
  • 21
  • 20