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 Bitbucket Cloud repository commit history based on a data triggered by a 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_NAME>
where REPOSITORY_NAME is the name you can find from the URL, and not the display 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 { BaseCommitAsResponse } from '@managed-api/bitbucket-cloud-v2-core/definitions/BaseCommitAsResponse';
import { SlashCommandEvent } from '@sr-connect/slack/events';
import BitbucketCloud from "./api/bitbucket/cloud";
import Slack from "./api/slack";
/**
* This function fetches Bitbucket 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.
* Make sure to use the dashed repository name that makes up the URL, and not the display name.
*
* Command syntax: /command_name <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 (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 { MAX_COMMITS } = getEnvVars(context);
try {
// Get the specified repository name from the event
const repositoryName = event.text;
// Find matching repositories
const repositories = await BitbucketCloud.Repository.getRepositories({
q: `name = "${repositoryName}"`,
role: 'member'
});
// Check if more than 1 repository was found
if ((repositories.values ?? []).length > 1) {
// If so, then send appropriate message back to Slack
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: `More than 1 repository found for given name: ${repositoryName}`
}
});
// And stop the function execution
return;
}
// Get the first repository found
const repository = repositories.values?.[0];
// Check if no repositories were found
if (!repository) {
// If so, then send an appropriate message back to Slack
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
text: `Repository not found with given name: ${repositoryName}`
}
});
// And stop the function execution
return;
}
// Find all the commits
const commits = await BitbucketCloud.Repository.Commit.getCommits({
workspace: repository.workspace?.slug ?? '',
repo_slug: repository.slug ?? ''
});
// And then filter the commits to include most recent number of MAX_COMMITS
const filteredCommits = (commits.values ?? []).slice(0, MAX_COMMITS);
// Format commits into Slack's BlockKit message back to Slack
await Slack.Chat.postMessage({
body: {
channel: event.channel_id,
blocks: [{
type: 'section',
text: {
type: 'mrkdwn',
text: `Commit history for <${repository.links?.html.href}|${repository.name}>${(commits.values ?? []).length > MAX_COMMITS ? ` (${MAX_COMMITS} most recent)` : ''}:\n${filteredCommits.map(c => getCommitMessage(c)).join('\n')}`
}
}]
}
});
} 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: BaseCommitAsResponse) {
let message = `โข <${commit.links?.html.href}|${commit.message?.replace('\n', '')}>`;
if (commit.author?.user) {
message += ` by <${commit.author.user.links?.html?.href}|${commit.author.user.display_name}>`
} else {
message += ` by ${commit.author?.raw}`
}
if (commit.date) {
message += ` at ${new Date(commit.date).toUTCString()}`;
}
return message;
}
interface EnvVars {
MAX_COMMITS: number;
}
export function getEnvVars(context: Context) {
return context.environment.vars as EnvVars;
}