# GitHub Actions workflow for QAAtlas
# Add this to .github/workflows/qaatlas.yml in your repository

name: QAAtlas Test Plan

on:
  pull_request:
    types: [opened, synchronize, reopened]

permissions:
  contents: read
  pull-requests: write

jobs:
  analyze:
    runs-on: ubuntu-latest
    name: Generate Test Plan

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # Full history for accurate diff

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install QAAtlas
        run: npm install -g qaatlas

      - name: Generate diff
        run: |
          git diff origin/${{ github.base_ref }}...HEAD > diff.patch
          echo "Diff size: $(wc -l < diff.patch) lines"

      - name: Run QAAtlas analysis
        env:
          ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
        run: |
          qaatlas analyze \
            --diff diff.patch \
            --output-dir ./qa-output \
            --verbose

      - name: Upload analysis artifacts
        uses: actions/upload-artifact@v4
        with:
          name: qa-reports
          path: ./qa-output
          retention-days: 30

      - name: Comment on PR
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const commentPath = './qa-output/pr-comment.md';

            if (!fs.existsSync(commentPath)) {
              console.log('No PR comment file found, skipping');
              return;
            }

            const comment = fs.readFileSync(commentPath, 'utf8');

            // Find existing QAAtlas comment to update
            const { data: comments } = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
            });

            const existingComment = comments.find(c =>
              c.body.includes('## QAAtlas Analysis')
            );

            if (existingComment) {
              // Update existing comment
              await github.rest.issues.updateComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                comment_id: existingComment.id,
                body: comment
              });
              console.log('Updated existing QAAtlas comment');
            } else {
              // Create new comment
              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: context.issue.number,
                body: comment
              });
              console.log('Created new QAAtlas comment');
            }

      - name: Check risk level
        run: |
          RISK_LEVEL=$(jq -r '.summary.riskLevel' ./qa-output/report.json)
          echo "Risk level: $RISK_LEVEL"

          if [ "$RISK_LEVEL" = "critical" ]; then
            echo "::error::Critical risk level detected - review required"
            exit 1
          fi
