SQB
SQB

Reputation: 4078

How do I query for issues in Jira that have a specific label and only that label?

I have a label in Jira, say Foo. I want to query for all issues that have that label and that label only. How do I do that?

I've tried

labels = Foo AND NOT(labels NOT IN (Foo))

but that returns issues labelled Foo and Bar as well. I want the issues labelled only Foo.

How do I query for issues in Jira that have a specific label and only that label?

Upvotes: 11

Views: 38534

Answers (3)

DVK
DVK

Reputation: 129363

There's no JQL way of doing this that I'm aware of (obviously, hard to prove a negative but I have fairly decent knowledge of JQL).

The obvious approaches don't work:

  • labels != Foo does NOT return tickets that have Foo, at all (by design, because != is 100% equivalent to NOT ... = as per documentation), so doing labels != Foo AND labels = Foo returns empty set.

  • Can't use text matching ~ or !~; JIRA will throw JQL errors: The operator '!~' is not supported by 'labels' field. That's because it's a picker/multiple choice field, not a text one.

  • The only value you can compare "labels" to using IS/IS NOT is "EMPTY"

The 2.5 workarounds (that all suck, admittedly) are:

  1. Find the most used "extra" tags, and build a query excluding them

     ... AND labels = Foo AND labels NOT IN (Bar1, Bar2, ...)
    

    Pros: Pure JQL, simple

    Cons: Doesn't catch less-used labels; needs to be updated when more labels are added; and may not scale well if you have super many extra labels that pair with Foo.

  2. Use a macro. This Atlassian Q&A details

    • Install JIRA Misc Custom Fields plugin
    • Create a custom numeric field labels_count, using the formula @@Formula: issue.get("labels").size()
    • Re-index JIRA
    • Include AND labels_count = 1 in your JQL

    Pros: Should work

    Cons: I didn't actually test it so not sure if it will work. It requires installing a new plugin (a useful one!) and reindexing. And I don't know if it will keep the field updated without further reindexing - I think it would but not 100% certain.

  3. Not sure if this will work, but you can look at another way to create custom fields:

    • Use Script Runner plugin

    • Create a field with Groovy code to return count of labels.

      Best as I can tell, something like return issue.getlabels().size()

    • Some sample code related to ScriptRunner and labels: ex1; ex2

    Pros: ???

    Cons: Paid plugin, not sure how to get this to work.

Upvotes: 14

ritusmart
ritusmart

Reputation: 325

Finally, figured it out! You can find issues that have only one label and the label is "Foo", and also exclude any other labels with the following query:

project = YourProjectName AND
labels = Foo AND
issueFunction IN issueFieldMatch("project = YourProjectName", labels, "^[^,]*$")

Just replace YourProjectName with your actual project name.

This JQL query works in the following way:

  1. project = YourProjectName AND labels = Foo
    This part of the query returns all issues in your project that have the "Foo" label.
  2. issueFunction in issueFieldMatch("project = YourProjectName", labels, "^[^,]*$")
    This part of the query uses the ScriptRunner JQL function issueFieldMatch to look at all issues in your project and return only those where the labels field matches the regular expression ^[^,]*$. This regular expression matches strings that do not have a comma, so it will return only those issues that have exactly one label.

Please note that issueFieldMatch is part of ScriptRunner, a third-party app for JIRA developed by Adaptavist. If you do not have ScriptRunner installed, you may not be able to use this query exactly as it is.

Upvotes: 1

mdoar
mdoar

Reputation: 6881

Not in JQL I think. If I really had to do it, I'd use a python script to get all issues with that label and then check for other labels. Or maybe in Server use a custom ScriptRunner JQL function. No easy way

Upvotes: 0

Related Questions