Revert Jira Cloud issue status back if an open PR is detected in GitHub


Get Started

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

About the template


This integration reverts Jira Cloud issue status back to old status when an open PR is detected in GitHub and sends a notification. 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

TypeScriptOnJiraCloudIssueUpdated
Issue Updated

README


๐Ÿ“‹ Overview

This integration reverts Jira Cloud issue status back to old status when an open PR is detected in GitHub and sends a notification.

๐Ÿ–Š๏ธ Setup

  • Configure the API Connection and Event Listener by creating connectors for Jira Cloud and GitHub, or use existing ones.
  • Go to Parameters and configure parameters to meet you needs.

๐Ÿš€ Usage

Change the status of an issue in Jira Cloud that is considered "Closed", after successful event processing when a matching PR is detected in GitHub, the issue will be reverted back to old status and issue level notification send to the user who triggered the event (if possible).

API Connections


TypeScriptOnJiraCloudIssueUpdated

import JiraCloud from './api/jira/cloud';
import GitHub from './api/github';
import { IssueUpdatedEvent } from '@sr-connect/jira-cloud/events';

/**
 * This function listens to an Issue change event. If the new status is considered "Closed", a search for GitHub PRs is conducted, looking for PRs that have a branch name starting with the issue key.
 * If one or more branches with matching issue key are found, status is reverted back and an issue notification is triggered for the user who triggered the status change event.
 *
 * @param event Object that holds Issue Updated event data
 * @param context Object that holds function invocation context data
 */
export default async function (event: IssueUpdatedEvent, 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 { CLOSED_STATUS_NAME, GITHUB_OWNER_NAME, GITHUB_REPO_NAME } = getEnvVars(context);

    const statusChangeChangelogItem = event.changelog.items.find(cl => cl.field === 'status');
    console.log(`Received status change event (${statusChangeChangelogItem.toString}) for issue: ${event.issue.key}`);

    if (statusChangeChangelogItem && statusChangeChangelogItem.toString === CLOSED_STATUS_NAME) {
        const pullRequests = await GitHub.Pull.getPullRequests({
            owner: GITHUB_OWNER_NAME,
            repo: GITHUB_REPO_NAME,
            state: 'open',
            per_page: 100 // PS! If you suspect you may have more than 100 PRs open at any given time, please implement the pagination logic for this call
        });

        const branchesWithOpenPRs = pullRequests.map(pr => pr.head.ref);

        const branchWithMatchingOpenPR = branchesWithOpenPRs.filter(pr => pr.toLowerCase().startsWith(event.issue.key.toLocaleLowerCase()));
        if (branchWithMatchingOpenPR.length > 0) {
            console.log(`Reverting back to old status, detected following branches with open PRs: ${branchWithMatchingOpenPR.join(', ')}`);
            const transitions = await JiraCloud.Issue.Transition.getTransitions({
                issueIdOrKey: event.issue.key
            });

            const matchingTransition = transitions.transitions.find(t => t.name === statusChangeChangelogItem.fromString);

            if (!matchingTransition) {
                throw Error(`No matching workflow transition was found for status to revert back to: ${statusChangeChangelogItem.fromString}`);
            }

            await JiraCloud.Issue.Transition.performTransition({
                issueIdOrKey: event.issue.key,
                body: {
                    transition: {
                        id: matchingTransition.id
                    }
                }
            });

            // This API call will fail with `No recipients were defined for notification` if the user has not checked to get notified for `You make changes to the issue` in their personal settings.
            // Consider using oher forms of notification if this is an issue, for example finding the user and sending a message in the Slack or MS Teams.
            await JiraCloud.Issue.sendNotification({
                issueIdOrKey: event.issue.key,
                body: {
                    to: {
                        users: [{ accountId: event.user.accountId }]
                    },
                    subject: `${event.issue.key} issue was reverted back to ${statusChangeChangelogItem.fromString} status`,
                    textBody: `${event.issue.key} issue was reverted back to ${statusChangeChangelogItem.fromString} status, because the following branches with open PRs were detected: ${branchWithMatchingOpenPR.join(', ')}`,
                    htmlBody: `<h1>${event.issue.key} issue was reverted back to ${statusChangeChangelogItem.fromString} status, because the following branches with open PRs were detected: ${branchWithMatchingOpenPR.join(', ')}<h1>`
                }
            });
        }
    }
}

interface EnvVars {
    CLOSED_STATUS_NAME: string;
    GITHUB_OWNER_NAME: string;
    GITHUB_REPO_NAME: string;
}

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