-
Notifications
You must be signed in to change notification settings - Fork 175
124 lines (105 loc) · 5.14 KB
/
ticket-issue-number-sync.yml
File metadata and controls
124 lines (105 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
name: ticket-issue-number-sync
on:
pull_request:
types: [opened, synchronize]
jobs:
ticket-issue-number-sync:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Extract JIRA ticket number from PR title
id: extract_ticket
run: |
# Extract JIRA ticket number from PR title (e.g., "fix(BA-1234): aaaa" -> "BA-1234")
TITLE="${{ github.event.pull_request.title }}"
echo "PR Title: $TITLE"
# Extract ticket number using regex pattern BA-XXXX format
TICKET_NUMBER=$(echo "$TITLE" | grep -oE 'BA-[0-9]+' | head -1)
if [ -n "$TICKET_NUMBER" ]; then
echo "Extracted ticket: $TICKET_NUMBER"
echo "ticket_number=$TICKET_NUMBER" >> $GITHUB_OUTPUT
echo "has_ticket=true" >> $GITHUB_OUTPUT
else
echo "No JIRA ticket number found in PR title"
echo "has_ticket=false" >> $GITHUB_OUTPUT
fi
- name: Get GitHub Issue URL from JIRA
if: steps.extract_ticket.outputs.has_ticket == 'true'
id: get_jira_issue
env:
JIRA_BASE_URL: ${{ secrets.JIRA_BASE_URL }}
JIRA_USERNAME: ${{ secrets.JIRA_USERNAME }}
JIRA_API_TOKEN: ${{ secrets.JIRA_API_TOKEN }}
run: |
TICKET_NUMBER="${{ steps.extract_ticket.outputs.ticket_number }}"
echo "Fetching GitHub Issue URL for JIRA ticket: $TICKET_NUMBER"
# Get ticket information through JIRA API
JIRA_RESPONSE=$(curl -s -u "$JIRA_USERNAME:$JIRA_API_TOKEN" \
-H "Accept: application/json" \
"$JIRA_BASE_URL/rest/api/3/issue/$TICKET_NUMBER")
echo "JIRA API Response: $JIRA_RESPONSE"
# Extract issue number from GitHub Issue URL field
# Search for fields containing GitHub issue URLs (customfield_* or regular fields)
GITHUB_ISSUE_URL=$(echo "$JIRA_RESPONSE" | jq -r '
.fields
| to_entries[]
| select(.value != null and .value != "")
| select(
(.value | type == "string" and test("github.com/.*/issues/[0-9]+"))
)
| .value
' 2>/dev/null | head -1)
# Fail immediately if GitHub Issue URL is not found
if [ -z "$GITHUB_ISSUE_URL" ] || [ "$GITHUB_ISSUE_URL" = "null" ]; then
echo "❌ No GitHub Issue URL found in JIRA ticket $TICKET_NUMBER"
echo "Available fields (first 20):"
echo "$JIRA_RESPONSE" | jq -r '.fields | keys[]' 2>/dev/null | head -20
exit 1
fi
echo "Found GitHub Issue URL: $GITHUB_ISSUE_URL"
# Extract GitHub issue number (e.g., https://github.com/owner/repo/issues/123 -> 123)
GITHUB_ISSUE_NUMBER=$(echo "$GITHUB_ISSUE_URL" | grep -oE '/issues/[0-9]+' | grep -oE '[0-9]+' | head -1)
if [ -n "$GITHUB_ISSUE_NUMBER" ]; then
echo "✅ Extracted GitHub Issue Number: $GITHUB_ISSUE_NUMBER"
echo "github_issue_number=$GITHUB_ISSUE_NUMBER" >> $GITHUB_OUTPUT
echo "has_github_issue=true" >> $GITHUB_OUTPUT
else
echo "❌ Could not extract issue number from GitHub Issue URL: $GITHUB_ISSUE_URL"
echo "Please verify the GitHub Issue URL format is correct (expected: https://github.com/owner/repo/issues/NUMBER)"
exit 1
fi
- name: Update PR body with resolves clause
if: steps.extract_ticket.outputs.has_ticket == 'true' && steps.get_jira_issue.outputs.has_github_issue == 'true'
env:
TICKET_NUMBER: ${{ steps.extract_ticket.outputs.ticket_number }}
GITHUB_ISSUE_NUMBER: ${{ steps.get_jira_issue.outputs.github_issue_number }}
run: |
echo "Using GitHub issue number: $GITHUB_ISSUE_NUMBER from JIRA"
# Get current PR body
PR_BODY=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" | \
jq -r '.body // ""')
echo "Current PR body: $PR_BODY"
# Generate resolves clause
RESOLVES_CLAUSE="resolves #$GITHUB_ISSUE_NUMBER ($TICKET_NUMBER)"
echo "Generated resolves clause: $RESOLVES_CLAUSE"
# Replace existing resolves clause or add new one
if echo "$PR_BODY" | grep -qiE "resolves #[0-9]+ ?\("; then
# Replace existing resolves clause (case-insensitive)
NEW_BODY=$(echo "$PR_BODY" | sed -E "s/[Rr]esolves #[0-9]+ ?\([^)]+\)/$RESOLVES_CLAUSE/g")
echo "Replaced existing resolves clause"
else
# Add new resolves clause at the beginning
NEW_BODY=$(printf "%s\n\n%s" "$RESOLVES_CLAUSE" "$PR_BODY")
echo "Added new resolves clause"
fi
# Update PR body
curl -s -X PATCH \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" \
-d "{\"body\": $(echo "$NEW_BODY" | jq -R -s .)}"
echo "✅ Added resolves clause: $RESOLVES_CLAUSE"