Very easy with ScriptRunner, just execute this script in the Script Console.

Don’t forgive to configure the date limit to archive (it depends of the release date)!


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

VersionManager versionManager = ComponentAccessor.getVersionManager()

List<VersionManager> versions=versionManager.getAllVersions() as List

//versionManager.archiveVersion(versionManager.getVersion(651326), true);
//return "To test just archiving one release";

def resultado = "";
for ( int i = 0; i < versions.size() ; i++)
{
def release_date = versions[i].getReleaseDate();
def archived = versions[i].isArchived();
if (!archived && release_date.toString() <= '2016-01-01') {
resultado+= i + " " +versions[i].getProject() + " " + versions[i].getId() + " " +versions[i].getName() + " " + release_date.toString() + "\n";
versionManager.archiveVersion(versions[i], true);
//return resultado; //To stop at first archived release (testing)
}
}
return resultado;

If you see useful, you can configure it in a scheduled Service!

Very useful in large Organizations!

Posted by:.

24 replies on “How to archive the old releases in JIRA of all projects at same time!

  1. Buenas Raúl!
    Creo que hay un error en el código, el getAllVersions() devuelve una lista de no de .

    Por lo demás muy buen aporte, me ha servido de gran ayuda!
    Un abrazo!

    Liked by 1 person

    1. Hola Eloi! Como estas crack? Pues acabo de probar el codigo en Jira 7.4 y me ha funcionado, quizás sea en versiones superiores? (es que creo que el wordpress te ha borrado palabras de tu comment! xD) Saludos!!

      Like

    1. Hello Eric,
      You can try to filter by Id versions[i].getId() or by name versions[i].getName() if there is no release date.
      You can replace the line 16
      if (!archived && release_date.toString() <= '2016-01-01') {
      for
      if (!archived && versions[i].getId() <= xxxxx) {
      Regards

      Like

      1. Thanks! that worked! but I have one more dilemma that i cant figure out. I need to run this script for 1 specific project. I tried the following:
        def project = ComponentAccessor.projectManager.getProjectObjByKey(“FAM”);

        Where “FAM” is the project key but that didnt work.

        Can you tell me what I’m doing wrong?

        Thanks again.
        Eric Sebian

        Liked by 1 person

      2. Hello Eric use the method getAllVersionsForProjects(Collection Project projects, boolean includeArchived) instead of getAllVersions()
        Regards!

        Like

      3. that sucks. You seem to be a good teacher and know what you’re talking about. I’m not a developer (and really don’t want to be.:)) but I need to be able to create groovy scripts for business requirements. By the way, I’ve been trying to create a listener that updates linked issues with the fix version of the issue that it’s linking to. I’ve found several pieces of code that I’ve tried to put together but with no success. In your many years of scripting have you ever created such an animal? The below looks like it might work but who knows…

        Here is the script I cobbled together:

        import com.atlassian.jira.component.ComponentAccessor
        import com.atlassian.jira.issue.IssueFieldConstants
        import com.atlassian.jira.issue.MutableIssue;
        import com.atlassian.jira.issue.link.IssueLink;
        import com.atlassian.jira.issue.link.IssueLinkManager;
        import com.atlassian.jira.component.ComponentAccessor;
        import com.atlassian.jira.event.type.EventDispatchOption;
        import org.apache.log4j.Logger
        import org.apache.log4j.Level

        // the name of the field to copy
        final String fieldNameToCopy = “Fix Version/s”

        // leave blank to copy from the last linked issue (regardless the link type)
        final String issueLinkTypeName = “”

        def fieldManager = ComponentAccessor.fieldManager

        def fieldToCopy = fieldManager.allAvailableNavigableFields.find { it.name == fieldNameToCopy }
        if (!fieldToCopy) {
        log.info “Could not find field with name $fieldNameToCopy”
        return
        }
        def issue = event.issue
        def linkedIssues = ComponentAccessor.issueLinkManager.getOutwardLinks(issue.id)
        if (!linkedIssues) {
        log.info “There are no linked issues”
        return
        }

        if (issueLinkTypeName && !(issueLinkTypeName in linkedIssues*.issueLinkType*.name)) {
        log.info “Could not find any issue, linked with the $issueLinkTypeName issue type”
        return
        }

        def linkedIssue = issueLinkTypeName ?
        linkedIssues.findAll { it.issueLinkType.name == issueLinkTypeName }.last().destinationObject :
        linkedIssues.last().destinationObject

        def fieldToCopyId = fieldToCopy.id

        switch (fieldToCopyId) {

        case IssueFieldConstants.FIX_FOR_VERSIONS:
        def commonFixedVersions = linkedIssue.fixVersions.findAll { it.name in issue.fixVersions*.name }
        //what we need to find is a method on the issue that updates a field
        issue.setFixVersions(commonFixedVersions)
        break

        default:
        issue[fieldToCopyId] = linkedIssue[fieldToCopyId]
        }

        Any assistance would be greatly appreciated.

        Like

      4. Ok. Last question (i hope). you said that to archive releases that have no release date I should try:

        if (!archived && versions[i].getId() <= xxxxx) {

        that doesn't find any of the release that don't have release dates. Should I be replacing xxxxx with anything?

        Like

  2. Hi. Sorry to be a pain but I added that line and I’m getting an error. here is the modified script:

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

    VersionManager versionManager = ComponentAccessor.getVersionManager()

    List versions=versionManager.getAllVersionsForProjects() as List

    //versionManager.archiveVersion(versionManager.getVersion(651326), true);
    //return “To test just archiving one release”;

    def resultado = “”;
    for ( int i = 0; i < versions.size() ; i++)
    {
    def release_date = versions[i].getReleaseDate();
    def archived = versions[i].isArchived();
    def project = ComponentAccessor.projectManager.getProjectObjByKey("FAM");
    if (!archived && release_date.toString() <= 'null') {
    resultado+= i + " " +versions[i].getProject() + " " + versions[i].getId() + " " +versions[i].getName() + " " + release_date.toString() + "\n";
    versionManager.archiveVersion(versions[i], true);
    //return resultado; //To stop at first archived release (testing)
    }
    }
    return resultado;

    the error that I'm getting is:
    groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.project.version.DefaultVersionManager.getAllVersionsForProjects() is applicable for argument types: () values: []
    Possible solutions: getAllVersionsForProjects(java.util.Collection, boolean)
    at Script257.run(Script257.groovy:6)

    As I am a groovy novice, I'm not sure what that means.

    Thanks for your assistance.

    Like

    1. Try this

      import com.atlassian.jira.component.ComponentAccessor
      import com.atlassian.jira.project.version.VersionManager
       
      VersionManager versionManager = ComponentAccessor.getVersionManager()
      boolean includeArchived = false;
      List<VersionManager> versions=versionManager.getAllVersionsForProjects([ComponentAccessor.getProjectManager().getProjectObjByKey("JIRA")], includeArchived)  as List
      return versions
      
      

      Like

      1. Still trying to get it. I tested what you sent and it indeed does gather the releases, but when I plug the new piece of code into your existing code it lists the releases but doesn’t archive them. It’s weird to me. I hate to keep bothering you. Please see the code below:

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

        VersionManager versionManager = ComponentAccessor.getVersionManager()
        boolean includeArchived = false;
        List versions=versionManager.getAllVersionsForProjects([ComponentAccessor.getProjectManager().getProjectObjByKey(“JCD”)], includeArchived) as List
        return versions

        //versionManager.archiveVersion(versionManager.getVersion(651326), true);
        //return “To test just archiving one release”;

        def resultado = “”;
        for ( int i = 0; i < versions.size() ; i++)
        {
        def release_date = versions[i].getReleaseDate();
        def archived = versions[i].isArchived();
        if (!archived && release_date.toString() <= 'null') {
        resultado+= i + " " +versions[i].getProject() + " " + versions[i].getId() + " " +versions[i].getName() + " " + release_date.toString() + "\n";
        versionManager.archiveVersion(versions[i], true);
        //return resultado; //To stop at first archived release (testing)
        }
        }
        return resultado;

        Like

      2. Try this

        import com.atlassian.jira.component.ComponentAccessor
        import com.atlassian.jira.project.version.VersionManager
         
        VersionManager versionManager = ComponentAccessor.getVersionManager()
        boolean includeArchived = false;
        List<VersionManager> versions=versionManager.getAllVersionsForProjects([ComponentAccessor.getProjectManager().getProjectObjByKey("JIRA")], includeArchived)  as List
        //return versions
        
        //versionManager.archiveVersion(versionManager.getVersion(651326), true);
        //return "To test just archiving one release";
         
        def resultado = "";
        for ( int i = 0; i < versions.size() ; i++)
        {
        def release_date = versions[i].getReleaseDate();
        def archived = versions[i].isArchived();
        if (!archived && release_date.toString() <= '2016-01-01') {
        resultado+= i + " " +versions[i].getProject() + " " + versions[i].getId() + " " +versions[i].getName() + " " + release_date.toString() + "\n";
        versionManager.archiveVersion(versions[i], true);
        //return resultado; //To stop at first archived release (testing)
        }
        }
        return resultado;
        
        

        Like

  3. Sorry. Still getting errors:

    2020-06-17 10:06:37,791 ERROR [common.UserScriptEndpoint]: *************************************************************************************
    2020-06-17 10:06:37,796 ERROR [common.UserScriptEndpoint]: Script console script failed:
    java.lang.NullPointerException
    at com.atlassian.jira.project.version.DefaultVersionManager.getAllVersionsForProjects(DefaultVersionManager.java:621)
    at com.atlassian.jira.project.version.VersionManager$getAllVersionsForProjects.call(Unknown Source)
    at Script284.run(Script284.groovy:6)

    Like

  4. you said that to archive releases that have no release date I should:

    replace the line 16
    if (!archived && release_date.toString() <= '2016-01-01') {
    for
    if (!archived && versions[i].getId() <= xxxxx) {

    but that throw the below error:

    groovy.lang.MissingPropertyException: No such property: xxxxx for class: Script292 at Script292.run(Script292.groovy:17)

    i tried googling it but couldn't find anything to help me resolve the issue.

    By the way, I signed up for your udemy groovy class. I hope that it helps me with this stuff.

    thanks.

    Like

  5. Hello Eric, replace xxxxx by the Id of the Version you want to archive (and will archive also the lower Id versions) Regards

    Like

    1. Excellent! I will give it a try. By the way, do you offer scriptrunner classes in english? I saw that you had some listed on your site but they all seemed to be in Spanish.

      Like

  6. For anyone still looking for solution for this, I have created a Github Action which can integrated to your CI and create the jira release as archived.

    for my case we automatically create jira release each time we build our app, and now the new builds (jira releases) are automatically created as archived and no longer need to worry about spend hours archiving them…

    Github Action link:

    https://github.com/marketplace/actions/jira-release-archive-action

    Liked by 1 person

Leave a comment