Date: Tue, 19 Mar 2024 07:55:49 +0000 (UTC) Message-ID: <1811777908.2171.1710834949561@docs.stiltsoft.com> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2170_2048092403.1710834949560" ------=_Part_2170_2048092403.1710834949560 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
This section of our documentation refers to the public Java API = available in Smart Attachments for Jira. Java API is available starting fro= m Smart Attachments for Jira 2.1.0.
Please, note that Java API methods are not exposed in Smart Attachments = versions older than 2.1.0. For older versions of the app, = use REST API instead.
In this section, you will find examples of the most common cases of usag= e of the public Java API available in Smart Attachments for Jira. If you ne= ed the whole Java API documentation for the app, click the button below.
Please note that we provide this public Java API to solve use cases not = included in the app functionality available. Our support doesn't include he= lp in writing scripts with the app public Java API. Our Support Team may as= sist with particular requests regarding writing scripts at our sole consent= .
This example illustrates creating a new category scheme with a pre-defin= ed set of categories.
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.user.util.UserManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade import com.atlassian.jira.user.util.UserUtil // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() // Specifying the user on the behalf of who the scheme will be created def currentUser =3D userManager.getUserByName("admin") // Specifying the name of a new category scheme def scheme =3D facade.createScheme("New Scheme", currentUser) // Specifying names of categories within the category def categoryDocuments =3D facade.generateNewConfigCategory("Documents") def categoryScreenshots =3D facade.generateNewConfigCategory("Screenshots") def categoryLogs =3D facade.generateNewConfigCategory("Logs") def categoryPrototypes =3D facade.generateNewConfigCategory("Prototypes") // Creating new categories within the category scheme scheme.categories.add(categoryDocuments) scheme.categories.add(categoryScreenshots) scheme.categories.add(categoryLogs) scheme.categories.add(categoryPrototypes) // Updating the category scheme and saving modifications facade.updateScheme(scheme, currentUser)
This example illustrates the way to update = the configuration of the category scheme (issue type visibility, access res= trictions. auto-distribution rules).
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.config.IssueTypeManager import com.atlassian.jira.security.groups.GroupManager import com.atlassian.jira.user.util.UserManager import com.atlassian.jira.security.roles.ProjectRoleManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade import com.stiltsoft.jira.attachcategory.facade.entity.config.AutoDistribut= ionType // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() IssueTypeManager issueTypeManager =3D ComponentAccessor.getComponent(IssueT= ypeManager.class) GroupManager groupManager =3D ComponentAccessor.getGroupManager() ProjectRoleManager projectRoleManager =3D ComponentAccessor.getComponent(Pr= ojectRoleManager.class) // Specifying the user name and name of the scheme that will be updated def currentUser =3D userManager.getUserByName("admin") def optionalScheme =3D facade.getSchemeByName("New Scheme") // Interrupting the script execution if the scheme with such name does not = exist if (!optionalScheme.isPresent()) { return } def scheme =3D optionalScheme.get() // Specifying the category which configuration should be updated def categoryDocuments =3D scheme.categories.find { category -> category.= name.equalsIgnoreCase("Documents") } // Specifying the issue type in which the category should be visible only def taskIssueType =3D issueTypeManager.getIssueType("10100") categoryDocuments.issueTypes.add(taskIssueType) // Specifying user groups for access restrictions of the category def softwareUserGroup =3D groupManager.getGroup("jira-software-users") categoryDocuments.accessRestrictions.groups.add(softwareUserGroup) categoryDocuments.accessRestrictions.users.add(currentUser) // Specifying project roles for access restrictions of the category def projectRole =3D projectRoleManager.getProjectRole("Administrators") categoryDocuments.accessRestrictions.projectRoles.add(projectRole) // Specifying the autodistribution rule that should be applied to the categ= ory categoryDocuments.autoDistribution.type =3D AutoDistributionType.NAME_PATTE= RN categoryDocuments.autoDistribution.value =3D ".doc, .pdf" facade.updateScheme(scheme, currentUser)
This example illustrates the way to associa= te the project to the specific category scheme.
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.ProjectManager import com.atlassian.jira.user.util.UserManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() ProjectManager projectManager =3D ComponentAccessor.getProjectManager() // Specifyng the category scheme for association with the project def currentUser =3D userManager.getUserByName("admin") def optionalScheme =3D facade.getSchemeByName("New Scheme") // Interrupting the script execution if the scheme with such name does not = exist if (!optionalScheme.isPresent()) { return } def scheme =3D optionalScheme.get() // Specifying the project key which the category scheme is associated with def project =3D projectManager.getProjectByCurrentKey("ONE") scheme.projects.add(project) facade.updateScheme(scheme, currentUser)
This example illustrates the way to remove = the association of the project to the specific category scheme.
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.project.ProjectManager import com.atlassian.jira.user.util.UserManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() ProjectManager projectManager =3D ComponentAccessor.getProjectManager() // Specifying the category scheme for association with the project def currentUser =3D userManager.getUserByName("admin") def optionalScheme =3D facade.getSchemeByName("New Scheme") // Interrupting the script execution if the scheme with such name does not = exist if (!optionalScheme.isPresent()) { return } def scheme =3D optionalScheme.get() // Specifying the project key which the category scheme is associated with def project =3D projectManager.getProjectByCurrentKey("ONE") scheme.projects.remove(project) facade.updateScheme(scheme, currentUser)
This example illustrates moving attachments= from one category to another within the same issue.
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.user.util.UserManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade import com.stiltsoft.jira.attachcategory.facade.entity.issue.AttachmentCate= gories // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() IssueManager issueManager =3D ComponentAccessor.getIssueManager() def currentUser =3D userManager.getUserByName("admin") // Specifying the issue which contains files for moving def issue =3D issueManager.getIssueObject("ONE-1") AttachmentCategories attachmentCategories =3D facade.getAttachmentCategorie= s(issue) // Specifying the source and target categories for moving attachments def categoryDocuments =3D attachmentCategories.categories.find { category -= > category.name.equalsIgnoreCase("Documents") } def categoryScreenshots =3D attachmentCategories.categories.find { category= -> category.name.equalsIgnoreCase("Screenshots") } // Specifying the attachment name def attachments =3D new ArrayList() def attachment =3D categoryDocuments.getAttachments(false).find { attachmen= t -> attachment.filename.equalsIgnoreCase("image.png") } attachments.add(attachment) facade.addAttachmentsToCategory(issue, categoryScreenshots.id, attachments,= currentUser)
This example illustrates the way t= o copy an issue with attachments and preserve attachments in their categori= es.
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.AttachmentManager import com.atlassian.jira.issue.IssueFactory import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.attachment.Attachment import com.atlassian.jira.user.util.UserManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() IssueFactory issueFactory =3D ComponentAccessor.getIssueFactory() IssueManager issueManager =3D ComponentAccessor.getIssueManager() AttachmentManager attachmentManager =3D ComponentAccessor.getAttachmentMana= ger() // Specifying the user and issue key def currentUser =3D userManager.getUserByName("admin") def sourceIssue =3D issueManager.getIssueObject("ONE-1") // Copying an issue and attachments within the same project def issueCopy =3D issueFactory.cloneIssue(sourceIssue) Map<String, Object> newIssueParams =3D ["issue": issueCopy] as Map<= ;String, Object> def clonedIssue =3D issueManager.createIssueObject(currentUser, newIssuePar= ams) attachmentManager.copyAttachments(sourceIssue, currentUser, clonedIssue.key= ) def sourceAttachmentCategories =3D facade.getAttachmentCategories(sourceIss= ue) List<Attachment> attachmentsFromClonedIssue =3D new ArrayList<>= (attachmentManager.getAttachments(clonedIssue)) sourceAttachmentCategories.categories.each({ category -> category.getAttachments(false).each({ attachment -> def attachmentCopy =3D attachmentsFromClonedIssue.find({clonedA= ttachment -> clonedAttachment.filename.equalsIgnoreCase(attachment.filen= ame)}) facade.addAttachmentsToCategory(clonedIssue, category.id, Colle= ctions.singletonList(attachmentCopy), currentUser) attachmentsFromClonedIssue.remove(attachmentCopy) }) })
This example illustrates the way to copy an issue with attachments in ca= tegories to another project with preserving files in categories as in the s= ource issue.
This script will work only in projects that are associated with the same= scheme.
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.AttachmentManager import com.atlassian.jira.issue.IssueFactory import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.attachment.Attachment import com.atlassian.jira.project.ProjectManager import com.atlassian.jira.user.util.UserManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() IssueFactory issueFactory =3D ComponentAccessor.getIssueFactory() IssueManager issueManager =3D ComponentAccessor.getIssueManager() AttachmentManager attachmentManager =3D ComponentAccessor.getAttachmentMana= ger() ProjectManager projectManager =3D ComponentAccessor.getProjectManager() // Specifying the user, issue key and key of the target project def currentUser =3D userManager.getUserByName("admin") def sourceIssue =3D issueManager.getIssueObject("ONE-1") def targetProject =3D projectManager.getProjectByCurrentKey("TWO") // Copying an issue with attachments to another project def issueCopy =3D issueFactory.cloneIssue(sourceIssue) issueCopy.setProjectObject(targetProject) Map<String, Object> newIssueParams =3D ["issue": issueCopy] as Map<= ;String, Object> def clonedIssue =3D issueManager.createIssueObject(currentUser, newIssuePar= ams) attachmentManager.copyAttachments(sourceIssue, currentUser, clonedIssue.key= ) def sourceAttachmentCategories =3D facade.getAttachmentCategories(sourceIss= ue) List<Attachment> attachmentsFromClonedIssue =3D new ArrayList<>= (attachmentManager.getAttachments(clonedIssue)) sourceAttachmentCategories.categories.each({ category -> category.getAttachments(false).each({ attachment -> def attachmentCopy =3D attachmentsFromClonedIssue.find({clonedA= ttachment -> clonedAttachment.filename.equalsIgnoreCase(attachment.filen= ame)}) facade.addAttachmentsToCategory(clonedIssue, category.id, Colle= ctions.singletonList(attachmentCopy), currentUser) attachmentsFromClonedIssue.remove(attachmentCopy) }) })
This example illustrates how to check for the presence of attachments in= a certain category depending on the custom field value.
import = com.opensymphony.workflow.InvalidInputException import com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.CustomFieldManager import com.atlassian.jira.issue.customfields.option.Option import com.atlassian.jira.issue.fields.CustomField import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade import com.stiltsoft.jira.attachcategory.facade.entity.issue.AttachmentCate= gories import com.stiltsoft.jira.attachcategory.facade.entity.issue.AttachmentCate= gory @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) CustomFieldManager customFieldManager =3D ComponentAccessor.getCustomFieldM= anager() CustomField customfield_10600 =3D customFieldManager.getCustomFieldObject("= customfield_10600") Option customFieldValue =3D (Option) issue.getCustomFieldValue(customfield_= 10600) String customFieldValueRaw =3D customFieldValue.getValue() if (customFieldValueRaw.equals("Server")) { =20 AttachmentCategories attachmentCategories =3D facade.getAttachmentCateg= ories(issue) AttachmentCategory serverCategory =3D attachmentCategories.getCategorie= s().find { category -> category.getName() =3D=3D "Server" } =20 def tempAttachmentsCount =3D serverCategory.getTempAttachments().size() def actualAttachmentsCount =3D serverCategory.getAttachments(true).size= () =20 if (tempAttachmentsCount =3D=3D 0 && actualAttachmentsCount =3D= =3D 0) { throw new InvalidInputException("customfield_10600", "The Server ca= tegory is empty") } }
The attachment commenting feature has been deprecated in Smart Attachmen= ts 2.15.2.
This example illustrates how to get the information about the attachment= comments (including the author, comment text, and creation time).
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.IssueManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade import com.stiltsoft.jira.attachcategory.facade.entity.issue.AttachmentCate= gories import com.atlassian.jira.issue.Issue @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) IssueManager issueManager =3D ComponentAccessor.getIssueManager() Issue issue =3D issueManager.getIssueObject("CVT-1") issue.attachments.each{ attachment ->=20 facade.getCommentThreadsForAttachment(attachment).each{ thread ->=20 =09log.info "Thread ID: $thread.threadId" thread.attachmentComments.each{ attachmentComment -> log.info "Comment ID: $attachmentComment.commentId, Author: $at= tachmentComment.author.displayName, Content: $attachmentComment.content, Cr= eated: $attachmentComment.createTime" } } }
This example illustrates getting information about the Jira projects wit= h the Smart Attachments category sche= me activated. The result will bring you the list of all projects in your in= stance, including the archived ones where the category scheme has been acti= vated.
import = com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) // Getting list of projects def projectsList =3D facade.getSchemes().collect { it.getProjects() }.flatt= en() def keys =3D projectsList.collect { it.getKey() } log.warn(keys) projectsList.forEach { log.warn(it.getKey() + " - " + it.getName()) }=
This example illustrates c<= span style=3D"color: rgb(23,43,77);">opying all the attachments from one is= sue and adding them to a linked issue with preserving the attachment catego= ry.
Please note that this script has wo variables. These are "STP-1" and "ST= P-2". You will need to replace these values with the following issue keys f= rom your Jira projects:
use the issue key of the source issue (from which you are copying) i= nstead of "STP-1"
use the issue key of the target issue (to which you are copying) ins= tead of "STP-2"
import = com.atlassian.jira.component.ComponentAccessor import com.atlassian.jira.issue.AttachmentManager import com.atlassian.jira.issue.IssueFactory import com.atlassian.jira.issue.IssueManager import com.atlassian.jira.issue.attachment.Attachment import com.atlassian.jira.user.util.UserManager import com.onresolve.scriptrunner.runner.ScriptRunnerImpl import com.onresolve.scriptrunner.runner.customisers.WithPlugin import com.stiltsoft.jira.attachcategory.facade.SmartAttachmentsFacade // Initializing the app components @WithPlugin("com.stiltsoft.jira.smart-attachments") SmartAttachmentsFacade facade =3D ScriptRunnerImpl.getPluginComponent(Smart= AttachmentsFacade) UserManager userManager =3D ComponentAccessor.getUserManager() IssueFactory issueFactory =3D ComponentAccessor.getIssueFactory() IssueManager issueManager =3D ComponentAccessor.getIssueManager() AttachmentManager attachmentManager =3D ComponentAccessor.getAttachmentMana= ger() // Specifying the user and source issue key, target issue key def currentUser =3D userManager.getUserByName("admin") def sourceIssue =3D issueManager.getIssueObject("STP-1") def targetIssue =3D issueManager.getIssueObject("STP-2") // Copying the source issue attachments to the target issue within the same= project List<Attachment> attachmentsFromSourceIssue =3D new ArrayList<>= (attachmentManager.getAttachments(sourceIssue)) List<Attachment> attachmentsFromTargetIssue =3D new ArrayList<>= (attachmentManager.getAttachments(targetIssue)) attachmentsFromSourceIssue.each { attachment -> def attachmentTarget =3D attachme= ntsFromTargetIssue.find { it.filename.equalsIgnoreCase(attachment.filename)= } if (null =3D=3D attachmentTarget)= { attachmentManager.c= opyAttachment(attachment, currentUser, targetIssue.key) attachmentsFromTarg= etIssue.add(attachment) } } def sourceAttachmentCategories =3D facade.getAttachmentCategories(sourceIss= ue) attachmentsFromTargetIssue =3D new ArrayList<>(attachmentManager.getA= ttachments(targetIssue)) sourceAttachmentCategories.categories.each { category -> category.getAttachments(false).each { attachment -> def attachmentCopy =3D attachment= sFromTargetIssue.find({clonedAttachment -> clonedAttachment.filename.equ= alsIgnoreCase(attachment.filename)}) facade.addAttachmentsToCategory(t= argetIssue, category.id, Collections.singletonList(attachmentCopy), current= User) attachmentsFromTargetIssue.remove= (attachmentCopy) } }