import groovy.xml.MarkupBuilder
//// When using this script in the script console uncomment the next line:
def issueKey = 'ISSUE-1'
//// When using this script in a transition uncomment the next line:
// def issueKey = issue.key
String formName = "Form Name"
String fieldName = "Users Field"
Closure sendNotification = { def user ->
def writer = new StringWriter()
def markupBuilder = new MarkupBuilder(writer)
markupBuilder.div {
p {
// update url below:
a(href: "http://mycloudjira.atlassian.net/issue/" + issueKey)
span("Please check approval action for form " + formName)
}
}
def htmlMessage = writer.toString()
def textMessage = new XmlSlurper().parseText(htmlMessage).text()
def resp = post("/rest/api/2/issue/" + issueKey + "/notify")
.header("Content-Type", "application/json")
.body([
subject: "Form Approval for issue: " + issueKey,
textBody: textMessage,
htmlBody: htmlMessage,
to: [
reporter: false,
assignee: false,
watchers: false,
voters: false,
users: [[
accountId: user.id,
//accountId: user.accountId,
active: true
]]
]
])
.asString()
}
Closure getFormId = { def allProperties, String name ->
for(def formProperty : allProperties){
if(formProperty.key.equals("proforma.forms")){
for(def p : formProperty.value.forms) {
if(p.name.equals(name)){
return p.id
}
}
}
}
}
Closure<String> getFieldId = { def property, String name ->
for (def question : property.value.design.questions){
if(name.equals(question.value.label)){
return question.key
}
}
}
Closure getUsersFieldValue = { def property, String name ->
def fieldId = getFieldId(property, name)
for (def answer : property.value.state.answers){
if(answer.key.equals(fieldId)){
return answer.value.users
}
}
}
def proformaPropertyKeys = []
def allIssueProperties = get('/rest/api/3/issue/' + issueKey + '/properties/')
.header('Content-Type', 'application/json')
.asObject(Map)
if (allIssueProperties.status == 200){
for(def property : allIssueProperties.body.keys){
if(property.key.contains("proforma.forms")){
proformaPropertyKeys.add(property.key)
}
}
}
def formProperties = []
if(!proformaPropertyKeys.isEmpty()){
for(def key : proformaPropertyKeys){
def property = get('/rest/api/3/issue/' + issueKey + '/properties/' + key)
.header('Content-Type', 'application/json')
.asObject(Map)
if(property.status == 200){
formProperties.add(property.body)
}
}
}
def formId
if(!formProperties.isEmpty()){
formId = getFormId(formProperties, formName)
for(def formProperty : formProperties){
if(formProperty.key.equals("proforma.forms" + ".i" + formId) && formProperty.value.schemaVersion.equals(8)){
def userList = getUsersFieldValue(formProperty, fieldName)
//def userList = get('/rest/api/2/issue/' + issueKey).header('Content-Type', 'application/json').asObject(Map).body.fields.customfield_10036
if(userList.getClass().toString().contains("java.util.ArrayList")){
for(def user : userList){
sendNotification(user)
}
} else {
sendNotification(userList)
}
}
}
}
|