Sidehistorik
...
Kodeblok | ||||
---|---|---|---|---|
| ||||
#!/bin/bash rm /tmp/crontab > /dev/null 2>&1 cd /pack/jira-cron/ IFS=$(echo -en "\n\b") TODAY=`date +%Y-%m-%d.%H:%M:%S` TransitionScript="/optpack/jira-cron/jiraTransitionIssue.sh" JIRAFilter="issuetype%20%3D%20%22Repeatable%20Task%22%20AND%20status%20in%20%28Frozen%29%20%20and%20%22Cron%20Scheduling%22%20IS%20NOT%20NULL&maxResults="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 " --------- " >>/opt999" JIRAUSER="" JIRAPASS="" #echo https://jira.netic.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.netic.dk/rest/api/2/search?jql=$JIRAFilter > /tmp/issuelist.json IssueTotal=`cat /tmp/issuelist.json | ./jq-linux64 '.total'` echo "$TODAY Total: $IssueTotal" >> /pack/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.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.netic.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" >> /optpack/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 - $CRONTABUSERjira -c "crontab /tmp/crontab~/jiraMakeCrontab.sh" |
After running the script the /var/spool/cron/crontabs/jira (on Ubuntu LTS) should look like this:
...
Kodeblok | ||||
---|---|---|---|---|
| ||||
#!/bin/bash # CLI Reference: https://bobswift.atlassian.net/wiki/display/JCLI/JIRA+Command+Line+Interface IFS=$(echo -en "\n\b") CHARTRANSITIONJSON="'{" OKSTATUS=transition"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: {"id": "61"}}' JIRAUSER="" JIRAPASS="" CURLOPT_HEADER=0 export CURLOPT_HEADER cd /tmp IssueKey=$1 if [ $IssueKey != '' ] then curl -D header.txt -u $JIRAUSER:$JIRAPASS -X POST --data $TRANSITIONJSON -H "Content-Type: application/json" https://jira.netic.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 |
---|
0 20 * * * root /opt/jira-cron/jiraMakeCrontab.sh |
Troubleshooting
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:
Kodeblok |
---|
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
Kodeblok |
---|
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:
...