|
| 1 | +name: jira-check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, edited, reopened, synchronize] |
| 6 | + branches: |
| 7 | + - master |
| 8 | + - stable/** |
| 9 | + |
| 10 | +permissions: |
| 11 | + pull-requests: read |
| 12 | + |
| 13 | +jobs: |
| 14 | + jira-check: |
| 15 | + name: JIRA ticket validation |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Validate JIRA reference in PR body |
| 19 | + env: |
| 20 | + PR_BODY: ${{ github.event.pull_request.body }} |
| 21 | + JIRA_PROJECT_KEY: ${{ vars.JIRA_PROJECT_KEY }} |
| 22 | + run: | |
| 23 | + set -euo pipefail |
| 24 | +
|
| 25 | + if [[ -z "${JIRA_PROJECT_KEY:-}" ]]; then |
| 26 | + echo "::error::Repository variable JIRA_PROJECT_KEY is not set." |
| 27 | + exit 1 |
| 28 | + fi |
| 29 | +
|
| 30 | + if [[ -z "${PR_BODY:-}" ]]; then |
| 31 | + echo "::error::PR body is empty. Please use the PR template and provide a JIRA ticket under '## Project tracking'." |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | +
|
| 35 | + # Extract the "Project tracking" section (between its heading and the next ## heading). |
| 36 | + SECTION=$(printf '%s\n' "$PR_BODY" | sed -n '/^## Project tracking/,/^## /{/^## Project tracking/d;/^## /d;p}') |
| 37 | +
|
| 38 | + if [[ -z "$SECTION" ]]; then |
| 39 | + echo "::error::Could not find the '## Project tracking' section in the PR body. Please use the PR template." |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +
|
| 43 | + if grep -iqE "${JIRA_PROJECT_KEY}-[0-9]+" <<<"$SECTION"; then |
| 44 | + echo "Found JIRA reference in Project tracking section." |
| 45 | + exit 0 |
| 46 | + fi |
| 47 | +
|
| 48 | + # No JIRA found — check for the override checkbox. |
| 49 | + if grep -qE '^\s*- \[[xX]\]' <<<"$SECTION"; then |
| 50 | + # Checkbox is checked. Now verify an explanation exists after the checkbox line. |
| 51 | + # Grab all lines after the checkbox, strip HTML comments and blank lines. |
| 52 | + EXPLANATION=$(printf '%s\n' "$SECTION" \ |
| 53 | + | sed -n '/^\s*- \[[xX]\]/,$ p' \ |
| 54 | + | tail -n +2 \ |
| 55 | + | sed 's/<!--.*-->//g' \ |
| 56 | + | grep -v '^\s*$' || true) |
| 57 | +
|
| 58 | + if [[ -n "$EXPLANATION" ]]; then |
| 59 | + echo "No JIRA required — override accepted with explanation." |
| 60 | + exit 0 |
| 61 | + else |
| 62 | + echo "::error::Override checkbox is checked but no explanation was provided. Please explain why a JIRA ticket is not needed." |
| 63 | + exit 1 |
| 64 | + fi |
| 65 | + fi |
| 66 | +
|
| 67 | + echo "::error::No JIRA ticket found in the '## Project tracking' section. Either add a JIRA reference or check the override checkbox and provide an explanation." |
| 68 | + exit 1 |
0 commit comments