Repository navigation
197 lines (180 loc) · 7.4 KB
/
Copy pathai-feedback-validation.yml
File metadata and controls
197 lines (180 loc) · 7.4 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
195
196
197
name: Validate AI Feedback & Issue Linkage
# Organisation reusable workflow. Call it from a pull_request or
# pull_request_target workflow:
#
# jobs:
# ai-feedback:
# permissions:
# contents: read
# pull-requests: write
# uses: lightspeedwp/.github/.github/workflows/ai-feedback-validation.yml@<commit-sha>
# with:
# enforce: false
#
# Security: no PR code is checked out or executed. Only this repository's
# helper scripts are checked out (at the called workflow's own commit), and
# FEEDBACK_RESPONSE.md is read from the PR head through the API as data.
on:
workflow_call:
inputs:
enforce:
description: Fail the check when validation fails (default warns only).
type: boolean
required: false
default: false
permissions:
contents: read
pull-requests: write
jobs:
validate-feedback-linkage:
name: Validate AI feedback and issue linkage
if: github.event.pull_request.number
runs-on: ubuntu-latest
timeout-minutes: 5
# Distinct from any caller group: a called workflow inherits the
# caller's github.workflow, so reusing it would cancel the caller.
concurrency:
group: ai-feedback-validation-${{ github.repository }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
steps:
- name: Check out validation helpers
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ job.workflow_repository }}
ref: ${{ job.workflow_sha }}
persist-credentials: false
fetch-depth: 1
sparse-checkout: scripts/validation
- name: Check PR-issue linkage and feedback response
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
ENFORCE: ${{ inputs.enforce }}
with:
script: |
const { validateAIFeedback } = require('./scripts/validation/ai-feedback-helpers.cjs');
const pr = context.payload.pull_request;
const author = pr.user?.login || '';
const botAuthors = ['dependabot[bot]', 'imgbot[bot]', 'lightspeed-docs-bot[bot]'];
const enforce = process.env.ENFORCE === 'true';
if (botAuthors.includes(author)) {
core.info(`Skipping AI feedback validation for bot author ${author}.`);
return;
}
// Read FEEDBACK_RESPONSE.md at the PR head commit as data, never
// by checking out PR code. The base repository serves fork head
// commits too, so this works for forks (even deleted ones) with a
// token scoped to the calling repository.
let feedbackContent = null;
try {
const { data } = await github.rest.repos.getContent({
owner: context.repo.owner,
repo: context.repo.repo,
path: 'FEEDBACK_RESPONSE.md',
ref: pr.head.sha,
});
if (!Array.isArray(data) && data.content) {
feedbackContent = Buffer.from(data.content, 'base64').toString('utf8');
}
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
const validation = await validateAIFeedback(
context.repo.owner,
context.repo.repo,
pr.number,
pr.body || '',
{ feedbackContent }
);
const marker = '<!-- ai-feedback-validation -->';
const message = buildValidationMessage(marker, validation);
// Comment writes need a write token; fork PRs on a pull_request
// trigger only get read access, so degrade to a log warning.
try {
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
});
const previous = comments.find(
(comment) => comment.user?.type === 'Bot' && comment.body?.includes(marker)
);
if (previous && message) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: previous.id,
body: message,
});
} else if (!previous && message) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: message,
});
} else if (previous && !message) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: previous.id,
});
}
} catch (error) {
if (error.status !== 403) {
throw error;
}
core.warning('No permission to comment on this PR; see the job log for the report.');
if (message) {
core.info(message);
}
}
if (!validation.passed) {
const summary = `AI feedback validation failed for PR #${pr.number}`;
if (enforce) {
core.setFailed(summary);
} else {
core.warning(`${summary} (warn-only; set enforce: true to fail).`);
}
}
function buildValidationMessage(marker, validation) {
const { issues } = validation;
const lines = [];
if (issues.missingIssueLink) {
lines.push('❌ **No issue link found**: the PR must include `Resolves #123` or `Closes #123`.');
}
if (issues.incompleteFeedbackTracking.length > 0) {
lines.push(
'❌ **Incomplete feedback tracking** in FEEDBACK_RESPONSE.md:',
...issues.incompleteFeedbackTracking.map((item) => ` - ${item}`)
);
}
if (issues.invalidStatus.length > 0) {
lines.push(
'❌ **Invalid feedback status** (allowed: ✅ Addressed, 📋 Deferred, ❌ Rejected):',
...issues.invalidStatus.map((item) => ` - ${item}`)
);
}
if (issues.deferredNoIssue.length > 0) {
lines.push(
'⚠️ **Deferred feedback without a follow-up issue:**',
...issues.deferredNoIssue.map((item) => ` - ${item}`)
);
}
if (validation.passed && lines.length === 0) {
return null;
}
return [
marker,
'## AI Feedback Validation Report',
'',
...lines,
'',
'### Required actions',
'1. Link this PR to its issue with `Resolves #123` or `Closes #123` in the description.',
'2. Track AI review feedback in `FEEDBACK_RESPONSE.md` with a status per item.',
'3. For deferred feedback, open a follow-up issue and link it.',
].join('\n');
}