Template Content
About the template
About ScriptRunner Connect
What is ScriptRunner Connect?
Can I try it out for free?
Yes. ScriptRunner Connect comes with a forever free tier.
Can I customize the integration logic?
Absolutely. The main value proposition of ScriptRunner Connect is that you'll get full access to the code that is powering the integration, which means you can make any changes to the the integration logic yourself.
Can I change the integration to communicate with additional apps?
Yes. Since ScriptRunner Connect specializes in enabling complex integrations, you can easily change the integration logic to connect to as many additional apps as you need, no limitations.
What if I don't feel comfortable making changes to the code?
First you can try out our AI assistant which can help you understand what the code does, and also help you make changes to the code. Alternatively you can hire our professionals to make the changes you need or build new integrations from scratch.
Do I have to host it myself?
No. ScriptRunner Connect is a fully managed SaaS (Software-as-a-Service) product.
What about security?
ScriptRunner Connect is ISO 27001 and SOC 2 certified. Learn more about our security.
This template demonstrates how to create an issue in Jira cloud from a Slack slash command, and then how to post back a message to Slack channel with the newly created issue key.
Parameters
and configure parameters to meet you needs.Trigger a Slack slash command (/command_name <ISSUE_SUMMARY>
), after successful event processing a new issue should be created in Jira Cloud and that issue's key be sent back to Slack.
import { SlashCommandEvent } from '@sr-connect/slack/events';
import JiraCloud from './api/jira/cloud';
import Slack from './api/slack';
/**
* This function creates a Jira Cloud issue based on data triggered by Slack's slash command,
* and then posts back a message to Slack containing newly created issue key.
*
* Command syntax: /command_name <ISSUE_SUMMARY>
*
* @param event Object that holds Slash Command event data
* @param context Object that holds function invocation context data
*/
export default async function (event: SlashCommandEvent, context: Context) {
if (context.triggerType === 'MANUAL') {
console.error('This script is designed to be triggered externally or manually from the Event Listener. Please consider using Event Listener Test Event Payload if you need to trigger this script manually.');
return;
}
const { ISSUE_TYPE_NAME, JIRA_PROJECT_KEY } = getEnvVars(context);
try {
// Log out the event that was received
console.log('Create Jira Cloud issue slash command triggered', event);
// Extract summary from Slack slash command event
const summary = event.text.trim();
// Check if the summary is specified
if (!summary) {
// If not, post approriate message back to Slack channel where the command originated from
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: 'Issue summary is required'
}
});
// And halt the operation
return;
}
// Find the project to use based on pre-defined project key
const project = await JiraCloud.Project.getProject({
projectIdOrKey: JIRA_PROJECT_KEY
});
// Find all the issue types for given project
const issueTypes = await JiraCloud.Issue.Type.getTypesForProject({
projectId: +(project.id ?? 0) // + sign converts the string to number
});
// Find the issue type to use based on pre-defined issue type name
const issueType = issueTypes.find(it => it.name === ISSUE_TYPE_NAME);
// Check if the issue type was found
if (!issueType) {
// If not, then throw an error
throw Error('Issue type not found');
}
// Log out the information that the issue is about to be created
console.log('Creating Issue', {
projectId: project.id,
issueTypeId: issueType.id,
summary
});
// Create the issue
const issue = await JiraCloud.Issue.createIssue({
body: {
fields: {
project: {
id: project.id ?? '0'
},
issuetype: {
id: issueType.id ?? '0'
},
summary
}
}
});
// Log out created issue key
console.log(`Issue created: ${issue.key}`);
// And post a messages with newly created issue key back to Slack channel where the command originated from
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: `Issue Created: ${issue.key}`
}
});
} catch (e) {
// If something goes wrong, log out the error
console.error('Error while processing slash command', e);
// And post appropriate message back to Slack channel where the command originated from
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: 'Something went wrong while creating issue'
}
});
}
}
interface EnvVars {
JIRA_PROJECT_KEY: string;
ISSUE_TYPE_NAME: string;
}
export function getEnvVars(context: Context) {
return context.environment.vars as EnvVars;
}