Sidehistorik
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.
Indholdsfortegnelse |
---|
Prerequsites
Linux User
All scripts and the crontab stuff assumes that there is a "jira" user with access to /opt/jira-cron/
iq processor
jq command-line JSON processor must be available to the scripts
https://stedolan.github.io/jq/Workflow
My IssueType is called "Repeatable Task" and has a very small workflow:
Upon the Transition of Initiate (id 61), the Post Function:
- Creates a copy of the Issue as a subtask
- Sets the the issue back to Status "Frozen"
A custom Field for crontab values
The Issuetype has
I erkendelse af at 1st forsøg var noget skralder, er vores Repeatable Task lavet om, så scheduleringen fungerer med almindelige cron Entries som i https://help.ubuntu.com/community/CronHowto
På alle Repeatable Tasks er felterne :
Make a custom Field
Make a custom field called "Cron Scheduling" - multiline. This (Text Field (single line)), as this will hold the scedules schedules for the Issue:
In the script, the field has the JIRA identifier customfield_12821 - You must change this to fit Your field.
Location
I have my stuff in /opt/jira-cron
Kodeblok |
---|
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 usefull Scripts
Make the crontab script
crontab script
The scripts makes and enables a crontab for the "jira" user. It search for issues that satisfies:
Tip |
---|
Issuetype=Repeatable Task Status=Frozen Cron Scheduling has a value |
Kodeblok | ||||
---|---|---|---|---|
| ||||
Kodeblok | ||||
#!/bin/bash rm /tmp/crontab > /dev/null 2>&1 cd /opt/jira-cron/ IFS=$(echo -en "\n\b") TODAY=`date +%Y-%m-%d.%H:%M:%S` TransitionScript="/packopt/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 " --------- " >>/packissuetype%20%3D%20%22Repeatable%20Task%22%20AND%20status%20in%20%28Frozen%29%20%20and%20%22Cron%20Scheduling%22%20IS%20NOT%20NULL&maxResults=999" JIRAUSER="" JIRAPASS="" #echo https://jira.server.dk/rest/api/2/search?jql=$JIRAFilter CURLOPT_HEADER=0 export CURLOPT_HEADER curl -D -k -u $JIRAUSER:$JIRAPASS -X GET -H "Content-Type: application/json" https://jira.server.dk/rest/api/2/search?jql=$JIRAFilter > /tmp/issuelist.json IssueTotal=`cat /tmp/issuelist.json | ./jq-linux64 '.total'` echo "$TODAY Total: $IssueTotal" >> /opt/jira-cron/jiraMakeCrontab.log issueList=`/pack/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=`/pack/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 >> /pack/jira-cron/jiraTransitionIssue.logCount=0 for IssueId in `cat /tmp/issuelist.json | ./jq-linux64 '.issues[] .id'` do Count=$(($Count + 1)) IssueId=`echo $IssueId | sed "s/\"//g"` curl -D -k -u $JIRAUSER:$JIRAPASS -X GET -H "Content-Type: application/json" https://jira.server.dk/rest/api/2/issue/$IssueId > /tmp/issue.json IssueKey=`cat /tmp/issue.json | ./jq-linux64 '.key'| sed "s/\"//g"` #IssueStatus=`cat /tmp/issue.json | ./jq-linux64 '.fields.status.name'i | sed "s/\"//g"` cat /tmp/issue.json | ./jq-linux64 '.fields.customfield_12821' | sed "s/\"//g" | sed "s/\\\r//g" | sed "s/\\\n/;/g" | sed "s/\\\t/ /g" > /tmp/cronfield.json for CronEntry in `cat /tmp/cronfield.json | tr ";" "\n"` do echo "$CronEntry $TransitionScript $IssueKey > /dev/null 2>&1" >> /tmp/crontab echo "$TODAY Added ($Count): $cronEntry $TransitionScript $issueKey$IssueKey to /tmp/crontab" >> /packopt/jira-cron/jiraMakeCrontab.log done echo "" >> /tmp/crontab done #Replace JIRA Users crontab if [ ! fi rm -f /tmp/value 2> /dev/null done rm /tmp/value 2> /dev/null #Replace JIRA Users crontab su - jira -c "crontab ] then echo "" > /tmp/crontab fi crontab /tmp/crontab"crontab |
The final line, making the JIRA crontab, can be extended with an error handler.
Make the Transition Script
After running the script; - the /var/spool/cron/crontabs/jira (on Ubuntu LTS) should look like this:
Kodeblok | ||
---|---|---|
| ||
#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 | ||
Kodeblok | ||
#!/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=`/pack/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
/pack/atlassian-cli/jira.sh --action progressIssue --issue "$issueKey" --step "Initiate" >> /pack/jira-cron/jiraTransitionIssue.log 2>&1 |
Make the Transition Script
Make sure the "jira" user has executeable acess to this. The TRANSITIONJSON defines the Initiate Transition (id 61)
Kodeblok | ||||
---|---|---|---|---|
| ||||
#!/bin/bash IFS=$(echo -en "\n\b") TRANSITIONJSON='{"transition": {"id": "61"}}' JIRAUSER="" JIRAPASS="" CURLOPT_HEADER=0 export CURLOPT_HEADER cd /tmp IssueKey=$1 if [ $IssueKey != else'' ] then curl -D header.txt -u $JIRAUSER:$JIRAPASS -X POST --data $TRANSITIONJSON -H echo "Issue $issueKey was not in $OKSTATUS State, but in $Status" fi fi else "Content-Type: application/json" https://jira.server.dk/rest/api/2/issue/$IssueKey/transitions rm header.txt else echo "No IssueKey as parameter" fi |
The Master Cron
To make the JIRA users crontab on a regular basis, I use /etc/crontab on Ubuntu:
Kodeblok |
---|
#Crontabs0 for20 NSP-513 * * 1 * *jira /packopt/jira-cron/jiraTransitionIssuejiraMakeCrontab.sh NSP-513 >> /pack/jira-cron/jiraTransitionIssue.log 2>&1 #Crontabs for NIS-3846 0 5 2 * * /pack/jira-cron/jiraTransitionIssue.sh NIS-3846 >> /pack/jira-cron/jiraTransitionIssue.log 2>&1 #Crontabs for NIS-2933 0 0 1 * * /pack/jira-cron/jiraTransitionIssue.sh NIS-2933 >> /pack/jira-cron/jiraTransitionIssue.log 2>&1 #Crontabs for NIS-2467 0 5 5 * * /pack |
Troubleshooting
Make sure the crontab is functioning, look in /var/log/syslog for tings like:
Cron reloading the new crontab for JIRA user:
Kodeblok |
---|
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:
Kodeblok |
---|
Apr 5 05:00:01 myserver: CRON[1563]: (jira) CMD (/opt/jira-cron/jiraTransitionIssue.sh NISHOMEPAGE-2467 >> /packopt/jira-cron/jiraTransitionIssue.log 2>&1) |
FIELD and STATE variables in the script files
Remember to change the variables to Your setupAlle Issues er lavet om.