Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How can I create an email notification every time an issue is labeled or unlabeled?

Matt Marino October 14, 2019

I would like to create an email notification that is triggered every time an issue is labeled or unlabeled with a specific label. I've attempted to and don't believe this can be achieved with out of the box notifications with Jira. I also don't want to use a post function because I don't want the trigger dependent on a transition. Will I need to write some custom groovy? Is there an out of the box listener that exists for this already? Thanks in advance.

1 answer

Suggest an answer

Log in or Sign up to answer
1 vote
Ravi Sagar _Sparxsys_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 15, 2019

Hi @Matt Marino 

What you can do is use the following code in Send a custom email listener's Condition and Configuration section.

Label added

import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def previousLabels = changeHistoryManager.getChangeItemsForField(issue, "labels") ? changeHistoryManager.getChangeItemsForField(issue, "labels").last().getFromString(): ""
def newLabels = changeHistoryManager.getChangeItemsForField(issue, "labels") ? changeHistoryManager.getChangeItemsForField(issue, "labels").last().getToString():""
if (previousLabels == "" && newLabels == "") {
//No existing labels
return false
}
else if (newLabels.indexOf("LABEL") == -1) {
//Label removed
return false
}
else if (previousLabels.indexOf("LABEL") == -1 && newLabels.indexOf("LABEL") > -1) {
//Label added
return true
}

Label removed

import com.atlassian.jira.component.ComponentAccessor

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def previousLabels = changeHistoryManager.getChangeItemsForField(issue, "labels") ? changeHistoryManager.getChangeItemsForField(issue, "labels").last().getFromString(): ""
def newLabels = changeHistoryManager.getChangeItemsForField(issue, "labels") ? changeHistoryManager.getChangeItemsForField(issue, "labels").last().getToString():""
if (previousLabels == "" && newLabels == "") {
//No existing labels
return false
}
else if (newLabels.indexOf("LABEL") == -1) {
//Label removed
return true
}
else if (previousLabels.indexOf("LABEL") == -1 && newLabels.indexOf("LABEL") > -1) {
//Label added
return false
}

You can create two listeners - one for label added and the other one for label removed.

Let me know if this works.

Ravi

Matt Marino October 22, 2019

That worked! The email isn't firing every time, but that is a separate problem for me to resolve. Thank you for your help!

Like Ravi Sagar _Sparxsys_ likes this
Matt Marino October 25, 2019

@Ravi Sagar _Sparxsys_ Let me know if this is the wrong forum for this followup. I've got the custom emails set up with above code above (adjusted for my custom label) in the Condition and Configuration, and have tried using All Issue Events, Generic Events, and Issue Updated as which events to fire on. Instead of sending an email only when the label is added or removed, it's seemingly firing on almost every update to issues. Any insight?

TAGS
AUG Leaders

Atlassian Community Events