From c5198ccd223eeee096b7819da1a5404640f4fba8 Mon Sep 17 00:00:00 2001 From: Refringe Date: Sat, 26 Jul 2025 19:20:46 -0400 Subject: [PATCH] Automatic JSON Formatting Workflow This change alters the existing format workflow to include another job that handles formatting JSON files. The JSON files are formatted using BiomeJS. The BiomeJS config file is dynamically created before Biome runs and removed after. This uses the existing `.editorconfig` settings as defaults, allowing overwrites and additional Biome configurations within the workflow file. Current JSON formatting rules: - end_of_line = lf - insert_final_newline = true - indent_style = space - trim_trailing_whitespace = true - charset = utf-8 - indent_size = 2 - trailingCommas = none - bracketSpacing = true - expand = always The existing Csharpier job runs at the same time as the new Biome job. The results (changes made) in each job are compressed into an artifact and downloaded into a final job that combines them and commits them back into the project (if there are changes, and if the workflow is not triggered within a PR). --- .editorconfig | 13 +- .github/workflows/dotnet-format.yml | 50 -------- .github/workflows/format.yml | 191 ++++++++++++++++++++++++++++ 3 files changed, 196 insertions(+), 58 deletions(-) delete mode 100644 .github/workflows/dotnet-format.yml create mode 100644 .github/workflows/format.yml diff --git a/.editorconfig b/.editorconfig index 6696ba18..0a4031d4 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,23 +1,20 @@ -# editorconfig.org - -# top-most EditorConfig file root = true -# Default settings: -# A newline ending every file -# Use 4 spaces as indentation [*] +end_of_line = lf insert_final_newline = true indent_style = space indent_size = 4 trim_trailing_whitespace = true [{*.yaml,*.yml}] +charset = utf-8 indent_size = 2 [*.json] -ij_formatter_enabled = true +charset = utf-8 indent_size = 2 +ij_formatter_enabled = true # C# files [*.cs] @@ -152,4 +149,4 @@ resharper_merge_into_pattern_highlighting = none indent_size = 2 [*.{csproj,vbproj,proj,nativeproj,locproj}] -charset = utf-8 \ No newline at end of file +charset = utf-8 diff --git a/.github/workflows/dotnet-format.yml b/.github/workflows/dotnet-format.yml deleted file mode 100644 index e9c9d8e4..00000000 --- a/.github/workflows/dotnet-format.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: .NET Format - -on: - push: - branches: - - develop - pull_request: - branches-ignore: - - main - -jobs: - dotnet-format: - if: "!contains(github.event.head_commit.message, '.NET Format Style Fixes')" - runs-on: ubuntu-latest - permissions: - contents: write - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - token: ${{ secrets.BYPASS_WORKFLOW_PAT || github.token }} - - - name: Setup .NET - uses: actions/setup-dotnet@v4 - with: - dotnet-version: 9.0 - - - name: Restore Tool Dependencies - run: dotnet tool install -g csharpier - - - name: CSharpier Format - run: csharpier format . - - - name: Check PR Formatting - if: github.event_name == 'pull_request' - run: | - if [[ -n $(git status --porcelain) ]]; then - echo "::error::Code formatting issues found. Please run 'csharpier format .' locally and push the changes to your PR branch. Read the 'README.md' file for more information." - git status --porcelain - exit 1 - fi - - - name: Auto Commit Changes (Push) - if: github.event_name != 'pull_request' - uses: stefanzweifel/git-auto-commit-action@v6 - with: - commit_message: .NET Format Style Fixes - commit_user_name: sp-tarkov-bot - commit_user_email: singleplayertarkov@gmail.com - commit_author: sp-tarkov-bot diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 00000000..78eb2639 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,191 @@ +name: Format + +on: + push: + branches: + - develop + pull_request: + branches-ignore: + - main + +jobs: + dotnet-format: + if: "!contains(github.event.head_commit.message, 'Format Style Fixes')" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + token: ${{ secrets.BYPASS_WORKFLOW_PAT || github.token }} + + - name: Setup .NET + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 9.0 + + - name: Restore Tool Dependencies + run: dotnet tool install -g csharpier + + - name: CSharpier Format + run: csharpier format . + + - name: Check for Changes + id: check-changes + run: | + if [[ -n $(git status --porcelain) ]]; then + echo "changes=true" >> $GITHUB_OUTPUT + else + echo "changes=false" >> $GITHUB_OUTPUT + fi + + - name: Upload Changes as Artifact + if: steps.check-changes.outputs.changes == 'true' && github.event_name != 'pull_request' + uses: actions/upload-artifact@v4 + with: + name: dotnet-changes + path: | + **/*.cs + **/*.csproj + retention-days: 1 + compression-level: 1 + + - name: Fail PR if Changes Detected + if: steps.check-changes.outputs.changes == 'true' && github.event_name == 'pull_request' + run: | + echo "::error::Code formatting issues found. Please run 'csharpier format .' locally and push the changes to your PR branch. Read the 'README.md' file for more information." + git status --porcelain + exit 1 + + biome-format: + if: "!contains(github.event.head_commit.message, 'Format Style Fixes')" + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + token: ${{ secrets.BYPASS_WORKFLOW_PAT || github.token }} + + - name: Setup Biome + uses: biomejs/setup-biome@v2 + with: + version: latest + + - name: Create Biome Config + run: | + cat > biome.json << 'EOF' + { + "vcs": { + "enabled": true, + "clientKind": "git", + "useIgnoreFile": true + }, + "files": { + "maxSize": 20971520, + "includes": [ + "**/*.json", + "!**/looseLoot.json", + "!**/items.json" + ] + }, + "formatter": { + "useEditorconfig": true + }, + "javascript": { + "formatter": { + "enabled": false + } + }, + "json": { + "formatter": { + "enabled": true, + "lineWidth": 120, + "trailingCommas": "none", + "bracketSpacing": true, + "expand": "always" + }, + "parser": { + "allowComments": true, + "allowTrailingCommas": true + }, + "linter": { + "enabled": false + } + } + } + EOF + + - name: Format JSON Files + run: | + biome format --write . || true + + - name: Remove Biome Config + if: always() + run: rm -f biome.json + + - name: Check for Changes + id: check-changes + run: | + if [[ -n $(git status --porcelain) ]]; then + echo "changes=true" >> $GITHUB_OUTPUT + else + echo "changes=false" >> $GITHUB_OUTPUT + fi + + - name: Upload Changes as Artifact + if: steps.check-changes.outputs.changes == 'true' && github.event_name != 'pull_request' + uses: actions/upload-artifact@v4 + with: + name: biome-changes + path: | + **/*.json + retention-days: 1 + compression-level: 1 + + - name: Fail PR if Changes Detected + if: steps.check-changes.outputs.changes == 'true' && github.event_name == 'pull_request' + run: | + echo "::error::JSON formatting issues found. Please run BiomeJS locally to format your JSON files and push the changes to your PR branch." + git status --porcelain + exit 1 + + commit-changes: + if: github.event_name != 'pull_request' && !contains(github.event.head_commit.message, 'Format Style Fixes') + needs: [dotnet-format, biome-format] + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + token: ${{ secrets.BYPASS_WORKFLOW_PAT || github.token }} + + - name: Download Dotnet Changes + uses: actions/download-artifact@v4 + with: + name: dotnet-changes + continue-on-error: true + + - name: Download Biome Changes + uses: actions/download-artifact@v4 + with: + name: biome-changes + continue-on-error: true + + - name: Check for Any Changes + id: check-changes + run: | + if [[ -n $(git status --porcelain) ]]; then + echo "changes=true" >> $GITHUB_OUTPUT + else + echo "changes=false" >> $GITHUB_OUTPUT + fi + + - name: Auto Commit Changes + if: steps.check-changes.outputs.changes == 'true' + uses: stefanzweifel/git-auto-commit-action@v6 + with: + commit_message: Format Style Fixes + commit_user_name: sp-tarkov-bot + commit_user_email: singleplayertarkov@gmail.com + commit_author: sp-tarkov-bot