Suggest clang-format changes on PRs #51
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check Clang-Format on Diff | |
on: [pull_request] | |
jobs: | |
clang-format: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Import LLVM GPG Key | |
run: | | |
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 15CF4D18AF4F7421 | |
- name: Add LLVM Repository | |
run: | | |
sudo add-apt-repository "deb http://apt.llvm.org/jammy llvm-toolchain-jammy-17 main" | |
sudo apt-get update | |
- name: Install clang-format 17 | |
run: | | |
sudo apt-get install clang-format-17 | |
- name: Fetch branches to check | |
run: | | |
git fetch origin ${{ github.base_ref }} | |
if [ "${{ github.repository_owner }}" != "${{ github.event.pull_request.head.repo.owner.login }}" ]; then | |
fork_owner="${{ github.event.pull_request.head.repo.owner.login }}" | |
fork_url=$(git remote get-url origin) | |
git remote add fork "$fork_url" | |
git fetch fork ${{ github.head_ref }} | |
else | |
git fetch origin ${{ github.head_ref }} | |
fi | |
- name: Apply clang-format locally | |
if: github.event_name == 'pull_request' | |
run: | | |
if [ "${{ github.repository_owner }}" != "${{ github.event.pull_request.head.repo.owner.login }}" ]; then | |
./clang-format-apply.sh origin/${{ github.base_ref }} fork/${{ github.head_ref }} | |
else | |
./clang-format-apply.sh origin/${{ github.base_ref }} origin/${{ github.head_ref }} | |
fi | |
- name: Save clang-format changes as patch | |
run: | | |
git diff -U0 > clang_format.patch | |
- name: Generate suggestions | |
run: | | |
echo "[]" > comments.json | |
echo "Created comments.json" | |
while IFS= read -r line; do | |
echo "Processing line: $line" | |
if [[ "$line" =~ ^@@ ]]; then | |
echo "Line matches diff hunk header" | |
file=$(echo "$line" | grep -oP '(?<= b/).*?(?= @@)') | |
echo "Got file name: $file" | |
position=$(echo "$line" | grep -oP '(?<=@@ -).*?(?= @@)' | awk '{split($0, a, " "); print a[2]}') | |
echo "Got line number: $position" | |
comment=$(jq -n --arg file "$file" --arg pos "$position" '{path: $file, position: ($pos|tonumber), body: "Please apply the suggested clang-format changes."}') | |
echo "Generated comment: $comment" | |
tmp=$(mktemp) | |
jq ". += [$comment]" comments.json > "$tmp" && mv "$tmp" comments.json | |
echo "Appended comment to comments.json" | |
fi | |
done < clang_format.patch | |
- name: Suggest changes via GitHub API | |
if: failure() | |
run: | | |
echo "Attempting to post review comments via GitHub API..." | |
PR_NUMBER="${{ github.event.pull_request.number }}" | |
COMMIT_ID=$(git rev-parse HEAD) | |
REVIEW_BODY="Please make the changes suggested by clang-format" | |
COMMENTS=$(cat comments.json) | |
curl -L \ | |
-X POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN_CLANG }}" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews \ | |
-d "$(jq -n --arg commit_id "$COMMIT_ID" --arg body "$REVIEW_BODY" --argjson comments "$COMMENTS" '{commit_id: $commit_id, body: $body, event: "REQUEST_CHANGES", comments: $comments}')" |