How to create issues inside methods in postfunctions?

Shagufta Gurmukhdas November 28, 2016

I have a groovy script and in it I create issues several times under different conditions. I have copied pasted the below code several times in my script. I want to write a function to do that. But when i write these lines in a function, nothing is accessible. The issueFactory, the functions of MutableIssue, nothing. They give an error. Why is that?!

MutableIssue newSubTask = issueFactory.getIssue() 
newSubTask.setAssigneeId(user.name) 
newSubTask.setParentObject(parentIssue) 
newSubTask.setFixVersions(Arrays.asList(versionn)) 
newSubTask.setProjectObject(parentIssue.getProjectObject()) 
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == "Sub-task"}.id)   
 
... 
..

6 answers

7 votes
Jon Mort [Adaptavist]
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.
November 28, 2016

Hi Shagufta,

The following code will create a issue in the project TP of type task:

def projectKey = 'TP'
def taskType = get('/rest/api/2/issuetype').asObject(List).body.find { it['name'] == 'Task' }['id']
post('/rest/api/2/issue')
        .header('Content-Type', 'application/json')
        .body(
        [
                fields: [
                        summary    : 'Task Summary',
                        description: "Don't forget to do this!.",
                        project    : [
                                key: projectKey
                        ],
                        issuetype  : [
                                id: taskType
                        ]
                ]
        ])
        .asString().body

This page details the differences between ScriptRunner Server and Cloud: http://scriptrunner-docs.connect.adaptavist.com/jiracloud/migrating.html

Regards, Jon

0 votes
Shagufta Gurmukhdas November 29, 2016

I know and i have done all of it already. I am just wondering if it is possible to put it in a user defined method, to enhance code reusability

0 votes
Martin Brehovsky November 28, 2016

As you are using JIRA Cloud, there are some differences in the usage. You will have to use JIRA's REST API to create new issue. Please have a look at the differences in Script Runner for JIRA Cloud documentation on the differences between Server and Cloud and JIRA REST API how to create an issue. In case it is suitable for your use case you can use Workflow Post function to create subtask described here.

0 votes
Shagufta Gurmukhdas November 28, 2016

JIRA cloud

 

0 votes
Jon Bevan [Adaptavist]
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.
November 28, 2016

Are you using JIRA Cloud or JIRA Server?

0 votes
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.
November 28, 2016

Here is code example for subtask creation. Here created a cubtask for each user into multyuser select field:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.issue.index.IssueIndexManager;
import com.atlassian.jira.config.SubTaskManager;
import java.text.SimpleDateFormat;
import java.text.FieldPosition;
import java.sql.Timestamp;
//Issue issue = ComponentAccessor.getIssueManager().getIssueObject("OEBSR12-3631")
if(issue.getDueDate() == null){
    Calendar dueDate = Calendar.getInstance();
    int addedDays = 0;
    while(addedDays < 2 ){
        dueDate.add(Calendar.DAY_OF_YEAR, 1)
        if( !((dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) || (dueDate.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) ){
            ++addedDays;
        }
    }
    issue.setDueDate(new Timestamp(dueDate.getTimeInMillis()))
}
IssueService issueService = ComponentAccessor.getIssueService();
IssueService.IssueResult issueResult;
IssueService.CreateValidationResult createValidationResult;
IssueInputParameters issueInputParameters =	issueService.newIssueInputParameters();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yy");
issueInputParameters.setDueDate(simpleDateFormat.format(new Date( issue.getDueDate().getTime()), new StringBuffer(), new FieldPosition(0)).toString());
issueInputParameters.setIssueTypeId("11104")
issueInputParameters.setProjectId(issue.getProjectId())
issueInputParameters.setSummary("Согласование документа")
issueInputParameters.setDescription(issue.getDescription());
curUser = ComponentAccessor.getJiraAuthenticationContext().getUser();
SubTaskManager subTaskManager = ComponentAccessor.getSubTaskManager();
IssueIndexManager issueIndexManager = ComponentAccessor.getIssueIndexManager();
long soglasovantId = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Согласовант").getIdAsLong();
for(user in  ComponentAccessor.getCustomFieldManager().getCustomFieldObject(14600L).getValue(issue)){
    issueInputParameters.setAssigneeId( user.name);
    issueInputParameters.addCustomFieldValue(soglasovantId, user.name)
    createValidationResult = issueService.validateSubTaskCreate(curUser, issue.getId(), issueInputParameters);
    if(createValidationResult.isValid()){
        issueResult = issueService.create(curUser, createValidationResult);
        //return "" + issueResult.getErrorCollection();
        subTaskManager.createSubTaskIssueLink(issue, issueResult.getIssue(), user.directoryUser);
        issueIndexManager.reIndex(issue);
        issueIndexManager.reIndex(issueResult.getIssue());
    }
}
Shagufta Gurmukhdas November 28, 2016

Is it possible to use methods ?

Jon Mort [Adaptavist]
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.
November 28, 2016

Given that this is ScriptRunner for JIRA Cloud the above code will not work

Suggest an answer

Log in or Sign up to answer