You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
I created a custom scripted field and want it to return a Version, so I picked the Version Picker template. When I return out of this script with that chosen, I get no results for my test issue. When I use the Free Text Picker, I get the text of the version I expected. Why wouldn't this script return the version? For my test case, it definitely prints out the line "Answer: 2019.1Q", so plannedFixVersion has a value.
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.version.Version
import java.sql.Timestamp
def log = Logger.getLogger("com.acme.CreateSubTask")
log.setLevel(Level.DEBUG)
def projectManager = ComponentAccessor.getProjectManager()
def versionManager = ComponentAccessor.getVersionManager()
def changeHistoryManager = ComponentAccessor.getChangeHistoryManager()
// Get some data we need
def issueProject = issue.getProjectObject() // get the project associated with the issue; returns type Project
List<Version> projectVersions = versionManager.getVersions(issueProject) // get the list of versions associated with the project; returns List<Version>
def changeHistoryForIssueFixVersion = changeHistoryManager.getChangeItemsForField(issue, 'Fix Version') // get the history of the fixVersion field for the issue; returns List<ChangeItemBean>
// Local storage
List<Timestamp> timesWhenFixVersionChanged = []
List<String> fixVersionHistory = []
List<Boolean> isToStringNull = []
Version plannedFixVersion = null
def numberOfFixVersions = 0
// Let's build an easier to look through list of times the fixVersion changed
if (changeHistoryForIssueFixVersion.size() != 0){
//DEBUG
log.debug "**** Start change history for issue fix version ****"
for (item in changeHistoryForIssueFixVersion) {
log.debug item
//Store every "toString".
Timestamp fixVersionChangedTime = item.getCreated() // returns TimeStamp
String issueFixVersion = item.getToString()
log.debug fixVersionChangedTime
log.debug issueFixVersion
if (issueFixVersion == null){
//if the fixVersion got removed, still need to log it somehow
// save the fromString and mark as invalid
issueFixVersion = item.getFromString()
timesWhenFixVersionChanged.add(fixVersionChangedTime)
fixVersionHistory.add(issueFixVersion)
isToStringNull.add(true)
}
else {
timesWhenFixVersionChanged.add(fixVersionChangedTime)
fixVersionHistory.add(issueFixVersion)
isToStringNull.add(false)
}
numberOfFixVersions = numberOfFixVersions + 1
}
log.debug "**** End change history for issue fix version ****"
}
else{
log.debug "No Change History for Fix Version"
// if there is no change history of the fixVersion and there is a fixVersion on the issue,
// log the time the issue was created
def Timestamp issueCreated = issue.getCreated() // returns TimeStamp
def Collection<Version> issueFixVersion = issue.getFixVersions()
log.debug issueCreated
log.debug issueFixVersion
if (!issueFixVersion.isEmpty()){
// store issueCreated / issue FixVersion combo
timesWhenFixVersionChanged.add(issueCreated)
fixVersionHistory.add(issueFixVersion.last().toString())
isToStringNull.add(false)
numberOfFixVersions = numberOfFixVersions + 1
}
}
// Debug
log.debug numberOfFixVersions
for (int i=0; i<numberOfFixVersions; i++){
log.debug timesWhenFixVersionChanged.get(i)
log.debug fixVersionHistory.get(i)
log.debug isToStringNull.get(i)
}
// End Debug
log.debug "*****************"
log.debug "START LOGIC"
log.debug "*****************"
// Now that we've built an easy history, let's see what fix version this was planned in.
// loop through the list of versions
for (version in projectVersions) {
if (plannedFixVersion == null){
log.debug "\n"
//log.debug "Version: " + version
// get the start date for the version
def versionStartDate = version.getStartDate() //returns Date
log.debug "Version Start Date: " + versionStartDate
// look up what the fixVersion was on the start date of this version
for (int i=0; i<numberOfFixVersions; i++) {
def currentFixVersion = fixVersionHistory.get(i)
def currentFixVersionChangeTime = timesWhenFixVersionChanged.get(i)
//log.debug currentFixVersion
//log.debug currentFixVersionChangeTime
if(versionStartDate != null){
log.debug "CHANGEITEMFIXVERSION: " + currentFixVersion + " ; VERSION: " + version
if (currentFixVersion.equals(version.toString())){
log.debug "THEY MATCH!!!!!"
if (!currentFixVersionChangeTime.after(versionStartDate) ) {
log.debug "****This change item history was created before the start of the fix version"
// if it's the same as the version we are looking at
// return the version we are looking at
//log.debug "****This fix version was planned for this issue before the start date of the fix version"
//plannedFixVersion = currentFixVersion.toString() // TBD, figure out how to have this return a Version
log.debug "Version: " + version
plannedFixVersion = version
log.debug "Answer: " + plannedFixVersion
return plannedFixVersion
}
}
}
}
}
} // end for
return plannedFixVersion