From 2c351caa160baac42554765a29c45d76ec6144c2 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Sat, 26 Oct 2024 10:32:35 +0800 Subject: [PATCH 1/4] Use github token for formatting command dispatch From 3f8224dd4c49e899f6cc94549d8d4ecdec1cbf3d Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Sat, 26 Oct 2024 10:32:35 +0800 Subject: [PATCH 2/4] Open PR instead of committing directly when formatting --- .github/actions/format-setup/action.yml | 20 ++++++ .github/workflows/format.yml | 72 +++++++++++++++----- .github/workflows/slash-command-dispatch.yml | 11 +-- extras/formatting.sh | 19 +++--- 4 files changed, 88 insertions(+), 34 deletions(-) create mode 100644 .github/actions/format-setup/action.yml diff --git a/.github/actions/format-setup/action.yml b/.github/actions/format-setup/action.yml new file mode 100644 index 0000000000..73e20154e1 --- /dev/null +++ b/.github/actions/format-setup/action.yml @@ -0,0 +1,20 @@ +name: Formatting tools setup + +description: Performs setup common to the code formatting actions + +runs: + using: composite + steps: + - name: install gersemi + shell: bash + run: pip3 install gersemi + + - name: install clang-format + shell: bash + run: | + tmpdir=$(mktemp -d) + curl -L -H "Authorization: token ${{github.token}}" \ + -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 diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 4a68882ae4..8a444f0ad2 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -6,38 +6,74 @@ jobs: format: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - name: Checkout PR branch + 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 }} + path: pr-branch - - run: pip3 install gersemi + - name: Checkout target branch + uses: actions/checkout@v4 + with: + token: ${{ secrets.SLANGBOT_PAT }} + repository: ${{ github.event.client_payload.pull_request.base.repo.full_name }} + ref: ${{ github.event.client_payload.pull_request.base.ref }} + path: target-branch - - 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 + - name: Setup + uses: ./target-branch/.github/actions/format-setup - - run: ./extras/formatting.sh + - name: Run formatting + id: format + run: | + ./target-branch/extras/formatting.sh --source ./pr-branch - name: Configure Git commit signing + id: git-info 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 + git -C pr-branch config commit.gpgsign true + git -C pr-branch config gpg.format ssh + git -C pr-branch config user.signingkey "${{runner.temp}}"/signing_key + bot_info=$(curl -s -H "Authorization: Bearer ${{ secrets.SLANGBOT_PAT }}" \ + "https://api.github.com/user") + echo "bot_identity=$(echo $bot_info | jq --raw-output '.login + " <" + (.id|tostring) + "+" + .login + "@users.noreply.github.com>"')" >> $GITHUB_OUTPUT + echo "bot_name=$(echo $bot_info | jq --raw-output '.login')" >> $GITHUB_OUTPUT + + - name: Create Pull Request + id: create-pr + uses: peter-evans/create-pull-request@v7 + with: + token: ${{ secrets.SLANGBOT_PAT }} + path: pr-branch + commit-message: "format code" + title: "Format code for PR #${{ github.event.client_payload.pull_request.number }}" + body: "Automated code formatting for ${{ github.event.client_payload.pull_request.url }}" + committer: ${{ steps.git-info.outputs.bot_identity }} + author: ${{ steps.git-info.outputs.bot_identity }} + branch: format-${{ github.event.client_payload.pull_request.number }}-${{ github.event.client_payload.pull_request.head.ref }} + base: ${{ github.event.client_payload.pull_request.head.ref }} + push-to-fork: ${{ steps.git-info.outputs.bot_name }}/slang + delete-branch: true - - uses: EndBug/add-and-commit@v9 + - name: Comment on PR + uses: peter-evans/create-or-update-comment@v4 + if: always() with: - fetch: false - message: "format code" - push: true + token: ${{ secrets.SLANGBOT_PAT }} + repository: ${{ github.event.client_payload.github.payload.repository.full_name }} + issue-number: ${{ github.event.client_payload.pull_request.number }} + body: | + ${{ + steps.format.conclusion == 'failure' + && format('❌ Formatting failed. Please check the [workflow run](https://github.com/{0}/actions/runs/{1})', github.repository, github.run_id) + || (steps.create-pr.conclusion == 'failure' + && format('❌ Failed to create formatting pull request. Please check the [workflow run](https://github.com/{0}/actions/runs/{1})', github.repository, github.run_id) + || format('🌈 Formatted, please merge the changes from [this PR]({0})', steps.create-pr.outputs.pull-request-url)) + }} - name: Add reaction uses: peter-evans/create-or-update-comment@v4 diff --git a/.github/workflows/slash-command-dispatch.yml b/.github/workflows/slash-command-dispatch.yml index c4c9a7ae7c..2e72c3bf49 100644 --- a/.github/workflows/slash-command-dispatch.yml +++ b/.github/workflows/slash-command-dispatch.yml @@ -12,14 +12,9 @@ jobs: with: token: ${{ secrets.GITHUB_TOKEN }} reaction-token: ${{ secrets.SLANGBOT_PAT }} - config: > - [ - { - "command": "format", - "permission": "none", - "issue_type": "pull-request" - } - ] + issue-type: pull-request + commands: | + format - name: Edit comment with error message if: steps.scd.outputs.error-message diff --git a/extras/formatting.sh b/extras/formatting.sh index dc3bbc7ca5..89c77d361e 100755 --- a/extras/formatting.sh +++ b/extras/formatting.sh @@ -2,8 +2,8 @@ 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 +script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" +source_dir="$(dirname "$script_dir")" check_only=0 @@ -11,6 +11,10 @@ while [[ "$#" -gt 0 ]]; do case $1 in -h | --help) help=1 ;; --check-only) check_only=1 ;; + --source) + source_dir="$2" + shift + ;; esac shift done @@ -20,14 +24,17 @@ if [ "$help" ]; then cat <] Options: --check-only Check formatting without modifying files + --source Path to source directory to format (defaults to parent of script directory) EOF exit 0 fi +cd "$source_dir" || exit 1 + require_bin() { local name="$1" local required="$2" @@ -47,11 +54,7 @@ require_bin() { } require_bin "git" "1.8" -require_bin "gersemi" "0.16.2" - -if [ "${missing_bin:-}" = "1" ]; then - exit 1 -fi +require_bin "gersemi" "0.17" if [ "$missing_bin" ]; then exit 1 From af0d3bfdbec3cfe2bb10d7d77c99e0a9eb432973 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Mon, 28 Oct 2024 18:14:46 +0800 Subject: [PATCH 3/4] Add --no-version-check option to formatting script --- extras/formatting.sh | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/extras/formatting.sh b/extras/formatting.sh index 89c77d361e..a53930eafe 100755 --- a/extras/formatting.sh +++ b/extras/formatting.sh @@ -6,11 +6,13 @@ script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" source_dir="$(dirname "$script_dir")" check_only=0 +no_version_check=0 while [[ "$#" -gt 0 ]]; do case $1 in -h | --help) help=1 ;; --check-only) check_only=1 ;; + --no-version-check) no_version_check=1 ;; --source) source_dir="$2" shift @@ -24,11 +26,12 @@ if [ "$help" ]; then cat <] +Usage: $me [--check-only] [--no-version-check] [--source ] Options: - --check-only Check formatting without modifying files - --source Path to source directory to format (defaults to parent of script directory) + --check-only Check formatting without modifying files + --no-version-check Skip version compatibility checks + --source Path to source directory to format (defaults to parent of script directory) EOF exit 0 fi @@ -46,10 +49,12 @@ require_bin() { 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 + if [ "$no_version_check" -eq 0 ]; then + 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 fi } @@ -82,3 +87,4 @@ cmake_formatting() { cmake_formatting exit $exit_code + From 29b50b8f8094b04ff27f5c8acc882ca7458ce866 Mon Sep 17 00:00:00 2001 From: Ellie Hermaszewska Date: Mon, 28 Oct 2024 18:17:30 +0800 Subject: [PATCH 4/4] Colorful diff output for cmake formatting --- .github/actions/format-setup/action.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/actions/format-setup/action.yml b/.github/actions/format-setup/action.yml index 73e20154e1..58eddac77a 100644 --- a/.github/actions/format-setup/action.yml +++ b/.github/actions/format-setup/action.yml @@ -7,7 +7,8 @@ runs: steps: - name: install gersemi shell: bash - run: pip3 install gersemi + run: | + pip3 install gersemi colorama - name: install clang-format shell: bash