One of the challenges that Jira admins face is monitoring the health of their Jira instance. While there are some built-in tools for doing this, it’s useful to know how to perform routine maintenance using ScriptRunner.
One such routine maintenance task is the monitoring of Jira Project sizes. There’s no direct method or way of doing this; instead we get all of the issues for a given project, and sum up the size of the attachments on each issue. In this way, we get an idea of which Jira Projects are becoming unwieldy.
The code required to perform this calculation isn’t complicated. However, if the script runs too long it’ll simply time out. This is especially true if you’re calculating the size of multiple Projects. Here’s the code to calculate the size of a single project:
import org.ofbiz.core.entity.GenericValue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager
def totalSize = 0
def attachmentManager = ComponentAccessor.getAttachmentManager()
ProjectManager projectManager = ComponentAccessor.getProjectManager()
def projectName = "<projectName>"
Project proj = projectManager.getProjectByCurrentKey(projectName)
IssueManager issueManager = ComponentAccessor.getIssueManager()
for (GenericValue issueValue: issueManager.getProjectIssues(proj.genericValue)) {
Issue issue = issueManager.getIssueObject(issueValue.id)
attachmentManager.getAttachments(issue).each {
attachment ->
totalSize += attachment.filesize
}
}
log.warn("Total size of attachments for ${proj.name} is ${totalSize / 1024}