PR Validation #6
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: PR Validation | |
| on: | |
| pull_request: | |
| branches: [main] | |
| paths: | |
| - 'tools/**' | |
| workflow_dispatch: | |
| inputs: | |
| pr_number: | |
| description: 'PR number to validate' | |
| required: true | |
| jobs: | |
| validate: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| contents: read | |
| steps: | |
| - name: Checkout PR | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Get changed files | |
| run: | | |
| if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then | |
| gh pr diff ${{ inputs.pr_number }} --name-only > changed_files.txt | |
| else | |
| git diff --name-only origin/main...HEAD > changed_files.txt | |
| fi | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.11' | |
| - name: Install dependencies | |
| run: pip install pyyaml | |
| - name: Run validation | |
| id: validate | |
| working-directory: ${{ github.workspace }} | |
| run: | | |
| python .github/scripts/validate_pr.py changed_files.txt 2>&1 | tee result.txt | |
| echo "exit_code=${PIPESTATUS[0]}" >> $GITHUB_OUTPUT | |
| - name: Print result | |
| if: always() | |
| run: cat result.txt | |
| - name: Comment on PR | |
| if: github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const result = fs.readFileSync('result.txt', 'utf8'); | |
| const exitCode = '${{ steps.validate.outputs.exit_code }}'; | |
| const icon = exitCode === '0' ? '✅' : '❌'; | |
| const status = exitCode === '0' ? '校验通过' : '校验失败,请修复后重新提交'; | |
| const prNumber = context.eventName === 'workflow_dispatch' | |
| ? parseInt('${{ inputs.pr_number }}') | |
| : context.payload.pull_request.number; | |
| github.rest.issues.createComment({ | |
| issue_number: prNumber, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: `## ${icon} PR 自动校验结果\n\n**状态**: ${status}\n\n\`\`\`\n${result}\n\`\`\`` | |
| }); | |
| - name: Fail if validation failed | |
| if: steps.validate.outputs.exit_code != '0' | |
| run: exit 1 |