Develop (#7) #34
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: Code Format Check | |
| on: | |
| push: | |
| branches: | |
| - master | |
| - main | |
| - develop | |
| pull_request: | |
| branches: | |
| - master | |
| - main | |
| - develop | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| format: | |
| name: Check & Fix Formatting | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: '1.24' | |
| - name: Run gofmt | |
| id: fmt | |
| run: | | |
| gofmt -w . | |
| if [ -n "$(git diff)" ]; then | |
| echo "needs_format=true" >> $GITHUB_OUTPUT | |
| git diff --name-only | |
| else | |
| echo "needs_format=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Run goimports | |
| id: imports | |
| run: | | |
| go install golang.org/x/tools/cmd/goimports@latest | |
| goimports -w . | |
| if [ -n "$(git diff)" ]; then | |
| echo "needs_imports=true" >> $GITHUB_OUTPUT | |
| git diff --name-only | |
| else | |
| echo "needs_imports=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Comment if formatting needed | |
| if: github.event_name == 'pull_request' && (steps.fmt.outputs.needs_format == 'true' || steps.imports.outputs.needs_imports == 'true') | |
| 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: '❌ Code formatting issues detected\n\nRun the following locally:\n```bash\ngofmt -w .\ngo install golang.org/x/tools/cmd/goimports@latest\ngoimports -w .\n```\n\nOr commit the auto-fixed changes from this workflow.' | |
| }) | |
| - name: Commit changes if needed (push) | |
| if: | | |
| github.event_name == 'push' && | |
| (steps.fmt.outputs.needs_format == 'true' || steps.imports.outputs.needs_imports == 'true') | |
| run: | | |
| git config --local user.email "bot@nodebyte.dev" | |
| git config --local user.name "NodeByte Bot" | |
| git add -A | |
| git commit -m "style: auto-format code with gofmt and goimports" | |
| git push | |
| - name: Fail if PR has formatting issues | |
| if: | | |
| github.event_name == 'pull_request' && | |
| (steps.fmt.outputs.needs_format == 'true' || steps.imports.outputs.needs_imports == 'true') | |
| run: | | |
| echo "❌ Code has formatting issues" | |
| echo "Run gofmt and goimports locally and push the changes" | |
| exit 1 |