Skip to content

Suggest clang-format changes on PRs #65

Suggest clang-format changes on PRs

Suggest clang-format changes on PRs #65

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
git diff -U0 > clang_format.patch
- name: Check if patch is not empty
id: check_patch
run: |
if [ -s clang_format.patch ]; then
echo "Patch is not empty"
echo "::set-output name=patch_not_empty::true"
else
echo "Patch is empty"
echo "::set-output name=patch_not_empty::false"
fi
- name: Post patch as comment
if: steps.check_patch.outputs.patch_not_empty == 'true'
uses: actions/github-script@v4
with:
github-token: ${{ secrets.GH_ACTION_TOKEN_CLANG_FORMAT }}
script: |
const fs = require('fs');
const { context, GitHub } = require('@actions/github');
const patchFile = 'clang_format.patch';
const patchContent = fs.readFileSync(patchFile, 'utf8');
const octokit = new GitHub(process.env.GH_ACTION_TOKEN_CLANG_FORMAT);
octokit.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: 'Here is the clang-format patch:\n```diff\n' + patchContent + '\n```'
});