Skip to content

Commit 27870e1

Browse files
committed
🐛 Preserve unary NOT in GitHub search terms (#815)
1 parent 1ee2671 commit 27870e1

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

‎packages/git/src/composition/components/GitHubSearch.ts‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,13 @@ function admitQuery(query: string): SearchRequest["kind"] {
8080
"select exactly one kind with is:issue or is:pr.",
8181
);
8282
}
83-
const operators = matches.map(([token]) => token);
83+
const tokens = matches.map(([token]) => token);
84+
const qualifierAt = (index: number) => /^[+-]*[\w-]+:/u.test(tokens[index] ?? "");
8485
if (
85-
operators.some(
86-
(token, index) =>
87-
/^(?:AND|OR|NOT)$/u.test(token) &&
88-
[operators[index - 1], operators[index + 1]].some((operand) =>
89-
/^[+-]*[\w-]+:/u.test(operand ?? ""),
90-
),
86+
tokens.some((token, index) =>
87+
token === "NOT"
88+
? qualifierAt(index + 1)
89+
: /^(?:AND|OR)$/u.test(token) && (qualifierAt(index - 1) || qualifierAt(index + 1)),
9190
)
9291
) {
9392
throw new GitOperationAdmissionError(

‎packages/git/tests/github-workflow-activation.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ describe("GitHub repository search", () => {
331331
],
332332
[checkout.root, 'query="(is:pr) is:open"', "parentheses"],
333333
[checkout.root, 'query="is:pr is:open OR label:bug"', "Boolean operators"],
334+
[checkout.root, 'query="is:pr NOT label:bug"', "Boolean operators"],
334335
[checkout.root, 'query="is:pr"', "has not authorized"],
335336
[local.root, 'query="is:pr"', "does not name a GitHub repository"],
336337
[outside, 'query="is:pr"', "repository"],
@@ -348,6 +349,28 @@ describe("GitHub repository search", () => {
348349
expect(provider.requests).toEqual([]);
349350
});
350351

352+
it("forwards native Boolean operators on search terms", function* () {
353+
const remote = yield* useBareRemote(REMOTE);
354+
const checkout = yield* useNamedOriginCheckout(remote, REPOSITORY);
355+
const root = yield* useTempDirectory("github-search");
356+
const provider = searchProvider([REPOSITORY], () => ({
357+
status: 200,
358+
body: JSON.stringify({ ...FIRST_PAGE, total_count: 1 }),
359+
}));
360+
const queries = ["is:pr NOT wip", "is:pr foo OR bar"];
361+
const source = queries
362+
.map((query, index) => `<GitHub.Search query={${JSON.stringify(query)}} as="page${index}" />`)
363+
.join("\n");
364+
yield* runOrdinaryDocument(source, {
365+
cwd: checkout.root,
366+
root,
367+
gitHubPullRequests: provider.options,
368+
});
369+
expect(provider.requests.map(({ url }) => new URL(url).searchParams.get("q"))).toEqual(
370+
queries.map((query) => `repo:octo/project ${query}`),
371+
);
372+
});
373+
351374
it("binds all 30 items on page 34 when GitHub returns a full terminal page", function* () {
352375
const remote = yield* useBareRemote(REMOTE);
353376
const checkout = yield* useNamedOriginCheckout(remote, REPOSITORY);

0 commit comments

Comments
 (0)