From 1d10ff70e8153dc30c6c37f4a1a9290a42d43049 Mon Sep 17 00:00:00 2001 From: Jagdish Prajapati Date: Sun, 31 May 2026 10:51:28 +0530 Subject: [PATCH] Add code formatter --- .github/workflows/format-command.yml | 72 +++++++++++++++++++ .../{prettier.yml => prettier-check.yml} | 2 +- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/format-command.yml rename .github/workflows/{prettier.yml => prettier-check.yml} (96%) diff --git a/.github/workflows/format-command.yml b/.github/workflows/format-command.yml new file mode 100644 index 00000000..60981ea6 --- /dev/null +++ b/.github/workflows/format-command.yml @@ -0,0 +1,72 @@ +name: Format Command + +on: + issue_comment: + types: [created] + +jobs: + format: + name: 'Format code via comment' + runs-on: ubuntu-latest + # Only run if it's a PR and the comment contains /format + if: github.event.issue.pull_request != '' && contains(github.event.comment.body, '/format') + permissions: + contents: write + pull-requests: write + issues: write + steps: + - name: React to comment + uses: actions/github-script@v7 + with: + script: | + github.rest.reactions.createForIssueComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: context.payload.comment.id, + content: '+1' + }) + + - name: Get PR details + id: pr + uses: actions/github-script@v7 + with: + script: | + const { data: pr } = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number + }); + return pr; + + - name: Checkout PR code + uses: actions/checkout@v4 + with: + repository: ${{ fromJSON(steps.pr.outputs.result).head.repo.full_name }} + ref: ${{ fromJSON(steps.pr.outputs.result).head.ref }} + token: ${{ secrets.GITHUB_TOKEN }} + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Run Prettier + run: npx prettier --write "frontend/**/*.{html,css,js}" "scripts/**/*.js" "server.js" + + - name: Commit and push changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "style: auto-format code with Prettier (/format)" + commit_author: "github-actions[bot] " + + - name: Post completion comment + if: success() + uses: actions/github-script@v7 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: 'I have successfully run Prettier and pushed the formatting fixes to this PR.\n\n **Note for Contributors:** Because this commit was pushed by a bot, GitHub will not automatically re-run the CI checks. To trigger them to pass, you must either:\n- Push an empty commit locally (`git commit --allow-empty -m "Trigger builds"` and push)\n- Close and immediately reopen this Pull Request.' + }) diff --git a/.github/workflows/prettier.yml b/.github/workflows/prettier-check.yml similarity index 96% rename from .github/workflows/prettier.yml rename to .github/workflows/prettier-check.yml index 15db1d29..13688e84 100644 --- a/.github/workflows/prettier.yml +++ b/.github/workflows/prettier-check.yml @@ -1,4 +1,4 @@ -name: Auto-Format Code +name: Prettier Check on: pull_request: