Copy sprint start and end time from issue type "Sprint" to issue type "Story"

Dan27 May 21, 2018

Hi all,

I created new issue type called "Sprint":

-With summary that looks like in regular issue sprint custom fieldץ

-With start and end time.

I would like to create a script listener with trigger "Issue Updated" that will copy the start and end time from "Sprint" issue (with the same summary of script custom field of the issue) and insert it to start and end time of the specific issue.

I didn't success to use 2 different issue type in the script listener, so for now I refer to specific issues.

Can anyone can help me understand why the start and end time of the relevant issue didn't changed? 

 

My Code for now:

IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class);
Issue issue = issueManager.getIssueObject("WBL-46003");

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();


//If Custom Field of issue is not null
if (customFieldManager.getCustomFieldObjectByName("Sprint")!=null)
{


List<String> curSpr = (List<String>) issue.getCustomFieldValue(customFieldManager.getCustomFieldObjectByName("Sprint"));
def name= curSpr[0].getName();


CustomField issueStartDate = customFieldManager.getCustomFieldObjectByName("Sprint Start Date");
CustomField issueEndDate = customFieldManager.getCustomFieldObjectByName("Sprint End Date");

Issue spr = issueManager.getIssueObject("WBL-46075");

//Take Sprint issue- start & end date
def issuetype = spr.getIssueType().name;

if(issuetype=="Sprint")
{

def summary = spr.getSummary().toString();
if(summary==name)
{
//sprint
def startDate = customFieldManager.getCustomFieldObjects(spr).find {it.name == "Sprint Start Date"};
def endDate = customFieldManager.getCustomFieldObjects(spr).find {it.name == "Sprint End Date"};
Object startDateVal = startDate.getValue(spr);
Object endDateVal = endDate.getValue(spr);


IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();
issueStartDate.updateValue(null, spr, new ModifiedValue(issue.getCustomFieldValue(issueStartDate), startDate), changeHolder);
issueEndDate.updateValue(null, spr, new ModifiedValue(issue.getCustomFieldValue(issueEndDate), endDate), changeHolder);

return issueStartDate;
return issueEndDate;
}
}

2 answers

1 accepted

0 votes
Answer accepted
Alexey Matveev
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.
May 21, 2018

Hello,

What are the error in atlassian-jira.log file. Also you should log some info about your script execution into the atlassia-jira.log file.

you can use log.error("summary: " + summary) to log the value of the summary variable.

Dan27 May 21, 2018

Hi,

I did it, the summary was good and this line: if(summary==name) was true.

Alexey Matveev
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.
May 21, 2018

I do not understand your code. You take two fields and then you update them with the values that are in this fields. Why would you do it? Usually a script takes a value from a field and updates another field.

Also the second return statement is never reached in your script. You can delete it. More over why would you return a value at all?

Dan27 May 21, 2018

I would like to take the values from fields start\end sprint date in "Sprint" issue and paste them in fields start\end sprint date in all other issues (story, bug..).

The trigger should be while the other issue updated.

I tried to do it with: compare the "sprint field name" (of the updated issue) with the summary of the "Sprint" issue -> then take the values from the start\end sprint field and do the copy.

I started work with Groovy and Jira one month ago so it's very new for me..

Alexey Matveev
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.
May 21, 2018

I got it. It should be like this

issueStartDate.updateValue(null, spr, new ModifiedValue("", issue.getCustomFieldValue(issueStartDate)), changeHolder);
issueEndDate.updateValue(null, spr, new ModifiedValue("", issue.getCustomFieldValue(issueEndDate)), changeHolder);

Dan27 May 21, 2018

it deleted my start and end date of Sprint issue instead...

Can you help me to change it ? I need:

issueStartDate = startDate

Alexey Matveev
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.
May 21, 2018

those lines above set the issueStartDate field of the spr issue from the values of the current issue. Is it what you want?

Dan27 May 21, 2018

The opposite.. Set issueStartDate of current issue from the values of spr issue (startDate) 

Alexey Matveev
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.
May 21, 2018

If you want from spr value to the current issue then change it like this

issueStartDate.updateValue(null, issue, new ModifiedValue("", spr.getCustomFieldValue(issueStartDate)), changeHolder);
issueEndDate.updateValue(null, issue, new ModifiedValue("", spr.getCustomFieldValue(issueEndDate)), changeHolder);

Dan27 May 21, 2018

Thank you!!!!!!!!! Work!

Now, Do you have an idea how can I change it to something generic ? not a specific "Sprint" issue (spr)... Maybe a list with issuetype=Sprint ?

Alexey Matveev
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.
May 21, 2018

You can use a jql query to get a list of issues where issuetype = Sprint.

You can find an example here:

https://community.atlassian.com/t5/Marketplace-Apps-questions/How-to-run-JQL-query-inside-Groovy-script/qaq-p/194691

Dan27 May 27, 2018

ok.. But where can I put the condition (issuetype=Sprint) in the JQL code ? And I write the code inside the script listener ?

Alexey Matveev
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.
May 28, 2018

A listener work just for an event for a single issue. If you want to choose other issues, you can get a list of issues by a JQL condition. The code would look like this:

def findIssues(String jqlQuery) {
def issueManager = ComponentAccessor.issueManager
def user = ComponentAccessor.jiraAuthenticationContext.user
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser.class)
def searchProvider = ComponentAccessor.getComponent(SearchProvider.class)

def query = jqlQueryParser.parseQuery(jqlQuery)
def results = searchProvider.search(query, user, PagerFilter.unlimitedFilter)
results.issues.collect
{ issue -> issueManager.getIssueObject(issue.id) }
}

def jqlQuery = "issuetype = Sprint"
def issues = findIssues(jqlQuery)
Dan27 May 28, 2018

Ok, and now I changed in my last code the "spr" to "issues" ? 

Dan27 May 28, 2018

work !!!! Thanks !!

0 votes
Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 21, 2018

Since you are using script runner, my suggestion to you is to try out the code in the "Script console" with proper logging so that you can see the log output on the console itself.

https://www.adaptavist.com/doco/display/SFJ/Set+logging+to+help+debug+your+scripts

Dan27 May 21, 2018

Hi

I try it with script console ..

return issueStartDate;
return issueEndDate;
}

The results was good and gave me the start and end dates of the 'Sprint' issue.

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 21, 2018

Here's the problem in your code

issueStartDate.updateValue(null, spr, new ModifiedValue(issue.getCustomFieldValue(issueStartDate), startDate), changeHolder);

You are passing custom field object "startDate" in the  ModifiedValue constructor, it expects an value and not an object. Thus, you should pass "startDateVal " instead of "startDate" and that should do the trick! 

Tarun Sapra
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 21, 2018
Dan27 May 21, 2018

Do I need the return in the end ?

Still start and end date of the updated issue didn't change.. :( 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events