# Validates rigg configuration on every pull request and posts the diff
# against the live environment as a PR comment. Generated by `rigg ci init`.
name: rigg validate

on:
  pull_request:
    branches: [main]

permissions:
  id-token: write      # OIDC federated login — no secrets
  contents: read
  pull-requests: write # diff comment

jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install rigg
        run: cargo install rigg --locked

      - name: Azure login (OIDC)
        uses: azure/login@v2
        with:
          client-id: ${{ vars.AZURE_CLIENT_ID }}
          tenant-id: ${{ vars.AZURE_TENANT_ID }}
          subscription-id: ${{ vars.AZURE_SUBSCRIPTION_ID }}

      - name: Validate
        run: rigg validate --strict

      - name: Diff against {{RIGG_ENV}}
        id: diff
        continue-on-error: true
        run: |
          rigg diff --all --env {{RIGG_ENV}} --format markdown > diff.md
          echo "exit=$?" >> "$GITHUB_OUTPUT"

      - name: Comment diff on PR
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const body = fs.readFileSync('diff.md', 'utf8');
            await github.rest.issues.createComment({
              ...context.repo,
              issue_number: context.issue.number,
              body: body.slice(0, 65000),
            });
