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.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.