Versioner sammenlignet

Nøgle

  • Linjen blev tilføjet.
  • Denne linje blev fjernet.
  • Formatering blev ændret.

...

Advarsel

With JIRA 7 the "ComponentManager" is removeddeprecated, use "ComponentAccessor" instead - the Scripts below wont work on JIRA 7

As per ScriptRunner 4.2.0.4 there is a bug in the embedded groovy, so scriptfiles can contain Hyphens "-"

.

With JIRA 8 the "ComponentManager" is removed, use "ComponentAccessor" instead.Do notice the Issue List is rather long and with some old problems still open

Info

The plugin has been purchased by Adaptavist in 2015 and is from version 4 Paid Product.

...

Kodeblok
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.CustomFieldManager
 
componentManager (componentManager) ComponentAccessor.getComponentManager();

optionsManager = (optionsManager) ComponentAccessor.getOptionsManager();
customFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Task Type'}
def fieldConfig = cf.getRelevantConfig(issue)
def optionClone = optionsManager.getOptions(fieldConfig).find {it.value == "Clone"}
issue.setCustomFieldValue(cf, optionClone)

...

Kodeblok
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink;
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.Issue
import org.apache.log4j.Category

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();

String IssueType      = issue.getIssueType().name
String ProjectName    = issue.getProjectObject().name
String IssueKey       = issue.key
passesCondition = true

System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " IssueType: " + IssueType + " ProjectName: " + ProjectName + " action=StartScript")

//Traverse all links
issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
   String LinkTypeName    = issueLink.issueLinkType.name
   String LinkStatus      = issueLink.getDestinationObject().getStatus().name
   String LinkIssueKey    = issueLink.getDestinationObject().key
   System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " Issue Link key: " + LinkIssueKey + " Issue Link Type: " + LinkTypeName + " Linked Issue Status: " + LinkStatus)
   if (LinkTypeName == "Awaiting")
   {
        passesCondition = false
        System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " IssueType: " + IssueType + " ProjectName: " + ProjectName + " action=Hide Transition")
   }
}
System.out.println("script=CheckTOPSLinkStatus.groovy Issuekey: " + IssueKey + " IssueType: " + IssueType + " ProjectName: " + ProjectName + " action=EndScript")

...

Kodeblok
issue.assignee == transientVars["issue"].assignee;
issue.setAssignee(null);
issue.setDescription("A new Description");
issue.setSummary("Unit Test - " + transientVars["issue"].key);

Getting a Custom Field Value (String)

Kodeblok
import com.atlassian.jira.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
customFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField UseCustomer = customFieldManager.getCustomFieldObject(10600);

System.out.println("Custom field value: " + issue.getCustomFieldValue(UseCustomer));


Setting a Custom Field Value (String)

...

Kodeblok
String userName="pho"
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserObject(userName)
issue.setAssignee(user)

//Update the issue - may not be nessesary
ComponentManager.getInstance().getIssueManager().updateIssue(ComponentManager.getInstance().jiraAuthenticationContext?.user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
ComponentManager.getInstance().getIndexManager().reIndex(issue);


A broader picture (ref: https://answers.atlassian.com/questions/98433/assigning-issues-via-groovy):

Kodeblok
import com.atlassian.jira.ComponentAccessor;
import com.atlassian.jira.security.Permissions;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.util.ImportUtils
 
//Examples, 8 is my subtask issue type id. Will be different for others
updateAssignee("PRJ-1485", "user1")
 
//Method to do all the work
def updateAssignee(parentId, userName) {
def issue = ComponentAccessor.getIssueManager().getIssueObject(parentId)
def userManager = ComponentAccessor.getUserManager()
def user = userManager.getUserObject(userName)
issue.setAssignee(user)
//Update the issue
ComponentAccessor.getIssueManager().updateIssue(ComponentAccessor.jiraAuthenticationContext?.user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
ComponentAccessor.getIndexManager().reIndex(issue);
}


Remove and Set labels

Kodeblok
import com.atlassian.jira.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.label.LabelManager;
import org.springframework.util.StringUtils;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

labelManager = ComponentAccessor.getLabelManager();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();


CustomField UseCustomer = customFieldManager.getCustomFieldObject(10600);
CustomField Customer = customFieldManager.getCustomFieldObject(10900);

labelManager.removeLabelsForCustomField(10900)

Set labelSet = new HashSet();
labelSet.add(issue.getCustomFieldValue(UseCustomer));

def authContext = ComponentAccessor.getJiraAuthenticationContext()
def user = authContext.getUser()

// issue.getCustomFieldValue(UseCustomer)
// Does not work - the "labelSet" is not a valid set - https://answers.atlassian.com/questions/332748/set-labels-in-jira
labelManager.setLabels(user, issue.id,labelSet, false,false)

...

Kodeblok
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink;

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

import org.apache.log4j.Category
import com.atlassian.jira.issue.comments.CommentManager
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;

def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)
log.debug "debug statements"

String currentUser = ((WorkflowContext) transientVars.get("context")).getCaller();
WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );


issueLinkManager.getOutwardLinks(issue.id)?.each {issueLink ->
        if (issueLink.issueLinkType.name == "parent-child") {
        // Transist Issue
                workflowTransitionUtil.setIssue(issueLink.getDestinationObject());
                workflowTransitionUtil.setUsername(currentUser);
                workflowTransitionUtil.setAction (741)    // 741 = Waiting
        // validate and transition issue
                workflowTransitionUtil.validate();
                workflowTransitionUtil.progress();
        // Add a comment so people have a clue why the child has been closed
                CommentManager commentManager = (CommentManager) ComponentAccessor.getCommentManager();
                String comment = "Status changed to *WAit for solution* as a result of the *Resolve* action being applied to the TD Issue.";
                commentManager.create(issueLink.getDestinationObject(), currentUser, comment, true);
       }
}
Tip

The

Kodeblok
workflowTransitionUtil.setIssue(issueLink.getDestinationObject());

should be

Kodeblok
workflowTransitionUtil.setIssue(issueLink.getSourceObject());

Going "the other way". There is also an getInwardLinks(issue.id) collection.

...

Kodeblok
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.ComponentManager;
User = ComponentAccessor.getJiraAuthenticationContext().getUser().getName();

Send Custom Mail

Some standard JIRA Field can be accessed very direct, other must be throught the 

...

Kodeblok
// This script makes a requirement: if the Custom Field "OprStatusShow" is set to public, Incident Start and Resolved must be set.

import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

//Project "Support" Id = 10130
if (issue.getProjectObject().getId() == 10130) {

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField IncidentResolvedField = customFieldManager.getCustomFieldObject("customfield_12224");
CustomField IncidentStartField = customFieldManager.getCustomFieldObject("customfield_10091");
CustomField OprStatusShowField = customFieldManager.getCustomFieldObject("customfield_11820");


String IncidentResolved = (String)issue.getCustomFieldValue(IncidentResolvedField);
String IncidentStart = (String)issue.getCustomFieldValue(IncidentStartField);
String OprStatusShow = (String)issue.getCustomFieldValue(OprStatusShowField);

if (OprStatusShow == "Yes") {

	if (IncidentStart == null || IncidentResolved == null) {
		InvalidInputException e= new InvalidInputException();
		e.addError("Incident Start And Incident Resolved must not be empty");
	 	throw e;
		}
	}
}


Scripted Fields

A "neat" but raw idea for a scripted field: https://answers.atlassian.com/questions/191893

...