Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Advanced Roadmaps: is there no way to auto-populate Target start and end dates?

Diana
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 14, 2023

I am trying to auto-populate Advanced Roadmaps fields "Target start" and Target end" dates.

If a Feature has a Fix Version or changed, and the Fix Version has Release start and end dates, then auto-fill those same dates to Target start and Target end dates. If Release dates has changed, Target start/end dates will auto-update as well.

I've tried Scriptrunner listener, but no luck.

 

import java.text.SimpleDateFormat
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import java.time.LocalDate
import java.time.format.DateTimeFormatter

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def versionManager = ComponentAccessor.getVersionManager()
def issueManager = ComponentAccessor.getIssueManager()
def issue = event.issue
def project = issue.getProjectObject()
def projectKey = "test"

// Check if the project key matches "test"
if (project.key == projectKey) {

    // Get the Fix Version field value from the issue
    def fixVersions = issue.getFixVersions()
    def projectID = issue.getProjectId()

    // Check if its a feature and it has a fix version
        if (issue.issueType == 'Feature' && fixVersions != null) {

            // Get all releases in the "test" project
            def testReleases = versionManager.getVersionsUnreleased(project.getId(), false)

            // Loop through the Fix Versions of the issue
            for (def fixVersion in fixVersions) {
                if(testReleases.find { it.id == fixVersion.id}){

                def releaseStartDate = fixVersion.startDate
                def releaseEndDate = fixVersion.releaseDate
                log.warn("start date: " + releaseStartDate)
                log.warn("end date: " + releaseEndDate)

                // Format the dates as strings with the format "dd-MMM-yyyy"
                def dateFormat = new SimpleDateFormat("dd-MMM-yyyy") as String
                def formattedStartDate = releaseStartDate.format(dateFormat) 
                def formattedEndDate = releaseEndDate.format(dateFormat)

                // Set the custom field values
                def targetStart = customFieldManager.getCustomFieldObjectsByName("Target start")
                def targetEnd = customFieldManager.getCustomFieldObjectsByName("Target end")

                // Start a new transaction to make the changes
                def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
                MutableIssue featureIssue = issue as MutableIssue
               
//error comes here, the method will not recognize the formatted date
                targetStart.updateValue(null, featureIssue, new ModifiedValue(featureIssue.getCustomFieldValue(targetStart), formattedStartDate), new DefaultIssueChangeHolder())
                targetEnd.updateValue(null, featureIssue, new ModifiedValue(featureIssue.getCustomFieldValue(targetEnd), formattedEndDate), new DefaultIssueChangeHolder())
//also tried the setCustomFieldValue method, but no luck
                //featureIssue.setCustomFieldValue(targetStart, releaseStartDate)
                //featureIssue.setCustomFieldValue(targetEnd, releaseEndDate)

                issueManager.updateIssue(currentUser, featureIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
                }
            break
        }
    }
}


Is it because the Target start and Target end dates are locked fields from Advanced Roadmaps that they can't be edited via Scriptrunner?



 

1 answer

0 votes
Hauke Bruno Wollentin
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 15, 2023

Not sure about Jira DC, but in Cloud the format is "yyyy-MM-dd" as String, which did the trick for me to update the Target End of Advanced Roadmaps.

Anyways, when using "getCustomFieldObjectsByName()" the return value is a list, so maybe this is just a copy/paste error but you need something like "getCustomFieldObjectsByName().first()" to catch the actual CustomField object.

Suggest an answer

Log in or Sign up to answer