Skip to content

Commit a1032dd

Browse files
authored
Improvements to formatting bot (#5421)
* Use github token for formatting command dispatch * Open PR instead of committing directly when formatting * Add --no-version-check option to formatting script * Colorful diff output for cmake formatting
1 parent b7a619b commit a1032dd

File tree

4 files changed

+100
-39
lines changed

4 files changed

+100
-39
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Formatting tools setup
2+
3+
description: Performs setup common to the code formatting actions
4+
5+
runs:
6+
using: composite
7+
steps:
8+
- name: install gersemi
9+
shell: bash
10+
run: |
11+
pip3 install gersemi colorama
12+
13+
- name: install clang-format
14+
shell: bash
15+
run: |
16+
tmpdir=$(mktemp -d)
17+
curl -L -H "Authorization: token ${{github.token}}" \
18+
-o "$tmpdir/clang-format" \
19+
https://github.com/shader-slang/slang-binaries/raw/4e88845ec51641b4c92e68027e359090bdb219e0/clang-format/x86_64-linux/bin/clang-format
20+
chmod +x "$tmpdir/clang-format"
21+
echo "$tmpdir" >> $GITHUB_PATH

.github/workflows/format.yml

+54-18
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,74 @@ jobs:
66
format:
77
runs-on: ubuntu-latest
88
steps:
9-
- uses: actions/checkout@v4
9+
- name: Checkout PR branch
10+
uses: actions/checkout@v4
1011
with:
1112
token: ${{ secrets.SLANGBOT_PAT }}
1213
repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
1314
ref: ${{ github.event.client_payload.pull_request.head.ref }}
15+
path: pr-branch
1416

15-
- run: pip3 install gersemi
17+
- name: Checkout target branch
18+
uses: actions/checkout@v4
19+
with:
20+
token: ${{ secrets.SLANGBOT_PAT }}
21+
repository: ${{ github.event.client_payload.pull_request.base.repo.full_name }}
22+
ref: ${{ github.event.client_payload.pull_request.base.ref }}
23+
path: target-branch
1624

17-
- name: install clang-format
18-
run: |
19-
tmpdir=$(mktemp -d)
20-
curl -L -H "Authorization: token ${{ secrets.SLANGBOT_PAT }}" \
21-
-o "$tmpdir/clang-format" \
22-
https://github.com/shader-slang/slang-binaries/raw/4e88845ec51641b4c92e68027e359090bdb219e0/clang-format/x86_64-linux/bin/clang-format
23-
chmod +x "$tmpdir/clang-format"
24-
echo "$tmpdir" >> $GITHUB_PATH
25+
- name: Setup
26+
uses: ./target-branch/.github/actions/format-setup
2527

26-
- run: ./extras/formatting.sh
28+
- name: Run formatting
29+
id: format
30+
run: |
31+
./target-branch/extras/formatting.sh --source ./pr-branch
2732
2833
- name: Configure Git commit signing
34+
id: git-info
2935
run: |
3036
echo "${{ secrets.SLANGBOT_SIGNING_KEY }}" > "${{runner.temp}}"/signing_key
3137
chmod 600 "${{runner.temp}}"/signing_key
32-
git config commit.gpgsign true
33-
git config gpg.format ssh
34-
git config user.signingkey "${{runner.temp}}"/signing_key
38+
git -C pr-branch config commit.gpgsign true
39+
git -C pr-branch config gpg.format ssh
40+
git -C pr-branch config user.signingkey "${{runner.temp}}"/signing_key
41+
bot_info=$(curl -s -H "Authorization: Bearer ${{ secrets.SLANGBOT_PAT }}" \
42+
"https://api.github.com/user")
43+
echo "bot_identity=$(echo $bot_info | jq --raw-output '.login + " <" + (.id|tostring) + "+" + .login + "@users.noreply.github.com>"')" >> $GITHUB_OUTPUT
44+
echo "bot_name=$(echo $bot_info | jq --raw-output '.login')" >> $GITHUB_OUTPUT
45+
46+
- name: Create Pull Request
47+
id: create-pr
48+
uses: peter-evans/create-pull-request@v7
49+
with:
50+
token: ${{ secrets.SLANGBOT_PAT }}
51+
path: pr-branch
52+
commit-message: "format code"
53+
title: "Format code for PR #${{ github.event.client_payload.pull_request.number }}"
54+
body: "Automated code formatting for ${{ github.event.client_payload.pull_request.url }}"
55+
committer: ${{ steps.git-info.outputs.bot_identity }}
56+
author: ${{ steps.git-info.outputs.bot_identity }}
57+
branch: format-${{ github.event.client_payload.pull_request.number }}-${{ github.event.client_payload.pull_request.head.ref }}
58+
base: ${{ github.event.client_payload.pull_request.head.ref }}
59+
push-to-fork: ${{ steps.git-info.outputs.bot_name }}/slang
60+
delete-branch: true
3561

36-
- uses: EndBug/add-and-commit@v9
62+
- name: Comment on PR
63+
uses: peter-evans/create-or-update-comment@v4
64+
if: always()
3765
with:
38-
fetch: false
39-
message: "format code"
40-
push: true
66+
token: ${{ secrets.SLANGBOT_PAT }}
67+
repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
68+
issue-number: ${{ github.event.client_payload.pull_request.number }}
69+
body: |
70+
${{
71+
steps.format.conclusion == 'failure'
72+
&& format('❌ Formatting failed. Please check the [workflow run](https://github.com/{0}/actions/runs/{1})', github.repository, github.run_id)
73+
|| (steps.create-pr.conclusion == 'failure'
74+
&& format('❌ Failed to create formatting pull request. Please check the [workflow run](https://github.com/{0}/actions/runs/{1})', github.repository, github.run_id)
75+
|| format('🌈 Formatted, please merge the changes from [this PR]({0})', steps.create-pr.outputs.pull-request-url))
76+
}}
4177
4278
- name: Add reaction
4379
uses: peter-evans/create-or-update-comment@v4

.github/workflows/slash-command-dispatch.yml

+3-8
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,9 @@ jobs:
1212
with:
1313
token: ${{ secrets.GITHUB_TOKEN }}
1414
reaction-token: ${{ secrets.SLANGBOT_PAT }}
15-
config: >
16-
[
17-
{
18-
"command": "format",
19-
"permission": "none",
20-
"issue_type": "pull-request"
21-
}
22-
]
15+
issue-type: pull-request
16+
commands: |
17+
format
2318
2419
- name: Edit comment with error message
2520
if: steps.scd.outputs.error-message

extras/formatting.sh

+22-13
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
set -e
44

5-
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
6-
cd "$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)" || exit 1
5+
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
6+
source_dir="$(dirname "$script_dir")"
77

88
check_only=0
9+
no_version_check=0
910

1011
while [[ "$#" -gt 0 ]]; do
1112
case $1 in
1213
-h | --help) help=1 ;;
1314
--check-only) check_only=1 ;;
15+
--no-version-check) no_version_check=1 ;;
16+
--source)
17+
source_dir="$2"
18+
shift
19+
;;
1420
esac
1521
shift
1622
done
@@ -20,14 +26,18 @@ if [ "$help" ]; then
2026
cat <<EOF
2127
$me: Format or check formatting of files in this repo
2228
23-
Usage: $me [--check-only]
29+
Usage: $me [--check-only] [--no-version-check] [--source <path>]
2430
2531
Options:
26-
--check-only Check formatting without modifying files
32+
--check-only Check formatting without modifying files
33+
--no-version-check Skip version compatibility checks
34+
--source Path to source directory to format (defaults to parent of script directory)
2735
EOF
2836
exit 0
2937
fi
3038

