Swapping Jira Cloud Notification Schemes With ScriptRunner

Here’s a relatively simple one for you. Say you had a Jira Cloud instance with hundreds or thousands of projects, and you wanted to swap them all over to using a new notification scheme.  How would you go about it?

Well you could certainly do it by hand. That’s an option.  Or you could write a little script and us an API endpoint that I’ve only just discovered.

The script below fetches all of the projects in the system that use a notification scheme. We then filter out only the ones we want to adjust, and update each of those projects to use the target notification scheme.

This script is a great example of the kind of thing that ScriptRunner excels at, and it’s a great script to use to start learning the REST API.

 

import groovy.json.JsonSlurper
 
// Notification scheme ID to search for
def searchSchemeId = "10000"
 
// Notification scheme ID to use for update
def updateSchemeId = 10002
 
def response = get("/rest/api/3/notificationscheme/project")
  .header('Content-Type', 'application/json')
  .asJson()
 
// Parse the JSON response
def jsonSlurper = new JsonSlurper()
def json = jsonSlurper.parseText(response.getBody().toString())
 
// Access the nodes in the JSON
json.values.each {
  project ->
 
    logger.warn(project.toString())
 
//We need to account for -1, which is always a value that the system stores as a project ID for some reason
  if ((project.notificationSchemeId == searchSchemeId) && (project.projectId != "-1")) {
     
    def update = put("/rest/api/2/project/" + project.projectId.toString())
      .header('Content-Type', 'application/json')
      .body(["notificationScheme": updateSchemeId])
      .asJson()
 
    if (update.status != 200) {
      logger.warn("ERROR: " + update.body)
    } else {
      logger.warn("SUCCESS: updated project with ID" + project.projectId.toString())
    }
 
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *