From 2646ea63179db32adf68f378dfd79b0d1a1a8283 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 9 Jan 2025 15:39:31 +0100 Subject: [PATCH] Update validate-formatting.yaml --- .github/workflows/validate-formatting.yaml | 60 ++++++++++++++-------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/.github/workflows/validate-formatting.yaml b/.github/workflows/validate-formatting.yaml index 896653be..8eadd0ac 100644 --- a/.github/workflows/validate-formatting.yaml +++ b/.github/workflows/validate-formatting.yaml @@ -1,4 +1,4 @@ -name: Validate and auto-fix script formatting +name: Validate script formatting on: push: @@ -11,10 +11,10 @@ on: jobs: shfmt: - name: Check and fix formatting + name: Check changed files runs-on: ubuntu-latest permissions: - contents: write + pull-requests: write steps: @@ -33,13 +33,13 @@ jobs: - name: Checkout code uses: actions/checkout@v4 with: - fetch-depth: 0 + fetch-depth: 0 # Ensure the full history is fetched for accurate diffing ref: ${{ github.event_name == 'pull_request_target' && fromJSON(steps.pr.outputs.result).merge_commit_sha || '' }} - name: Get changed files id: changed-files run: | - if [ "${{ github.event_name }}" == "pull_request_target" ]; then + if ${{ github.event_name == 'pull_request_target' }}; then echo "files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ steps.pr.outputs.result && fromJSON(steps.pr.outputs.result).merge_commit_sha }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT else echo "files=$(git diff --name-only ${{ github.event.before }} ${{ github.event.after }} | grep -E '\.(sh|func)$' | xargs)" >> $GITHUB_OUTPUT @@ -55,36 +55,43 @@ jobs: go install mvdan.cc/sh/v3/cmd/shfmt@latest echo "$GOPATH/bin" >> $GITHUB_PATH - - name: Fix formatting + - name: Run shfmt if: steps.changed-files.outputs.files != '' + id: shfmt run: | - shfmt -w ${{ steps.changed-files.outputs.files }} + set +e - - name: Commit and push changes - if: steps.changed-files.outputs.files != '' && github.event_name == 'pull_request_target' - run: | - git config user.name "github-actions[bot]" - git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git add ${{ steps.changed-files.outputs.files }} - git commit -m "chore: auto-fix formatting issues" - git push + + shfmt_output=$(shfmt -d ${{ steps.changed-files.outputs.files }}) + if [[ $? -eq 0 ]]; then + exit 0 + else + echo "diff=\"$(echo -n "$shfmt_output" | base64 -w 0)\"" >> $GITHUB_OUTPUT + printf "%s" "$shfmt_output" + exit 1 + fi - name: Post comment with results - if: always() && github.event_name == 'pull_request_target' + if: always() && steps.changed-files.outputs.files != '' && github.event_name == 'pull_request_target' uses: actions/github-script@v7 with: script: | const result = "${{ job.status }}" === "success" ? "success" : "failure"; + const diff = Buffer.from( + ${{ steps.shfmt.outputs.diff }}, + "base64", + ).toString(); const issueNumber = context.payload.pull_request ? context.payload.pull_request.number : null; const commentIdentifier = "validate-formatting"; - let newCommentBody = `\n### Script Formatting Results\n\n`; + let newCommentBody = `\n### Script formatting\n\n`; if (result === "failure") { - newCommentBody += `:x: Formatting issues were found and automatically fixed.\n`; + newCommentBody += + `:x: We found issues in the formatting of the following changed files:\n\n\`\`\`diff\n${diff}\n\`\`\`\n`; } else { - newCommentBody += `:rocket: All scripts are properly formatted!\n`; + newCommentBody += `:rocket: All changed shell scripts are formatted correctly!\n`; } newCommentBody += `\n\n`; @@ -96,11 +103,21 @@ jobs: }); const existingComment = comments.find( - (comment) => comment.user.login === "github-actions[bot]" && - comment.body.includes(commentIdentifier) + (comment) => comment.user.login === "github-actions[bot]", + ); if (existingComment) { + if (existingComment.body.includes(commentIdentifier)) { + const re = new RegExp( + String.raw`[\s\S]*?`, + "", + ); + newCommentBody = existingComment.body.replace(re, newCommentBody); + } else { + newCommentBody = existingComment.body + "\n\n---\n\n" + newCommentBody; + } + await github.rest.issues.updateComment({ ...context.repo, comment_id: existingComment.id, @@ -113,3 +130,4 @@ jobs: body: newCommentBody, }); } + }