The code provided is a script written in Groovy programming language (Scriptrunner plugin) that converts image attachments of a JIRA issue into PDF format. It makes use of several libraries and APIs, including the Atlassian JIRA API, Apache Commons IO, iText PDF library, and JSoup HTML parser.
In summary, this script can be useful for teams who want to convert image attachments to PDF format for easier sharing and collaboration. It can be customized and extended to support other file formats and JIRA operations.
Here is the code:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment
import com.atlassian.jira.issue.Issue
import org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils
import java.awt.image.BufferedImage
import com.itextpdf.text.Document
import com.itextpdf.text.Image
import com.itextpdf.text.PageSize
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.tool.xml.XMLWorkerHelper
import org.jsoup.Jsoup
import com.itextpdf.text.FontFactory
import com.itextpdf.text.pdf.BaseFont
import java.nio.charset.StandardCharsets
// Converts the given image file to a PDF file
def convertImageToPdf(imageFile, pdfFile) {
// Read the image file as a BufferedImage
BufferedImage image = ImageIO.read(imageFile)
// Create a new PDF document with A4 page size
Document document = new Document(PageSize.A4, 0, 0, 0, 0)
// Create a PdfWriter to write the PDF to the output stream
PdfWriter.getInstance(document, new FileOutputStream(pdfFile))
// Open the PDF document
document.open()
// Create an iText Image object from the image file
Image pdfImage = Image.getInstance(imageFile.path)
// Scale the image to fit the A4 page size
pdfImage.scaleToFit(PageSize.A4.width, PageSize.A4.height)
// Add the image to the PDF document
document.add(pdfImage)
// Close the PDF document
document.close()
}
// Processes the attachments of the given JIRA issue
def processAttachments(Issue issue) {
// Get the AttachmentManager instance
def attachmentManager = ComponentAccessor.getAttachmentManager()
// Get the list of attachments for the issue
def attachments = attachmentManager.getAttachments(issue)
// Iterate over each attachment
attachments.each { Attachment attachment ->
// Create a temporary file for the attachment
def tempFile = File.createTempFile("attachment-", "-" + attachment.filename)
// Mark the temporary file for deletion when the JVM exits
tempFile.deleteOnExit()
// Stream the attachment content to the temporary file
attachmentManager.streamAttachmentContent(attachment, { inputStream ->
FileUtils.copyInputStreamToFile(inputStream, tempFile)
})
// Get the file extension of the attachment
String extension = FilenameUtils.getExtension(tempFile.name).toLowerCase()
// Check if the attachment is an image file (JPEG, JPG, or PNG)
if (["jpg", "jpeg", "png"].contains(extension)) {
// Create a new PDF file for the converted attachment
def pdfFile = new File(tempFile.path + ".pdf")
// Convert the image attachment to PDF format
switch (extension) {
case ["jpg", "jpeg", "png"]:
convertImageToPdf(tempFile, pdfFile)
break
}
// Create a new PDF attachment for the converted PDF file
attachmentManager.createAttachment(pdfFile, pdfFile.name, "application/pdf", ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser(), issue)
// Delete the original image attachment
attachmentManager.deleteAttachment(attachment)
}
}
}
// The entry point of the script
def main() {
// Get the current issue
Issue issue = issue
// Process the attachments of the issue
processAttachments(issue)
}
// Call the main function
main()