The settings or preferences for a given user in Jira Cloud are stored in a number of locations within the system.   The User Properties section contains settings relating to which interface elements the user sees or doesn’t see.  

For example, when you first access ScriptRunner on a Jira instance, you’re presented with a little quiz.  It looks like this:

After you click through this quiz it goes away forever. Someone recently remarked that they’d love to not have their ScriptRunner users be presented with this quiz in the first place.

…Okay, we can make that happen!

 

First we need to query the user properties for a given user with this code:

def userProps = get("rest/api/2/user/properties") 
.header("Accept", "application/json")
.queryString("accountId", "<user account ID>")
.asJson(); 

return userProps.body

The results look like something like this:

{
  "array": {
    "empty": false
  },
  "object": {
    "keys": [
      {
        "self": "  https://some-jira-instance.atlassian.net/rest/api/2/user/properties/navigation_next_ui_state?accountId=12345678910abcdef123456",
        "key": "navigation_next_ui_state"
      },
      {
        "self": "  https://some-jira-instance.atlassian.net/rest/api/2/user/properties/onboarding?accountId=12345678910abcdef123456",
        "key": "onboarding"
      },
      {
        "self": "  https://some-jira-instance.atlassian.net/rest/api/2/user/properties/project.config.dw.create.last.template?accountId=12345678910abcdef123456",
        "key": "project.config.dw.create.last.template"
      },
      {
        "self": "  https://some-jira-instance.atlassian.net/rest/api/2/user/properties/sr-post-install-quiz?accountId=12345678910abcdef123456",
        "key": "sr-post-install-quiz"
      }
    ]
  }
}

 

The key we’re interested in is the one at the bottom, called sr-post-install-quiz.  A new user isn’t going to have this property; it only gets added to the list of user properties after a user has completed the quiz. If we were to somehow add this to the list of user properties for a new user, they’d never see the little quiz in the first place. As far as the system is concerned, they already clicked through the quiz.

 

We want this change to take effect every time a new user is created, so that the new user never sees the quiz.  Therefor, we need to set this as a listener that runs on user create.   Here’s the surprisingly simple full code for the listener:

def userId = event.user.accountId.toString()
//Get the userID of the new user from the event

def userProps = put('rest/api/2/user/properties/sr-post-install-quiz?accountId='+userId)
//Feed the userId variable into the PUT request 
.header('Accept', 'application/json')
.header('Content-Type', 'application/json') 
.body(["value":"your value"]) 
//The body values don't have to change, the API simply needs something in the body of the request 
.asJson()

 

Worth noting is that the JSON in the body of the request has to be present, even though it has nothing to do with the end result.  For whatever reason, this API call demands that some kind of JSON be included.

 

 

 

1 thought on “100 Days of Code – Day 5 – Setting User Properties With ScriptRunner For Jira Cloud

  1. I’d be interested in more info about listeners. I never worked out how to monitor the number of events in our system well.

    Also “Therefor” looke like a typo

Leave a Reply

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

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>