diff --git a/.github/workflows/post_jira_comment.yml b/.github/workflows/post_jira_comment.yml new file mode 100644 index 00000000..65056a73 --- /dev/null +++ b/.github/workflows/post_jira_comment.yml @@ -0,0 +1,54 @@ +name: post_jira_comment + +on: + workflow_call: + inputs: + jira_id: + type: string + required: true + default: "" + comment: + type: string + required: true + default: "" + +jobs: + post_jira_comment: + runs-on: ubuntu-latest + steps: + - name: Post JIRA Comment + id: post_jira_comment + env: + JIRA_USERNAME: ${{ secrets.JIRA_USERNAME }} + JIRA_PASSWORD: ${{ secrets.JIRA_PASSWORD }} + run: | + echo "Received JIRA ID: ${{ inputs.jira_id }}" + echo "Received COMMENT: ${{ inputs.comment }}" + + # Define the JIRA comment API endpoint + JIRA_API_URL="https://digital-vic.atlassian.net/rest/api/2/issue/${{ inputs.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\": \"${{ inputs.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 ${{ inputs.jira_id }}" + echo "Response Body: $response_body" + else + echo "Failed to post comment to Jira issue ${{ inputs.jira_id }}. HTTP Code: $http_code" + echo "Response Body: $response_body" + + echo '### ⚠️ Failed to post comment to Jira' >> $GITHUB_STEP_SUMMARY + echo "#### Issue ID: ${{ inputs.jira_id }}" >> $GITHUB_STEP_SUMMARY + echo "#### HTTP Code: $http_code" >> $GITHUB_STEP_SUMMARY + echo "#### Response Body: $response_body" >> $GITHUB_STEP_SUMMARY + + fi diff --git a/.github/workflows/test_post_jira.yml b/.github/workflows/test_post_jira.yml new file mode 100644 index 00000000..f30ded4b --- /dev/null +++ b/.github/workflows/test_post_jira.yml @@ -0,0 +1,73 @@ +name: test_post_jira + +on: + workflow_call: + inputs: + status: + type: string + required: true + default: "" + +jobs: + test_prepare_jira: + runs-on: ubuntu-latest + outputs: + jira_id: ${{ steps.extract_jira_id.outputs.jira_id }} + comment: ${{ steps.prepare_jira_comment.outputs.comment }} + 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" + else + echo "Extracted JIRA ID: $JIRA_ID" + echo "JIRA_ID=$JIRA_ID" >> $GITHUB_ENV + echo "jira_id=$JIRA_ID" >> "$GITHUB_OUTPUT" + fi + + - name: Prepare JIRA Comment + id: prepare_jira_comment + if: env.JIRA_ID != '' + env: + WORKFLOW_LINK: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" # Link to the workflow run + run: | + echo "Preparing JIRA comment..." + + # Determine the branch name from the Git reference + GIT_BRANCH=${GITHUB_REF##*/} + + # Construct the comment template + COMMENT="E2E Test - ${{ inputs.status }} on branch ${GIT_BRANCH}\n" + COMMENT+="Workflow URL: $WORKFLOW_LINK" + + # Print the comment to the console for debugging + echo "comment=$COMMENT" >> "$GITHUB_OUTPUT" + echo "$COMMENT" + echo "${{ inputs.status }}" + + post_jira_comment: + uses: ./.github/workflows/post_jira_comment.yml + needs: [test_prepare_jira] + secrets: inherit + with: + jira_id: ${{ needs.test_prepare_jira.outputs.jira_id }} + comment: ${{ needs.test_prepare_jira.outputs.comment }}