Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct create-repository-by-default logic #922

Merged
merged 2 commits into from
Oct 1, 2023
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
42 changes: 26 additions & 16 deletions src/shared/options/ensureRepositoryExists.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const createMockOctokit = () =>
describe("ensureRepositoryExists", () => {
it("returns the repository when octokit is undefined", async () => {
const actual = await ensureRepositoryExists(undefined, {
mode: "initialize",
owner,
repository,
});
Expand All @@ -51,6 +52,7 @@ describe("ensureRepositoryExists", () => {
const actual = await ensureRepositoryExists(
{ auth, octokit },
{
mode: "initialize",
owner,
repository,
},
Expand All @@ -67,10 +69,7 @@ describe("ensureRepositoryExists", () => {

const actual = await ensureRepositoryExists(
{ auth, octokit },
{
owner,
repository,
},
{ mode: "initialize", owner, repository },
);

expect(actual).toEqual({ github: { auth, octokit }, repository });
Expand All @@ -82,6 +81,26 @@ describe("ensureRepositoryExists", () => {
});
});

it("defaults to creating a repository when mode is 'create'", async () => {
const octokit = createMockOctokit();

mockDoesRepositoryExist.mockResolvedValue(false);

const actual = await ensureRepositoryExists(
{ auth, octokit },
{ mode: "create", owner, repository },
);

expect(actual).toEqual({ github: { auth, octokit }, repository });
expect(octokit.rest.repos.createUsingTemplate).toHaveBeenCalledWith({
name: repository,
owner,
template_owner: "JoshuaKGoldberg",
template_repo: "create-typescript-app",
});
expect(mockSelect).not.toHaveBeenCalled();
});

it("returns the second repository when the prompt is 'different', the first repository does not exist, and the second repository exists", async () => {
const octokit = createMockOctokit();
const newRepository = "new-repository";
Expand All @@ -94,10 +113,7 @@ describe("ensureRepositoryExists", () => {

const actual = await ensureRepositoryExists(
{ auth, octokit },
{
owner,
repository,
},
{ mode: "initialize", owner, repository },
);

expect(actual).toEqual({
Expand All @@ -119,10 +135,7 @@ describe("ensureRepositoryExists", () => {

const actual = await ensureRepositoryExists(
{ auth, octokit },
{
owner,
repository,
},
{ mode: "initialize", owner, repository },
);

expect(actual).toEqual({
Expand All @@ -145,10 +158,7 @@ describe("ensureRepositoryExists", () => {

const actual = await ensureRepositoryExists(
{ auth, octokit },
{
owner,
repository,
},
{ mode: "initialize", owner, repository },
);

expect(actual).toEqual({ octokit: undefined, repository });
Expand Down
42 changes: 24 additions & 18 deletions src/shared/options/ensureRepositoryExists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { GitHub } from "./getGitHub.js";

export type EnsureRepositoryExistsOptions = Pick<
Options,
"owner" | "repository"
"mode" | "owner" | "repository"
>;

export interface RepositoryExistsResult {
Expand All @@ -19,7 +19,9 @@ export async function ensureRepositoryExists(
github: GitHub | undefined,
options: EnsureRepositoryExistsOptions,
): Promise<Partial<RepositoryExistsResult>> {
// We'll only respect input options once before prompting for them
let { repository } = options;
let createRepository = options.mode === "create";

// We'll continuously pester the user for a repository
// until they bail, create a new one, or it exists.
Expand All @@ -28,23 +30,27 @@ export async function ensureRepositoryExists(
return { github, repository };
}

const selection = filterPromptCancel(
(await prompts.select({
message: `Repository ${options.repository} doesn't seem to exist under ${options.owner}. What would you like to do?`,
options: [
{ label: "Create a new repository", value: "create" },
{
label: "Switch to a different repository name",
value: "different",
},
{
label: "Keep changes local",
value: "local",
},
{ label: "Bail out and maybe try again later", value: "bail" },
],
})) as "bail" | "create" | "different" | "local",
);
const selection = createRepository
? "create"
: filterPromptCancel(
(await prompts.select({
message: `Repository ${options.repository} doesn't seem to exist under ${options.owner}. What would you like to do?`,
options: [
{ label: "Create a new repository", value: "create" },
{
label: "Switch to a different repository name",
value: "different",
},
{
label: "Keep changes local",
value: "local",
},
{ label: "Bail out and maybe try again later", value: "bail" },
],
})) as "bail" | "create" | "different" | "local",
);

createRepository = false;

switch (selection) {
case undefined:
Expand Down
1 change: 1 addition & 0 deletions src/shared/options/readOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export async function readOptions(
? undefined
: await withSpinner("Checking GitHub authentication", getGitHub),
{
mode,
owner: options.owner,
repository: options.repository,
},
Expand Down