|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright (c) HashiCorp, Inc. |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | + |
| 5 | +source "$(dirname "${BASH_SOURCE[0]}")/../shared.bash" |
| 6 | + |
| 7 | +# Check for required input values |
| 8 | +if [ -z "${ISSUE_TYPE}" ]; then |
| 9 | + error "Missing 'issueType' input value" |
| 10 | + exit 1 |
| 11 | +fi |
| 12 | + |
| 13 | +if [ -z "${PROJECT}" ]; then |
| 14 | + error "Missing 'project' input value" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +if [ -z "${SUMMARY}" ]; then |
| 19 | + error "Missing 'summary' input value" |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Grab the issue type ID |
| 24 | +result="$(jira-request "${JIRA_BASE_URL}/rest/api/3/issuetype")" || exit |
| 25 | +query="$(printf '.[] | select(.name == "%s").id' "${ISSUE_TYPE}")" |
| 26 | +type_id="$(jq -r "${query}" <<< "${result}")" |
| 27 | + |
| 28 | +if [ -z "${type_id}" ]; then |
| 29 | + error "Could not find issue type with name '%s'" "${ISSUE_TYPE}" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +info "Issue type ID for '%s': %s" "${ISSUE_TYPE}" "${type_id}" |
| 34 | + |
| 35 | +if [ -n "${DESCRIPTION}" ]; then |
| 36 | + description="$(convert-gfm-to-jira "${DESCRIPTION}")" || exit |
| 37 | +fi |
| 38 | + |
| 39 | +# Base template for issue creation |
| 40 | +template=' |
| 41 | +{ |
| 42 | + description: $description, |
| 43 | + issuetype: { |
| 44 | + id: $issuetype |
| 45 | + }, |
| 46 | + project: { |
| 47 | + key: $project |
| 48 | + }, |
| 49 | + summary: $summary |
| 50 | +}' |
| 51 | +new_issue="$(jq -n --arg description "${description}" --arg issuetype "${type_id}" --arg project "${PROJECT}" --arg summary "${SUMMARY}" "${template}")" || exit |
| 52 | + |
| 53 | +# If there are extra fields provided, merge them in |
| 54 | +if [ -n "${EXTRA_FIELDS}" ]; then |
| 55 | + new_issue="$(printf "%s %s" "${new_issue}" "${EXTRA_FIELDS}" | jq -s add)" |
| 56 | +fi |
| 57 | + |
| 58 | +# Wrap the payload for submission |
| 59 | +template='{fields: $fields}' |
| 60 | +new_issue="$(jq -n --argjson fields "${new_issue}" "${template}")" || exit |
| 61 | + |
| 62 | +info "JIRA new issue payload:\n%s" "${new_issue}" |
| 63 | + |
| 64 | +# Create the issue |
| 65 | +# NOTE: The v2 API is used here for creating the issue. This is because |
| 66 | +# the v3 API only supports the Atlassian Document Format for which pandoc |
| 67 | +# currently does not have support (https://github.com/jgm/pandoc/issues/9898) |
| 68 | +result="$(jira-request --request "POST" --data "${new_issue}" "${JIRA_BASE_URL}/rest/api/2/issue")" || exit |
| 69 | +key="$(jq -r '.key' <<< "${result}")" |
| 70 | +id="$(jq -r '.id' <<< "${result}")" |
| 71 | + |
| 72 | +printf "issue=%s\n" "${id}" >> "${GITHUB_OUTPUT}" |
| 73 | +printf "issue-key=%s\n" "${key}" >> "${GITHUB_OUTPUT}" |
| 74 | + |
| 75 | +info ">> New JIRA issue created: %s/browse/%s" "${JIRA_BASE_URL}" "${key}" |
0 commit comments