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: notify_jira | ||
on: | ||
workflow_call: | ||
inputs: | ||
status: | ||
type: string | ||
required: true | ||
default: "" | ||
jobs: | ||
notify_jira: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Extract JIRA ID from the commit message | ||
id: extract_jira_id | ||
run: | | ||
# Fetch commit message from the GitHub API | ||
commit_info=$(curl -L \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | ||
https://api.github.com/repos/${{ github.repository }}/commits/${{ github.sha }}) | ||
# Check if curl was successful | ||
if [ $? -ne 0 ]; then | ||
echo "Failed to fetch commit message." | ||
exit 0 # Exit gracefully on curl failure | ||
fi | ||
commit_message=$(echo "$commit_info" | jq -r '.commit.message') | ||
# Extract JIRA ID | ||
JIRA_ID=$(echo "$commit_message" | grep -oE 'SCFA-[0-9]{1,5}' || true) # Prevents non-zero exit code | ||
# Check if JIRA ID is found | ||
if [ -z "$JIRA_ID" ]; then | ||
echo "No JIRA ID found in commit message: $commit_message" | ||
exit 0 # Exit gracefully without failing the job | ||
else | ||
echo "Extracted JIRA ID: $JIRA_ID" | ||
echo "JIRA_ID=$JIRA_ID" >> $GITHUB_ENV | ||
fi | ||
- name: Determine the workflow status | ||
id: determine_e2e_status | ||
if: success() && env.JIRA_ID != '' | ||
env: | ||
JIRA_ID: ${{ env.JIRA_ID }} # Use the extracted JIRA ID | ||
STATUS: ${{ inputs.status }} | ||
run: | ||
echo "Overall workflow status: $STATUS" | ||
echo "STATUS=$STATUS" >> $GITHUB_ENV | ||
- name: Prepare JIRA Comment | ||
id: prepare_jira_comment | ||
if: success() && env.JIRA_ID != '' | ||
env: | ||
JIRA_ID: ${{ env.JIRA_ID }} # Use the extracted JIRA ID | ||
WORKFLOW_LINK: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" # Link to the workflow run | ||
STATUS: ${{ env.STATUS }} | ||
run: | | ||
echo "Preparing JIRA comment..." | ||
# Determine the branch name from the Git reference | ||
GIT_BRANCH=${GITHUB_REF##*/} | ||
# Construct the comment template | ||
COMMENT="E2E Test - ${STATUS} on branch ${GIT_BRANCH}\n" | ||
COMMENT+="Workflow URL: $WORKFLOW_LINK" | ||
# Print the comment to the console for debugging | ||
echo -e "COMMENT:\n${COMMENT}" | ||
echo "COMMENT=$COMMENT" >> $GITHUB_ENV | ||
- name: Post JIRA Comment | ||
id: post_jira_comment | ||
if: success() && env.JIRA_ID != '' | ||
env: | ||
JIRA_ID: ${{ env.JIRA_ID }} # Use the extracted JIRA ID from the commit message | ||
JIRA_USERNAME: ${{ secrets.JIRA_USERNAME }} # Jira username from secrets | ||
JIRA_PASSWORD: ${{ secrets.JIRA_PASSWORD }} # Jira password from secrets | ||
COMMENT: ${{ env.COMMENT }} # The comment prepared in the previous step | ||
run: | | ||
echo -e "${JIRA_ID}" | ||
# Define the JIRA comment API endpoint | ||
JIRA_API_URL="https://digital-vic.atlassian.net/rest/api/2/issue/${JIRA_ID}/comment" | ||
# Post the comment to JIRA using basic auth with username and password | ||
response=$(curl -s -w "%{http_code}" -X POST \ | ||
-H "Content-Type: application/json" \ | ||
-u "${JIRA_USERNAME}:${JIRA_PASSWORD}" \ | ||
--data "{\"body\": \"${COMMENT}\"}" \ | ||
"${JIRA_API_URL}") | ||
http_code="${response:(-3)}" # Extract HTTP status code (last 3 characters) | ||
response_body="${response:0:-3}" # The response body | ||
# Handle the API response | ||
if [ "$http_code" -eq 201 ]; then | ||
echo "Comment posted successfully to Jira issue ${JIRA_ID}" | ||
elif [ "$http_code" -eq 404 ]; then | ||
echo "Jira issue ${JIRA_ID} not found. No comment posted." | ||
else | ||
echo "Failed to post comment to Jira issue ${JIRA_ID}. HTTP Code: $http_code" | ||
echo "Response Body: $response_body" | ||
fi |