The main purpose here is to utilize the Linux Cron facility to Transition Issues, rather than using the almost useless JIRA builtin services, or buying an expensixe cron/scheduling plugin for JIRA
The solution is pretty flexible for all issue types, by making the "--step "Initiate" a parameter instead of hardcoded, all issuetypes can be transitioned.
All scripts and the crontab stuff assumes that there is a "jira" user with access to /opt/jira-cron/
The Atlassian CLI must be installed and prepared
My IssueType is called "Repeatable Task" and has a very small workflow:
Upon the Transition of Initiate, the Post Function:
The Issuetype has a custom field called "Cron Scheduling" (Text Field (single line)), as this will hold the schedules for the Issue:
In the script, the field has the JIRA identifier customfield_12821 - You must change this to fit Your field.
I have my stuff in /opt/jira-cron
user@myserver:/opt/jira-cron$ ls -l total 152 -rw-rw-rw- 1 jira root 121906 2014-04-02 13:39 jiraMakeCrontab.log -rwxr-xr-x 1 root root 1454 2014-04-02 13:44 jiraMakeCrontab.sh -rw-rw-rw- 1 jira root 18132 2014-04-05 05:00 jiraTransitionIssue.log -rwxr-xr-x 1 root root 909 2014-02-17 11:51 jiraTransitionIssue.sh user@myserver:/opt/jira-cron$ |
The scripts makes and enables a crontab for the "jira" user. It search for issues that satisfies:
Issuetype=Repeatable Task Status=Frozen Cron Scheduling has a value |
#!/bin/bash IFS=$(echo -en "\n\b") TransitionScript="/opt/jira-cron/jiraTransitionIssue.sh" JIRAFilter="issuetype = \"Repeatable Task\" AND status in (Frozen) and \"Cron Scheduling\" IS NOT NULL" FIELD="12821" CHAR="'" CRONTABUSER="jira" rm /tmp/crontab 2> /dev/null rm /tmp/value 2> /dev/null echo " --------- " >>/opt/jira-cron/jiraMakeCrontab.log issueList=`/opt/atlassian-cli/jira.sh --action getIssueList --search "$JIRAFilter" --outputFormat 200 | grep -v "issues" | grep -v "Created"` for issue in $issueList do issueKey=`echo $issue | awk '{print $1}' | sed 's/,//g'` CustomFieldValue=`/opt/atlassian-cli/jira.sh --action GetFieldValue --issue "$issueKey" --field "customfield_$FIELD" --file /tmp/value` if [ -f /tmp/value ] then crontabEntry=`cat /tmp/value | sed 's/'"$CHAR"'//g'` echo "#Crontabs for $issueKey" >> /tmp/crontab for cronEntry in $crontabEntry do echo "$cronEntry $TransitionScript $issueKey >> /opt/jira-cron/jiraTransitionIssue.log 2>&1" >> /tmp/crontab echo "Added: $cronEntry $TransitionScript $issueKey to /tmp/crontab" >> /opt/jira-cron/jiraMakeCrontab.log done echo "" >> /tmp/crontab fi rm /tmp/value 2> /dev/null done rm /tmp/value 2> /dev/null #Replace JIRA Users crontab su - $CRONTABUSER -c "crontab /tmp/crontab" |
After running the script the /var/spool/cron/crontabs/jira (on Ubuntu LTS) should look like this:
#Crontabs for SUPPORT-513 * * 1 * * /opt/jira-cron/jiraTransitionIssue.sh SUPPORT-513 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1 #Crontabs for HOMEPAGE-3846 0 5 2 * * /opt/jira-cron/jiraTransitionIssue.sh HOMEPAGE-3846 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1 #Crontabs for HOMEPAGE-2933 0 0 1 * * /opt/jira-cron/jiraTransitionIssue.sh HOMEPAGE-2933 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1 #Crontabs for PROJECTTOOLS-2467 0 5 5 * * /opt/jira-cron/jiraTransitionIssue.sh PROJECTTOOLS-2467 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1 |
#!/bin/bash # CLI Reference: https://bobswift.atlassian.net/wiki/display/JCLI/JIRA+Command+Line+Interface IFS=$(echo -en "\n\b") CHAR="'" OKSTATUS="Frozen (10003)" issueKey=$1 if [ $issueKey != '' ] then StatusFieldValue=`/opt/atlassian-cli/jira.sh --action GetFieldValue --issue "$issueKey" --field "Status" --file /tmp/value` if [ -f /tmp/value ] then Status=`cat /tmp/value | sed 's/'"$CHAR"'//g'` if [ $Status == $OKSTATUS ] then # Transition issue /opt/atlassian-cli/jira.sh --action progressIssue --issue "$issueKey" --step "Initiate" >> /opt/jira-cron/jiraTransitionIssue.log 2>&1 else echo "Issue $issueKey was not in $OKSTATUS State, but in $Status" fi fi else echo "No IssueKey as parameter" fi |
To make the JIRA users crontab on a regular basis, I use /etc/crontab on Ubuntu:
0 20 * * * root /opt/jira-cron/jiraMakeCrontab.sh |
Make sure the Atlassian CLI user (typically hardcoded in jira.sh) has proper access to the Issues that are being Transitioned, this is not good:
Remote error: com.atlassian.jira.rpc.exception.RemotePermissionException: This issue does not exist or you don't have permission to view it. Could not find issue with key: 'PORTAL-4283' |
or
root@myserver:/opt/jira-cron# ./jiraMakeCrontab.sh Remote error: com.atlassian.jira.rpc.exception.RemoteValidationException: Query validation failed: Field 'issuetype' does not exist or you do not have permission to view it. Field 'Cron Scheduling' does not exist or you do not have permission to view it. |
Make sure the crontab is functioning, look in /var/log/syslog for tings like:
Cron reloading the new crontab for JIRA user:
Apr 5 13:28:50 myserver crontab[12857]: (jira) REPLACE (jira) Apr 5 13:29:01 myserver cron[4946]: (jira) RELOAD (crontabs/jira) |
Transitions via jira users cron entries:
Apr 5 05:00:01 myserver: CRON[1563]: (jira) CMD (/opt/jira-cron/jiraTransitionIssue.sh HOMEPAGE-2467 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1) |
Issue Key Extraction
I have seen problems with the line:
issueKey=`echo $issue | awk '{print $1}' | sed 's/,//g'` |
giving back
"SAMPLES-125""10501""SAMPLES""Repeatable |
instead of an expected:
SAMPLES-125 |
Problably a CLI version dependancy, change it to:
issueKey=`echo $issue | awk -F "," '{print $1}' | sed 's/\"//g'` |