-
-
Save slachiewicz/868db7276c3b0aab71166edc9172b4d2 to your computer and use it in GitHub Desktop.
Script creating a JIRA issue for a given PR
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 | |
# 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_TOKEN" ]; then | |
echo "Error: JIRA_TOKEN environment variable is not set." | |
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" | |
GITHUB_API="https://api.github.com/repos/$REPO_NAME/pulls/$PR_NUMBER" | |
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" | |
# 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... in project: $JIRA_PROJECT" | |
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" | |
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 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" | |
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