Repository navigation
194 lines (176 loc) · 8.48 KB
/
Copy pathsquad-heartbeat.yml
File metadata and controls
194 lines (176 loc) · 8.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
name: Squad Heartbeat (Ralph)
# Canonical source: .squad-templates/workflows/squad-heartbeat.yml.
# Run node scripts/sync-templates.mjs --sync for root, CLI and SDK mirrors;
# update the active .github/workflows copy while preserving repo-specific steps.
#
# 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]
# PR closes (same-repo, fork and Dependabot). The github org blocks
# pull_request_target, and a fork `pull_request` run only gets a read-only
# token, so write steps below would 403 there (#2080-class bug). The
# unprivileged squad-heartbeat-pr-closed.yml run only signals the close;
# workflow_run then runs THIS default-branch file with the write token. The
# checkout below has no `ref:`, so it is the default branch, and this job
# reads no artifact, output or field from the PR run.
workflow_run:
workflows: ["Squad Heartbeat PR Closed"]
types: [completed]
# Manual trigger
workflow_dispatch:
permissions:
issues: write
contents: read
pull-requests: read
actions: write
# 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
if: >-
github.event_name != 'workflow_run'
|| (github.event.workflow_run.event == 'pull_request'
&& github.event.workflow_run.conclusion == 'success'
&& github.event.workflow_run.path == '.github/workflows/squad-heartbeat-pr-closed.yml')
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7
# 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@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v9
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@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v9
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 "templates/ralph-triage.js" ]; then
echo "has_script=true" >> "$GITHUB_OUTPUT"
else
echo "has_script=false" >> "$GITHUB_OUTPUT"
echo "⚠️ product-owned ralph-triage.js not found"
fi
- name: Ralph — Smart triage
if: steps.check-script.outputs.has_script == 'true'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
node 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@3a2844b7e9c422d3c10d287c895573f7108da1b3 #v9
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;
}
const autoAssign = /<!--\s*copilot-auto-assign:\s*true\s*-->/i
.test(fs.readFileSync('.squad/team.md', 'utf8'));
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')
});
if (decision.label === 'squad:copilot' && autoAssign) {
await github.rest.actions.createWorkflowDispatch({
owner: context.repo.owner,
repo: context.repo.repo,
workflow_id: 'squad-issue-assign.yml',
ref: context.payload.repository.default_branch,
inputs: { issue_number: String(decision.issueNumber) }
});
core.info(`Dispatched squad-issue-assign.yml for auto-assigned issue #${decision.issueNumber}`);
}
core.info(`Triaged #${decision.issueNumber} → ${decision.assignTo} (${decision.source})`);
} catch (e) {
core.warning(`Failed to triage #${decision.issueNumber}: ${e.message}`);
if (decision.label === 'squad:copilot' && autoAssign) {
core.setFailed(`Failed to dispatch Copilot assignment for #${decision.issueNumber}: ${e.message}`);
}
}
}
core.info(`🔄 Ralph triaged ${results.length} issue(s)`);
# Copilot assignment belongs exclusively to squad-issue-assign.yml.