1+ ---
2+ # FILE: .github/workflows/jira-pr-comment.yml
3+ #
4+ # This GitHub Actions workflow automatically adds a PR link comment to the corresponding Jira ticket.
5+ # It extracts the Jira issue key from the PR title (format: IEP-12345: description) and posts a comment.
6+ #
7+ # Error Handling:
8+ # - If no issue key is found in the PR title, the workflow skips silently (no failure)
9+ # - If the Jira issue doesn't exist or API fails, the workflow logs a warning but doesn't fail
10+ # - This ensures developers are never blocked by Jira integration issues
11+
12+ name : 🔗 Add PR Link to Jira
13+
14+ run-name : >
15+ Add PR Link to Jira - PR #${{ github.event.pull_request.number }}
16+
17+ on :
18+ pull_request :
19+ types : [opened, reopened]
20+
21+ jobs :
22+ add-pr-comment-to-jira :
23+ name : Add PR Comment to Jira
24+ runs-on : ubuntu-latest
25+ steps :
26+ - name : Extract Jira Issue Key from PR Title
27+ id : extract
28+ run : |
29+ TITLE="${{ github.event.pull_request.title }}"
30+ echo "PR Title: $TITLE"
31+
32+ # Match pattern: IEP-12345: description
33+ if [[ "$TITLE" =~ ^(IEP-[0-9]+): ]]; then
34+ ISSUE_KEY="${BASH_REMATCH[1]}"
35+ echo "Found Jira issue key: $ISSUE_KEY"
36+ echo "issue_key=$ISSUE_KEY" >> $GITHUB_OUTPUT
37+ echo "found=true" >> $GITHUB_OUTPUT
38+ else
39+ echo "No Jira issue key found in PR title. Expected format: IEP-12345: description"
40+ echo "Skipping Jira comment..."
41+ echo "found=false" >> $GITHUB_OUTPUT
42+ fi
43+
44+ - name : Add PR Comment to Jira Issue
45+ if : steps.extract.outputs.found == 'true'
46+ env :
47+ JIRA_USER : ${{ secrets.JIRA_USER }}
48+ JIRA_PASS : ${{ secrets.JIRA_PASS }}
49+ JIRA_URL : ${{ secrets.JIRA_URL }}
50+ ISSUE_KEY : ${{ steps.extract.outputs.issue_key }}
51+ PR_URL : ${{ github.event.pull_request.html_url }}
52+ run : |
53+ echo "Adding comment to Jira issue: $ISSUE_KEY"
54+ echo "PR URL: $PR_URL"
55+
56+ # Handle JIRA_PASS format - it may have "token:" prefix (per espressif/sync-jira-actions convention)
57+ # If prefixed with "token:", use Bearer token auth; otherwise use Basic Auth
58+ if [[ "$JIRA_PASS" == token:* ]]; then
59+ # Strip "token:" prefix and use Bearer token authentication
60+ TOKEN="${JIRA_PASS#token:}"
61+ AUTH_HEADER="Authorization: Bearer $TOKEN"
62+ echo "Using Bearer token authentication"
63+ else
64+ # Use Basic Auth with username:password
65+ AUTH_HEADER="Authorization: Basic $(echo -n "$JIRA_USER:$JIRA_PASS" | base64)"
66+ echo "Using Basic authentication"
67+ fi
68+
69+ # Make the API call and capture the response
70+ HTTP_RESPONSE=$(curl -s -w "\n%{http_code}" \
71+ -X POST \
72+ -H "Content-Type: application/json" \
73+ -H "$AUTH_HEADER" \
74+ -d "{\"body\": \"PR: $PR_URL\"}" \
75+ "$JIRA_URL/rest/api/2/issue/$ISSUE_KEY/comment")
76+
77+ # Extract HTTP status code (last line) and response body
78+ HTTP_STATUS=$(echo "$HTTP_RESPONSE" | tail -n 1)
79+ RESPONSE_BODY=$(echo "$HTTP_RESPONSE" | sed '$d')
80+
81+ echo "Jira API Response Status: $HTTP_STATUS"
82+
83+ # Check if the request was successful
84+ if [[ "$HTTP_STATUS" -ge 200 && "$HTTP_STATUS" -lt 300 ]]; then
85+ echo "Successfully added PR comment to Jira issue $ISSUE_KEY"
86+ else
87+ echo "::warning::Failed to add comment to Jira issue $ISSUE_KEY (HTTP $HTTP_STATUS)"
88+ echo "This could mean the issue doesn't exist or there's an authentication problem."
89+ echo "Response: $RESPONSE_BODY"
90+ # Don't fail the workflow - just warn
91+ echo "Continuing without failing the workflow..."
92+ fi
0 commit comments