Last active
July 6, 2023 21:32
-
-
Save shurkin18/4f53b182fbe8ce3dcec4662b9cf9362c to your computer and use it in GitHub Desktop.
Search JIRA Cloud for specific Issue and if none found - creates a new Issue via API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#################################################################################################################################### | |
# NOTE: you will need to generate a token (https://id.atlassian.com/manage-profile/security/api-tokens and click "Create API token") | |
# Make sure that account has necessary access (JIRA groups) on the JIRA's end, especially if you will be creating tickets via API | |
# In this example, we will search JIRA for issues with a specific title and specific reporter | |
# and if nothing found the script will create a new issue/ticket under Project "IT" and a specific Issue Type ID we set, assigned (Reporter) to currently logged in user (NOTE: this is for mac only), with specific Issue title and description | |
#################################################################################################################################### | |
#################################################################################################################################### | |
mm_jira_api_auth="[email protected]:TOKEN" | |
mm_jira_api_url="https://COMPANY.atlassian.net/rest/api/2" | |
mm_jira_cloud_it_tic_issue_type="10103" | |
# Issue Type can be found in JIRA > Administration > Issues > Issue Types > copy link of Edit button next to the issue type and there will be ID at the end | |
#################################################################################################################################### | |
#################################################################################################################################### | |
# Obtain currently logged in user account so that it can be passed to JIRA as the Reporter of the ticket | |
currentLoggedInUser=`stat -f '%u %Su' /dev/console | awk '{ print $2 }'` | |
# Obtain currently logged in user JIRA ID | |
curlLoggedInUserID=`curl --request GET \ | |
--url "$mm_jira_api_url/user/search?query=$currentLoggedInUser" \ | |
--user "$mm_jira_api_auth" \ | |
--header 'Accept: application/json' | cut -d '"' -f4 | cut -d "=" -f2` | |
echo "Currently logged in user is: $currentLoggedInUser, userID: $curlLoggedInUserID" | |
#################################################################################################################################### | |
#################################################################################################################################### | |
# Search JIRA for Tickets with title "AUTOMATED TICKET", with Reporter - currently logged in user(by user JIRA ID) | |
curlInputData=`curl \ | |
-D- \ | |
-u $mm_jira_api_auth \ | |
-X GET \ | |
-H "Content-Type: application/json" \ | |
$mm_jira_api_url/search?jql=summary%20~%20%22AUTOMATED%20TICKET%22%20AND%20reporter%20in%20%28$curlLoggedInUserID%29` | |
#Obtain the total number of found tickets, ie 0/1/2/3/etc. | |
totalResults=$(echo $curlInputData | sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | grep 'total' | awk -F \" '{ print $3 }' | sed 's/://' | sed '2,100d') | |
# Check if no tickets are found | totalResults = 0 > no tickets found > create a ticket then | |
if [[ $totalResults -eq "0" ]]; then | |
echo "No Tickets found, ticket found results variable totalResults is: $totalResults" | |
echo "Creating a JIRA ticket for $currentLoggedInUser" | |
#Create JIRA API ticket - JIRA CLOUD / USING accountId (not name, name can't be used in Jira Cloud) | |
curl \ | |
-D- \ | |
-u $mm_jira_api_auth \ | |
-X POST \ | |
-H "Content-Type: application/json" \ | |
$mm_jira_api_url/issue/ \ | |
--data ' | |
{ | |
"fields": | |
{ | |
"project": | |
{ | |
"key": "IT" | |
}, | |
"issuetype": | |
{ | |
"id": "'$mm_jira_api_issue_type'" | |
}, | |
"summary": "JIRA REST API TESTING - AUTOMATED TICKET - account: '$currentLoggedInUser'@mediamath.com", | |
"description": "Creating an issue using project key and issue type ID using JIRA API. Ticket was created by running API script on '$currentLoggedInUser'@mediamath.com", | |
"reporter": | |
{ | |
"accountId": "'$curlLoggedInUserID'" | |
} | |
} | |
} | |
' | |
else | |
echo "Ticket(s) are present, NOT OPENING ANOTHER ONE!" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment