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 retrieve 10 (configurable) most recent assets created in the default schema (Service objects) from Jira Service Management Cloud Assets when a Slack slash command is triggered, and then how to send back the assets to the Slack channel where the command originated from.
GetWorkspaceId
script and copy the appropriate workspace Id from the console.Parameters
and configure parameters to meet you needs.WORKSPACE_ID
variable./command_name
), after successful processing you should receive a message containing the 10 latest assets.import JSMCloud from "./api/jsm/cloud";
// This function retrieves JSM Cloud workspace Id
export default async function (): Promise<void> {
const workspaceId = ((await JSMCloud.Assets.getWorkspaces()).values?.map(w => w.workspaceId) ?? []).join('\n');
// Log out the workspace Id's
console.log(workspaceId)
}
import JSMCloudAssets from "./api/jsm/cloud/assets";
import Slack from "./api/slack";
import { SlashCommandEvent } from '@sr-connect/slack/events';
import { ObjectAsResponse } from '@managed-api/jira-service-management-cloud-assets-core/definitions/ObjectAsResponse';
/**
* This function fetches JSM Cloud Assets 10 most recent assets created in the default schema (Service objects) by Slack's slash command,
* and then sends back information to Slack channel where the message originated from.
* Command syntax: /command_name
*
* @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): Promise<void> {
if (Object.keys(event).length === 0) {
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 { WORKSPACE_ID, MAX_ASSETS } = getEnvVars(context);
try {
// Check if WORKSPACE_ID was set
if (!WORKSPACE_ID) {
// If not, then send approproate message back to Slack
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: 'Missing WORKSPACE_ID'
}
});
// And halt the script execution
return;
}
// Retrieve the assets
const assets = (await JSMCloudAssets.Aql.getObjects({
workspaceId: WORKSPACE_ID,
resultPerPage: MAX_ASSETS,
qlQuery: 'objectType = Service ORDER BY created DESC',
})).objectEntries;
// Check if there are any assets
if (assets.length > 0) {
// If there are then send a message to Slack with all the assets found
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
blocks: [{
type: 'section',
text: {
type: 'mrkdwn',
text: `${assets.map(a => getAssetsInfo(a)).join('\n')}`
}
}]
}
});
} else {
// If no assets are found, sent appropriate message
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: 'No assets found'
}
});
}
} catch (e) {
// If something went wrong, then log it out
console.error('Error while processing slash command', event, e);
// And send appropriate message back to Slack
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: 'Something went wrong while retrieving assets'
}
});
}
}
/**
* Function that transforms information into Slack's compatible markdown format
*/
function getAssetsInfo(assets: ObjectAsResponse) {
// Check if object contains description field
const containsDescription = assets.attributes?.some(x => x.objectTypeAttributeId === "5");
return `โข Key: ${assets.objectKey} | Name: ${assets.label} | Description: ${containsDescription ? assets.attributes?.filter(x => x.objectTypeAttributeId === "5").map(x => x.objectAttributeValues.map(x => x.displayValue)) : 'N/A'}`
};
interface EnvVars {
MAX_ASSETS: number;
WORKSPACE_ID: string;
}
export function getEnvVars(context: Context) {
return context.environment.vars as EnvVars;
}