Send Custom Notification to Slack

Overview

Add custom slack notifications on a workflow post function, or as a custom listener after an issue event. Configure the message content and direct the message to a room or specific user.

Example

As a project manager, I want to keep track of all changes to my watched issues. I can use this script to send me a slack notification when one of my watched issues is transitioned.

Good to Know

Requirements

Jira Jira (7.7 - 8.6)

    
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 // You have to create a webhook first: // https://api.slack.com/messaging/webhooks#posting_with_webhooks // Your webhook is related to one channel or user to which you will be able to send messages // Once you have your webhook, split it into it's base URL and it's URL path final webhookBase = 'https://hooks.slack.com' final webhookPath = '/services/XYZ/XYZ/XYZ' def jiraBaseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL) // We have to convert this GString into String explicitly here def message = "New issue created in project $issue.projectObject.name : " as String def body = [ text : message, attachments: [ [ color : '#f2c744', blocks: [ [ type : 'section', fields: [ [ type: 'mrkdwn', text: "*Summary*\n$issue.summary" as String ], [ type: 'mrkdwn', text: "*Reporter*\n$issue.reporter.displayName" as String ], [ type: 'mrkdwn', text: "*Description*\n$issue.description" as String ] ] ] ] ] ] ] def response = new RESTClient(webhookBase).post( path: webhookPath, contentType: ContentType.HTML, body: body, requestContentType: ContentType.JSON ) as HttpResponseDecorator assert response.status == 200: "Request failed with status $response.status. $response.entity.content.text"
Discovered an issue? Report it here

Suggested for you

Appears in these Collections