Simple Script Validator

Mani February 26, 2018

I am using the below script in custom validator on the create transition, to check if the linked issue on the create screen is a part of a specific project (ABC). Link type I am using is blocks.

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.link.IssueLink

import com.atlassian.jira.issue.MutableIssue

def issueLinkManager = ComponentAccessor.getIssueLinkManager() issueLinkManager.getOutwardLinks(issue.id).any{it.getDestinationObject().getProjectObject().getKey() == 'ABC'}

Expected result should be: It should allow user to create issues if the issue selected in Linked issue field is from ABC project. If not it should throw an error.

 

Any help would be appreciated.

 

 

1 answer

1 vote
Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 28, 2018

Here is code example:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink

for( IssueLink link: ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId())){
if(!link.getIssueLinkType().getName().equals("blocks"))
continue;

if(link.getDestinationObject().getProjectObject().getKey().equals('ABC'))
return true
}

return false;
Mani March 30, 2018

@VasiliyZ Thanks for sharing the script.. It worked like a charm.

Mani April 16, 2018

@Vasiliy Zverev

With the below script I am able to add new components ( Component name, Lead, Description ) every time I create a new issue based on the summary, description, assignee.

However I want to be able to update the existing Component Information by create a look back transition and applied the below script, it did not change the existing component and created a new component instead.

 

Any help would be appriciated

___________________

//begin script
//required import statements

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.Project
import org.apache.log4j.Logger
import org.apache.log4j.Level
import static com.atlassian.jira.project.AssigneeTypes.COMPONENT_LEAD
import com.atlassian.jira.bc.project.component.MutableProjectComponent
import com.atlassian.jira.issue.history.ChangeItemBean

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

//logging
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)


def projectComponentManager = ComponentAccessor.getProjectComponentManager()


def project = issue.getProjectObject()

def summary = issue.getSummary()

def description = issue.getDescription()

def assignee = issue.getAssignee().getUsername()

def changeItems = changeHistoryManager.getChangeItemsForField(issue, "Summary")

def component
def componentFound = false

for (ChangeItemBean summaryItem : changeItems)
{
if(summaryItem.toString != null && summaryItem.fromString != null)
{
if (projectComponentManager.containsName(summaryItem.fromString, project.id))
{
componentFound = true
component = projectComponentManager.findByComponentName(project.id, summary)
break
}
}
}

if (componentFound)
{
//component = projectComponentManager.findByComponentName(project.id, summary)
projectComponentManager.update(new MutableProjectComponent(component.id,summary, description, assignee, COMPONENT_LEAD, project.id))
log.info("The assignee value is " + assignee + " and the summary is " + summary + " and the description is " + description + " and the component is " + component + " and the project is " + project + " for this issue")
}
else
{
//creating a new component
def newComponent = projectComponentManager.create(summary, description, assignee, COMPONENT_LEAD, project.id)

//save the newly added component
def components = issue.components
components.add(newComponent)
log.info("The assignee value is " + assignee + " and the summary is " + summary + " and the description is " + description + " and the component is " + newComponent + " and the project is " + project + " for this issue")
}

Vasiliy Zverev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 17, 2018

Here is a refactored version of your code. It seems to be a trouble into changehistory loop. See my comment

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.bc.project.component.ProjectComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.project.Project
import org.apache.log4j.Logger
import org.apache.log4j.Level
import static com.atlassian.jira.project.AssigneeTypes.COMPONENT_LEAD
import com.atlassian.jira.bc.project.component.MutableProjectComponent
import com.atlassian.jira.issue.history.ChangeItemBean

//logging
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

Issue issue;

ProjectComponentManager projectComponentManager = ComponentAccessor.getProjectComponentManager()
Project project = issue.getProjectObject()
ProjectComponent component = null;

for (ChangeItemBean summaryItem : ComponentAccessor.getChangeHistoryManager().getChangeItemsForField(issue, "Summary"))
{
if(summaryItem.toString != null && summaryItem.fromString != null)
{
if (projectComponentManager.containsName(summaryItem.fromString, project.id))
{
//are you sure that component is found
component = projectComponentManager.findByComponentName(project.id, summary)
break
}
}
}

String summary = issue.getSummary();
String description = issue.getDescription();
String assignee = issue.getAssigneeId();
if (component != null)
{
//component = projectComponentManager.findByComponentName(project.id, summary)
projectComponentManager.update(new MutableProjectComponent(component.id, summary, description, assignee, COMPONENT_LEAD, project.id))
log.info("The assignee value is " + assignee + " and the summary is " + summary + " and the description is " + description + " and the component is " + component + " and the project is " + project + " for this issue")
}
else
{
//creating a new component
def newComponent = projectComponentManager.create(summary, description, assignee, COMPONENT_LEAD, project.id)

//save the newly added component
def components = issue.getComponentObjects()
components.add(newComponent)
log.info("The assignee value is " + assignee + " and the summary is " + summary + " and the description is " + description + " and the component is " + newComponent + " and the project is " + project + " for this issue")
}

Suggest an answer

Log in or Sign up to answer