|
1 | 1 | #!/bin/bash |
2 | 2 |
|
| 3 | +# ============================================================================== |
| 4 | +# Executes When: |
| 5 | +# - Run by GitHub Actions workflow: .github/workflows/pr-check-changelog.yml |
| 6 | +# - Triggers: workflow_dispatch (manual) and pull_request (opened, edited, synch). |
| 7 | +# |
| 8 | +# Goal: |
| 9 | +# It acts as a gatekeeper for Pull Requests, blocking any merge unless the user |
| 10 | +# has added a new entry to CHANGELOG.md and correctly placed it under the |
| 11 | +# [Unreleased] section with a proper category subtitle. |
| 12 | +# |
| 13 | +# ------------------------------------------------------------------------------ |
| 14 | +# Flow: Basic Idea |
| 15 | +# 1. Grabs the official blueprints (upstream/main) to compare against current work. |
| 16 | +# 2. Checks if anything new was written. If not, fails immediately. |
| 17 | +# 3. Walks through the file line-by-line to ensure new notes are strictly filed |
| 18 | +# under [Unreleased] and organized under a category (e.g., "Added", "Fixed"). |
| 19 | +# 4. If notes are missing, misplaced, or dangling, it fails the build. |
| 20 | +# If filed correctly, it approves the build. |
| 21 | +# |
| 22 | +# ------------------------------------------------------------------------------ |
| 23 | +# Flow: Detailed Technical Steps |
| 24 | +# |
| 25 | +# 1️⃣ Network Setup & Fetch |
| 26 | +# - Action: Sets up a remote connection to GitHub and runs 'git fetch upstream main'. |
| 27 | +# - Why: Needs the "Source of Truth" to compare the Pull Request against. |
| 28 | +# |
| 29 | +# 2️⃣ Diff Analysis & Visualization |
| 30 | +# - Action: Runs 'git diff upstream/main -- CHANGELOG.md'. |
| 31 | +# - UX/Display: Prints raw diff with colors (Green=Additions, Red=Deletions) |
| 32 | +# strictly for human readability in logs; logic does not rely on colors. |
| 33 | +# - Logic: Extracts two lists: |
| 34 | +# * added_bullets: Every line starting with '+' (new text). |
| 35 | +# * deleted_bullets: Every line starting with '-' (removed text). |
| 36 | +# - Immediate Fail Check: If 'added_bullets' is empty, sets failed=1 and exits. |
| 37 | +# (You cannot merge code without a changelog entry). |
| 38 | +# |
| 39 | +# 3️⃣ Context Tracking |
| 40 | +# As the script reads the file line-by-line, it tracks: |
| 41 | +# - current_release: Main version header (e.g., [Unreleased] or [1.0.0]). |
| 42 | +# - current_subtitle: Sub-category (e.g., ### Added, ### Fixed). |
| 43 | +# - in_unreleased: Flag (0 or 1). |
| 44 | +# * 1 (True) -> Currently inside [Unreleased] (Safe Zone). |
| 45 | +# * 0 (False) -> Reading an old version (Danger Zone). |
| 46 | +# |
| 47 | +# 4️⃣ Sorting |
| 48 | +# Flag is ON (1) AND Subtitle is Set -> correctly_placed -> PASS ✅ |
| 49 | +# Flag is ON (1) BUT Subtitle is Empty -> orphan_entries -> FAIL ❌ (It's dangling, not under a category) |
| 50 | +# Flag is OFF (0) -> wrong_release_entries -> FAIL ❌ (edited old history) |
| 51 | +# |
| 52 | +# 5️⃣ Final Result |
| 53 | +# Aggregates failures from Step 4. If any FAIL buckets are not empty, exit 1. |
| 54 | +# |
| 55 | +# ------------------------------------------------------------------------------ |
| 56 | +# Parameters: |
| 57 | +# None. (The script accepts no command-line arguments). |
| 58 | +# |
| 59 | +# Environment Variables (Required): |
| 60 | +# - GITHUB_REPOSITORY: Used to fetch the upstream 'main' branch for comparison. |
| 61 | +# |
| 62 | +# Dependencies: |
| 63 | +# - git (must be able to fetch upstream) |
| 64 | +# - grep, sed (standard Linux utilities) |
| 65 | +# - CHANGELOG.md (file must exist in the root directory) |
| 66 | +# |
| 67 | +# Permissions: |
| 68 | +# - 'contents: read' (to access the file structure). |
| 69 | +# - Network access (to run 'git fetch upstream'). |
| 70 | +# |
| 71 | +# Returns: |
| 72 | +# 0 (Success) - Changes are valid and correctly placed. |
| 73 | +# 1 (Failure) - Missing entries, wrong placement (e.g. under released version), |
| 74 | +# orphan entries (no subtitle), or accidental deletions. |
| 75 | +# ============================================================================== |
| 76 | + |
3 | 77 | CHANGELOG="CHANGELOG.md" |
4 | 78 |
|
5 | 79 | # ANSI color codes |
|
0 commit comments