Snippets

Darryl Lee REz8Mp: Untitled snippet

Created by Darryl Lee last modified
# Script mentioned in "how to audit for unused automation rules?"
# https://community.atlassian.com/t5/Jira-questions/how-to-audit-for-unused-automation-rules/qaq-p/2803602#M1051601
#
# It will:
# * Download your Automation Rules as a JSON file
# * Convert those Rules to a CSV with just the metadata for each rule
# * Extract Author IDs for creating a lookup file to map ID to Display Name and Email
# * Download the last month's usage data and convert it to a CSV file
# * Attach the metadata, author mapping, and usage CSV files to a Confluence page
# * Update the Confluence page with today's date
#
# Dependencies: curl, jq, Appfire's CLI for Confluence

# Re: Authentication
# We are using the -n flag for curl, which allows you to store your credentials in a ~/.netrc file
# Mine looks like this:
# machine SITENAME.atlassian.net login MYEMAIL password MYAPITOKEN
#
# You could also use -u MYEMAIL:MYAPITOKEN instead of -n 

PAGEID="" # ID of Confluence Page where you will attach CSVs"
CLOUDID="YOUR TENANT ID" # https://SITENAME.atlassian.net/_edge/tenant_info
SITENAME="YOUR SITE NAME" 

# Kick off a rules export and grab the DOWNLOADID

DOWNLOADID="$(curl -n -s --location https://${SITENAME}.atlassian.net/gateway/api/automation/internal-api/jira/${CLOUDID}/pro/rest/GLOBAL/rule/export \
--header 'Content-Type: application/json' \
--header 'Cache-Control: no-cache' \
| jq -r .id)"

# Let's wait a minute for the rules export file to be generated
# (The proper thing to do would be to use the "progress" endpoint to see if your download is complete. But... lazy.
# https://${SITENAME}.atlassian.net/gateway/api/automation/internal-api/jira/${CLOUDID}/pro/rest/GLOBAL/rule/export/${DOWNLOADID}/progress
sleep 60 

curl -n -s --location "https://${SITENAME}.atlassian.net/gateway/api/automation/internal-api/jira/${CLOUDID}/pro/rest/GLOBAL/rule/export/${DOWNLOADID}/download" > automation-rules.json

# Let's make sure the export file isn't empty
#
# [If I was a real programmer, I would use the ${DOWNLOADID}/progress 
# endpoint to make sure it was done before I try to download it.]

if [ -s automation-rules.json ]
then

# Extract just the name, state, description, dates, and authorIds metadata from the export file
echo '"id","name","state","description","created","updated","authorAccountId"' > automation-rules.csv
jq -r '.rules[]|[.id,.name,.state,.description,.created,.updated,.authorAccountId]|@csv' automation-rules.json >> automation-rules.csv

# Extract just the authorIDs from the export file
jq -r '.rules[].authorAccountId' automation-rules.json|sort -u > authorIds.csv

# Generate a mapping file for Author IDs to get names and emails
echo '"id","email","name"' > authormap.csv

for x in `cat authorIds.csv`
do 
curl -n -s "https://${SITENAME}.atlassian.net/rest/api/latest/user?accountId=$x"|jq -r '[.accountId,.emailAddress,.displayName]|@csv' >> authormap.csv
done

# Attach extract of rules metadata to Confluence Page
curl -n -s -X PUT "https://${SITENAME}.atlassian.net/wiki/rest/api/content/${PAGEID}/child/attachment" -H 'X-Atlassian-Token: nocheck' -F 'file=@"automation-rules.csv"'

# Attach author mapping file to Confluence Page
curl -n -s -X PUT "https://${SITENAME}.atlassian.net/wiki/rest/api/content/${PAGEID}/child/attachment" -H 'X-Atlassian-Token: nocheck' -F 'file=@"authormap.csv"'

# Don't try to do stuff if the export failed.
else
echo "Export of rules failed"
fi

# Export usage file
echo '"id","executionCount"' > usage.csv
curl -n -s "https://${SITENAME}.atlassian.net/gateway/api/automation/internal-api/jira/${CLOUDID}/pro/rest/GLOBAL/highestUsages?limit=100" | jq -r '.[]|[.ruleId,.executionCount]|@csv' >> usage.csv

# Attach usage file to Confluence Page
curl -s -n -X PUT "https://${SITENAME}.atlassian.net/wiki/rest/api/content/${PAGEID}/child/attachment" -H 'X-Atlassian-Token: nocheck' -F 'file=@"usage.csv"'

# Update the Confluence Page with today's date
acli --action modifyPage --id ${PAGEID} --findReplaceRegex "(Last updated: <time datetime=\").+?(\")#\$1`date -I`\$2" --special " #"

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.