In origami, there’s lots of types of base folds that give you a good starting point for more complex creations. For example, in order to make an origami crane, you first start out with the folds that make a basic bird base. The same principle can be applied to using Power Scripts™ for Jira.
At the recent Atlassian Summit in Barcelona, we gave visitors to the Power Scripts™ for Jira booth an opportunity to challenge our experts with something in what we called the 5-minute challenge. Users came to us with a problem to see if our experts could solve it using Power Scripts™ for Jira in 5 minutes or less.
As more and more customers tried unsuccessfully to stump our experts, we excitedly discovered that the majority of the solutions actually started out from the very same script base. Much like a base fold in origami, this base was an important building block for many different types of scripts. This script is called the loop base.
LOOP BASE SCRIPT
1 2 3 4 |
string [] issues = selectIssues( "project = TEMP" ); for (string i in issues) { runnerLog(i); } |
COMMON USE CASES FOR THE LOOP BASE
- Checking status of sub-tasks or a value of a custom field within the subtask
- Getting the status of linked issues or a value of a custom field within the linked issue
- Creating custom JQL functions
- Escalation services
- Checking attachment names
- Working with database search results
- Working with results from REST API
- Searching for other issues in Jira
- Creating a scheduled service
- And many more
SECTIONS
The loop base is made of 3 sections, the List section, the Loop section, and the Action section. In its most basic form, the script is only 4 lines of code.
LOOP BASE SCRIPT
1 2 3 4 5 6 7 8 9 10 11 |
//List section string [] issues = selectIssues( "project = TEMP" ); //Loop section for (string i in issues) { //Action section runnerLog(i); } //end Loop section |
LIST SECTION
The List section is the part of the code where a list of objects is gathered. This list could be any other issues in Jira, a list of sub-tasks, links, rows in a database, etc. In almost every case this section only needs to be one line of code.
EXAMPLES OF HOW TO RETRIEVE DIFFERENT TYPES OF INFORMATION IN THE LIST SECTION:
Select issues using JQL
string [] issues = selectIssues( "project = TEMP" ); |
Get an issues sub-tasks
string [] subtasks = subtasks(key); |
Get the issues links
string [] links = linkedIssues(key); |
Get the attachments for an issue
string [] attchmnts = attachments; |
Get data from a database
string [] data = sql( "External_DB" , "SELECT * FROM TABLE" ); |
LOOP SECTION
The Loop Section is the part of the code where we go through the list of objects one by one. The syntax for this loop is almost always the same. This section is essentially one line of code with a closing bracket at the end.
EXAMPLE OF A LOOP SECTION:
Select issues using JQL
for (string i in issues) { ... |
ACTION SECTION
This section is where actions are performed on the objects gathered in the list section. This could be writing a message to the screen, closing a subtask, or writing data to a database. This section could have as little as one line of code or many more lines depending on the action being performed.
EXAMPLE ACTIONS:
Close subtasks
autotransition( "Close" , s); |
Copy data to a different customfield
%i%.customDate = %i%.dueDate; |
Escalate priority
%i%.priority = "High" ; |
Send an email
sendEmail( "projectmanager" , "teamleader1" , "Transition executed" , currentUser() + " has executed a transition" ); |
COMPLETE EXAMPLES:
Change priority of issues that are past due
1 2 3 4 |
string [] issues = selectIssues( "statusCategory != Done AND duedate < startOfDay()" ); for (string i in issues) { %i%.priority = "High" ; } |
Close sub-tasks when main issue is closed
1 2 3 4 |
string [] subtasks = subtasks(key); for (string s in subtasks) { autotransition( "Close" , s); } |
Copy data to a different customfield
1 2 3 4 |
string [] issues = selectIssues( "project = TEMP" ); for (string i in issues) { %i%.newCustomField = %i%.oldCustomField; } |
Copy data from the parent
1 2 3 4 |
string [] subtasks = subtasks(key); for (string s in subtasks) { %s%.assignee = assignee; } |
Tagged with: checking attachments, custom fields, database results, database search, escalation of services, jira admin scheduled events, JQL, linked issues, REST API, scheduling services, status checks, sub tasks
Categorized in: Advanced JQL, custom fields, Data Integration, Issue Template Automation, Jira Admin, SIL, SIL Listeners, SIL Scheduler, SIL scripting