Using:
Jira 9.4.4
Adaptavist ScriptRunner for JIRA 7.10.0
I need to send a Slack message using a cron job that isn't possible with Jira Automation.
I've tried copying a couple examples found online, as well as using the script from the Adaptavist Library here: https://library.adaptavist.com/entity/send-notification-to-slack (tried both Data Center version and Server version). I tried a test in both the Scriptrunner console and in jobs to simply send a Slack message "This is a test" and do nothing else, but was unsuccessful. The script seemed to load/buffer for a bit after I clicked "Run" and then nothing happened - no message in the Slack channel, no error message or logs.
I have no coding experience. What am I doing wrong? Do I need to replace the part that says "JIRA_BASEURL"?
This is what I used from the Adaptavist Library (but with a real webhook URL):
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/actual/webhookURL/here'
def jiraBaseUrl = ComponentAccessor.applicationProperties.getString(APKeys.JIRA_BASEURL)
// We have to convert this GString into String explicitly here
def message = "This is a test" as String
def body = [
text : message,
]
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"
Hello:
No, you don't need to replace jirl_base_url. That value is being pulled from whichever instance the script is run under.
Here's something I'd previously written that is based on the same library example. You'll have to fill in your own webhook value (I altered the one below so it's not viable, but has the right number of characters):
//AUTHOR: Kenneth McClean / kmcclean@adaptavist.com
//The Slack integration code comes from the Adaptavist Script Library:
//https://library.adaptavist.com/entity/send-custom-notification-to-slack
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 com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getIssueManager()
// 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/<>/B04KM008B7X/Ftw4sQL2MzivvG5ZCS1hooCo'
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueByKeyIgnoreCase("<issue key")
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 : <https://$jiraBaseUrl/browse/$issue.key|$issue.key>"
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 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"
Thanks, Ken.
I received this error:
The script could not be compiled: <pre>org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script14.groovy: 23: The current scope already contains a variable of the name issueManager @ line 23, column 5. def issueManager = ComponentAccessor.issueManager ^ 1 error </pre>.
Since I don't need it to tell me about specific issues, I commented out that line, as well as deleted all lines re: "attachments" in the body. Same result: The script seemed to load/buffer for a bit after I clicked "Run" and then nothing happened - no message in the Slack channel, no error message or logs.
My actual goal is to have a cron job where I get a count of issues that matcher a query (got that piece down using this), and then I want to receive a Slack message telling me that count.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Ken, I realized that the issue may have been that we didn't have a Slack connection in Resources. I thought that a regular webhook that was being used in Jira automations would also work here.
Anyway, we ended up achieving our goal with a different method.
Thanks.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.