linting.yaml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. name: Linting and Formatting
  2. on:
  3. push:
  4. branches: [ main, dev ]
  5. pull_request:
  6. branches: [ main, dev ]
  7. types: [opened, synchronize, reopened, ready_for_review]
  8. permissions:
  9. pull-requests: write
  10. jobs:
  11. lint-and-format:
  12. name: Linting and Formatting
  13. if: ${{ github.event_name != 'pull_request' || !github.event.pull_request.draft }}
  14. runs-on: ubuntu-latest
  15. steps:
  16. - name: Checkout code
  17. uses: actions/checkout@v6
  18. - name: Set up Python
  19. uses: actions/setup-python@v6
  20. with:
  21. python-version: '3.x'
  22. - name: Install dependencies
  23. run: |
  24. python -m pip install --upgrade pip
  25. pip install pre-commit
  26. - name: Run pre-commit
  27. id: pre-commit
  28. run: pre-commit run --all-files --show-diff-on-failure
  29. - name: Post fix instructions on failure
  30. if: failure() && steps.pre-commit.outcome == 'failure'
  31. run: |
  32. cat >> "$GITHUB_STEP_SUMMARY" << 'EOF'
  33. ## ❌ Linting / Formatting checks failed
  34. Pre-commit found issues in your code. Fix them locally and push again:
  35. ```bash
  36. # Install pre-commit (one-time setup)
  37. pip install pre-commit
  38. pre-commit install
  39. # Auto-fix all issues
  40. pre-commit run --all-files
  41. # Commit the fixes
  42. git add -u
  43. git commit -m "fix: apply pre-commit formatting fixes"
  44. git push
  45. ```
  46. ### What was checked
  47. | Hook | Tool | What it fixes |
  48. |------|------|---------------|
  49. | `trailing-whitespace` | pre-commit-hooks | Removes trailing whitespace |
  50. | `end-of-file-fixer` | pre-commit-hooks | Ensures files end with a newline |
  51. | `requirements-txt-fixer` | pre-commit-hooks | Sorts requirements.txt entries |
  52. | `ruff-format` | Ruff | Auto-formats Python code (like Black) |
  53. | `ruff` | Ruff | Fixes Python lint errors (`--fix`) |
  54. > See the diff above for the exact changes needed.
  55. EOF
  56. - name: Comment on PR with fix instructions
  57. if: failure() && steps.pre-commit.outcome == 'failure' && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository
  58. uses: actions/github-script@v9
  59. with:
  60. script: |
  61. const body = `## ❌ Linting / Formatting checks failed
  62. Pre-commit found issues in your code. Run the following locally, then push again:
  63. \`\`\`bash
  64. # Install pre-commit (one-time setup)
  65. pip install pre-commit
  66. pre-commit install
  67. # Auto-fix all issues
  68. pre-commit run --all-files
  69. # Commit the fixes
  70. git add -u
  71. git commit -m "fix: apply pre-commit formatting fixes"
  72. git push
  73. \`\`\`
  74. <details>
  75. <summary>What was checked</summary>
  76. | Hook | Tool | What it fixes |
  77. |------|------|---------------|
  78. | \`trailing-whitespace\` | pre-commit-hooks | Removes trailing whitespace |
  79. | \`end-of-file-fixer\` | pre-commit-hooks | Ensures files end with a newline |
  80. | \`requirements-txt-fixer\` | pre-commit-hooks | Sorts requirements.txt entries |
  81. | \`ruff-format\` | Ruff | Auto-formats Python code (like Black) |
  82. | \`ruff\` | Ruff | Fixes Python lint errors (\`--fix\`) |
  83. </details>
  84. > See the [workflow run](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) for the exact diff of required changes.`;
  85. // Find existing bot comment to avoid duplicates
  86. const { data: comments } = await github.rest.issues.listComments({
  87. owner: context.repo.owner,
  88. repo: context.repo.repo,
  89. issue_number: context.issue.number,
  90. });
  91. const existing = comments.find(c =>
  92. c.user.type === 'Bot' && c.body.includes('Linting / Formatting checks failed')
  93. );
  94. if (existing) {
  95. await github.rest.issues.updateComment({
  96. owner: context.repo.owner,
  97. repo: context.repo.repo,
  98. comment_id: existing.id,
  99. body,
  100. });
  101. } else {
  102. await github.rest.issues.createComment({
  103. owner: context.repo.owner,
  104. repo: context.repo.repo,
  105. issue_number: context.issue.number,
  106. body,
  107. });
  108. }