How to Bulk Archive Jira projects using groovy scripts

Vinay January 19, 2025

I was trying with below script.


 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.groups.GroupManager
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Logger

// Initialize logger
def log = Logger.getLogger("com.acme.archive")

// Define the list of project keys to archive
def projectKeys = ["TVP"] // Modify this list with your project keys

// Get the Project Manager component
def projectManager = ComponentAccessor.getComponent(ProjectManager)

// Define the user to perform the actions as (you can replace this with an actual user, or assume current user)
def userManager = ComponentAccessor.getUserManager()
def adminUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // Use admin user for actions

// Define a helper function to archive a project
def archiveProject(Project project) {
    try {
        // Disable issue creation for the project
        //project.setAllowCreateIssues(false)
        //projectManager.updateProject(project)

        // Make the project read-only
        project.setArchived(true)

        // project.archive() // Mark the project as archived (this step may need to be adjusted based on Jira's version)
        projectManager.updateProject(project)

        log.info("Project ${project.getKey()} archived successfully.")
    } catch (Exception e) {
        log.error("Error archiving project ${project.getKey()}: ${e.message}")
    }
}

// Loop through each project key and attempt to archive
projectKeys.each { projectKey ->
    Project project = projectManager.getProjectObjByKey(projectKey)
    if (project) {
        log.info("Found project: ${project.getName()} (${project.getKey()})")
        archiveProject(project)
    } else {
        log.warn("Project with key ${projectKey} not found.")
    }
}

log.info("Bulk project archiving completed.")

1 answer

0 votes
Renata_Getint
Atlassian Partner
January 20, 2025

 

Hi @Vinay 

Bulk archiving Jira projects using Groovy scripts is indeed a great approach for managing project lifecycles efficiently. Your script is a good starting point, but it seems to have a few areas that might need refinement to ensure compatibility and correctness, especially depending on the Jira version you're using. Here’s an improved version with some insights and best practices:

Updated Script 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import org.apache.log4j.Logger

// Logger setup
def log = Logger.getLogger("com.acme.archive")

// List of project keys to archive
def projectKeys = ["TVP"] // Replace with your project keys

// Access ProjectManager
def projectManager = ComponentAccessor.getComponent(ProjectManager)

// Loop through project keys and archive
projectKeys.each { projectKey ->
def project = projectManager.getProjectObjByKey(projectKey)
if (project) {
try {
log.info("Archiving project: ${project.name} (${project.key})")

// Set project to archived
project.setArchived(true)
projectManager.updateProject(project)

log.info("Project ${project.key} archived successfully.")
} catch (Exception e) {
log.error("Failed to archive project ${project.key}: ${e.message}")
}
} else {
log.warn("Project with key ${projectKey} not found.")
}
}

log.info("Bulk archiving completed.")

 

Key Adjustments:

  1. Version Compatibility: The method setArchived(true) should work for Jira versions that support project archiving. Ensure that you're using a Jira version where this API is valid.
  2. Permission Context: Always execute the script with appropriate permissions. Using the JiraAuthenticationContext ensures the correct user context.
  3. Error Handling: Improved error handling to avoid breaking the loop when one project fails to archive.
  4. Logging: Added detailed logs for better debugging and monitoring.

Important Notes:

  • Permissions: Ensure the user executing the script has admin permissions for all projects listed in projectKeys.
  • Test Environment: Always test the script in a staging environment before running it in production to prevent unintended consequences.
  • API Deprecation: Some methods like projectManager.updateProject(project) may vary depending on your Jira version or might require alternative approaches in newer APIs.

If you're looking to streamline bulk actions like this or have more advanced integration needs, Getint specializes in integrating Jira with other tools and automating workflows. Let us know if you'd like assistance!

Happy scripting!

Best regards,
Renata

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events