mirror of
https://github.com/Leonmmcoset/cleonos.git
synced 2026-04-21 10:40:00 +00:00
109 lines
3.4 KiB
YAML
109 lines
3.4 KiB
YAML
name: Code Style Check
|
|
|
|
on:
|
|
push:
|
|
pull_request:
|
|
workflow_dispatch:
|
|
|
|
permissions:
|
|
contents: write
|
|
pull-requests: write
|
|
|
|
jobs:
|
|
clang-format:
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Install clang-format
|
|
run: |
|
|
sudo apt-get update
|
|
sudo apt-get install -y clang-format
|
|
|
|
- name: Resolve base ref
|
|
id: base
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
|
|
echo "base_ref=${{ github.event.pull_request.head.ref }}" >> "$GITHUB_OUTPUT"
|
|
else
|
|
echo "base_ref=${{ github.ref_name }}" >> "$GITHUB_OUTPUT"
|
|
fi
|
|
|
|
- name: Detect auto-fix capability
|
|
id: capability
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
can_fix=false
|
|
if [[ "${{ github.event_name }}" == "push" || "${{ github.event_name }}" == "workflow_dispatch" ]]; then
|
|
can_fix=true
|
|
elif [[ "${{ github.event_name }}" == "pull_request" && "${{ github.event.pull_request.head.repo.full_name }}" == "${{ github.repository }}" ]]; then
|
|
can_fix=true
|
|
fi
|
|
echo "can_fix=$can_fix" >> "$GITHUB_OUTPUT"
|
|
|
|
- name: Run clang-format and detect drift
|
|
id: format
|
|
shell: bash
|
|
run: |
|
|
set -euo pipefail
|
|
|
|
mapfile -t files < <(
|
|
git ls-files \
|
|
'*.c' '*.h' '*.cc' '*.cpp' '*.cxx' '*.hpp' '*.hxx' \
|
|
| grep -Ev '^(limine/|third_party/|build/|build-cmake/)' || true
|
|
)
|
|
|
|
if [[ ${#files[@]} -eq 0 ]]; then
|
|
echo "No C/C++ files to check."
|
|
echo "changed=false" >> "$GITHUB_OUTPUT"
|
|
exit 0
|
|
fi
|
|
|
|
for f in "${files[@]}"; do
|
|
clang-format -i "$f"
|
|
done
|
|
|
|
if git diff --quiet -- "${files[@]}"; then
|
|
echo "changed=false" >> "$GITHUB_OUTPUT"
|
|
echo "Code style check passed."
|
|
exit 0
|
|
fi
|
|
|
|
echo "changed=true" >> "$GITHUB_OUTPUT"
|
|
echo "Style drift detected. Auto-fix patch:"
|
|
echo ""
|
|
git diff -- "${files[@]}"
|
|
|
|
- name: Create auto-fix PR
|
|
if: steps.format.outputs.changed == 'true' && steps.capability.outputs.can_fix == 'true'
|
|
uses: peter-evans/create-pull-request@v7
|
|
with:
|
|
token: ${{ secrets.GITHUB_TOKEN }}
|
|
commit-message: "style: auto-format C/C++ sources with clang-format"
|
|
title: "style: auto-format C/C++ sources"
|
|
body: |
|
|
This PR was generated automatically by CI style check.
|
|
|
|
- Trigger: `${{ github.event_name }}`
|
|
- Base branch: `${{ steps.base.outputs.base_ref }}`
|
|
- Formatter: `clang-format`
|
|
branch: codex/style-autofix-${{ github.run_id }}-${{ github.run_attempt }}
|
|
base: ${{ steps.base.outputs.base_ref }}
|
|
delete-branch: true
|
|
labels: |
|
|
automated
|
|
style
|
|
|
|
- name: Fail when style drift cannot be auto-fixed
|
|
if: steps.format.outputs.changed == 'true' && steps.capability.outputs.can_fix != 'true'
|
|
shell: bash
|
|
run: |
|
|
echo "Style drift found, but this event does not have permission to auto-create a fix PR."
|
|
echo "Please run clang-format locally and push the result."
|
|
exit 1
|