Skip to content

Instantly share code, notes, and snippets.

@gnodet
Created September 30, 2024 20:07
Show Gist options
  • Save gnodet/f70c42d2c2e98526f783f5fc4083aaa6 to your computer and use it in GitHub Desktop.
Save gnodet/f70c42d2c2e98526f783f5fc4083aaa6 to your computer and use it in GitHub Desktop.
create-jira-link-pr.sh
#!/bin/bash
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "Error: jq is not installed. Please install jq to run this script."
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 "$GITHUB_TOKEN" ]; then
echo "Error: GITHUB_TOKEN environment variable is not set."
exit 1
fi
if [ -z "$JIRA_TOKEN" ]; then
echo "Error: JIRA_TOKEN environment variable is not set."
exit 1
fi
PR_NUMBER=$1
GITHUB_API="https://api.github.com/repos/apache/maven/pulls/$PR_NUMBER"
JIRA_API="https://issues.apache.org/jira/rest/api/2"
JIRA_PROJECT="MNG"
echo "Fetching PR details from GitHub..."
PR_DATA=$(curl -s -H "Authorization: token $GITHUB_TOKEN" $GITHUB_API)
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
# Create JIRA issue
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"}
}
}'
)
echo "Creating JIRA issue..."
JIRA_RESPONSE=$(curl -s -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $JIRA_TOKEN" --data "$JIRA_ISSUE_DATA" $JIRA_API/issue)
JIRA_KEY=$(echo "$JIRA_RESPONSE" | jq -r .key)
if [ -z "$JIRA_KEY" ] || [ "$JIRA_KEY" == "null" ]; then
echo "Failed to create JIRA issue. Error message:"
echo "$JIRA_RESPONSE" | jq -r '.errorMessages[]? // empty'
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
NEW_PR_TITLE="[$JIRA_KEY] $PR_TITLE"
NEW_PR_BODY="[JIRA issue $JIRA_KEY](https://issues.apache.org/jira/browse/$JIRA_KEY)
$PR_BODY"
UPDATE_PR_DATA=$(jq -n \
--arg title "$NEW_PR_TITLE" \
--arg body "$NEW_PR_BODY" \
'{title: $title, body: $body}'
)
echo "Updating PR title and description..."
UPDATE_PR_RESPONSE=$(curl -s -X PATCH -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" --data "$UPDATE_PR_DATA" $GITHUB_API)
# Check if PR was updated successfully
if [ "$(echo "$UPDATE_PR_RESPONSE" | jq 'has("title") and has("body")')" = "true" ]; then
echo "Successfully updated PR title and description"
else
echo "Failed to update PR. Error message:"
echo "$UPDATE_PR_RESPONSE" | jq -r '.message'
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