Version picker not returning anything

Rebecca Ebersol January 15, 2019

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

 

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events