summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEllie Hermaszewska <ellieh@nvidia.com>2024-10-26 10:06:46 +0800
committerGitHub <noreply@github.com>2024-10-26 10:06:46 +0800
commit311413d382625613ef09036336fb89dbd4d3c7e7 (patch)
tree66777f033b984071f8f573007eaffcf91349d143
parentd8969d87dcc9eea3f186a0c93c5e48d3d1659e05 (diff)
Actions workflow to format code on command (#5400)
* Add script to perform or check formatting * Actions workflow to format code on command Comment on a PR with /format
-rw-r--r--.github/workflows/format.yml49
-rw-r--r--.github/workflows/slash-command-dispatch.yml32
-rwxr-xr-xextras/formatting.sh81
3 files changed, 162 insertions, 0 deletions
diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml
new file mode 100644
index 000000000..4a68882ae
--- /dev/null
+++ b/.github/workflows/format.yml
@@ -0,0 +1,49 @@
+name: Format
+on:
+ repository_dispatch:
+ types: [format-command]
+jobs:
+ format:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v4
+ with:
+ token: ${{ secrets.SLANGBOT_PAT }}
+ repository: ${{ github.event.client_payload.pull_request.head.repo.full_name }}
+ ref: ${{ github.event.client_payload.pull_request.head.ref }}
+
+ - run: pip3 install gersemi
+
+ - name: install clang-format
+ run: |
+ tmpdir=$(mktemp -d)
+ curl -L -H "Authorization: token ${{ secrets.SLANGBOT_PAT }}" \
+ -o "$tmpdir/clang-format" \
+ https://github.com/shader-slang/slang-binaries/raw/4e88845ec51641b4c92e68027e359090bdb219e0/clang-format/x86_64-linux/bin/clang-format
+ chmod +x "$tmpdir/clang-format"
+ echo "$tmpdir" >> $GITHUB_PATH
+
+ - run: ./extras/formatting.sh
+
+ - name: Configure Git commit signing
+ run: |
+ echo "${{ secrets.SLANGBOT_SIGNING_KEY }}" > "${{runner.temp}}"/signing_key
+ chmod 600 "${{runner.temp}}"/signing_key
+ git config commit.gpgsign true
+ git config gpg.format ssh
+ git config user.signingkey "${{runner.temp}}"/signing_key
+
+ - uses: EndBug/add-and-commit@v9
+ with:
+ fetch: false
+ message: "format code"
+ push: true
+
+ - name: Add reaction
+ uses: peter-evans/create-or-update-comment@v4
+ with:
+ token: ${{ secrets.SLANGBOT_PAT }}
+ repository: ${{ github.event.client_payload.github.payload.repository.full_name }}
+ comment-id: ${{ github.event.client_payload.github.payload.comment.id }}
+ reactions-edit-mode: replace
+ reactions: hooray
diff --git a/.github/workflows/slash-command-dispatch.yml b/.github/workflows/slash-command-dispatch.yml
new file mode 100644
index 000000000..fc357f89e
--- /dev/null
+++ b/.github/workflows/slash-command-dispatch.yml
@@ -0,0 +1,32 @@
+name: Slash Command Dispatch
+on:
+ issue_comment:
+ types: [created]
+jobs:
+ slashCommandDispatch:
+ runs-on: ubuntu-latest
+ steps:
+
+ - name: Slash Command Dispatch
+ id: scd
+ uses: peter-evans/slash-command-dispatch@v4
+ with:
+ token: ${{ secrets.SLANGBOT_PAT }}
+ reaction-token: ${{ secrets.SLANGBOT_PAT }}
+ config: >
+ [
+ {
+ "command": "format",
+ "permission": "none",
+ "issue_type": "pull-request"
+ }
+ ]
+
+ - name: Edit comment with error message
+ if: steps.scd.outputs.error-message
+ uses: peter-evans/create-or-update-comment@v4
+ with:
+ token: ${{ secrets.SLANGBOT_PAT }}
+ comment-id: ${{ github.event.comment.id }}
+ body: |
+ > ${{ steps.scd.outputs.error-message }}
diff --git a/extras/formatting.sh b/extras/formatting.sh
new file mode 100755
index 000000000..dc3bbc7ca
--- /dev/null
+++ b/extras/formatting.sh
@@ -0,0 +1,81 @@
+#!/usr/bin/env bash
+
+set -e
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)"
+cd "$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)" || exit 1
+
+check_only=0
+
+while [[ "$#" -gt 0 ]]; do
+ case $1 in
+ -h | --help) help=1 ;;
+ --check-only) check_only=1 ;;
+ esac
+ shift
+done
+
+if [ "$help" ]; then
+ me=$(basename "$0")
+ cat <<EOF
+$me: Format or check formatting of files in this repo
+
+Usage: $me [--check-only]
+
+Options:
+ --check-only Check formatting without modifying files
+EOF
+ exit 0
+fi
+
+require_bin() {
+ local name="$1"
+ local required="$2"
+ local version
+
+ if ! command -v "$name" &>/dev/null; then
+ echo "This script needs $name, but it isn't in \$PATH"
+ missing_bin=1
+ return
+ fi
+
+ version=$("$name" --version | grep -oP "$name(?:\s+version)?\s+\K\d+\.\d+\.?\d*")
+ if ! printf '%s\n%s\n' "$required" "$version" | sort -V -C; then
+ echo "$name version $version is too old. Version $required or newer is required."
+ missing_bin=1
+ fi
+}
+
+require_bin "git" "1.8"
+require_bin "gersemi" "0.16.2"
+
+if [ "${missing_bin:-}" = "1" ]; then
+ exit 1
+fi
+
+if [ "$missing_bin" ]; then
+ exit 1
+fi
+
+exit_code=0
+
+cmake_formatting() {
+ readarray -t files < <(git ls-files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
+
+ common_args=(
+ # turn on warning when this is fixed https://github.com/BlankSpruce/gersemi/issues/39
+ --no-warn-about-unknown-commands
+ --definitions "${files[@]}"
+ )
+
+ if [ "$check_only" -eq 1 ]; then
+ gersemi "${common_args[@]}" --diff --color "${files[@]}"
+ gersemi "${common_args[@]}" --check "${files[@]}" || exit_code=1
+ else
+ gersemi "${common_args[@]}" --in-place "${files[@]}"
+ fi
+}
+
+cmake_formatting
+
+exit $exit_code