scripts/build: add a python script for checking style rules #8
Workflow file for this run
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: Style Check | |
| on: | |
| pull_request: | |
| paths: | |
| - ".github/workflows/**" | |
| - "src/**.cpp" | |
| - "src/**.h" | |
| jobs: | |
| stylecheck: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 2 | |
| - name: Run style check | |
| id: style-check | |
| run: echo "reviews=$(python3 scripts/build/style.py --base-sha ${{ github.event.pull_request.base.sha }} | jq -s -c .)" >> $GITHUB_OUTPUT | |
| - name: Dismiss previous review | |
| uses: actions/github-script@v8 | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const pr = context.payload.pull_request; | |
| const reviews = await github.paginate( | |
| github.rest.pulls.listReviews, | |
| { | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: pr.number, | |
| per_page: 100, | |
| } | |
| ); | |
| for (const review of reviews) { | |
| if (review.user && review.user.login === "github-actions[bot]") { | |
| await github.rest.pulls.dismissReview({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: pr.number, | |
| review_id: review.id | |
| }); | |
| } | |
| } | |
| - name: Post review | |
| uses: actions/github-script@v8 | |
| id: post-comments | |
| env: | |
| REVIEWS: ${{ steps.style-check.outputs.reviews }} | |
| with: | |
| github-token: ${{secrets.GITHUB_TOKEN}} | |
| script: | | |
| const reviews = JSON.parse(process.env.REVIEWS || "[]"); | |
| const comments = reviews.map(r => ({ | |
| path: r.path, | |
| line: r.line, | |
| side: "RIGHT", | |
| body: r.body | |
| })); | |
| if (comments.length > 0) { | |
| console.debug("Reviews: ", reviews); | |
| await github.rest.pulls.createReview({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.payload.pull_request.number, | |
| event: "COMMENT", | |
| comments, | |
| }); | |
| } else { | |
| console.info("Review list empty."); | |
| } | |
| if (comments.length > 50) { | |
| console.error("More than 50 comments"); | |
| } |