Forums

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

Archive versions

Yogesh Mude
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.
July 7, 2020

Hi Team,

whats the wrong in versionManager.archiveVersion(version, true) ....

tempList.each{ version ->
def version_releaseDate = version.getReleaseDate()
def released = version.isReleased()
if (released && version_releaseDate.toString() <= preYear)
{
versionManager.archiveVersion(version, true)
}

}

 getting below error...

error.JPG

1 answer

1 accepted

0 votes
Answer accepted
Leo
Community Champion
July 7, 2020

Hi @Yogesh Mude,

Looks like the passed parameter type is not suitable for the method used

it should be archiveVersion(Version version, boolean archive) 

Can you pass me your complete code to have a quick look

 

BR,

Leo

Yogesh Mude
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.
July 7, 2020

Hello @Leo 

thanks for the response.

below is the complete code...

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.version.VersionManager

import java.text.SimpleDateFormat

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("MTV")
def versions = ComponentAccessor.getVersionManager().getVersions(project)

def filteredVersions = []
versions.each { version ->
if(version.getReleaseDate() != null ){
filteredVersions.add(version)
}
}

def uniqueNamesList = []
for(int i=0; i<filteredVersions.size(); i++){
if(!(uniqueNamesList.contains(filteredVersions[i].toString().substring(0, filteredVersions[i].toString().lastIndexOf("-"))))){
uniqueNamesList.add(filteredVersions[i].toString().substring(0, filteredVersions[i].toString().lastIndexOf("-")))
}
}
log.error " Component Count ${uniqueNamesList.size()} " + " Component Name : ${uniqueNamesList}"

uniqueNamesList.each { componentName ->
def tempList = []
filteredVersions.each { version ->
if(version.toString().substring(0, version.toString().lastIndexOf("-")) == componentName)
{
tempList.add(version)
}
}
//log.error " Version count in each component : ${tempList.size()} " + " Name : ${tempList}"
//Run the archiving code over tempList
ArchiveVersions(tempList)
}

def ArchiveVersions(def tempList){

VersionManager versionManager = ComponentAccessor.getVersionManager()

// def result = ""
def count = 0
def bundledVersions = 0
def lastDate

Calendar cal = Calendar.getInstance()
cal.add(Calendar.YEAR, -2)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
def preYear = sdf.format(cal.getTime()) //2018-04-21

tempList.sort{it.getReleaseDate()}

for(int i = tempList.size()-1; i>=0; i--)
{
if(lastDate == null){
count++
}
else if (lastDate == tempList[i].getReleaseDate()){
count++
}
else if (lastDate != tempList[i].getReleaseDate() && bundledVersions ==0){
bundledVersions++
count++
}
else if (lastDate != tempList[i].getReleaseDate() && bundledVersions ==1){
i = -1
}
lastDate = tempList[i].getReleaseDate()
}

def whileIndex = 1
def filteredVersionsSize = tempList.size()
while(whileIndex <= count){
int final_index = filteredVersionsSize - whileIndex
tempList.remove(final_index)
whileIndex++
}

tempList.each{ version ->
def version_releaseDate = version.getReleaseDate()
def released = version.isReleased()
if (released && version_releaseDate.toString() <= preYear)
{
versionManager.archiveVersion(version, true)
}

}
log.error "Total version archived count ${tempList.size()}"
}
Leo
Community Champion
July 7, 2020

Can you try below code, I just adjusted data type on the fields you used to store version nothing else

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.project.version.Version

import java.text.SimpleDateFormat

def project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("MTV")
def versions = ComponentAccessor.getVersionManager().getVersions(project)

List<Version> filteredVersions
versions.each { version ->
if(version.getReleaseDate() != null ){
filteredVersions.add(version)
}
}

def uniqueNamesList = []
for(int i=0; i<filteredVersions.size(); i++){
if(!(uniqueNamesList.contains(filteredVersions[i].toString().substring(0, filteredVersions[i].toString().lastIndexOf("-"))))){
uniqueNamesList.add(filteredVersions[i].toString().substring(0, filteredVersions[i].toString().lastIndexOf("-")))
}
}
log.error " Component Count ${uniqueNamesList.size()} " + " Component Name : ${uniqueNamesList}"

uniqueNamesList.each { componentName ->
def temList = []
filteredVersions.each { version ->
if(version.toString().substring(0, version.toString().lastIndexOf("-")) == componentName)
{
temList.add(version)
}
}
//log.error " Version count in each component : ${tempList.size()} " + " Name : ${tempList}"
//Run the archiving code over tempList
ArchiveVersions(temList)
}

def ArchiveVersions(temList){

VersionManager versionManager = ComponentAccessor.getVersionManager()
List<Version> tempList = temList as List
tempList.sort{it.getReleaseDate()}
// def result = ""
def count = 0
def bundledVersions = 0
def lastDate
Calendar cal = Calendar.getInstance()
cal.add(Calendar.YEAR, -2)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
def preYear = sdf.format(cal.getTime()) //2018-04-21

for(int i = tempList.size()-1; i>=0; i--)
{
if(lastDate == null){
count++
}
else if (lastDate == tempList[i].getReleaseDate()){
count++
}
else if (lastDate != tempList[i].getReleaseDate() && bundledVersions ==0){
bundledVersions++
count++
}
else if (lastDate != tempList[i].getReleaseDate() && bundledVersions ==1){
i = -1
}
lastDate = tempList[i].getReleaseDate()
}

def whileIndex = 1
def filteredVersionsSize = tempList.size()
while(whileIndex <= count){
int final_index = filteredVersionsSize - whileIndex
tempList.remove(final_index)
whileIndex++
}

tempList.each{ version ->
def version_releaseDate = version.getReleaseDate()
def released = version.isReleased()
if (released && version_releaseDate.toString() <= preYear)
{
versionManager.archiveVersion(version, true)
}

}
log.error "Total version archived count ${tempList.size()}"
}

 

BR,

Leo

Yogesh Mude
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.
July 7, 2020

On the below line i got the error saying can not invoke add method on the null object so i changed it to the previous one and works fine.

List<Version> filteredVersions

Thanks for your quick response. 

Suggest an answer

Log in or Sign up to answer