drinkits

Rename attachments in Jira using Groovy

While Atlassian is “GATHERING INTEREST” to implement file renaming feature in Jira, it is possible to rename attachments using Groovy.

If you use JMWE, Scriptrunner, etc.  in Jira, you can use following Groovy script:

import org.ofbiz.core.entity.GenericValue
import com.atlassian.jira.ofbiz.OfBizDelegator
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.attachment.Attachment

def attachmentManager = ComponentAccessor.getAttachmentManager()

OfBizDelegator delegator = ComponentAccessor.getComponentOfType(OfBizDelegator.class)
issue.get("attachment")?.each {
  for(GenericValue attachment : delegator.findByField("FileAttachment", "id", it.id)){ 
      attachment.setString("filename", "no_klienta_"+it.filename)
      attachment.store()
  } 
}

 

Kategorijas: Skripti

Paštaisīts digitālais fotorāmis, versija 2.0 » « Paštaisīts digitālais fotorāmis

2 komentāri

  1. This was helpful, thank you!

  2. For Scriptrunner :

    import org.ofbiz.core.entity.GenericValue;
    import com.atlassian.jira.ofbiz.OfBizDelegator;
    import com.atlassian.jira.component.ComponentAccessor;
    import com.atlassian.jira.issue.attachment.Attachment;

    def attachmentManager = ComponentAccessor.getAttachmentManager();
    OfBizDelegator delegator = ComponentAccessor.getComponentOfType(OfBizDelegator.class)

    // issue.getAttachments() replace issue.get(“attachment”)
    issue.getAttachments()?.each {
    for(GenericValue attachment : delegator.findByField(“FileAttachment”, “id”, it.id)){
    if (it.filename.endsWith(“.jpg”)) {
    attachment.setString(“filename”, “Photo.jpg”)
    attachment.store()
    }
    if (it.filename.endsWith(“.jpeg”)) {
    attachment.setString(“filename”, “Photo.jpeg”)
    attachment.store()
    }
    if (it.filename.endsWith(“.pdf”)) {
    attachment.setString(“filename”, “Photo.pdf”)
    attachment.store()
    }
    }
    }

Atbildēt

Jūsu e-pasta adrese netiks publicēta. Obligātie lauki ir atzīmēti kā *

Autortiesības © 2021 drinkits

Theme by Anders NorenLēkt uz augšu ↑