This is what I want to achieve:
Once an issue is opened we "Qualify" it and defined a team that will handle it, that team is identified by an email, and each team is a group in our JIRA system, eg.:
- development [AT] acme.com
- it [AT] acme.com
- etc.
I want that team to be notified that an issue has been assigned to them.
I have written a scripted field that get the email address of the team based on the Tempo Team selected:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.http.HttpRequestInterceptor
import org.apache.http.HttpRequest
import org.apache.http.HttpResponse
import org.apache.http.protocol.HttpContext
import groovyx.net.http.RESTClient
import com.atlassian.jira.user.ApplicationUser
/*
* Initialize the API connection by getting the JIRA-SD base url
* and building the Rest client with the fields needed in order
* to register a worklog.
*/
String baseUrl = ComponentAccessor.getApplicationProperties().getString(APKeys.JIRA_BASEURL)
RESTClient http = new RESTClient(baseUrl)
HttpResponse response
def statusCode
String apiuser = 'admin'
String apipasswd = 'admin'
String auth = "${apiuser}:${apipasswd}".bytes.encodeBase64().toString()
// append the authentication header
http.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + auth)
httpRequest.addHeader('X-Atlassian-Token', "no-check")
}})
// get the issue Team identifier, and retrieve its name with the Tempo API
CustomFieldManager customFieldManager = (new ComponentAccessor()).getCustomFieldManager()
CustomField teamCustomField = customFieldManager.getCustomFieldObjectByName('Team')
String tempoTeamId
String tempoTeamName
ApplicationUser user
try {
tempoTeamId = teamCustomField.getValue(issue)
if (!tempoTeamId) throw new Exception()
response = http.get(path:"/rest/tempo-teams/1/team/${tempoTeamId}")
statusCode = response.getStatusLine().statusCode
tempoTeamName = response.getData().name
user = getUserByTempoTeam(tempoTeamName)
if (!user) throw new Exception()
return user
} catch (Exception ex) {
return null
}
def getUserByTempoTeam(teamName) {
UserManager userManager = ComponentAccessor.getUserManager()
def users = [
0: [name: 'Accounting', username: 'compta [AT] acme.ch'],
1: [name: 'Direction', username: 'direction [AT] acme.ch'],
2: [name: 'Exploit-Dev', username: 'dev [AT] acme.ch'],
3: [name: 'Exploit-NOC', username: 'noc [AT] acme.ch'],
4: [name: 'Exploit-System', username: 'systeme [AT] acme.ch'],
5: [name: 'Exploit-VOIP', username: 'systeme [AT] acme.ch'],
6: [name: 'Infra-Dev', username: 'dev [AT] acme.ch'],
7: [name: 'Infra-NOC', username: 'noc [AT] acme.ch'],
8: [name: 'Infra-System', username: 'systeme [AT] acme.ch'],
9: [name: 'Sales', username: 'vente [AT] acme.ch'],
10: [name: 'Security', username: 'security [AT] acme.ch'],
11: [name: 'Support-N1', username: 'dev [AT] acme.ch']
]
def match = users.find { it.value.name.toLowerCase() == teamName.toLowerCase() }
if (!match) throw new Exception()
return userManager.getUserByKey(match.value.username)
}This ends up in filling the scripted field correctly:
image2016-6-27 16:40:4.png
But then, on a "Qualify Issue" event, I want to send an email to that user:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.watchers.WatcherManager
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.http.HttpRequestInterceptor
import org.apache.http.HttpRequest
import org.apache.http.HttpResponse
import org.apache.http.protocol.HttpContext
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import com.atlassian.jira.user.ApplicationUser
// get the issue Team identifier, and retrieve its name with the Tempo API
CustomFieldManager customFieldManager = (new ComponentAccessor()).getCustomFieldManager()
CustomField fld = customFieldManager.getCustomFieldObjectByName('Assigned-team')
String tempoTeamId
String tempoTeamName
ApplicationUser user
user = fld.getValue(event.issue)
log.error(user)And "User" is always empty, so no mail is sent.
Can someone help me with that ?
My other option would be to add the user to the watchers list instead of a scripted field, but he will be notified everytime a notification scheme send an email to all watchers, and I'd like to avoid that.
Thank you,
Cheers, Giulio