|
| 1 | +name: Validate YAML Formatting |
| 2 | +on: |
| 3 | + pull_request: |
| 4 | + paths: |
| 5 | + - 'Unpublished/**/rule.yml' |
| 6 | + - 'Published/**/rule.yml' |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | + workflow_dispatch: {} |
| 9 | +jobs: |
| 10 | + check-yaml-format: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + pull-requests: write |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v4 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + - name: Set up Python 3.12 |
| 21 | + uses: actions/setup-python@v5 |
| 22 | + with: |
| 23 | + python-version: '3.12' |
| 24 | + - name: Install pyyaml |
| 25 | + run: pip install pyyaml |
| 26 | + - name: Detect changed rule.yml files |
| 27 | + id: changed-files |
| 28 | + run: | |
| 29 | + if [ "${{ github.event_name }}" = "pull_request" ]; then |
| 30 | + FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD \ |
| 31 | + | grep -E '^(Published|Unpublished)/.*/rule\.yml$' || true) |
| 32 | + else |
| 33 | + FILES=$(git diff --name-only HEAD~1 HEAD \ |
| 34 | + | grep -E '^(Published|Unpublished)/.*/rule\.yml$' || true) |
| 35 | + fi |
| 36 | + if [ -z "$FILES" ]; then |
| 37 | + echo "No rule.yml files changed." |
| 38 | + echo "has_files=false" >> $GITHUB_OUTPUT |
| 39 | + else |
| 40 | + echo "has_files=true" >> $GITHUB_OUTPUT |
| 41 | + echo "$FILES" > /tmp/changed_rule_files.txt |
| 42 | + fi |
| 43 | + - name: Check YAML sorting and formatting |
| 44 | + id: format-check |
| 45 | + if: steps.changed-files.outputs.has_files == 'true' |
| 46 | + run: | |
| 47 | + FILES=$(cat /tmp/changed_rule_files.txt | tr '\n' ' ') |
| 48 | + python scripts/sort_yaml.py --check $FILES |
| 49 | + continue-on-error: true |
| 50 | + - name: Post format check result to PR |
| 51 | + if: always() && github.event_name == 'pull_request' && steps.changed-files.outputs.has_files == 'true' |
| 52 | + uses: actions/github-script@v7 |
| 53 | + with: |
| 54 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 55 | + script: | |
| 56 | + const outcome = '${{ steps.format-check.outcome }}'; |
| 57 | + const marker = '<!-- yaml-format-check -->'; |
| 58 | + let body = marker + '\n'; |
| 59 | + if (outcome === 'success') { |
| 60 | + body += '## \u2705 YAML Format Check Passed\n\nAll changed `rule.yml` files are correctly sorted and formatted.'; |
| 61 | + } else { |
| 62 | + body += '## \u274c YAML Format Check Failed\n\n'; |
| 63 | + body += 'One or more `rule.yml` files are not correctly sorted/formatted alphabetically by key.\n\n'; |
| 64 | + body += 'Run the following command locally to fix them:\n\n```bash\npython scripts/sort_yaml.py\n```\n\nThen commit and push.'; |
| 65 | + } |
| 66 | + const { data: comments } = await github.rest.issues.listComments({ |
| 67 | + owner: context.repo.owner, repo: context.repo.repo, |
| 68 | + issue_number: context.issue.number, |
| 69 | + }); |
| 70 | + const existing = comments.find(c => c.user.type === 'Bot' && c.body.includes(marker)); |
| 71 | + if (existing) { |
| 72 | + await github.rest.issues.updateComment({ |
| 73 | + owner: context.repo.owner, repo: context.repo.repo, |
| 74 | + comment_id: existing.id, body, |
| 75 | + }); |
| 76 | + } else { |
| 77 | + await github.rest.issues.createComment({ |
| 78 | + owner: context.repo.owner, repo: context.repo.repo, |
| 79 | + issue_number: context.issue.number, body, |
| 80 | + }); |
| 81 | + } |
| 82 | + - name: Fail if format check failed |
| 83 | + if: steps.format-check.outcome == 'failure' |
| 84 | + run: | |
| 85 | + echo "YAML format check failed. Run 'python scripts/sort_yaml.py' to fix." |
| 86 | + exit 1 |
0 commit comments