|
| 1 | +name: Fix missing final newline (CodeRabbit docstring PRs) |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request_target: |
| 5 | + types: [opened, synchronize, reopened] |
| 6 | + |
| 7 | +permissions: |
| 8 | + contents: write |
| 9 | + pull-requests: read |
| 10 | + |
| 11 | +jobs: |
| 12 | + fix_eof_newline: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Guard - only CodeRabbit docstring PRs from same repo |
| 16 | + id: guard |
| 17 | + shell: bash |
| 18 | + run: | |
| 19 | + set -euo pipefail |
| 20 | +
|
| 21 | + AUTHOR='${{ github.event.pull_request.user.login }}' |
| 22 | + BASE_REPO='${{ github.event.pull_request.base.repo.full_name }}' |
| 23 | + HEAD_REPO='${{ github.event.pull_request.head.repo.full_name }}' |
| 24 | + TITLE='${{ github.event.pull_request.title }}' |
| 25 | +
|
| 26 | + if [[ "$AUTHOR" != "coderabbitai[bot]" ]]; then |
| 27 | + echo "run=false" >> "$GITHUB_OUTPUT" |
| 28 | + exit 0 |
| 29 | + fi |
| 30 | +
|
| 31 | + # Safety: only push to branches within the same repo |
| 32 | + if [[ "$BASE_REPO" != "$HEAD_REPO" ]]; then |
| 33 | + echo "run=false" >> "$GITHUB_OUTPUT" |
| 34 | + exit 0 |
| 35 | + fi |
| 36 | +
|
| 37 | + # only run for docstring PRs |
| 38 | + if ! echo "$TITLE" | grep -qi "docstring"; then |
| 39 | + echo "run=false" >> "$GITHUB_OUTPUT" |
| 40 | + exit 0 |
| 41 | + fi |
| 42 | +
|
| 43 | + echo "run=true" >> "$GITHUB_OUTPUT" |
| 44 | +
|
| 45 | + - name: Checkout PR head |
| 46 | + if: steps.guard.outputs.run == 'true' |
| 47 | + uses: actions/checkout@v4 |
| 48 | + with: |
| 49 | + ref: ${{ github.event.pull_request.head.ref }} |
| 50 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 51 | + fetch-depth: 0 |
| 52 | + |
| 53 | + - name: Append final newline when missing (changed files only) |
| 54 | + if: steps.guard.outputs.run == 'true' |
| 55 | + shell: bash |
| 56 | + run: | |
| 57 | + set -euo pipefail |
| 58 | +
|
| 59 | + BASE_SHA="${{ github.event.pull_request.base.sha }}" |
| 60 | + HEAD_SHA="${{ github.event.pull_request.head.sha }}" |
| 61 | +
|
| 62 | + files=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" -- \ |
| 63 | + '*.C' '*.c' '*.cc' '*.cpp' '*.cxx' '*.h' '*.hh' '*.hpp' '*.hxx' || true) |
| 64 | +
|
| 65 | + if [[ -z "${files}" ]]; then |
| 66 | + echo "No relevant files changed." |
| 67 | + exit 0 |
| 68 | + fi |
| 69 | +
|
| 70 | + changed=0 |
| 71 | + for f in $files; do |
| 72 | + [[ -f "$f" ]] || continue |
| 73 | +
|
| 74 | + # For non-empty files: ensure last byte is '\n' |
| 75 | + if [[ -s "$f" ]]; then |
| 76 | + last_byte="$(tail -c 1 "$f" || true)" |
| 77 | + if [[ "$last_byte" != $'\n' ]]; then |
| 78 | + printf '\n' >> "$f" |
| 79 | + echo "Fixed EOF newline: $f" |
| 80 | + changed=1 |
| 81 | + fi |
| 82 | + fi |
| 83 | + done |
| 84 | +
|
| 85 | + if [[ "$changed" -eq 0 ]]; then |
| 86 | + echo "All files already end with a newline." |
| 87 | + exit 0 |
| 88 | + fi |
| 89 | +
|
| 90 | + git status --porcelain |
| 91 | + git add -A |
| 92 | +
|
| 93 | + git config user.name "github-actions[bot]" |
| 94 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 95 | +
|
| 96 | + git commit -m "Fix missing final newline in docstring PR" |
| 97 | +
|
| 98 | + - name: Push fix commit back to PR branch |
| 99 | + if: steps.guard.outputs.run == 'true' |
| 100 | + shell: bash |
| 101 | + run: | |
| 102 | + set -euo pipefail |
| 103 | + # If no commit was created, pushing will fail; so only push if HEAD is ahead. |
| 104 | + if git rev-parse HEAD~1 >/dev/null 2>&1; then |
| 105 | + git push origin "HEAD:${{ github.event.pull_request.head.ref }}" |
| 106 | + fi |
0 commit comments