dotnet lint #151
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: dotnet lint | |
| on: | |
| workflow_dispatch: | |
| schedule: | |
| - cron: 0 1 * * 1 # At AM10:00 JST on Monday | |
| env: | |
| SLN_ROOT: src/dotnet/ | |
| jobs: | |
| lint_csharp: | |
| permissions: | |
| contents: read | |
| runs-on: ubuntu-24.04 | |
| timeout-minutes: 10 | |
| steps: | |
| - uses: actions/create-github-app-token@29824e69f54612133e76f7eaac726eef6c875baf # v2.2.1 | |
| id: app-token | |
| with: | |
| app-id: ${{ secrets.SYNCED_ACTIONS_BOT_APPID }} | |
| private-key: ${{ secrets.SYNCED_ACTIONS_BOT_PRIVATE_KEY }} | |
| permission-contents: write | |
| permission-pull-requests: write | |
| - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 | |
| with: | |
| persist-credentials: false | |
| - uses: actions/setup-dotnet@baa11fbfe1d6520db94683bd5c7a3818018e4309 # v5.1.0 | |
| with: | |
| dotnet-version: 10.0.x | |
| # dotnet list | |
| - name: Obtain csproj to lint | |
| run: | | |
| projects=$(dotnet sln list | tail -n +3 | sort | xargs -n 1 echo ' *') | |
| { | |
| echo "FORMAT_PROJECTS<<EOF" | |
| echo "${projects}" | |
| echo "EOF" | |
| } >> "$GITHUB_ENV" | |
| working-directory: ${{ env.SLN_ROOT }} | |
| - name: Add dotnet-format problem matcher | |
| uses: xt0rted/dotnet-format-problem-matcher@b90c4f18e3daa4f8fd266e41eba4f351b2e00b75 # v1.2.0 | |
| # dotnet format is build-in from dotnet 6.0 sdk | |
| - name: Dotnet Format | |
| run: dotnet format --verbosity diagnostic --exclude "Sandbox.Fail.Tests" | |
| working-directory: ${{ env.SLN_ROOT }} | |
| # is change happen? | |
| - name: Check for modified files | |
| id: git-check | |
| run: echo "modified=$(if git diff-index --quiet HEAD --; then echo "false"; else echo "true"; fi)" >> "$GITHUB_OUTPUT" | |
| # get directory stats | |
| - name: List modified directories | |
| if: ${{ steps.git-check.outputs.modified == 'true' }} | |
| run: | | |
| dirs=$(git diff --dirstat=files) | |
| { | |
| echo "CHANGED_DIRS<<EOF" | |
| echo "${dirs}" | |
| echo "EOF" | |
| } | tee -a "$GITHUB_ENV" | |
| # get files stats | |
| - name: List modified files | |
| if: ${{ steps.git-check.outputs.modified == 'true' }} | |
| run: | | |
| files=$(git diff --name-only) | |
| { | |
| echo "CHANGED_FILES<<EOF" | |
| echo "${files}" | |
| echo "EOF" | |
| } | tee -a "$GITHUB_ENV" | |
| # Commit if change happen, then craete PR. force push when branch/pr already exists. | |
| - name: commit | |
| if: ${{ steps.git-check.outputs.modified == 'true' }} | |
| run: | | |
| git remote set-url origin "https://github-actions:${GITHUB_TOKEN}@github.com/${{ github.repository }}" | |
| git config user.name github-actions[bot] | |
| git config user.email 41898282+github-actions[bot]@users.noreply.github.com | |
| git checkout -b "auto-pr/dotnet-format" | |
| git add . | |
| git commit -m "[automated] dotnet format by $GITHUB_WORKFLOW" || true | |
| git push -u origin "auto-pr/dotnet-format" --force | |
| env: | |
| GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} | |
| GITHUB_WORKFLOW: ${{ github.workflow }} | |
| - name: Create PullRequest | |
| if: ${{ steps.git-check.outputs.modified == 'true' }} | |
| run: | | |
| pr_exists=$(gh pr list --head "auto-pr/dotnet-format" --base "${DEFAULT_BRANCH}") | |
| if [ -n "$pr_exists" ]; then | |
| echo "PR already exists for auto-pr/dotnet-format -> ${DEFAULT_BRANCH}. Skipping creation." | |
| exit 0 | |
| fi | |
| title="[dotnet format] Automated changes" | |
| cat << 'EOF' > pr_body.txt | |
| ## tl;dr; | |
| dotnet format generated changes based on .editorconfig | |
| ## Stats | |
| changed directories | |
| ``` | |
| ${{ env.CHANGED_DIRS }} | |
| ``` | |
| ## Files | |
| <details> | |
| <summary>Click to show.</summary> | |
| ``` | |
| ${{ env.CHANGED_FILES }} | |
| ``` | |
| </details> | |
| ## Target Projects | |
| Target project number is ${{ env.PROJECT_LENGTH }}. | |
| ${{ env.FORMAT_PROJECTS }} | |
| --- | |
| This PR was created automatically via [${{ env.WORKFLOW_NAME }}](${{ env.GITHUB_ACTIONS_RUN_URL }}), job [link](${{ env.GITHUB_ACTIONS_JOB_URL }}). | |
| EOF | |
| gh pr create --title "$title" --body-file "./pr_body.txt" --label "automated pr" | |
| env: | |
| DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} | |
| GH_TOKEN: ${{ steps.app-token.outputs.token }} | |
| GITHUB_DEFAULT_BRANCH: ${{ github.event.repository.default_branch }} | |
| GITHUB_ACTIONS_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} | |
| GITHUB_ACTIONS_JOB_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}/job/${{ job.check_run_id }} | |
| WORKFLOW_NAME: ${{ github.workflow }} |