Scriptruuner groovy date null

Modha K Khammammettu August 3, 2017

Hllo

 

Getting null exception 

java.lang.NullPointerException: Cannot invoke method getTime() on null object at java_sql_Timestamp$getTime.call(Unknown Source) at Script37.run(Script37.groovy:28)

 

How com after checking resolution for null date can be null

How can I check for Date null condition

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.builder.JqlClauseBuilder
import com.atlassian.jira.jql.builder.JqlQueryBuilder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.Project

def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
def projectManager = ComponentAccessor.getProjectManager()
import org.apache.log4j.Category
def Category log = Category.getInstance("com.samsung.groovy")
log.setLevel(org.apache.log4j.Level.DEBUG)
Project project = ComponentAccessor.getProjectManager().getProjectObjByKey("JPM");
//CustomField customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("");

IssueManager issueManager = ComponentAccessor.getIssueManager();

log.debug ("Key | Created | Resolved | Elapsed")

for(Issue issue: issueManager.getIssueObjects( issueManager.getIssueIdsForProject(project.getId()))){

if (issue.issueType.name == "Story")
if ( issue.getResolution() != null ) {
log.debug(issue.getKey()+"|"+ new Date( issue.getCreated().getTime())+ "|"+
new Date(issue.resolutionDate .getTime())
+"|"+
(issue.resolutionDate.getTime() - issue.getCreated().getTime()) / 1000 / 3600 / 24 +" | "+
issue.getComponents()*.name)
}
}

 

 

Also how do I get value from a custom field called "mycustom"

 

Please let me know

 

Thanks

 

2 answers

1 accepted

1 vote
Answer accepted
Henning Tietgens
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.
August 4, 2017

In case you want to restrict your issues, you should use a search like this.

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.web.bean.PagerFilter

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)

def jqlSearch = "project = JPM and created > '2017-05-01'"
List<Issue> issues = null
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def result = ""

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlSearch)
if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
issues = searchResult.issues
result += "Found ${issues.size()} issues."
} else {
result += "Invalid JQL: " + jqlSearch
}
result

Henning

Henning Tietgens
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.
August 4, 2017

For searching for issues created in the last two weeks you could use

project = JPM and created > startOfWeek(-2w)

for the complete last two weeks or

project = JPM and created > -2w

 for exactly the last two weeks.

Modha K Khammammettu August 4, 2017

Thanks for he help Henning.

Modha K Khammammettu August 4, 2017

I have already accepted answer, just a followup question...

Like we use eclipse for java programming which helps with syntax, api suggestions etc what editor to use to develop grrovy scripting.

Iam looking for some thing which can suggest available methods on object, code hlp etc just like clipse.

 

Is there any editor which can suggest Jira grrovy api while creating a script? What do you use..?

 

I was looking at https://community.atlassian.com/t5/Answers-Developer-Questions/Where-to-start-looking-when-learning-Groovy-for-JIRA/qaq-p/577129

but it looks dated.

Henning Tietgens
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.
August 6, 2017

Just look in the documentation, there is a chapter about setting up a development environment: https://scriptrunner.adaptavist.com/latest/jira/DevEnvironment.html

Henning

0 votes
Henning Tietgens
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.
August 4, 2017

This shouldn't be the case, maybe corrupt data? Try this in the script console:

import com.atlassian.jira.component.ComponentAccessor

def project = ComponentAccessor.projectManager.getProjectObjByKey("JPM")
def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager

def cf = customFieldManager.getCustomFieldObjectsByName('mycustom').first()

def result = "Key | Created | Resolved | Elapsed | Components | myname<br>"

issueManager.getIssueObjects( issueManager.getIssueIdsForProject(project.getId())).each{ issue ->
if (issue.issueType.name == "Story" && issue.resolution) {
def duration
if (issue.resolutionDate) {
duration = (issue.resolutionDate.getTime() - issue.getCreated().getTime()) / 1000 / 3600 / 24
}
result += "$issue.key|" +
"${issue.getCreated()?.format("YYYY-MM-dd HH:mm:ss")}|"+
"${issue.resolutionDate?.format("YYYY-MM-dd HH:mm:ss")}|" +
"$duration|" +
"${issue.getComponents()*.name?.join(',')}|" +
"${issue.getCustomFieldValue(cf)}"
"<br>"
}
}
result

This also shows you how to get the value of a custom field. In this case I suspect a text field. If it's a different field type, you have to do a little more, like issue.getCustomFieldValue(cf)?.value if it's a select field.

Henning

Modha K Khammammettu August 4, 2017

Thanks Henning

it works great.

If I need to look at issues that are created 

- Only after a certain date 

- Only last 2 weeks 

How do I compare dates?

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events