Duplicate Jira Cloud issue attachments


Get Started

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

About the template


This template demonstrates how to manage issue attachments in Jira Cloud by duplicating the attachments for a specified issue. 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

TypeScriptCopyAttachments

README


๐Ÿ“‹ Overview

This template demonstrates how to manage issue attachments in Jira Cloud by duplicating the attachments for a specified issue.

๐Ÿ–Š๏ธ Setup

  1. Set up the API connection: Create a new connector for Jira Cloud or use an existing one.
  2. Go to Parameters and set the ISSUE_KEY to the desired Jira issue.

๐Ÿš€ Usage the template

Run the CopyAttachments script manually.

If successful, each attachment in the specified issue will have a duplicate.

API Connections


./api/jira/cloud@managed-api/jira-cloud-v3-sr-connect
TypeScriptCopyAttachments

import JiraCloud from './api/jira/cloud';

/**
 * This function creates a copy of all attachments on a single issue level.
 */
export default async function (event: any, context: Context): Promise<void> {
    const { ISSUE_KEY } = context.environment.vars as EnvVars;

    // Get the issue
    const issue = await JiraCloud.Issue.getIssue({
        issueIdOrKey: ISSUE_KEY
    });

    // Iterate over the found attachments
    for (const attachment of issue.fields.attachment) {
        // For each of the attachment
        try {
            // Fetch the attachment content
            const attachmentContentResponse = await JiraCloud.fetch(attachment.content);

            // Check if the attachment content response is OK
            if (!attachmentContentResponse.ok) {
                // If not, then throw an error
                throw Error(`Unexpected response while downloading attachment: ${attachmentContentResponse.status}`);
            }

            // Get the attachment body as array buffer
            const attachmentBody = await attachmentContentResponse.arrayBuffer();

            // Create an attachment under the same issue with the same content but prepend `copy_of_` to the attachment name
            await JiraCloud.Issue.Attachment.addAttachments({
                issueIdOrKey: ISSUE_KEY,
                body: [{
                    content: attachmentBody,
                    fileName: `copy_of_${attachment.filename}`
                }]
            });

            // Print out the attachment name that was successfully copied
            console.log(`Copied attachment: ${attachment.filename}`);
        } catch (e) {
            // If something went wrong, log out the error and attachment details
            console.error('Error while copying attachment', e, {
                attachment
            })
        }
    }
}

interface EnvVars {
    readonly ISSUE_KEY: string;
}
Documentation ยท Support ยท Suggestions & feature requests