Retrieve Tempo Cloud worklog history from Slack command


Get Started

Not the template you're looking for? Browse more.

About the template


This template demonstrates how to fetch Tempo Cloud issue worklog history when a Slack slash command is triggered, and then how to send back the history to the Slack channel where the command originated from. Get started to learn more.

About ScriptRunner Connect


What is ScriptRunner Connect?

ScriptRunner Connect is an AI assisted code-first (JavaScript/TypeScript) integration platform (iPaaS) for building complex integrations and automations.

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.

Template Content


README

Scripts

TypeScriptOnSlackSlashCommand
Slash Command

README


๐Ÿ“‹ Overview

This template demonstrates how to fetch Tempo Cloud issue worklog history when a Slack slash command is triggered, and then how to send back the history to the Slack channel where the command originated from.

๐Ÿ–Š๏ธ Setup

  • Configure the API Connection and Event Listener by creating connectors for JiraCloud, Tempo Cloud and Slack, or use existing ones.
  • (Optional) Go to Parameters and configure parameter to meet you needs.
  • PS! Make sure the Jira Cloud and Tempo Cloud connectors have an access to issues that users may want to read from.

๐Ÿš€ Usage

Trigger a Slack slash command (/command_name <ISSUE_KEY>), after successful event processing a message should be posted back to Slack with the worklog history for the issue the user asked for.

API Connections


TypeScriptOnSlackSlashCommand

import JiraCloud from './api/jira/cloud';
import TempoCloud from './api/tempo/cloud';
import Slack from './api/slack';
import { SlashCommandEvent } from '@sr-connect/slack/events';
import { WorklogAsResponse } from '@managed-api/tempo-cloud-v4-core/definitions/WorklogAsResponse';

/**
 * This function fetches Tempo Cloud worklog history based on the data trigger by Slack's slash command, and then sends back history to Slack channel where the message originated from.
 * Command syntax: /command_name <ISSUE_KEY>
 *
 * @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_WORKLOGS } = getEnvVars(context);

    try {
        // Get the issue key from command
        const issueKey = event.text.trim();

        // Check if issue key was specified
        if (!issueKey) {
            // 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: <ISSUE_KEY>'
                }
            });

            // And halt the script execution
            return;
        }

        const issue = await JiraCloud.Issue.getIssue<null>({
            issueIdOrKey: issueKey,
            errorStrategy: builder => builder.http404Error(() => null) // In case the issue is not found, return null when 404 is returned
        });

        if (!issue) {
            // If not, then send appropriate message back to Slack
            await Slack.Chat.postMessage({
                body: {
                    channel: event.channel_id,
                    blocks: [{
                        type: 'section',
                        text: {
                            type: 'mrkdwn',
                            text: `Issue not found with key: ${issueKey}`
                        }
                    }]
                }
            });

            // And halt the script execution
            return;
        }

        // Find the worklogs
        const worklogs = await TempoCloud.Worklog.getWorklogs({
            issueId: [+issue.id!],
        });

        if (worklogs.results.length === 0) { // Check if any worklogs were found
            // If not, then send appropriate message back to Slack
            await Slack.Chat.postMessage({
                body: {
                    channel: event.channel_id,
                    blocks: [{
                        type: 'section',
                        text: {
                            type: 'mrkdwn',
                            text: `No worklogs were found for issue with key: ${issueKey}`
                        }
                    }]
                }
            });
        } else {
            // If worklogs were found, take only the first 10 number of MAX_WORKLOGS
            const filteredWorklogs = worklogs.results.slice(0, MAX_WORKLOGS);

            // Format worklogs 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: `Worklogs for ${issueKey}${worklogs.results.length > MAX_WORKLOGS ? ` (first ${MAX_WORKLOGS})` : ''}:\n${filteredWorklogs.map(c => getWorklogMessage(c)).join('\n')}`
                        }
                    }]
                }
            });
        }
    } catch (e) {
        // If something went front, 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 Tempo worklogs'
            }
        });
    }
}

/**
 * Function that transforms a worklog into Slack's compatible markdown format
 */
function getWorklogMessage(worklog: WorklogAsResponse) {
    return `โ€ข ${worklog.startDate} ${worklog.startTime} - ${worklog.timeSpentSeconds / 60 / 60}h`;
}

interface EnvVars {
    MAX_WORKLOGS: number;
}

export function getEnvVars(context: Context) {
    return context.environment.vars as EnvVars;
}
Documentation ยท Support ยท Suggestions & feature requests