Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Cloud behavior script to read a yaml file from issue attachment

Jagadeswar Reddy Muddireddy
November 28, 2024

Hello Team Good day,

In the jira cloud behavior i have a requirement to read some input yaml file attached in one of the issues can some help with the code snippet for this.

1 answer

0 votes
Germán Morales _ Hiera
Atlassian Partner
May 10, 2026

Hi Jagadeswar! To read a YAML file from an issue attachment in Jira Cloud using ScriptRunner Behaviors (Groovy), you need to:

 

1. Get the issue key/ID from the current issue context

2. Use the Jira REST API to fetch the list of attachments

3. Download the attachment content using the attachment URL

4. Parse the YAML content

 

Here's a code snippet to get you started:

 

import com.atlassian.jira.component.ComponentAccessor

import groovy.yaml.YamlSlurper

 

def issueKey = issue.key

def attachmentManager = ComponentAccessor.attachmentManager

def attachments = attachmentManager.getAttachments(issue)

 

// Find the YAML attachment

def yamlAttachment = attachments.find { it.filename.endsWith('.yaml') || it.filename.endsWith('.yml') }

 

if (yamlAttachment) {

    def attachmentData = attachmentManager.streamAttachmentContent(yamlAttachment) { inputStream ->

        inputStream.text  // reads the stream as a String

    }

    

    // Parse YAML

    def yaml = new YamlSlurper()

    def parsedData = yaml.parseText(attachmentData)

    

    // Now use parsedData as a Map

    log.warn("YAML content: ${parsedData}")

} else {

    log.warn("No YAML attachment found on issue ${issueKey}")

}

 

Note: YamlSlurper is available in Groovy 3+. If you're on an older version of ScriptRunner, you may need to add the SnakeYAML library as a dependency, or use the REST API approach to fetch the attachment content.

 

Let me know if you need further help adapting this for your specific use case!

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events