|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script creates a JIRA ticket if one does not already exist with the same title. |
| 4 | +# It performs the following steps: |
| 5 | +# 1. Defines a function to URL encode a given string. |
| 6 | +# 2. URL encodes the JIRA ticket title. |
| 7 | +# 3. Checks if a JIRA ticket with the same title already exists in the specified project (id=10984) and component (id=35986). |
| 8 | +# 4. If a ticket already exists, it exits without creating a new ticket. |
| 9 | +# 5. If no ticket exists, it creates a new JIRA ticket with the provided title, description and team id. |
| 10 | +# 6. Outputs the ID of the created JIRA ticket and sets it as a GitHub Actions output variable. |
| 11 | + |
| 12 | +set -eou pipefail |
| 13 | + |
| 14 | +url_encode() { |
| 15 | + local string="$1" |
| 16 | + local encoded="" |
| 17 | + encoded=$(python3 -c "import urllib.parse; print(urllib.parse.quote('''$string'''))") |
| 18 | + echo "$encoded" |
| 19 | +} |
| 20 | + |
| 21 | +encoded_jira_ticket_title=$(url_encode "${JIRA_TICKET_TITLE:?}") |
| 22 | +echo "encoded_jira_ticket_title: ${encoded_jira_ticket_title}" |
| 23 | + |
| 24 | +found_issue_response=$(curl --request GET \ |
| 25 | + --url 'https://jira.mongodb.org/rest/api/2/search?jql=project=10984%20AND%20issuetype=12%20AND%20component=35986%20AND%20summary~"'"${encoded_jira_ticket_title:?}"'"' \ |
| 26 | + --header 'Authorization: Bearer '"${JIRA_API_TOKEN:?}" \ |
| 27 | + --header 'Accept: application/json' \ |
| 28 | + --header 'Content-Type: application/json') |
| 29 | + |
| 30 | +echo "found_issue_response: ${found_issue_response}" |
| 31 | + |
| 32 | +found_issue=$(echo "${found_issue_response}" | jq .total) |
| 33 | +echo "found_issue: ${found_issue}" |
| 34 | +if [ "$found_issue" -ne 0 ]; then |
| 35 | + echo "There is already a Jira ticket with the title \"${JIRA_TICKET_TITLE:?}\"" |
| 36 | + echo "No new Jira ticket will be created." |
| 37 | + exit 0 |
| 38 | +fi |
| 39 | + |
| 40 | +if [ "${found_issue}" == "null" ]; then |
| 41 | + echo "There was an error in retrieving the jira ticket: ${found_issue_response}" |
| 42 | + echo "No new Jira ticket will be created." |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +echo "Creating Jira ticket...." |
| 47 | +echo "JIRA_TICKET_TITLE: ${JIRA_TICKET_TITLE}" |
| 48 | +echo "JIRA_TICKET_DESCRIPTION: ${JIRA_TICKET_DESCRIPTION}" |
| 49 | + |
| 50 | +json_response=$(curl --request POST \ |
| 51 | +--url 'https://jira.mongodb.org/rest/api/2/issue' \ |
| 52 | +--header 'Authorization: Bearer '"${JIRA_API_TOKEN:?}" \ |
| 53 | +--header 'Accept: application/json' \ |
| 54 | +--header 'Content-Type: application/json' \ |
| 55 | +--data @- <<EOF |
| 56 | +{ |
| 57 | + "fields": { |
| 58 | + "project": { |
| 59 | + "id": "10984" |
| 60 | + }, |
| 61 | + "summary": "${JIRA_TICKET_TITLE:?}", |
| 62 | + "issuetype": { |
| 63 | + "id": "12" |
| 64 | + }, |
| 65 | + "customfield_12751": [ |
| 66 | + { |
| 67 | + "id": "${JIRA_TEAM_ID:?}" |
| 68 | + } |
| 69 | + ], |
| 70 | + "description": "${JIRA_TICKET_DESCRIPTION:?}", |
| 71 | + "components": [ |
| 72 | + { |
| 73 | + "id": "35986" |
| 74 | + } |
| 75 | + ] |
| 76 | + } |
| 77 | +} |
| 78 | +EOF |
| 79 | +) |
| 80 | + |
| 81 | +echo "Response: ${json_response}" |
| 82 | + |
| 83 | +JIRA_TICKET_ID=$(echo "${json_response}" | jq -r '.key') |
| 84 | + |
| 85 | +echo "The following JIRA ticket has been created: ${JIRA_TICKET_ID}" |
| 86 | +if [ "${JIRA_TICKET_ID}" != "null" ]; then |
| 87 | + echo "jira-ticket-id=${JIRA_TICKET_ID}" >> "${GITHUB_OUTPUT}" |
| 88 | + exit 0 |
| 89 | +fi |
| 90 | + |
| 91 | +exit 1 |
| 92 | + |
0 commit comments