39+
cd "$source_dir" || exit 1
40+
3141
require_bin() {
3242
local name="$1"
3343
local required="$2"
@@ -39,19 +49,17 @@ require_bin() {
3949
return
4050
fi
4151

42-
version=$("$name" --version | grep -oP "$name(?:\s+version)?\s+\K\d+\.\d+\.?\d*")
43-
if ! printf '%s\n%s\n' "$required" "$version" | sort -V -C; then
44-
echo "$name version $version is too old. Version $required or newer is required."
45-
missing_bin=1
52+
if [ "$no_version_check" -eq 0 ]; then
53+
version=$("$name" --version | grep -oP "$name(?:\s+version)?\s+\K\d+\.\d+\.?\d*")
54+
if ! printf '%s\n%s\n' "$required" "$version" | sort -V -C; then
55+
echo "$name version $version is too old. Version $required or newer is required."
56+
missing_bin=1
57+
fi
4658
fi
4759
}
4860

4961
require_bin "git" "1.8"
50-
require_bin "gersemi" "0.16.2"
51-
52-
if [ "${missing_bin:-}" = "1" ]; then
53-
exit 1
54-
fi
62+
require_bin "gersemi" "0.17"
5563

5664
if [ "$missing_bin" ]; then
5765
exit 1
@@ -79,3 +87,4 @@ cmake_formatting() {
7987
cmake_formatting
8088

8189
exit $exit_code
90+

0 commit comments

Comments
 (0)