Convert files uploaded through Document Vault into regular JIRA issue attachments

Rei Song February 16, 2016

I noticed that if the Document Vault gets disabled for a given project, the files uploaded through it disappears, but come back upon re-activation. This means the files themselves exist somewhere, but are placed at a different location than regular issue attachments. I wanted to know if there is a way to convert such files into regular JIRA issue attachments? If it can be done in a bulk for the entire project, that would be great as well.

1 answer

1 vote
Paul Clark _Redmoon Software_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 16, 2016

Hi Rei

Normal attachments are stored in the JIRA application data directory under data/attachments.  The document vault files are stored under data/attachments/DocumentVault (with each project in a directory under that and issues at the next level.  There is no method of converting these from Document Vault attachments to normal attachments.  They can not just be copied across to the attachments directory because normal JIRA attachments have accompanying database records for each attachment.

Rei Song February 17, 2016

Thanks for the quick response smile

Rei Song February 17, 2016

I actually was able to utilize the Script Runner add-on to perform this ask. I tested this on our Dev environment and was able to move re-upload attachments with success. Just in case, I wanted to run the code by you to see if you see any dangers in performing the task this way. Please let me know smile

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;

// Create necessary objects
def im = ComponentAccessor.getIssueManager();
def am = ComponentAccessor.getAttachmentManager();
def um = ComponentAccessor.getUserManager();

// Dive into the Document Vault directory (project specific)
Path dir = Paths.get('Enter directory here');

// Loop through each issue directory
dir.eachFile {
    if (Files.isDirectory(it) && im.isExistingIssueKey(it.fileName.toString())) {
    	
    	// Create issue object
        def currentIssue = im.getIssueByCurrentKey(it.fileName.toString());
        // Append issue key to the Document Vault directory
        Path newDir = Paths.get('Enter Document Vault directory' + it.fileName.toString());
        
        newDir.eachFile {
            am.createAttachment(it.toFile(), it.fileName.toString(), Files.probeContentType(it).toString(), um.getUserByName("enter username"), currentIssue);
        }
    }
}
Paul Clark _Redmoon Software_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 17, 2016

Hi Rei

I'm not too sure what language you are using but with the comments I think I can follow it (although I think the comment "Create Issue Object" should be "Get Issue Object").  So I think you are:

  • Going to the project directory that you want (under Document Vault)
  • For each directory, look up the issue
  • For each file in the issue directory, loop through and create an attachment.

That sounds about right.  Would be good if you had a test instance of JIRA you could practise on.  Copy the a project and a couple of issue (2 should be fine) directories from the Document Vault directory onto the test box.  Make sure you have a project and issues with the same keys, then try your script.

Let me know how you go

Thanks

Paul

Rei Song February 18, 2016

Hi Paul,

I was able to run the script successfully on our Dev instance and the files indeed get uploaded as regular JIRA issue attachments. However, I noticed that the files inside the Document Vault directory disappeared afterwards. Also, clicking the attachment links under the Document Vault UI on the actual issue results in 500 error. Even still, I am able to delete the supposedly "non-existent" attachment from the Document Vault UI.

Let me know your thoughts smile

Paul Clark _Redmoon Software_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 18, 2016

Hi Rei

I am not completely sure why they are disappearing but it must have something to do with the attach to JIRA process you are running in script runner.  Probably the simplist answer would be to take a copy of the Document Vault directory before you run script and then copy it back afterwards.  That way all of the files removed will be put back and Document Vault will be able to find them

Thanks

Paul

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events