-
Notifications
You must be signed in to change notification settings - Fork 2
130 lines (117 loc) · 5.16 KB
/
jira-id-check.yml
File metadata and controls
130 lines (117 loc) · 5.16 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
125
126
127
128
129
130
name: 🏷️ JIRA ID Validator
# Define all valid JIRA project keys for the entire organization here
env:
ORGANIZATION_JIRA_PROJECT_KEYS: "MLE,PDP,COR"
on:
# Required for repository rulesets - works for PRs from forks
pull_request_target:
types: [opened, edited, synchronize, reopened]
# Also support being called as a reusable workflow
workflow_call:
inputs:
pr-title:
required: false
type: string
description: 'The PR title to check (for pull_request_target support)'
regex-pattern:
required: false
type: string
description: 'Custom regex pattern to match JIRA IDs (defaults to "[A-Z]+-[0-9]+")'
default: '[A-Z]+-[0-9]+'
fail-if-no-jira-id:
required: false
type: string
description: 'Whether to fail the check if no JIRA ID is found'
default: 'true'
allow-wip:
required: false
type: string
description: 'Allow PR titles starting with "WIP:" without checking for JIRA ID'
default: 'false'
case-sensitive:
required: false
type: string
description: 'Whether the JIRA project key check should be case-sensitive'
default: 'true'
jobs:
check-jira-id:
runs-on: ubuntu-latest
steps:
- name: 🏷️ Validate JIRA ticket ID in PR title
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
# Get PR title from context or input parameter (for pull_request_target support)
if [[ -n "${{ inputs.pr-title }}" ]]; then
PR_TITLE="${{ inputs.pr-title }}"
echo "Using PR title from input parameter"
else
PR_TITLE="${{ github.event.pull_request.title }}"
echo "Using PR title from GitHub event context"
fi
# Fetch the current PR title from the API to handle re-runs correctly.
# When a workflow is re-run (either directly or via a reusable workflow caller),
# the event payload contains the title from the original trigger, not the current
# state. If the user edited the title (e.g., to add a JIRA ID), we need to detect
# that and use the updated title instead.
PR_NUMBER="${{ github.event.pull_request.number }}"
if [[ -n "$PR_NUMBER" ]]; then
LIVE_TITLE=$(gh pr view "$PR_NUMBER" \
--repo "${{ github.repository }}" \
--json title --jq '.title' 2>/dev/null || true)
if [[ -n "$LIVE_TITLE" && "$LIVE_TITLE" != "$PR_TITLE" ]]; then
echo "⚠️ PR title was updated since workflow was triggered"
echo " Event title: $PR_TITLE"
echo " Current title: $LIVE_TITLE"
PR_TITLE="$LIVE_TITLE"
fi
fi
echo "PR Title: $PR_TITLE"
# Set up inputs as environment variables
# JIRA project keys are defined at the organization level
JIRA_PROJECT_KEYS="${{ env.ORGANIZATION_JIRA_PROJECT_KEYS }}"
REGEX_PATTERN="${{ inputs.regex-pattern }}"
FAIL_IF_NO_JIRA_ID="${{ inputs.fail-if-no-jira-id }}"
ALLOW_WIP="${{ inputs.allow-wip }}"
CASE_SENSITIVE="${{ inputs.case-sensitive }}"
echo "Using organization-wide JIRA project keys: $JIRA_PROJECT_KEYS"
echo "Using regex pattern: $REGEX_PATTERN"
echo "Fail if no JIRA ID: $FAIL_IF_NO_JIRA_ID"
echo "Allow WIP PRs: $ALLOW_WIP"
echo "Case sensitive: $CASE_SENSITIVE"
# Handle WIP PRs
if [[ "$ALLOW_WIP" == "true" && "${PR_TITLE,,}" =~ ^wip: ]]; then
echo "This is a WIP PR. Skipping JIRA ID check."
exit 0
fi
# Convert comma-separated project keys to array
IFS=',' read -ra PROJECT_KEYS <<< "$JIRA_PROJECT_KEYS"
echo "Valid project keys: ${PROJECT_KEYS[*]}"
# Directly check for valid JIRA IDs in the PR title
VALID_ID_FOUND=false
for VALID_KEY in "${PROJECT_KEYS[@]}"; do
# Create a pattern specifically for this project key
if [[ "$CASE_SENSITIVE" == "true" ]]; then
PATTERN="$VALID_KEY-[0-9]+"
if echo "$PR_TITLE" | grep -q -E "$PATTERN"; then
FOUND_ID=$(echo "$PR_TITLE" | grep -o -E "$PATTERN" | head -1)
echo "Found JIRA ID: $FOUND_ID"
VALID_ID_FOUND=true
break
fi
else
PATTERN="$VALID_KEY-[0-9]+"
if echo "$PR_TITLE" | grep -q -i -E "$PATTERN"; then
FOUND_ID=$(echo "$PR_TITLE" | grep -o -i -E "$PATTERN" | head -1)
echo "Found JIRA ID: $FOUND_ID"
VALID_ID_FOUND=true
break
fi
fi
done
if [[ "$VALID_ID_FOUND" != "true" ]]; then
echo "ERROR: No JIRA ID found in PR title: \"$PR_TITLE\". Valid project keys are: $JIRA_PROJECT_KEYS"
echo "::error::No JIRA ID found in PR title: \"$PR_TITLE\". Valid project keys are: $JIRA_PROJECT_KEYS"
exit 1
fi