Script Console

 

Migrating from ScriptRunner for Jira Server/DC to Cloud? Learn more in our Feature Parity overview.

Before you start 


Visit ScriptRunner HQ to see example scripts. 


Learn how to modify existing scripts in the Script Console.


What is the Script Console?

The Script Console is a place to run scripts. Using the Script Console, you can copy and paste or write a script to run in Jira Cloud. The Script Console enables you to run one-off ad hoc scripts and helps you learn and experiment with the Jira REST API from ScriptRunner. 

You find a script editor, similar to the Script Console, anywhere you choose to use a custom script option (for example, when adding a Script Listener or Scheduled Job).

The Script Console is useful for testing scripts or performing operations that you only want to do once. So if you want a list of all the spaces on your instance and some details about them, you can run a script for that. Or if you want to delete all spaces that were created by a certain person, you can do that. You can also run maintenance scripts that modify something on your instance.

Like all coding fields in ScriptRunner for Jira Cloud, the Script Console uses an intelligent code editor. Learn more in the Code Editor documentation. The editor has autocomplete for the following code: 

  • Groovy
  • Atlassian REST API
  • Automatically available variables

The image below illustrates how autocomplete appears in the Script Console: 

You can read more about Completions if you are using HAPI in the code editor.

AutoCompletions for Atlassian's REST API

Similar to HAPI's automatic completions, ScriptRunner for Jira Cloud also provides completions for calls to Atlassian's REST API, helping to make script writing easier and ensuring accuracy. You can follow the example provided below to see how this works.

Example: Retrieve a list of users who commented on an issue

For example, suppose you want your script to retrieve a list of everyone who commented on an issue. Since you're getting information, you start with the get method and check which APIs relate to comments. You type get("comment")and see the following options

For this script, you know the issue key, so select /rest/api/3/issue/{issueIdOrKey}/comment. Enter the issue key manually for now; you can replace it with a variable later. Append asObject(Map).body to the get method call to retrieve the list of comments from the Atlassian REST API. To see available properties, you could go to the Atlassian Cloud REST API documentation, however, instead you can rely on the autocompletions. Add a period after body to show available completions, the first of which is comments, which returns a list of comments.

Using your knowledge of Groovy, apply the spread operator (*.) to collect information from each element in the list. Autocomplete shows that author is available on the target.

A single user may comment multiple times, so you only need unique authors. Start typing uni to see the unique method in the completions list.

Using completions reduces the need to switch back and forth between the documentation, helping you code without breaking your flow. While HAPI offers the gold standard for ScriptRunner code editor completions, if you need to fall back to the Atlassian API for more complex cases, we still provide guidance where possible.

How to use the Script Console

You can either enter the script you want to run directly in the Script field or click the Example scripts button to select an example script. 

You also have the option to click Load and reuse a script you previously saved in Script Manager. Details on how to do this can be found in Reuse scripts in the UI.

If you choose to reuse one of the many examples provided, rather than writing your own script, you will see the following screen:

  1. Choose an example script from the list provided and the code automatically appears. You also have the option to search for a particular script.
  2. Click Copy Code and then Close.
  3. Paste the copied code in the code editor.

You can use the Script Console to:

  • Run a script to display information.
  • Run a one-off clean up task.
  • Make one-off or bulk updates to issues, projects, users, versions etc.

For example, as an admin, you have been given a list of users who have left the company. For security reasons, you need to remove these users as soon as possible. Usually, you would need to search for each name individually and manually delete each user. However, I can enter the list of user names and bulk delete all of them in one action using a script in the Script Console.

Using the Script Console is an easy way to make bulk changes to issues returned by a JQL query. For example, I can look for issues with linked support cases and no watchers so I can then automatically add the linked support cases reporter to the related bug as a watcher.

Run code as user

Code that is run from the Script Console can make requests back to Jira using either the ScriptRunner Add-on user or the Current User. See the Run As User section of Workflow Extensions for more information.


Related content

On this page