mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-21 15:33:45 +00:00
🤵 ci: Allow Trusted Bots to Request Quality Checks (#18119)
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
d89ab2887e
commit
adf4b56f28
4 changed files with 137 additions and 14 deletions
2
.github/workflows/change-quality.yml
vendored
2
.github/workflows/change-quality.yml
vendored
|
|
@ -78,7 +78,7 @@ jobs:
|
|||
queue: max
|
||||
if: >-
|
||||
github.event_name == 'issue_comment' && github.event.issue.pull_request &&
|
||||
startsWith(github.event.comment.body, '/quality') && github.event.comment.user.type != 'Bot'
|
||||
startsWith(github.event.comment.body, '/quality')
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
permissions:
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ import { reportSelection } from './changes.mts';
|
|||
export const GATE = 'Quality decision';
|
||||
const PREFIX = '<!-- quality-request ';
|
||||
const SHA = /^[a-f0-9]{40}$/u;
|
||||
// GitHub App bots do not have collaborator permissions; trust exact account identities instead.
|
||||
const TRUSTED_BOTS = [{ login: 'lia-by-librechat[bot]', id: 328778573 }] as const;
|
||||
const positive = (value: number) => Number.isSafeInteger(value) && value > 0;
|
||||
|
||||
export function parseCommand(body: string) {
|
||||
|
|
@ -47,7 +49,7 @@ export interface Request extends Command {
|
|||
export interface Comment {
|
||||
id: number;
|
||||
body: string;
|
||||
user: { login: string; type: string };
|
||||
user: { login: string; type: string; id?: number };
|
||||
}
|
||||
interface Pull {
|
||||
number: number;
|
||||
|
|
@ -97,7 +99,7 @@ export function readRequest(comment: Comment): Request | undefined {
|
|||
![value.request, value.pr, value.run, value.attempt].every(positive) ||
|
||||
!SHA.test(value.head) ||
|
||||
!SHA.test(value.base) ||
|
||||
!/^[a-z\d-]+$/iu.test(value.actor) ||
|
||||
!/^[a-z\d-]+(?:\[bot\])?$/iu.test(value.actor) ||
|
||||
!['queued', 'running', 'completed', 'ignored'].includes(value.phase) ||
|
||||
typeof value.staticMutants !== 'boolean' ||
|
||||
!Array.isArray(value.scopes) ||
|
||||
|
|
@ -303,16 +305,22 @@ async function updateGate(api: API, context: Context, pr: number) {
|
|||
|
||||
/** Authorization precedes parsing or any expensive work. No comment text is executed. */
|
||||
export async function acceptRequest(api: API, context: Context, pr: number, comment: Comment) {
|
||||
if (comment.user.type === 'Bot' || !/^\/quality(?:\s|$)/u.test(comment.body.trim())) return;
|
||||
const permission = await api.request<{
|
||||
permission: string;
|
||||
user?: { permissions?: { push?: boolean } };
|
||||
}>('GET', `collaborators/${encodeURIComponent(comment.user.login)}/permission`);
|
||||
if (
|
||||
!permission.user?.permissions?.push &&
|
||||
!['admin', 'maintain', 'write'].includes(permission.permission)
|
||||
)
|
||||
return;
|
||||
if (!/^\/quality(?:\s|$)/u.test(comment.body.trim())) return;
|
||||
const trustedBot =
|
||||
comment.user.type === 'Bot' &&
|
||||
TRUSTED_BOTS.some(({ login, id }) => comment.user.login === login && comment.user.id === id);
|
||||
if (!trustedBot) {
|
||||
if (comment.user.type !== 'User') return;
|
||||
const permission = await api.request<{
|
||||
permission: string;
|
||||
user?: { permissions?: { push?: boolean } };
|
||||
}>('GET', `collaborators/${encodeURIComponent(comment.user.login)}/permission`);
|
||||
if (
|
||||
!permission.user?.permissions?.push &&
|
||||
!['admin', 'maintain', 'write'].includes(permission.permission)
|
||||
)
|
||||
return;
|
||||
}
|
||||
const pull = await api.request<Pull>('GET', `pulls/${pr}`);
|
||||
if (pull.state !== 'open') return;
|
||||
let command: Command;
|
||||
|
|
|
|||
|
|
@ -28,6 +28,11 @@ const command = (id = 10, body = '/quality both scopes=all static=off'): Comment
|
|||
body,
|
||||
user: { login: 'maintainer', type: 'User' },
|
||||
});
|
||||
const lia = {
|
||||
login: 'lia-by-librechat[bot]',
|
||||
type: 'Bot',
|
||||
id: 328778573,
|
||||
} satisfies Comment['user'];
|
||||
const record: Request = {
|
||||
request: 10,
|
||||
pr: 1,
|
||||
|
|
@ -190,7 +195,7 @@ test('commands validate choices and lists as data, with all scopes and static of
|
|||
}
|
||||
});
|
||||
|
||||
test('read access, triage, bots and closed PRs cannot start work or clear the gate', async () => {
|
||||
test('read access, triage, untrusted bots and closed PRs cannot start work or clear the gate', async () => {
|
||||
for (const permission of ['read', 'triage', 'none']) {
|
||||
const api = new GitHub();
|
||||
api.permission = permission;
|
||||
|
|
@ -213,6 +218,103 @@ async function accept(api: GitHub, ctx: Context, pr: number, comment: Comment) {
|
|||
return result;
|
||||
}
|
||||
|
||||
test('trusted bots start exact-head requests without collaborator permissions and publish completion', async () => {
|
||||
const api = new GitHub();
|
||||
api.permission = 'none';
|
||||
const accepted = await accept(api, context, 1, { ...command(), user: lia });
|
||||
assert.equal(accepted?.request.actor, lia.login);
|
||||
assert.equal(accepted?.request.head, head);
|
||||
assert.equal(
|
||||
api.calls.some(({ path }) => path.endsWith('/permission')),
|
||||
false,
|
||||
);
|
||||
assert.equal(readRequest(api.comments[0])?.actor, lia.login);
|
||||
assert.equal(api.statuses.at(-1)?.state, 'pending');
|
||||
await publishRun(api, context, run());
|
||||
assert.equal(readRequest(api.comments[0])?.conclusion, 'success');
|
||||
assert.equal(api.statuses.at(-1)?.state, 'success');
|
||||
api.head = next;
|
||||
await refreshPull(api, context, 1);
|
||||
assert.match(api.comments[0].body, /Outdated:/);
|
||||
assert.equal(api.statuses.at(-1)?.state, 'pending');
|
||||
});
|
||||
|
||||
test('only exact trusted bot identities can submit commands or disturb an active request', async () => {
|
||||
for (const user of [
|
||||
{ ...lia, id: undefined },
|
||||
{ ...lia, id: lia.id + 1 },
|
||||
{ ...lia, login: 'untrusted[bot]' },
|
||||
{ ...lia, login: 'lia-by-librechat-copy[bot]' },
|
||||
{ ...lia, type: 'Organization' },
|
||||
bot('').user,
|
||||
]) {
|
||||
for (const body of ['/quality check-all', '/quality ignore', '/quality both static=on']) {
|
||||
const api = new GitHub();
|
||||
running(api);
|
||||
const original = api.comments[0].body;
|
||||
assert.equal(
|
||||
await acceptRequest(api, { ...context, run: 56 }, 1, { ...command(11, body), user }),
|
||||
undefined,
|
||||
);
|
||||
assert.equal(api.calls.length, 0);
|
||||
assert.equal(api.comments.length, 1);
|
||||
assert.equal(api.comments[0].body, original);
|
||||
assert.equal(api.runs.get(55)?.status, 'in_progress');
|
||||
assert.equal(api.statuses.length, 0);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('trusted command bots cannot forge publisher records', () => {
|
||||
const body = renderRequest({ ...record, actor: lia.login }, context, head);
|
||||
assert.equal(readRequest({ ...command(), body, user: lia }), undefined);
|
||||
assert.equal(readRequest(bot(body))?.actor, lia.login);
|
||||
for (const actor of ['lia-by-librechat[bot]extra', 'lia-by-librechat[other]', 'bad\nactor']) {
|
||||
assert.equal(readRequest(bot(renderRequest({ ...record, actor }, context, head))), undefined);
|
||||
}
|
||||
});
|
||||
|
||||
test('trusted bots respect closed PRs and command validation before cancelling work', async () => {
|
||||
const api = new GitHub();
|
||||
running(api);
|
||||
api.state = 'closed';
|
||||
assert.equal(
|
||||
await acceptRequest(api, { ...context, run: 56 }, 1, { ...command(11), user: lia }),
|
||||
undefined,
|
||||
);
|
||||
assert.equal(api.comments.length, 1);
|
||||
assert.equal(api.statuses.length, 0);
|
||||
api.state = 'open';
|
||||
assert.equal(
|
||||
await acceptRequest(api, { ...context, run: 56 }, 1, {
|
||||
...command(11, '/quality mutation scopes=server'),
|
||||
user: lia,
|
||||
}),
|
||||
undefined,
|
||||
);
|
||||
assert.equal(cancellations(api).length, 0);
|
||||
assert.equal(api.runs.get(55)?.status, 'in_progress');
|
||||
assert.equal(api.statuses.length, 0);
|
||||
assert.ok(api.calls.every(({ path }) => !path.endsWith('/reactions')));
|
||||
});
|
||||
|
||||
test('trusted bots can explicitly ignore only after stopping the active quality run', async () => {
|
||||
const api = new GitHub();
|
||||
api.permission = 'none';
|
||||
running(api);
|
||||
const accepted = await acceptRequest(api, { ...context, run: 56 }, 1, {
|
||||
...command(11, '/quality ignore'),
|
||||
user: lia,
|
||||
});
|
||||
assert.equal(accepted?.request.actor, lia.login);
|
||||
assert.equal(cancellations(api).length, 1);
|
||||
const stoppedAt = api.calls.findIndex(({ path }) => path.endsWith('/force-cancel'));
|
||||
const clearedAt = api.calls.findIndex(({ path }) => path.startsWith('statuses/'));
|
||||
assert.ok(stoppedAt < clearedAt);
|
||||
assert.equal(readRequest(api.comments[1])?.check, 'ignore');
|
||||
assert.equal(api.statuses.at(-1)?.state, 'success');
|
||||
});
|
||||
|
||||
test('one exact-head request comment moves through progress and completion with artifact links', async () => {
|
||||
const api = new GitHub();
|
||||
const accepted = await accept(api, context, 1, command());
|
||||
|
|
|
|||
|
|
@ -180,6 +180,19 @@ test('PR updates retain PostgreSQL contracts without starting source quality rep
|
|||
assert.equal(report.steps[prewarm].with?.['working-directory'], 'source');
|
||||
});
|
||||
|
||||
test('comment commands from humans and bots reach trusted admission before report jobs', () => {
|
||||
const request = workflow.jobs.request;
|
||||
assert.equal(
|
||||
request.if,
|
||||
"github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/quality')",
|
||||
);
|
||||
assert.equal(checkouts(request)[0].with?.ref, '${{ github.sha }}');
|
||||
assert.ok(request.steps.some(({ run }) => run === 'node scripts/quality/comment.mts request'));
|
||||
for (const name of ['verify', 'select']) {
|
||||
assert.match(workflow.jobs[name].if!, /needs\.request\.outputs\.accepted == 'true'/);
|
||||
}
|
||||
});
|
||||
|
||||
test('comment publication is trusted and serialized while PR execution stays read-only', (t) => {
|
||||
assert.ok(workflow.on.pull_request?.paths.includes('scripts/quality/**'));
|
||||
assert.equal(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue