Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Update of fixversion field through scriptrunner listners in the specified format.

Ramesh Penuballi June 2, 2023

Dear All,

I am trying to schedule a script  via script runner to update the fixversion field in the specified format with the help of listners.

I used the below  groovy script 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.version.VersionManager
 
def versionNameFormat = /^Version-\d+$/   // Customize the version name format here
 
// Get the version manager component
VersionManager versionManager = ComponentAccessor.getVersionManager()
 
// Get the issue and project from the event
Issue issue = event.issue
def project = issue.getProjectObject()
 
// Check if the project matches the desired format
if (project.key == "YOUR_PROJECT_KEY") {
    // Get or create the fix version with the desired format
    Version fixVersion = versionManager.getByProjectAndName(project, "Version-1")
    if (!fixVersion) {
        fixVersion = versionManager.createVersion(project.id, "Version-1", "")
    }
 
    // Add the fix version to the issue
    MutableIssue mutableIssue = issue as MutableIssue
    mutableIssue.fixVersions = [fixVersion]
 
    // Update the issue and re-index it
    def issueManager = ComponentAccessor.getIssueManager()
    def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
 
    // Log a message to the Jira log
    log.info("Fix version 'Version-1' added to issue ${issue.key}")
}

 

I am getting an error [[Static type checking] - Cannot find matching method com.atlassian.jira.project.version.VersionManager#createVersion(java.lang.Long, java.lang.String).] in the routines "getByProjectAndName" and "createVersion"  of the version.

Version fixVersion = versionManager.getByProjectAndName(project, "Version-1")
    if (!fixVersion) {
        fixVersion = versionManager.createVersion(project.id, "Version-1", "")

 

It would be great any other alternate routines to resolve the errors to resolve my purpose.

 

Thanks and Regards,

Ramesh Penuballi

2 comments

Comment

Log in or Sign up to comment
Radek Dostál
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.
June 2, 2023

Use the javadocs https://docs.atlassian.com/software/jira/docs/api/9.4.1 (appropriate to your version)

 

From which there appears to be no "getByProjectAndName()", and where you can see the syntax for "createVersion()"

 

Since that method appears to not exist, we could replace that with:

// getVersion(Long projectId, String versionName)
def version = versionManager.getVersion(project.getId(), "Version-1")

 

And to create it:

createVersion(String name,
              Date startDate,
              Date releaseDate,
              String description,
              Long projectId,
              Long scheduleAfterVersion,
              boolean released)
Ramesh Penuballi June 5, 2023

Hi Radek,

Thanks for valuable suggestion and I tried with the above code and stuck with syntax error in closing "}"

Please help if the below snippet is correct.


if (!fixversion)
{
fixversion = versionManager.createVersion (String name,Date startDate,Date releaseDate,String description,Long projectId,Long scheduleAfterVersion,boolean released )
}
Ramesh Penuballi June 5, 2023

Hi All,

I am able to resolve the syntax error but stuck at updating the issue with the below error.

[Static type checking] - The variable [EventDispatchOption] is undeclared.

It seems to be that the error is that the variable EventDispatchOption is undeclared in the program but not sure where to declare the variable in the program to fix the issue.

The error is at the below line and I am running the code on the script runner 7.11.

 

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

 

I have tried to added the import utility  "import com.atlassian.jira.event.type.EventDispatchOption" but still doesn't help to resolve the issue

 

Below is the full code I am trying to update the fix version value in a specific project.

Kindly help me to resolve the issue.

 

Thanks,

Ramesh 

***********************************************************************************

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue

def versionNameFormat = /^Version-\d+$/ // Customize the version name format here
// Get the version manager component
VersionManager versionManager = ComponentAccessor.getVersionManager()
// Get the issue and project from the event
Issue issue = event.issue
def project = issue.getProjectObject()
// Check if the project matches the desired format
if (project.key == "YOUR_PROJECT_KEY") {
// Get or create the fix version with the desired format
//Version fixVersion = versionManager.getByProjectAndName(project, "Version-1")
def fixVersion = versionManager.getVersion(project.getId(), "Version-1")
if (!fixVersion) {
fixVersion = ComponentAccessor.versionManager.createVersion(versionName, startDate, releaseDate, description, project.id, scheduleAfterVersion, released)
}
// Add the fix version to the issue
MutableIssue mutableIssue = issue as MutableIssue
mutableIssue.fixVersions = [fixVersion]
// Update the issue and re-index it
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUser, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)
// Log a message to the Jira log
log.info("Fix version 'Version-1' added to issue ${issue.key}")
}


PRAMOD KUMAR REDDY KASIREDDY June 8, 2023

thanks

TAGS
AUG Leaders

Atlassian Community Events