Get projectID for versionID in scriptrunner

Chuck Vanderwist February 27, 2018

I have a versionID and I want to retrieve the project's ID, name, Key, Lead, Category, etc

I retrieve the projectID with a versionID via http/REST with:

rest/api/2/version/{versionId}

I can then retrieve the other project details from

rest/api/2/project/{projectId}

... How would I go about retrieving the same information from within scriptrunner script, without using http requests?

I've tried using the ComponentAccessor's versionManager like this:

import com.atlassian.jira.component.ComponentAccessor
def ProjectManager = ComponentAccessor.projectManager
def VersionManager = ComponentAccessor.versionManager

long version = 23958
def versionObject = VersionManager.getVersion(version)
log.warn(versionObject)

 

...but I can't figure out how to get the projectID from the version object in this manner. 

1 answer

1 accepted

0 votes
Answer accepted
Joshua Yamdogo @ Adaptavist
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.
February 27, 2018

Charles,

Is this for ScriptRunner for JIRA Server?

If so, you should should be able to call getProjectId() on the Version object.

import com.atlassian.jira.component.ComponentAccessor
def ProjectManager = ComponentAccessor.projectManager
def VersionManager = ComponentAccessor.versionManager

long version = 23958
def versionObject = VersionManager.getVersion(version)
log.warn(versionObject.projectId)

See https://docs.atlassian.com/software/jira/docs/api/7.8.0/com/atlassian/jira/project/version/Version.html

Chuck Vanderwist February 28, 2018

Thanks Joshua!

I actually used .getProjectId() on the version object for my final script (putting that into a new variable)

Now I know to keep digging in that api doc :)

Joshua Yamdogo @ Adaptavist
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.
February 28, 2018

No problem. If you plan to write a lot of scripts, you might consider following our "Setting up a dev environment guide"

https://scriptrunner.adaptavist.com/latest/jira/DevEnvironment.html#_connecting_intellij_idea_with_the_atlassian_source_code

That functionality makes it a lot easier to see which methods are available for your objects (like Version).

Ignacio Flores May 25, 2021

Joshua Yamdogo @ Adaptavist 

How can I get "project ids" from a collection of version ids?

VERSIONOBJECT.png

Its necessary use for.each ? how I can show a later out the for.each the list with projects ids ?

Suggest an answer

Log in or Sign up to answer