|
| 1 | +name: Check Code Changes |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + event_name: |
| 7 | + required: true |
| 8 | + type: string |
| 9 | + # For pull_request, base_sha is github.event.pull_request.base.sha (target branch tip) |
| 10 | + # For push, base_sha is github.event.before |
| 11 | + base_sha: |
| 12 | + required: true |
| 13 | + type: string |
| 14 | + # For pull_request, head_sha is github.event.pull_request.head.sha (PR branch tip) |
| 15 | + # For push, head_sha is github.sha |
| 16 | + head_sha: |
| 17 | + required: true |
| 18 | + type: string |
| 19 | + outputs: |
| 20 | + has_code_changes: |
| 21 | + description: "True if non-markdown code files were changed or event is workflow_dispatch/schedule, false otherwise." |
| 22 | + value: ${{ jobs.check_files.outputs.has_code_changes }} |
| 23 | + |
| 24 | +jobs: |
| 25 | + check_files: |
| 26 | + runs-on: ubuntu-24.04 |
| 27 | + outputs: |
| 28 | + has_code_changes: ${{ steps.perform_check.outputs.has_code_changes }} |
| 29 | + steps: |
| 30 | + - name: Checkout code for diff (if needed) |
| 31 | + # Checkout only if a diff is actually needed |
| 32 | + if: inputs.event_name != 'workflow_dispatch' && inputs.event_name != 'schedule' |
| 33 | + uses: actions/checkout@v4 |
| 34 | + with: |
| 35 | + # Fetch all history for all branches and tags. |
| 36 | + # This is necessary for `git diff A...B` (three-dot diff) to find the merge base |
| 37 | + # and correctly diff PR changes against the point where it diverged. |
| 38 | + # It's also needed for `git diff A B` if A and B are far apart. |
| 39 | + fetch-depth: 0 |
| 40 | + |
| 41 | + - name: Perform file content check |
| 42 | + id: perform_check |
| 43 | + run: | |
| 44 | + echo "Event Name: ${{ inputs.event_name }}" |
| 45 | + echo "Base SHA input (for PR: target branch; for Push: before SHA): ${{ inputs.base_sha }}" |
| 46 | + echo "Head SHA input (for PR: PR head; for Push: current SHA): ${{ inputs.head_sha }}" |
| 47 | +
|
| 48 | + # Handle workflow_dispatch and schedule events first |
| 49 | + if [[ "${{ inputs.event_name }}" == "workflow_dispatch" || "${{ inputs.event_name }}" == "schedule" ]]; then |
| 50 | + echo "Event is ${{ inputs.event_name }}. Assuming code changes or full run needed." |
| 51 | + echo "has_code_changes=true" >> "$GITHUB_OUTPUT" |
| 52 | + exit 0 # Exit early, no diff needed |
| 53 | + fi |
| 54 | +
|
| 55 | + # Handle initial push (base SHA is all zeros) |
| 56 | + # For an initial push, all files in the head_sha are considered "changed" (new). |
| 57 | + if [[ "${{ inputs.base_sha }}" == "0000000000000000000000000000000000000000" ]]; then |
| 58 | + echo "Initial push (base SHA is zeros). Assuming code changes." |
| 59 | + # We can list all files in the current commit (inputs.head_sha) if needed, |
| 60 | + # but for simplicity, just assuming code changes is often sufficient. |
| 61 | + # To be precise, one could do: git ls-tree -r --name-only ${{ inputs.head_sha }} > changed_files.txt |
| 62 | + # And then apply the markdown filter. For now, we'll assume changes. |
| 63 | + echo "has_code_changes=true" >> "$GITHUB_OUTPUT" |
| 64 | + exit 0 |
| 65 | + fi |
| 66 | +
|
| 67 | + # Handle cases where base and head are the same (e.g., re-run on a specific commit, or a push with no new commits) |
| 68 | + # This can happen if a workflow is re-run, or if a branch is pushed without new commits (e.g., force push to same SHA). |
| 69 | + if [[ "${{ inputs.base_sha }}" == "${{ inputs.head_sha }}" ]]; then |
| 70 | + echo "Base SHA is the same as Head SHA. No file changes. Assuming no code changes for skipping purposes." |
| 71 | + echo "has_code_changes=false" >> "$GITHUB_OUTPUT" |
| 72 | + exit 0 |
| 73 | + fi |
| 74 | +
|
| 75 | + # Ensure SHAs are valid before attempting diff |
| 76 | + # (git rev-parse --verify will exit with non-zero if SHA is not found) |
| 77 | + git rev-parse --verify ${{ inputs.base_sha }}^{commit} >/dev/null 2>&1 || { echo "Error: Base SHA ${{ inputs.base_sha }} not found or invalid."; exit 1; } |
| 78 | + git rev-parse --verify ${{ inputs.head_sha }}^{commit} >/dev/null 2>&1 || { echo "Error: Head SHA ${{ inputs.head_sha }} not found or invalid."; exit 1; } |
| 79 | +
|
| 80 | +
|
| 81 | + # Determine the diff command based on the event type |
| 82 | + if [[ "${{ inputs.event_name }}" == "pull_request" ]]; then |
| 83 | + # For pull requests, use three-dot diff (A...B). |
| 84 | + # This shows changes on the PR branch (inputs.head_sha) |
| 85 | + # since it diverged from the target branch (inputs.base_sha). |
| 86 | + # inputs.base_sha is github.event.pull_request.base.sha |
| 87 | + # inputs.head_sha is github.event.pull_request.head.sha |
| 88 | + echo "Pull Request: Diffing ${{ inputs.base_sha }}...${{ inputs.head_sha }}" |
| 89 | + git diff --name-only --no-renames ${{ inputs.base_sha }}...${{ inputs.head_sha }} > changed_files.txt |
| 90 | + else # For 'push' and potentially other events not explicitly handled above |
| 91 | + # For pushes, use two-dot diff (A B). |
| 92 | + # inputs.base_sha is github.event.before |
| 93 | + # inputs.head_sha is github.sha |
| 94 | + echo "Push or other event: Diffing ${{ inputs.base_sha }} ${{ inputs.head_sha }}" |
| 95 | + git diff --name-only --no-renames ${{ inputs.base_sha }} ${{ inputs.head_sha }} > changed_files.txt |
| 96 | + fi |
| 97 | +
|
| 98 | + echo "Changed files:" |
| 99 | + cat changed_files.txt |
| 100 | +
|
| 101 | + if [ ! -s changed_files.txt ]; then # Check if changed_files.txt is empty |
| 102 | + echo "No files changed in the diff." |
| 103 | + echo "has_code_changes=false" >> "$GITHUB_OUTPUT" |
| 104 | + elif grep -q -v -E '\.md$' changed_files.txt; then |
| 105 | + echo "Non-markdown code changes detected." |
| 106 | + echo "has_code_changes=true" >> "$GITHUB_OUTPUT" |
| 107 | + else |
| 108 | + echo "Only markdown changes detected or no non-markdown changes found in diff." |
| 109 | + echo "has_code_changes=false" >> "$GITHUB_OUTPUT" |
| 110 | + fi |
| 111 | + shell: bash |
0 commit comments