Skip to content
Open
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
14 changes: 12 additions & 2 deletions sdk/typescript/src/bulk-scan-discovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,19 @@ export async function runBulkScanWizard(
}

prompt.write(`\nFound ${discovered.length} repositories.\n`);
const repositories = await selectGitHubRepositories(discovered, prompt);
const repositories = await selectGitHubRepositories(
discovered,
prompt,
signal,
);

const outputDir = resolve(
dependencies.currentDirectory(),
expandHome(
await prompt.input(
"Where should scan results be saved?",
defaultOutputDir,
signal,
),
),
);
Expand All @@ -164,7 +169,7 @@ export async function runBulkScanWizard(
`\nReady to scan ${repositories.length} repositories?\n` +
`Results: ${outputDir}\nRepository list: ${inputPath}\n`,
);
if (!(await prompt.confirm("Start scanning?"))) {
if (!(await prompt.confirm("Start scanning?", false, signal))) {
prompt.write("\nScan canceled.\n");
return null;
}
Expand Down Expand Up @@ -212,6 +217,8 @@ async function selectGitHubOwner(
return await prompt.select(
"Which account or organization should we scan?",
owners.map((owner) => ({ label: owner, value: owner })),
undefined,
signal,
);
}
prompt.write(`\nFinding repositories in ${personal}.\n`);
Expand All @@ -221,6 +228,7 @@ async function selectGitHubOwner(
async function selectGitHubRepositories(
repositories: GitHubRepository[],
prompt: BulkScanPrompt,
signal?: AbortSignal,
): Promise<GitHubRepository[]> {
const selected = new Set<string>();
while (selected.size < repositories.length) {
Expand All @@ -237,6 +245,8 @@ async function selectGitHubRepositories(
.filter(({ fullName }) => !selected.has(fullName))
.map(({ fullName }) => ({ label: fullName, value: fullName })),
],
undefined,
signal,
);
if (!choice) break;
selected.add(choice);
Expand Down
30 changes: 28 additions & 2 deletions sdk/typescript/tests-ts/bulk-scan-discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async function temporaryDirectory(): Promise<string> {
class FakePrompt implements BulkScanPrompt {
public readonly messages: string[] = [];
public readonly questions: string[] = [];
public readonly signals: (AbortSignal | undefined)[] = [];
public interactive = true;
public confirms: boolean[] = [];
public inputs: string[] = [];
Expand All @@ -51,21 +52,34 @@ class FakePrompt implements BulkScanPrompt {
this.messages.push(value);
}

public async confirm(question: string, fallback = false): Promise<boolean> {
public async confirm(
question: string,
fallback = false,
signal?: AbortSignal,
): Promise<boolean> {
this.questions.push(question);
this.signals.push(signal);
return this.confirms.shift() ?? fallback;
}

public async input(question: string, fallback = ""): Promise<string> {
public async input(
question: string,
fallback = "",
signal?: AbortSignal,
): Promise<string> {
this.questions.push(question);
this.signals.push(signal);
return this.inputs.shift() ?? fallback;
}

public async select<Value extends string>(
question: string,
options: readonly { label: string; value: Value }[],
_presentation?: unknown,
signal?: AbortSignal,
): Promise<Value> {
this.questions.push(question);
this.signals.push(signal);
this.searchOptions.push(options.map(({ label }) => label));
const value = this.choices.shift();
return (options.find((option) => option.value === value) ?? options[0]!)
Expand Down Expand Up @@ -461,4 +475,16 @@ describe("bulk scan repository discovery", () => {
expect(prompt.questions).toEqual([]);
expect(requests).toEqual([]);
});

test("passes cancellation to every setup prompt", async () => {
const root = await temporaryDirectory();
const { dependencies, prompt } = discoveryDependencies(root, {
organizations: ["acme"],
});
const signal = new AbortController().signal;

await runBulkScanWizard(dependencies, signal);

expect(prompt.signals).toEqual([signal, signal, signal, signal]);
});
});
Loading