Repository navigation
feat(ci): automated comment spam protection — block spam accounts and auto-moderate PR comments #2452
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: Squad Heartbeat (Ralph) | |
| # ⚠️ SYNC: This workflow is maintained in 4 locations. Changes must be applied to all: | |
| # - templates/workflows/squad-heartbeat.yml (source template) | |
| # - packages/squad-cli/templates/workflows/squad-heartbeat.yml (CLI package) | |
| # - .squad/templates/workflows/squad-heartbeat.yml (installed template) | |
| # - .github/workflows/squad-heartbeat.yml (active workflow) | |
| # Run 'squad upgrade' to sync installed copies from source templates. | |
| # | |
| # CI Hardening: Cron schedule audit (item 8, refs diberry/squad#120) | |
| # ───────────────────────────────────────────────────────────────── | |
| # Audit finding: No cron schedule is configured here, which is INTENTIONAL. | |
| # Ralph should only run reactively (on issue/PR events) or manually. | |
| # A cron schedule would waste Actions minutes (~20 min/week) for minimal | |
| # benefit since Ralph's triage logic is event-driven. | |
| # If scheduled runs appear in Actions history, they are event-triggered | |
| # (issue close/label), not cron-triggered. Do NOT add a schedule trigger. | |
| on: | |
| # React to completed work or new squad work | |
| issues: | |
| types: [closed, labeled] | |
| pull_request: | |
| types: [closed] | |
| # Manual trigger | |
| workflow_dispatch: | |
| permissions: | |
| issues: write | |
| contents: read | |
| pull-requests: read | |
| # CI Hardening Phase 3 item A1: Prevent parallel runs from competing for resources | |
| concurrency: | |
| group: ${{ github.workflow }}-${{ github.event.issue.number || github.ref }} | |
| cancel-in-progress: true | |
| jobs: | |
| heartbeat: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| # CI Hardening: GitHub API rate limit monitoring (item 9) | |
| # Ralph makes multiple API calls (list issues, add labels, create comments). | |
| # Check rate limit early to avoid silent failures mid-workflow. | |
| - name: "📊 Check GitHub API rate limit" | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { data: rateLimit } = await github.rest.rateLimit.get(); | |
| const { remaining, limit, reset } = rateLimit.rate; | |
| // Security: mask numeric values to prevent fingerprinting via CI logs | |
| core.setSecret(String(remaining)); | |
| core.setSecret(String(limit)); | |
| const resetDate = new Date(reset * 1000).toISOString(); | |
| if (remaining < 10) { | |
| core.setFailed(`❌ Rate limit: CRITICAL — below minimum threshold. Aborting to prevent silent failures.`); | |
| } else if (remaining < 100) { | |
| core.warning(`⚠️ Rate limit: WARNING — below safe threshold (resets ${resetDate})`); | |
| } else { | |
| core.info(`✅ Rate limit: OK — above safe threshold (resets ${resetDate})`); | |
| } | |
| - name: Ralph — Repair orphaned squad labels | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| // Find issues with squad:{member} labels but missing parent "squad" label | |
| const { data: openIssues } = await github.rest.issues.listForRepo({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| state: 'open', | |
| per_page: 100 | |
| }); | |
| let repaired = 0; | |
| for (const issue of openIssues) { | |
| const labels = issue.labels.map(l => l.name); | |
| const hasSquadMember = labels.some(l => l.startsWith('squad:')); | |
| const hasSquadParent = labels.includes('squad'); | |
| if (hasSquadMember && !hasSquadParent) { | |
| await github.rest.issues.addLabels({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issue.number, | |
| labels: ['squad'] | |
| }); | |
| core.info(`Repaired orphaned label on #${issue.number} — added parent "squad" label`); | |
| repaired++; | |
| } | |
| } | |
| if (repaired > 0) { | |
| core.info(`🔧 Ralph repaired ${repaired} orphaned squad label(s)`); | |
| } | |
| - name: Check triage script | |
| id: check-script | |
| run: | | |
| if [ -f ".squad/templates/ralph-triage.js" ]; then | |
| echo "has_script=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "has_script=false" >> $GITHUB_OUTPUT | |
| echo "⚠️ ralph-triage.js not found — run 'squad upgrade' to install" | |
| fi | |
| - name: Ralph — Smart triage | |
| if: steps.check-script.outputs.has_script == 'true' | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| node .squad/templates/ralph-triage.js \ | |
| --squad-dir .squad \ | |
| --output triage-results.json | |
| - name: Ralph — Apply triage decisions | |
| if: steps.check-script.outputs.has_script == 'true' && hashFiles('triage-results.json') != '' | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const fs = require('fs'); | |
| const path = 'triage-results.json'; | |
| if (!fs.existsSync(path)) { | |
| core.info('No triage results — board is clear'); | |
| return; | |
| } | |
| const results = JSON.parse(fs.readFileSync(path, 'utf8')); | |
| if (results.length === 0) { | |
| core.info('📋 Board is clear — Ralph found no untriaged issues'); | |
| return; | |
| } | |
| for (const decision of results) { | |
| try { | |
| await github.rest.issues.addLabels({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: decision.issueNumber, | |
| labels: [decision.label] | |
| }); | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: decision.issueNumber, | |
| body: [ | |
| '### 🔄 Ralph — Auto-Triage', | |
| '', | |
| `**Assigned to:** ${decision.assignTo}`, | |
| `**Reason:** ${decision.reason}`, | |
| `**Source:** ${decision.source}`, | |
| '', | |
| '> Ralph auto-triaged this issue using routing rules.', | |
| '> To reassign, swap the `squad:*` label.' | |
| ].join('\n') | |
| }); | |
| core.info(`Triaged #${decision.issueNumber} → ${decision.assignTo} (${decision.source})`); | |
| } catch (e) { | |
| core.warning(`Failed to triage #${decision.issueNumber}: ${e.message}`); | |
| } | |
| } | |
| core.info(`🔄 Ralph triaged ${results.length} issue(s)`); | |
| # Copilot auto-assign step (uses PAT if available) | |
| - name: Ralph — Assign @copilot issues | |
| if: success() | |
| uses: actions/github-script@v7 | |
| with: | |
| github-token: ${{ secrets.COPILOT_ASSIGN_TOKEN || secrets.GITHUB_TOKEN }} | |
| script: | | |
| const fs = require('fs'); | |
| let teamFile = '.squad/team.md'; | |
| if (!fs.existsSync(teamFile)) { | |
| teamFile = '.ai-team/team.md'; | |
| } | |
| if (!fs.existsSync(teamFile)) return; | |
| const content = fs.readFileSync(teamFile, 'utf8'); | |
| // Check if @copilot is on the team with auto-assign | |
| const hasCopilot = content.includes('🤖 Coding Agent') || content.includes('@copilot'); | |
| const autoAssign = content.includes('<!-- copilot-auto-assign: true -->'); | |
| if (!hasCopilot || !autoAssign) return; | |
| // Find issues labeled squad:copilot with no assignee | |
| try { | |
| const { data: copilotIssues } = await github.rest.issues.listForRepo({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| labels: 'squad:copilot', | |
| state: 'open', | |
| per_page: 5 | |
| }); | |
| const unassigned = copilotIssues.filter(i => | |
| !i.assignees || i.assignees.length === 0 | |
| ); | |
| if (unassigned.length === 0) { | |
| core.info('No unassigned squad:copilot issues'); | |
| return; | |
| } | |
| // Get repo default branch | |
| const { data: repoData } = await github.rest.repos.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo | |
| }); | |
| for (const issue of unassigned) { | |
| try { | |
| await github.request('POST /repos/{owner}/{repo}/issues/{issue_number}/assignees', { | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: issue.number, | |
| assignees: ['copilot-swe-agent[bot]'], | |
| agent_assignment: { | |
| target_repo: `${context.repo.owner}/${context.repo.repo}`, | |
| base_branch: repoData.default_branch, | |
| custom_instructions: `Read .squad/team.md (or .ai-team/team.md) for team context and .squad/routing.md (or .ai-team/routing.md) for routing rules.` | |
| } | |
| }); | |
| core.info(`Assigned copilot-swe-agent[bot] to #${issue.number}`); | |
| } catch (e) { | |
| core.warning(`Failed to assign @copilot to #${issue.number}: ${e.message}`); | |
| } | |
| } | |
| } catch (e) { | |
| core.info(`No squad:copilot label found or error: ${e.message}`); | |
| } |