Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/search-terms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'gcb-core': minor
'gcb-client': minor
---

Search actions accept multiple positional terms (or one quoted phrase): the final positional is
repeatable, and each term is passed to `gh` as its own argument after `--`. Repository-scope
qualifier rejection applies to every term.
20 changes: 18 additions & 2 deletions packages/client/src/commands/gh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import { remoteRepo } from './remotes.ts';
const ACTION_LINES = Object.values(GH_ACTIONS)
.map((action) => {
const positionals = action.positionals
.map((positional) => (positional.required === true ? `<${positional.name}>` : `[${positional.name}]`))
.map((positional) => {
const repeat = positional.repeatable === true ? '...' : '';
return positional.required === true
? `<${positional.name}${repeat}>`
: `[${positional.name}${repeat}]`;
})
.join(' ');
const flags = action.flags
.map((flag) => {
Expand Down Expand Up @@ -300,15 +305,26 @@ export async function runGh(

const values: Record<string, string | boolean | string[]> = {};
const bodyFile = args.value('body-file') !== undefined;
let repeatable = false;
for (const [index, positional] of action.positionals.entries()) {
if (positional.repeatable === true) {
const remaining = args.positionals.slice(index);
if (remaining.length === 0) {
if (positional.required === true) fail(`gcb gh ${domain} ${command} needs ${positional.name}`);
} else {
values[positional.name] = remaining;
}
repeatable = true;
break;
}
const value = args.positionals[index];
if (value === undefined) {
if (positional.required === true) fail(`gcb gh ${domain} ${command} needs ${positional.name}`);
continue;
}
values[positional.name] = value;
}
if (args.positionals.length > action.positionals.length) {
if (!repeatable && args.positionals.length > action.positionals.length) {
fail(`gcb gh ${domain} ${command} has too many positional arguments`);
}
for (const flag of action.flags) {
Expand Down
39 changes: 25 additions & 14 deletions packages/core/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface PositionalSpec {
readonly name: string;
readonly type: 'number' | 'string';
readonly required?: boolean;
/** The final positional collects all remaining arguments as a list. */
readonly repeatable?: boolean;
}

/** A long flag an action accepts. */
Expand Down Expand Up @@ -761,7 +763,7 @@ const LABEL_DELETE: GhAction = {
const SEARCH_ISSUES: GhAction = {
name: 'search:issues',
command: ['search', 'issues'],
positionals: [{ name: 'query', type: 'string' }],
positionals: [{ name: 'query', type: 'string', repeatable: true }],
flags: [
{ name: 'assignee', type: 'string' },
{ name: 'author', type: 'string' },
Expand All @@ -785,7 +787,7 @@ const SEARCH_ISSUES: GhAction = {
const SEARCH_PRS: GhAction = {
name: 'search:prs',
command: ['search', 'prs'],
positionals: [{ name: 'query', type: 'string' }],
positionals: [{ name: 'query', type: 'string', repeatable: true }],
flags: [
{ name: 'assignee', type: 'string' },
{ name: 'author', type: 'string' },
Expand Down Expand Up @@ -956,10 +958,16 @@ export function buildGhArgv(
if (positional.required === true) throw new ActionError(`${name} needs ${positional.name}`);
continue;
}
if (positional.type === 'number') {
argv.push(String(asNumber(value, positional.name)));
} else {
trailingPositionals.push(asString(value, positional.name));
const items = positional.repeatable && Array.isArray(value) ? value : [value];
if (positional.repeatable !== true && Array.isArray(value)) {
throw new ActionError(`positional ${positional.name} is not repeatable`);
}
for (const item of items) {
if (positional.type === 'number') {
argv.push(String(asNumber(item, positional.name)));
} else {
trailingPositionals.push(asString(item, positional.name));
}
}
}
}
Expand Down Expand Up @@ -1053,14 +1061,17 @@ export function buildGhArgv(
}
}

if (
action.command[0] === 'search' &&
typeof values['query'] === 'string' &&
/(?:^|[\s(])-?(?:repo|org|user):/iu.test(values['query'])
) {
throw new ActionError(
'search query cannot contain repo:, org: or user:; the broker scopes it to --repo',
);
if (action.command[0] === 'search') {
const terms = Array.isArray(values['query']) ? values['query'] : [values['query']];
if (
terms.some(
(term) => typeof term === 'string' && /(?:^|[\s(])-?(?:repo|org|user):/iu.test(term),
)
) {
throw new ActionError(
'search query cannot contain repo:, org: or user:; the broker scopes it to --repo',
);
}
}
if (typeof values['title'] === 'string') {
if (values['title'].trim() === '') {
Expand Down
19 changes: 19 additions & 0 deletions test/unit/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ describe('buildGhArgv', () => {
'bug',
],
);
for (const query of [
['bug', 'repo:other/repo'],
['is:issue', '(org:other)'],
['ok', '-user:x'],
]) {
assert.throws(
() => buildGhArgv('search:issues', { query }, 'acme/widget'),
/cannot contain repo:, org: or user:/u,
);
}
for (const query of [
'repo:other/repo bug',
'org:other bug',
Expand All @@ -295,6 +305,15 @@ describe('buildGhArgv', () => {
}
assert.doesNotThrow(() => buildGhArgv('search:issues', { query: 'is:issue label:bug' }, 'acme/widget'));

assert.deepEqual(
buildGhArgv('search:issues', { query: ['is:issue', 'label:bug'] }, 'acme/widget'),
['search', 'issues', '--repo', 'acme/widget', '--', 'is:issue', 'label:bug'],
);
assert.deepEqual(
buildGhArgv('search:issues', { query: ['authentication bug'] }, 'acme/widget'),
['search', 'issues', '--repo', 'acme/widget', '--', 'authentication bug'],
);

assert.deepEqual(
buildGhArgv('search:prs', { query: 'fix', draft: true }, 'acme/widget'),
['search', 'prs', '--draft', '--repo', 'acme/widget', '--', 'fix'],
Expand Down
41 changes: 41 additions & 0 deletions test/unit/gh-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,47 @@ describe('gcb gh', () => {
}
});

it('preserves multiple search terms and quoted phrases through the client', async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gcb-gh-search-'));
const socketPath = path.join(root, 'broker.sock');
const block: StaticHostConfig = { provider: 'static', allow: ['acme/widget'] };
const config: BrokerConfig = {
socketPath,
socketMode: 0o600,
auditPath: null,
tokenCacheSkewSeconds: 300,
hosts: { 'github.com': block },
};
const audit: AuditSink = { path: null, record: () => {}, close: () => Promise.resolve() };
const seen: unknown[] = [];
const provider: Provider = {
name: 'static',
getCredential: async () => ({ username: 'u', password: 'p', expiresAt: Date.now() + 1000 }),
runAction: async (request) => {
seen.push(request.values['query']);
return { stdout: '', stderr: '', code: 0, signal: null, truncated: false, timedOut: false };
},
};
const broker = await startBroker({ config, audit, providers: new Map([['github.com', provider]]) });
try {
const terms = await runGhCli(
['search', 'issues', 'is:issue', 'label:bug', '--repo', 'acme/widget', '--socket', socketPath],
root,
);
assert.equal(terms.status, 0, terms.stderr);
assert.deepEqual(seen[0], ['is:issue', 'label:bug']);
const phrase = await runGhCli(
['search', 'issues', 'authentication bug', '--repo', 'acme/widget', '--socket', socketPath],
root,
);
assert.equal(phrase.status, 0, phrase.stderr);
assert.deepEqual(seen[1], ['authentication bug']);
} finally {
await broker.close();
fs.rmSync(root, { recursive: true, force: true });
}
});

it('reports truncation and timed-out writes', async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gcb-gh-output-'));
const socketPath = path.join(root, 'broker.sock');
Expand Down