LIBAAEC-33 Modify PR template body and create github actions to add Jira issue #19
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
name: Update Pull Request with Jira Issue Link | |
on: | |
pull_request: | |
types: [opened, edited, synchronize] | |
jobs: | |
update-pr: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out code | |
uses: actions/checkout@v3 | |
- name: Ensure jq is installed | |
run: | | |
if ! command -v jq &> /dev/null; then | |
echo "jq is not installed. Installing jq..." | |
sudo apt-get update | |
sudo apt-get install -y jq | |
else | |
echo "jq is already installed." | |
fi | |
- name: Extract Jira Issue from branch name | |
id: extract_branch | |
run: | | |
echo "Extracting Jira issue from branch name..." | |
branch_name=$(echo "${{ github.event.pull_request.head.ref }}" | grep -oEi 'LIBAAEC-[0-9]+') | |
if [ -z "$branch_name" ]; then | |
echo "No Jira issue found in branch name. Exiting." | |
exit 0 | |
fi | |
jira_link="https://ucdts.atlassian.net/browse/$branch_name" | |
echo "Extracted Jira Issue: $branch_name" | |
echo "Generated Jira Link: $jira_link" | |
echo "branch_name=$branch_name" >> $GITHUB_OUTPUT | |
echo "jira_link=$jira_link" >> $GITHUB_OUTPUT | |
- name: Check if Jira link exists in PR body | |
id: check_jira_link | |
run: | | |
echo "Checking if Jira link exists in the PR body..." | |
jira_link="${{ steps.extract_branch.outputs.jira_link }}" | |
if grep -qF "$jira_link" <<< "${{ github.event.pull_request.body || '' }}"; then | |
echo "Jira link already exists in the PR body." | |
echo "skip_update=true" >> "$GITHUB_OUTPUT" | |
else | |
echo "Jira link does not exist in the PR body." | |
echo "skip_update=false" >> "$GITHUB_OUTPUT" | |
fi | |
- name: Prepend Jira link and update PR description | |
if: steps.check_jira_link.outputs.skip_update == 'false' | |
run: | | |
echo "Prepending Jira link to PR body..." | |
jira_link="Jira Issue: [${{ steps.extract_branch.outputs.branch_name }}](${{ steps.extract_branch.outputs.jira_link }})" | |
# Safely read the original PR body using jq | |
original_body=$(jq -r .pull_request.body "$GITHUB_EVENT_PATH") | |
pr_body="$jira_link"$'\n\n'"$original_body" | |
# Escape the updated PR body for JSON | |
pr_body_escaped=$(echo "$pr_body" | jq -Rs .) | |
echo "Sending updated PR body to GitHub..." | |
curl -s -X PATCH -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-d "{\"body\": $pr_body_escaped}" \ | |
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" | |
echo "PR body updated successfully." |