Last active
March 21, 2025 18:45
-
-
Save gnodet/78c30360a56c02a5cd777a624b5549c3 to your computer and use it in GitHub Desktop.
Script creating a JIRA issue for a given PR
This file contains hidden or 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 | |
# Initialize variables | |
VERBOSE=false | |
# Process command line options | |
while getopts "v" opt; do | |
case $opt in | |
v) | |
VERBOSE=true | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
exit 1 | |
;; | |
esac | |
done | |
# Shift to get the non-option arguments | |
shift $((OPTIND-1)) | |
# Debug function | |
debug() { | |
if $VERBOSE; then | |
echo "$@" | |
fi | |
} | |
# Check if required commands are installed | |
for cmd in jq yq gh; do | |
if ! command -v $cmd &> /dev/null; then | |
echo "Error: $cmd is not installed. Please install $cmd to run this script." | |
exit 1 | |
fi | |
done | |
# Check if gh is authenticated | |
if ! gh auth status &> /dev/null; then | |
echo "Error: gh CLI is not authenticated. Please run 'gh auth login' first." | |
exit 1 | |
fi | |
# Check if a PR number was provided | |
if [ $# -eq 0 ]; then | |
echo "Please provide a PR number as an argument." | |
exit 1 | |
fi | |
# Check if required environment variables are set | |
if [ -z "$JIRA_PAT" ]; then | |
echo "Error: JIRA_PAT environment variable is not set." | |
echo "Please set it with your Personal Access Token from Apache JIRA." | |
exit 1 | |
fi | |
PR_NUMBER=$1 | |
# Get current repository information using gh cli | |
echo "Detecting repository information..." | |
REPO_NAME=$(gh repo view --json nameWithOwner -q .nameWithOwner) | |
if [ $? -ne 0 ] || [ -z "$REPO_NAME" ]; then | |
echo "Error: Could not detect repository information. Are you in a git repository?" | |
exit 1 | |
fi | |
echo "Working with repository: $REPO_NAME" | |
# Fetch PR details | |
echo "Fetching PR details..." | |
PR_DATA=$(gh api "repos/$REPO_NAME/pulls/$PR_NUMBER") | |
if [ $? -ne 0 ]; then | |
echo "Error: Could not fetch PR details. Does PR #$PR_NUMBER exist?" | |
exit 1 | |
fi | |
PR_TITLE=$(echo "$PR_DATA" | jq -r .title) | |
PR_BODY=$(echo "$PR_DATA" | jq -r .body) | |
PR_URL=$(echo "$PR_DATA" | jq -r .html_url) | |
# Check if PR data was fetched successfully | |
if [ -z "$PR_TITLE" ] || [ -z "$PR_URL" ]; then | |
echo "Failed to fetch PR data." | |
exit 1 | |
fi | |
# Fetch and parse .asf.yaml from the repository | |
echo "Fetching .asf.yaml configuration..." | |
if gh api "repos/$REPO_NAME/contents/.asf.yaml" > /dev/null 2>&1; then | |
ASF_YAML_CONTENT=$(gh api "repos/$REPO_NAME/contents/.asf.yaml" | jq -r '.content' | base64 -d) | |
else | |
echo "Error: Could not fetch .asf.yaml file. Does it exist in the repository?" | |
exit 1 | |
fi | |
# Extract JIRA project key from .asf.yaml and clean it up | |
JIRA_PROJECT=$(echo "$ASF_YAML_CONTENT" | yq eval '.github.autolink_jira' - | sed 's/^- //') | |
if [ -z "$JIRA_PROJECT" ] || [ "$JIRA_PROJECT" == "null" ]; then | |
echo "Error: Could not find JIRA project key in .asf.yaml" | |
echo "Please ensure the .asf.yaml file contains the 'github.autolink_jira' configuration." | |
exit 1 | |
fi | |
JIRA_API="https://issues.apache.org/jira/rest/api/2" | |
AUTH_HEADER="Authorization: Bearer $JIRA_PAT" | |
# Test auth before proceeding by querying myself endpoint | |
echo "Testing JIRA authentication..." | |
debug "Using Bearer token authentication" | |
if $VERBOSE; then | |
AUTH_TEST=$(curl -v -s -X GET -H "$AUTH_HEADER" "$JIRA_API/myself" 2>&1) | |
debug "Auth test response (verbose):" | |
debug "$AUTH_TEST" | |
else | |
AUTH_TEST=$(curl -s -X GET -H "$AUTH_HEADER" "$JIRA_API/myself" 2>/dev/null) | |
# Check if authentication was successful | |
if ! echo "$AUTH_TEST" | jq -e . >/dev/null 2>&1; then | |
echo "Authentication failed. Please check your JIRA_PAT." | |
exit 1 | |
fi | |
fi | |
# Create JIRA issue | |
echo "Creating JIRA issue in project: $JIRA_PROJECT" | |
JIRA_ISSUE_DATA=$(jq -n \ | |
--arg project "$JIRA_PROJECT" \ | |
--arg summary "$PR_TITLE" \ | |
--arg description "GitHub Pull Request: $PR_URL" \ | |
'{ | |
fields: { | |
project: {key: $project}, | |
summary: $summary, | |
description: $description, | |
issuetype: {name: "Task"} | |
} | |
}' | |
) | |
# Debug: show the payload | |
debug "Request payload:" | |
debug "$(echo "$JIRA_ISSUE_DATA" | jq .)" | |
# Make the actual request | |
if $VERBOSE; then | |
# Verbose curl output with headers | |
JIRA_RESPONSE=$(curl -v -S -X POST "$JIRA_API/issue" \ | |
-H "$AUTH_HEADER" \ | |
-H "Content-Type: application/json" \ | |
-d "$JIRA_ISSUE_DATA" 2>&1) | |
debug "Full JIRA API Response (including HTTP headers):" | |
debug "$JIRA_RESPONSE" | |
else | |
# Just get the JSON response | |
JIRA_RESPONSE=$(curl -s -X POST "$JIRA_API/issue" \ | |
-H "$AUTH_HEADER" \ | |
-H "Content-Type: application/json" \ | |
-d "$JIRA_ISSUE_DATA") | |
fi | |
# Process the response properly | |
if echo "$JIRA_RESPONSE" | jq -e . >/dev/null 2>&1; then | |
# Response is valid JSON | |
JIRA_KEY=$(echo "$JIRA_RESPONSE" | jq -r .key 2>/dev/null) | |
else | |
# Extract JSON part from verbose output if needed | |
debug "Attempting to extract JSON from verbose output..." | |
JSON_RESPONSE=$(echo "$JIRA_RESPONSE" | grep -o '{.*}' | tail -1) | |
if [ -n "$JSON_RESPONSE" ]; then | |
debug "Extracted JSON:" | |
debug "$JSON_RESPONSE" | |
JIRA_KEY=$(echo "$JSON_RESPONSE" | jq -r .key 2>/dev/null) | |
else | |
JIRA_KEY="" | |
fi | |
fi | |
if [ -z "$JIRA_KEY" ] || [ "$JIRA_KEY" == "null" ]; then | |
echo "Failed to create JIRA issue or extract JIRA key from response." | |
exit 1 | |
else | |
echo "Successfully created JIRA issue: $JIRA_KEY" | |
echo "JIRA issue URL: https://issues.apache.org/jira/browse/$JIRA_KEY" | |
fi | |
# Update PR title and description using gh cli | |
echo "Updating PR title to include JIRA key: $JIRA_KEY" | |
NEW_PR_TITLE="[$JIRA_KEY] $PR_TITLE" | |
NEW_PR_BODY="JIRA issue: [$JIRA_KEY](https://issues.apache.org/jira/browse/$JIRA_KEY) | |
$PR_BODY" | |
if gh api -X PATCH "repos/$REPO_NAME/pulls/$PR_NUMBER" \ | |
-f title="$NEW_PR_TITLE" \ | |
-f body="$NEW_PR_BODY" > /dev/null 2>&1; then | |
echo "Successfully updated PR title and description" | |
else | |
echo "Failed to update PR title and description" | |
exit 1 | |
fi | |
echo "Script completed successfully." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment