Returning Jira Cloud Inactive Users With Timestamp

This is an extension of a previous post that I did, on returning all of the users in a Jira Cloud instance.

The request was for a script that returns all users in a Jira Cloud instance who have been inactive for over 30 days. Unfortunately, only part of this request is doable.  The Jira Cloud REST API does not currently provide any way of returning login or access timestamp information.  The most it will tell you is whether a user account is active or inactive. The only way to get access information is to export the Jira userbase through the Atlassian portal.

So the script below simply returns all Jira users in a Cloud instance who have an inactive account.   If/when the day comes that Atlassian adds login information to the API, I’ll update this post.

import groovy.json.JsonSlurper

def sb = []
def page = 0

def lastPage = false

while (lastPage == false) {
  //run this loop until we detect the last page of results

  def x = 0
  //Every time the loop runs, set x to 0. We use x to count the number of users returned in this batch of results

  def getUsers = get("/rest/api/2/user/search?query=&maxResults=200&startAt=" + page)
    .header('Content-Type', 'application/json')
    .asJson()
  //Get the current batch of users as an HTTP GET request

  def content = getUsers.properties.rawBody
  //Get the body contents of the HTTP response

  InputStream inputStream = new ByteArrayInputStream(content.getBytes());
  String text = new String(inputStream.readAllBytes());
  def parser = new JsonSlurper()
  def json = parser.parseText(text)
  //Convert the resulting bytestream first to a string, and then to JSON So we can work with it

  json.each {
    userAccount ->

      if (userAccount.active == false) {
      //We only want users who aren't active

        //For each result in the JSON
        sb.add(userAccount.displayName.toString() + " has an inactive account")
        //write the user account ID to the log
      }

  }

  if (json.size() < 200) {
    lastPage = true
    logger.warn("Setting lastPage to true")
    //If the number of users in the current batch is less than 200, we must have reached the end and we can kill the loop
  } else {
    page += 200
    //Otherwise, increase pagination by 200 and keep going

  }

}

return sb

Leave a Reply

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