Wiki source code of Making a free JIRA Scheduler

Last modified by Normann P. Nielsen on 2017/01/18 15:28

Hide last authors
Normann P. Nielsen 1.1 1 The main purpose here is to utilize the Linux [[Cron>>http://en.wikipedia.org/wiki/Cron]] facility to Transition Issues, rather than using the almost useless JIRA builtin services, or buying an expensixe cron/scheduling plugin for JIRA
2
3 The solution is pretty flexible for all issue types, by making the "~-~-step "Initiate" a parameter instead of hardcoded, all issuetypes can be transitioned.
4
5
6
7 {{toc/}}
8
9 = Prerequsites =
10
11 === Linux User ===
12 All scripts and the crontab stuff assumes that there is a "jira" user with access to /opt/jira-cron/
13
14 === iq processor ===
15 jq command-line JSON processor must be available to the scripts
16
17 [[https://stedolan.github.io/jq/]]Workflow
18
19
20
21 My IssueType is called "Repeatable Task" and has a very small workflow:
22
23 [[image:Screen Shot 2014-04-05 at 12.57.59.jpg||width="258"||height="324"||alt="Screen Shot 2014-04-05 at 12.57.59.jpg"]]
24
25 Upon the Transition of **Initiate **(id 61), the Post Function:
26
27 * Creates a copy of the Issue as a subtask
28 * Sets the the issue back to Status "Frozen"
29 [[image:Screen Shot 2014-04-05 at 12.59.34.jpg||width="578"||height="404"||alt="Screen Shot 2014-04-05 at 12.59.34.jpg"]]
30
31 === A custom Field for crontab values ===
32 The Issuetype has a custom field called "Cron Scheduling" (Text Field (single line)), as this will hold the schedules for the Issue:
33
34 [[image:Screen Shot 2014-02-17 at 10.25.03.png||width="687"||height="153"||alt="Screen Shot 2014-02-17 at 10.25.03.png"]]
35
36 In the script, the field has the JIRA identifier customfield_12821 - You must change this to fit Your field.
37
38 === Location ===
39 I have my stuff in /opt/jira-cron
40
41
42 {{code}}
43 user@myserver:/opt/jira-cron$ ls -l
44 total 152
45 -rw-rw-rw- 1 jira root 121906 2014-04-02 13:39 jiraMakeCrontab.log
46 -rwxr-xr-x 1 root root 1454 2014-04-02 13:44 jiraMakeCrontab.sh
47 -rw-rw-rw- 1 jira root 18132 2014-04-05 05:00 jiraTransitionIssue.log
48 -rwxr-xr-x 1 root root 909 2014-02-17 11:51 jiraTransitionIssue.sh
49 user@myserver:/opt/jira-cron$
50 {{/code}}
51
52
53
54 = The usefull Scripts =
55
56 === Make the crontab script  ===
57 The scripts makes and enables a crontab for the "jira" user. It search for issues that satisfies:
58
59 {{success}}//Issuetype=Repeatable Task//
60
61 //Status=Frozen//
62
63 //Cron Scheduling has a value//{{/success}}
64
65
66 {{code language="jiraMakeCrontab.sh"}}
67 #!/bin/bash
68
69 rm /tmp/crontab > /dev/null 2>&1
70
71 cd /opt/jira-cron/
72
73 IFS=$(echo -en "\n\b")
74 TODAY=`date +%Y-%m-%d.%H:%M:%S`
75
76 TransitionScript="/opt/jira-cron/jiraTransitionIssue.sh"
77 JIRAFilter="issuetype%20%3D%20%22Repeatable%20Task%22%20AND%20status%20in%20%28Frozen%29%20%20and%20%22Cron%20Scheduling%22%20IS%20NOT%20NULL&maxResults=999"
78 JIRAUSER=""
79 JIRAPASS=""
80
81 #echo https://jira.server.dk/rest/api/2/search?jql=$JIRAFilter
82
83 CURLOPT_HEADER=0
84 export CURLOPT_HEADER
85 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
86
87 IssueTotal=`cat /tmp/issuelist.json | ./jq-linux64 '.total'`
88 echo "$TODAY Total: $IssueTotal" >> /opt/jira-cron/jiraMakeCrontab.log
89
90 Count=0
91 for IssueId in `cat /tmp/issuelist.json | ./jq-linux64 '.issues[] .id'`
92 do
93
94 Count=$(($Count + 1))
95
96 IssueId=`echo $IssueId | sed "s/\"//g"`
97 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
98
99 IssueKey=`cat /tmp/issue.json | ./jq-linux64 '.key'| sed "s/\"//g"`
100 #IssueStatus=`cat /tmp/issue.json | ./jq-linux64 '.fields.status.name'i | sed "s/\"//g"`
101 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
102
103 for CronEntry in `cat /tmp/cronfield.json | tr ";" "\n"`
104 do
105
106 echo "$CronEntry $TransitionScript $IssueKey > /dev/null 2>&1" >> /tmp/crontab
107 echo "$TODAY Added ($Count): $cronEntry $TransitionScript $IssueKey to /tmp/crontab" >> /opt/jira-cron/jiraMakeCrontab.log
108
109 done
110
111 echo "" >> /tmp/crontab
112
113 done
114
115 #Replace JIRA Users crontab
116 if [ ! -f /tmp/crontab ]
117 then
118  
119 echo "" > /tmp/crontab
120  
121 fi
122  
123 crontab /tmp/crontab
124 {{/code}}
125
126 The final line, making the JIRA crontab, can be extended with an error handler.
127
128
129
130 After running the script; - the /var/spool/cron/crontabs/jira (on Ubuntu LTS) should look like this:
131
132
133 {{code language="true"}}
134 #Crontabs for SUPPORT-513
135 * * 1 * * /opt/jira-cron/jiraTransitionIssue.sh SUPPORT-513 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1
136
137 #Crontabs for HOMEPAGE-3846
138 0 5 2 * * /opt/jira-cron/jiraTransitionIssue.sh HOMEPAGE-3846 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1
139
140 #Crontabs for HOMEPAGE-2933
141 0 0 1 * * /opt/jira-cron/jiraTransitionIssue.sh HOMEPAGE-2933 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1
142
143 #Crontabs for PROJECTTOOLS-2467
144 0 5 5 * * /opt/jira-cron/jiraTransitionIssue.sh PROJECTTOOLS-2467 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1
145 {{/code}}
146
147
148
149
150
151 === Make the Transition Script ===
152 Make sure the "jira" user has executeable acess to this. The TRANSITIONJSON defines the **Initiate** Transition (id 61)
153
154
155 {{code language="jiraTransitionIssue.sh"}}
156 #!/bin/bash
157
158 IFS=$(echo -en "\n\b")
159 TRANSITIONJSON='{"transition": {"id": "61"}}'
160 JIRAUSER=""
161 JIRAPASS=""
162 CURLOPT_HEADER=0
163 export CURLOPT_HEADER
164
165 cd /tmp
166
167 IssueKey=$1
168
169 if [ $IssueKey != '' ]
170 then
171
172 curl -D header.txt -u $JIRAUSER:$JIRAPASS -X POST --data $TRANSITIONJSON -H "Content-Type: application/json" https://jira.server.dk/rest/api/2/issue/$IssueKey/transitions
173 rm header.txt
174
175 else
176
177 echo "No IssueKey as parameter"
178
179 fi
180 {{/code}}
181
182
183
184 === The Master Cron ===
185 To make the JIRA users crontab on a regular basis, I use /etc/crontab on Ubuntu:
186
187
188 {{code}}
189 0 20 * * * jira /opt/jira-cron/jiraMakeCrontab.sh
190 {{/code}}
191
192
193
194 = Troubleshooting =
195
196 Make sure the crontab is functioning, look in /var/log/syslog for tings like:
197
198 **Cron reloading the new crontab for JIRA user:**
199
200
201 {{code}}
202 Apr 5 13:28:50 myserver crontab[12857]: (jira) REPLACE (jira)
203 Apr 5 13:29:01 myserver cron[4946]: (jira) RELOAD (crontabs/jira)
204 {{/code}}
205
206 **Transitions via jira users cron entries:**
207
208
209 {{code}}
210 Apr 5 05:00:01 myserver: CRON[1563]: (jira) CMD (/opt/jira-cron/jiraTransitionIssue.sh HOMEPAGE-2467 >> /opt/jira-cron/jiraTransitionIssue.log 2>&1)
211
212 {{/code}}
213
214
215
216 **FIELD and STATE variables in the script files**
217
218 Remember to change the variables to Your setup