Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
package com.acme.scriptrunner.test
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.version.Version
import com.onresolve.scriptrunner.test.AbstractScriptFieldSpecification
import com.onresolve.scriptrunner.test.ScriptFieldCreationInfo
import spock.lang.Shared
class TestVersionDueDateScriptedField extends AbstractScriptFieldSpecification {
@Shared def versionManager = ComponentAccessor.getVersionManager()
@Shared Version VER1_0
@Shared Version VER1_1
@Shared Version VER1_2
@Shared CustomField fieldUnderTest
def setupSpec() {
// add a few of Versions for this project, with varying release dates
VER1_0 = versionManager.createVersion("1.0", null, "no release date",, null)
VER1_1 = versionManager.createVersion("1.1", new Date() + 7, "release date one week in future",, null)
VER1_2 = versionManager.createVersion("1.2", new Date() + 14, "release date two weeks in future",, null)
// easy way to create a new scripted custom field, and set its configuration
def scriptFieldCreation = ScriptFieldCreationInfo.Builder.newBuilder()
.setName("Fix Version Due Date")
// .setScript("a script here") use this instead of setScriptFile to set up code inline
.setNote("shows closest fixversion due date")
fieldUnderTest = scriptFieldCreation.create()
def "test due date script field"() {
def project = getTestProject()
// create an issue to test the fields with
def params = new IssueInputParametersImpl()
params.with {
setSummary("my summary")
setFixVersionIds(versions*.id as Long[])
def createValidationResult = issueService.validateCreate(cwdUser, params)
def issue = issueService.create(cwdUser, createValidationResult).issue
assert issue.isCreated()
log.debug("Create issue ${issue.key}")
then: "cf value should be null as no versions are associated"
expectedValue == issue.getCustomFieldValue(fieldUnderTest)
versions || expectedValue
[] || null // no fix version, no value
[VER1_0] || null // this release has no release date, no value
[VER1_1] || VER1_1.releaseDate // show ver1 release date
[VER1_1, VER1_2] || VER1_1.releaseDate // show ver1 release date, as before ver2
[VER1_0, VER1_1, VER1_2] || VER1_1.releaseDate // checking same result with a version with no release date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment