kategorijā Skripti

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()
  } 
}

 

Komentēt

Komentēt

  1. 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()
    }
    }
    }