Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 50 additions & 129 deletions .github/workflows/clang-format-checker.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
name: "Check code formatting"
on:
pull_request_target:
types: [opened,synchronize]
issue_comment:
types: edited

on:
pull_request:
types: [opened, synchronize]

permissions:
contents: read

concurrency:
group: clang-format-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
code_formatter:
if: github.event_name == 'pull_request_target'
runs-on: ubuntu-latest
permissions:
pull-requests: write
timeout-minutes: 30
steps:
- name: Fetch DirectXShaderCompiler sources
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0
with:
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false

- name: Checkout through merge base
uses: rmacklin/fetch-through-merge-base@bfe4d03a86f9afa52bc1a70e9814fc92a07f7b75 # v0.3.0
Expand All @@ -24,139 +29,55 @@ jobs:
head_ref: ${{ github.event.pull_request.head.sha }}
deepen_length: 500

- name: Get changed files
id: changed-files
uses: step-security/changed-files@3dbe17c78367e7d60f00d78ae6781a35be47b4a1 # v45.0.1
with:
separator: ","
skip_initial_fetch: true

# We need to pull the script from the main branch, so that we ensure
# we get the latest version of this script.
- name: Fetch code formatting utils
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
repository: microsoft/DirectXShaderCompiler
ref: ${{ github.event.repository.default_branch }}
sparse-checkout: |
utils/git/requirements_formatting.txt
utils/git/code-format-helper.py
utils/git/code-format-save-diff.py
sparse-checkout-cone-mode: false
path: code-format-tools

- name: "Listed files"
env:
LISTED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
run: |
echo "Formatting files:"
echo "$LISTED_FILES"

- name: Install clang-format
uses: aminya/setup-cpp@8170d66c458f4a045220b7b0966c10940bb2a15d # v1.8.1
with:
clangformat: 17.0.1

- name: Setup Python env
uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4.9.1
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: 'code-format-tools/utils/git/requirements_formatting.txt'

- name: Install python dependencies
run: pip install -r code-format-tools/utils/git/requirements_formatting.txt

- name: Run code formatter
id: formatter
- name: Check code formatting
env:
GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }}
START_REV: ${{ github.event.pull_request.base.sha }}
END_REV: ${{ github.event.pull_request.head.sha }}
CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}
shell: bash
run: |
python code-format-tools/utils/git/code-format-helper.py \
--token ${{ secrets.GITHUB_TOKEN }} \
--issue-number $GITHUB_PR_NUMBER \
--start-rev $START_REV \
--end-rev $END_REV \
--changed-files "$CHANGED_FILES"
apply_diff:
if: ${{ github.event_name == 'issue_comment' && github.event.comment.user.login == 'github-actions[bot]' && endsWith(github.event.comment.body, '- [x] Check this box to apply formatting changes to this branch.') }}
runs-on: ubuntu-latest
env:
TMP_DIFF_FILE: /tmp/diff.patch
permissions:
pull-requests: write
contents: write
steps:
- uses: actions/github-script@ffc2c79a5b2490bd33e0a41c1de74b877714d736 # v3.2.0
id: get-pr
with:
script: |
const request = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number
}
core.info(`Getting PR #${request.pull_number} from ${request.owner}/${request.repo}`)
try {
const result = await github.pulls.get(request)
return result.data
} catch (err) {
core.setFailed(`Request failed with error ${err}`)
}

# We need to pull the script from the main branch, so that we ensure
# we get the latest version of this script.
- name: Fetch code formatting utils
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
repository: microsoft/DirectXShaderCompiler
ref: ${{ github.event.repository.default_branch }}
sparse-checkout: |
utils/git/requirements_formatting.txt
utils/git/code-format-helper.py
utils/git/code-format-save-diff.py
sparse-checkout-cone-mode: false
path: code-format-tools
set -euo pipefail

- name: Setup Python env
uses: actions/setup-python@7f4fc3e22c37d6ff65e88745f38bd3157c663f7c # v4.9.1
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: 'code-format-tools/utils/git/requirements_formatting.txt'
output_file="$(mktemp)"
error_file="$(mktemp)"
patch_file="$RUNNER_TEMP/clang-format.patch"
trap 'rm -f "$output_file" "$error_file"' EXIT
rm -f "$patch_file"

- name: Install python dependencies
run: pip install -r code-format-tools/utils/git/requirements_formatting.txt
merge_base="$(git merge-base "$START_REV" "$END_REV")"

- name: Apply code diff
env:
GITHUB_PR_NUMBER: ${{ github.event.issue.number }}
COMMENT_ID: ${{ github.event.comment.id }}
run: |
python code-format-tools/utils/git/code-format-save-diff.py \
--token ${{ secrets.GITHUB_TOKEN }} \
--issue-number $GITHUB_PR_NUMBER \
--tmp-diff-file $TMP_DIFF_FILE \
--comment-id $COMMENT_ID
set +e
git-clang-format \
--diff \
--extensions=cpp,c,h,hpp,hxx,cxx \
"$merge_base" \
"$END_REV" >"$output_file" 2>"$error_file"
formatter_status=$?
set -e

- name: Fetch LLVM sources for head
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
fetch-depth: 2
ref: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
sed 's/^/[clang-format] /' "$output_file"
sed 's/^/[clang-format] /' "$error_file"
if [[ "$formatter_status" -gt 1 ]]; then
echo "::error::git-clang-format failed."
exit "$formatter_status"
fi

- name: apply diff
run: |
git apply $TMP_DIFF_FILE
git add .
if [[ "$formatter_status" -eq 1 ]] || grep -q '^diff --git ' "$output_file"; then
sed -n '/^diff --git /,$p' "$output_file" >"$patch_file"
git apply --check "$patch_file"
echo "::error::C/C++ formatting changes are required. Run 'git clang-format $merge_base' locally and update the pull request."
exit 1
fi

- name: Commit & Push changes
uses: actions-js/push@968f4695ca558093eadb24ad83cc5891f47e0cdc # v1.6
- name: Upload formatting patch
if: ${{ failure() }}
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
with:
branch: ${{ fromJSON(steps.get-pr.outputs.result).head.ref }}
repository: ${{ fromJSON(steps.get-pr.outputs.result).head.repo.full_name }}
github_token: ${{ secrets.GITHUB_TOKEN }}
name: clang-format-patch
path: ${{ runner.temp }}/clang-format.patch
if-no-files-found: ignore
retention-days: 7
Loading
Loading