Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Script

Code Block
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.mail.Email
import com.atlassian.mail.server.SMTPMailServer
import com.atlassian.jira.bc.issue.properties.IssuePropertyService
import org.apache.commons.lang3.StringUtils

////If used in a transition, comment next line
Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ISSUE-1")
String baseurl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl")
String issueUrl = baseurl + "/browse/" + issue.key
String formName = "Form Name"
String subject = "Form Approval for issue: " + issue.key
String body = "Please check approval action for form " + formName + " in issue " + issueUrl
ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
IssuePropertyService issuePropertyService = ComponentAccessor.getComponentOfType(IssuePropertyService.class)

Closure sendEmail = { String mailAddress, String mailSubject, String mailBody ->
    SMTPMailServer mailServer = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer()
    if (mailServer){
        Email email = new Email(mailAddress)
        email.setSubject(mailSubject)
        email.setBody(mailBody)
        ClassLoader threadClassLoader = Thread.currentThread().getContextClassLoader()
		Thread.currentThread().setContextClassLoader(SMTPMailServer.class.classLoader)
		mailServer.send(email)
		Thread.currentThread().setContextClassLoader(threadClassLoader)
    }
}

Closure<Long> getFormId = { def property, String name ->
	def slurper = new groovy.json.JsonSlurper()
    def parsedJson = slurper.parseText(property.value)
    String id
    parsedJson.forms.each(){ form ->
        if(form.name.equals(name)){
            id = form.id.toString()
            return
        }
    }
    if(id){
        return Long.valueOf(id)
    }
}

Closure<String> getFieldId = { def property, String fieldName ->
    def slurper = new groovy.json.JsonSlurper()
    def parsedJson = slurper.parseText(property.value)
    for (def question : parsedJson.design.questions){
        if(fieldName.equals(StringUtils.substringBetween(question.toString(), "label=", ","))){
            return question.toString().substring(0, question.toString().indexOf("="))
        }
    }
}

Closure getFieldValue = { def property, String fieldName ->
    def slurper = new groovy.json.JsonSlurper()
    def parsedJson = slurper.parseText(property.value)
    String fieldId = getFieldId(property, fieldName)
    for (def answer : parsedJson.state.answers){
        if(fieldId.equals(answer.toString().substring(0, answer.toString().indexOf("=")))){
            return answer.value.users
        }
    }
}

Long approversFieldId = 10000l	
def approversField = ComponentAccessor.getCustomFieldManager().getCustomFieldObject(approversFieldId)
def approversValue = issue.getCustomFieldValue(approversField)

for (def approver : approversValue){
    sendEmail(approver.getEmailAddress(), subject, body)
}

/* String fieldName = "Approvers Field in Form"
def allProperties = issuePropertyService.getProperties(loggedInUser, issue.id)
def formId
for(def property : allProperties){
    if(property.key.equals("proforma.forms")){
   		formId = getFormId(property, formName)
    }
}

def result
for(def property : allProperties){
    if(property.key.equals("proforma.forms" + ".i" + formId) && property.value.contains("\"schemaVersion\":8")){
		result = getFieldValue(property, fieldName)
    }
}

for(def user : result){
    sendEmail(ComponentAccessor.getUserManager().getUserByKey(user.id).getEmailAddress(), subject, body)
}*/

Installation instructions

  • If used in a transition:

    • Comment line 10

    • Change formName in line 13 to the name of the form that needs the approval.

    • Change subject in line 14 to the subject of the email you want to send.

    • Change body in line 15 to the body of the email you want to send.

    • If using Jira approvers field:

      • Change approversFieldId in line 68 to the id of the Approvers field in JIRA.

    • If using a ProForma multiselect/single select user field:

      • Comment from line 68 to line 74

      • Uncomment lines 76 to 94

      • Change fieldName in line 76 to the name/label of the ProForma field with the approvers.

  • If used in the script console:

    • Change issue key in line 10 to the issue that has the form that needs approval.

    • Change formName in line 13 to the name of the form that needs the approval.

    • Change subject in line 14 to the subject of the email you want to send.

    • Change body in line 15 to the body of the email you want to send.

    • If using Jira approvers field:

      • Change approversFieldId in line 68 to the id of the Approvers field in JIRA.

    • If using a ProForma multiselect/single select user field:

      • Comment from line 68 to line 74

      • Uncomment lines 76 to 94

      • Change fieldName in line 76 to the name/label of the ProForma field with the approvers.

Possible use cases

Use this script to notify approvers (or other team members) that there is a form awaiting their approval. The notification will include a link to the issue and the name of the form needing approval.

Limitations

  • Notifications will only be sent if allowed by Jira’s notification scheme and the user’s notification preferences.

  • This If using the Jira Approvers field, the script will not check to confirm that the form is present. You may want to configure this to fire after adding a specific form to ensure that the script does not arbitrarily send notifications even if the form hasn’t been added to the issue.

  • If the approver is listed in ProForma field, then the script may not work if the form is over 32 KB. You can limit the size of your forms by using multiple smaller forms, and by including character/word limits on your text fields.

...