|
| 1 | +name: API Compatibility Check |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "pull-request/[0-9]+" |
| 7 | + paths: |
| 8 | + - 'megatron/core/**/*.py' |
| 9 | + - '!megatron/core/tests/**' |
| 10 | + - '!megatron/legacy/**' |
| 11 | + |
| 12 | + # Allow manual trigger |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + baseline: |
| 16 | + description: 'Baseline git reference (tag/branch/commit)' |
| 17 | + required: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + check-compatibility: |
| 21 | + name: Check API Backward Compatibility |
| 22 | + runs-on: ubuntu-latest |
| 23 | + |
| 24 | + # ============================================================================ |
| 25 | + # Configuration Parameters (modify here) |
| 26 | + # ============================================================================ |
| 27 | + env: |
| 28 | + # Default baseline for automatic PR checks |
| 29 | + # Can be: branch name (e.g., 'main'), commit hash, or tag |
| 30 | + # Will be resolved to commit hash during execution |
| 31 | + DEFAULT_BASELINE: '712dff880cdf88e51289ad71e47d92f46d25a2d3' |
| 32 | + # Tag pattern for auto-detection (e.g., 'core_r*', 'core_v*') |
| 33 | + TAG_PATTERN: 'core_v*' |
| 34 | + # Tag regex filter (e.g., '^core_v[0-9]+\.[0-9]+\.[0-9]+$' for stable versions only) |
| 35 | + TAG_REGEX_FILTER: '^core_v[0-9]+\.[0-9]+\.[0-9]+$' |
| 36 | + # ============================================================================ |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Checkout code |
| 40 | + uses: actions/checkout@v4 |
| 41 | + with: |
| 42 | + fetch-depth: 0 # Need full history to access baseline ref |
| 43 | + |
| 44 | + - name: Set up Python |
| 45 | + uses: actions/setup-python@v5 |
| 46 | + with: |
| 47 | + python-version: '3.12' |
| 48 | + |
| 49 | + - name: Install griffe |
| 50 | + run: | |
| 51 | + python -m pip install --upgrade pip |
| 52 | + python -m pip install griffe |
| 53 | + python -c "import griffe; print('Griffe installed successfully')" |
| 54 | + python -c "from griffe import Object; print('Object import successful')" || echo "Object import from griffe failed" |
| 55 | + python -c "from griffe.dataclasses import Object; print('Object import from dataclasses successful')" || echo "Object import from dataclasses failed" |
| 56 | + |
| 57 | + - name: Determine baseline reference |
| 58 | + id: baseline |
| 59 | + run: | |
| 60 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 61 | + # Use manually specified baseline (branch, tag, or commit hash) |
| 62 | + BASELINE_REF="${{ github.event.inputs.baseline }}" |
| 63 | + else |
| 64 | + # Use the configured default baseline |
| 65 | + BASELINE_REF="${{ env.DEFAULT_BASELINE }}" |
| 66 | + |
| 67 | + # Uncomment below to auto-detect from tags instead: |
| 68 | + # BASELINE_REF=$(git tag -l '${{ env.TAG_PATTERN }}' | grep -E '${{ env.TAG_REGEX_FILTER }}' | sort -V | tail -1) |
| 69 | + # if [ -z "$BASELINE_REF" ]; then |
| 70 | + # echo "Warning: No tags matching pattern found. Using default: ${{ env.DEFAULT_BASELINE }}" >&2 |
| 71 | + # BASELINE_REF="${{ env.DEFAULT_BASELINE }}" |
| 72 | + # fi |
| 73 | + fi |
| 74 | + |
| 75 | + # Resolve baseline to commit hash (works for branches, tags, or commit hashes) |
| 76 | + BASELINE_HASH=$(git rev-parse "$BASELINE_REF") |
| 77 | + |
| 78 | + echo "baseline=$BASELINE_HASH" >> $GITHUB_OUTPUT |
| 79 | + echo "Using baseline: $BASELINE_REF (resolved to commit: $BASELINE_HASH)" |
| 80 | + |
| 81 | + - name: Run compatibility check |
| 82 | + id: compat_check |
| 83 | + run: | |
| 84 | + # Save output to file for later display |
| 85 | + python scripts/check_api_backwards_compatibility.py \ |
| 86 | + --baseline ${{ steps.baseline.outputs.baseline }} \ |
| 87 | + --verbose 2>&1 | tee compat_check_output.txt |
| 88 | + |
| 89 | + # Capture exit code |
| 90 | + EXIT_CODE=${PIPESTATUS[0]} |
| 91 | + echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT |
| 92 | + exit $EXIT_CODE |
| 93 | + continue-on-error: true |
| 94 | + |
| 95 | + - name: Fail job if breaking changes detected |
| 96 | + if: steps.compat_check.outcome == 'failure' |
| 97 | + run: | |
| 98 | + echo "" |
| 99 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 100 | + echo "🔍 WHAT IS THIS CHECK?" |
| 101 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 102 | + echo "" |
| 103 | + echo "This check ensures that changes to Megatron Core's public API do not" |
| 104 | + echo "break backward compatibility for users. It compares your PR against" |
| 105 | + echo "the latest stable release to detect breaking changes in:" |
| 106 | + echo "" |
| 107 | + echo " • Function signatures (parameters, order, types)" |
| 108 | + echo " • Class structures and methods" |
| 109 | + echo " • Return types and public interfaces" |
| 110 | + echo "" |
| 111 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 112 | + echo "🛠️ HOW TO FIX THIS" |
| 113 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 114 | + echo "" |
| 115 | + echo "Choose ONE of these resolution strategies:" |
| 116 | + echo "" |
| 117 | + echo "1️⃣ REVERT THE BREAKING CHANGE (Recommended)" |
| 118 | + echo " → Modify your code to preserve backward compatibility" |
| 119 | + echo " → Add new parameters as optional (with defaults)" |
| 120 | + echo " → Keep existing parameters in the same order" |
| 121 | + echo "" |
| 122 | + echo "2️⃣ MARK AS INTERNAL API (If this is internal code)" |
| 123 | + echo " → Add @internal_api decorator from megatron.core.utils" |
| 124 | + echo "" |
| 125 | + echo " Example (for classes):" |
| 126 | + echo " from megatron.core.utils import internal_api" |
| 127 | + echo "" |
| 128 | + echo " @internal_api" |
| 129 | + echo " class ExperimentalFeature:" |
| 130 | + echo " pass" |
| 131 | + echo "" |
| 132 | + echo " Example (for functions):" |
| 133 | + echo " from megatron.core.utils import internal_api" |
| 134 | + echo "" |
| 135 | + echo " @internal_api" |
| 136 | + echo " def internal_helper_function():" |
| 137 | + echo " pass" |
| 138 | + echo "" |
| 139 | + echo "3️⃣ USE DEPRECATION (For gradual API changes)" |
| 140 | + echo " → Add @deprecated decorator for transition period" |
| 141 | + echo " → Example:" |
| 142 | + echo " from megatron.core.utils import deprecated" |
| 143 | + echo "" |
| 144 | + echo " @deprecated(version='1.0', removal_version='2.0'," |
| 145 | + echo " alternative='new_function')" |
| 146 | + echo " def old_function():" |
| 147 | + echo " pass" |
| 148 | + echo "" |
| 149 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 150 | + echo "📋 BREAKING CHANGES DETECTED" |
| 151 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 152 | + echo "" |
| 153 | + cat compat_check_output.txt |
| 154 | + echo "" |
| 155 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 156 | + echo "📚 MORE INFORMATION" |
| 157 | + echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 158 | + echo "" |
| 159 | + echo "📖 Full documentation: docs/api-backwards-compatibility-check.md" |
| 160 | + echo "🔧 Checker script: scripts/check_api_backwards_compatibility.py" |
| 161 | + echo "❓ Questions? Check the docs or ask in #megatron-core" |
| 162 | + echo "" |
| 163 | + |
| 164 | + echo "::error::Breaking API changes detected. Please review the output above and choose a resolution strategy." |
| 165 | + exit 1 |
| 166 | + |
| 167 | + - name: Success message |
| 168 | + if: steps.compat_check.outcome == 'success' |
| 169 | + run: | |
| 170 | + echo "::notice::✅ No breaking API changes detected!" |
| 171 | +
|
0 commit comments