In this exercise, we will use a sample code of Adaptavist library in order to create a new “Escalation Service” in Jira (using the plugin Scriptrunner for Jira) to create scheduled alerts from a Jira JQL query to a Slack channel (using a Webhook). (click here to see the sample code)

As first step, go to api.slack.com –> Your Apps –> Create new App

Now go to “Incoming Webhooks” –> Activate incoming Webhook

Now push the button “Add New Webhook to Workspace” and connect to a #channel of your Slack workspace.

Now Allow the Slackbot (App) connect to your channel. The WebHook is installed.

You will see in the Slack channel a message like this:

Copy the WebHook url (we will use it in the source code of the script of the Escalation Service).

Now we are ready to do a test from the Script Console of Scriptrunner for Jira.

Copy this code and replace the KEY of the Issue to be shown in the channel also the WebHook url. Run the code in the Script Console and see the result in the Slack channel.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.URIBuilder

def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueByCurrentKey("KAN-7")

// Once you have your webhook, Note the whole url for it in the variable below
final String webhookURL = "https://hooks.slack.com/services/XXX"

// Enter a channel name like #channel or a SlackUserId like @UH3DJXYZ7
final String channelOrUserId = "#jira2slackchannel"


def baseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
def message = "Issue still open in project ${issue.projectObject.name} : "
def client = new RESTClient("https://hooks.slack.com")
def data = [:]

data.put("channel", channelOrUserId)
data.put("text", message)
data.put("iron_emoji", ":ghost:")
data.put("attachments",
    [
        [
            "fallback": "Summary: ${issue.summary}, Reporter: ${issue.reporter}",
            "color"   : "warning",
            "fields"  : [
                [
                    "title": "Summary",
                    "value": issue.summary,
                    "short": true
                ],
                [
                    "title": "Reporter",
                    "value": issue.reporter.displayName,
                    "short": true
                ],
                [
                    "title": "Status",
                    "value": issue.getStatus().getName(),
                    "short": false
                ],
                [
                    "title": "Labels",
                    "value": issue.getLabels().toString(),
                    "short": false
                ],
                [
                    "title": "Creation date",
                    "value": issue.getCreated().format("dd/MMM/yy"),
                    "short": false
                ]
            ]
        ]
    ])

def response = client.post(
    path: new URIBuilder(webhookURL).path,
    contentType: ContentType.HTML,
    body: data,
    requestContentType: ContentType.JSON) as HttpResponseDecorator

assert response.status == 200 : "Request failed with status $response.status. $response.entity.content.text"


Now we are ready to see the result in Slack:

If all is ok, we can create the “Escalation Service” in Jira (using Scriptrunner) to schedule the task.

(Remember to disable two lines in the code)

//def issueManager = ComponentAccessor.getIssueManager()
//def issue = issueManager.getIssueByCurrentKey(“KAN-6”)

That’s all! Enjoy and Happy integration! 😀

By MrAddon

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s