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 fetch the GitLab repository commit history based on data triggered by Slack slash command, and then how to send the history back to the Slack channel where the message originated from.
Parameters
and configure parameter to meet you needs.Trigger a Slack slash command (/command_name <REPOSITORY_OWNER> <REPOSITORY_NAME>
), after successful event processing a message should be posted back to Slack with the commit history for the repository the user asked for.
import GitLab from './api/gitlab';
import Slack from './api/slack';
import { SlashCommandEvent } from '@sr-connect/slack/events';
import { Commit } from '@managed-api/gitlab-v4-core/definitions/commit';
/**
* Entry point to Slash Command event
* This function fetches GitLab repository commit history based on data triggered by Slack's slash command, and then sends back history to Slack channel where the message originated from.
* Command syntax: /command_name <REPOSITORY_OWNER> <REPOSITORY_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 { MAX_COMMITS } = getEnvVars(context);
try {
// Split the command params
const params = event.text.split(' ');
// Check if correct number of params were passed along
if (params.length !== 2) {
// If not, then send appropriate message back to Slack
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: 'Invalid number of parameters, required parameters: <REPOSITORY_OWNER> <REPOSITORY_NAME>'
}
});
// And halt the script execution
return;
}
// Get the specified repository owner from the params array
const repositoryOwner = params[0]
// Get the specified repository name from the params array
const repositoryName = params[1];
// Find all the commits
const commits = await GitLab.Commit.getCommits<null>({
id: `${encodeURIComponent(repositoryOwner)}/${encodeURIComponent(repositoryName)}`, // ID must be URL encoded
errorStrategy: builder => builder.http404Error(() => null) // In case the repository is not found, return null when 404 is returned
});
// If commits were found
if (commits) {
// Then take only the most recent number of MAX_COMMIST
const filteredCommits = commits.slice(0, MAX_COMMITS);
// Format commits into Slack's BlockKit message and send message back to Slack
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
blocks: [{
type: 'section',
text: {
type: 'mrkdwn',
text: `Commit history for ${repositoryOwner}/${repositoryName}${commits.length > MAX_COMMITS ? ` (${MAX_COMMITS} most recent)` : ''}:\n${filteredCommits.map(c => getCommitMessage(c)).join('\n')}`
}
}]
}
});
} else {
// If repository not found, send appropriate message
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: `Repository not found with repository owner ${repositoryOwner} and name ${repositoryName}`
}
});
}
} catch (e) {
// If something went wrong, then log it out
console.error('Error while processing getCommitHistory 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 commit history'
}
});
}
}
/**
* Function that transforms a commit into Slack's compatible markdown format
*/
function getCommitMessage(commit: Commit) {
return `โข <${commit.web_url}|${commit.message}> by ${commit.author_name ?? 'Unknown'} at ${new Date(commit.committed_date ?? 0).toUTCString()}`;
}
interface EnvVars {
MAX_COMMITS: number;
}
export function getEnvVars(context: Context) {
return context.environment.vars as EnvVars;
}