How do I query for issues in JIRA that have a specific label and only that label?
Asked Answered
P

3

10

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?

Principality answered 2/2, 2017 at 15:51 Comment(0)
R
11

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 retun 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 matcing ~ 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.

Rossner answered 3/2, 2017 at 1:42 Comment(0)
V
1

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.

Vannoy answered 16/6, 2023 at 8:29 Comment(1)
Well, if I can match against the labels, why not just match ^Foo$ and be done with it? But I'll look into ScriptTunner, thanks.Principality
I
0

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

Intolerant answered 2/2, 2017 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.