scripts/build: add a python script for checking style rules #13
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: Post review | |
| uses: actions/github-script@v8 | |
| id: post-comments | |
| env: | |
| COMMENTS: ${{ steps.style-check.outputs.reviews }} | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const reviews = JSON.parse(process.env.COMMENTS || "[]"); | |
| const comments = reviews.map(r => ({ | |
| path: r.path, | |
| line: r.line, | |
| side: "RIGHT", | |
| body: r.body | |
| })); | |
| if (comments.length > 0) { | |
| console.debug("Reviews: ", reviews); | |
| // Get previous reviews from github-actions[bot] | |
| const { data: existingReviews } = await github.rest.pulls.listReviews({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.payload.pull_request.number, | |
| }); | |
| const botReview = existingReviews.find(review => review.user.login === "github-actions[bot]"); | |
| let existingComments = []; | |
| if (botReview) { | |
| // List comments for the bot's review | |
| const { data: reviewComments } = await github.rest.pulls.listCommentsForReview({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.payload.pull_request.number, | |
| review_id: botReview.id, | |
| }); | |
| existingComments = reviewComments; | |
| } | |
| // Filter out comments that already exist | |
| const newComments = comments.filter(newComment => { | |
| return !existingComments.some(existingComment => | |
| existingComment.path === newComment.path && | |
| existingComment.line === newComment.line && | |
| existingComment.body === newComment.body | |
| ); | |
| }); | |
| if (newComments.length > 0) { | |
| console.debug("Posting new comments: ", newComments); | |
| await github.rest.pulls.createReview({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: context.payload.pull_request.number, | |
| event: "COMMENT", | |
| comments: newComments, | |
| }); | |
| } else { | |
| console.info("All comments already exist, skipping review creation."); | |
| } | |
| } else { | |
| console.info("Review list empty."); | |
| } | |
| if (comments.length > 50) { | |
| console.error("More than 50 comments"); | |
| } |