-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add opt-in --preserve-generated-from option (#940)
## PR Checklist - [x] Addresses an existing open issue: fixes #913 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview GitHub doesn't have a unified _"create a new blank repository"_ API. So unless the new `--preserve-generated-from` option is true, repo creation will check whether the authenticated user has the same login as the `--owner` option, then use either `createForAuthenticatedUser` or `createInOrg. Slightly rephrases the end-of-README notice while I'm here to remove my username from it. And in doing so, expands the _"does the notice already exist?"_ logic to check previous variants.
- Loading branch information
1 parent
766a735
commit fc1eda7
Showing
15 changed files
with
239 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Octokit } from "octokit"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
|
||
import { createRepositoryWithApi } from "./createRepositoryWithApi.js"; | ||
|
||
const options = { owner: "StubOwner", repository: "stub-repository" }; | ||
|
||
const mockCreateUsingTemplate = vi.fn(); | ||
const mockCreateInOrg = vi.fn(); | ||
const mockCreateForAuthenticatedUser = vi.fn(); | ||
const mockGetAuthenticated = vi.fn(); | ||
|
||
const createMockOctokit = () => | ||
({ | ||
rest: { | ||
repos: { | ||
createForAuthenticatedUser: mockCreateForAuthenticatedUser, | ||
createInOrg: mockCreateInOrg, | ||
createUsingTemplate: mockCreateUsingTemplate, | ||
}, | ||
users: { | ||
getAuthenticated: mockGetAuthenticated, | ||
}, | ||
}, | ||
}) as unknown as Octokit; | ||
|
||
describe("createRepositoryWithApi", () => { | ||
it("creates using a template when preserveGeneratedFrom is true", async () => { | ||
await createRepositoryWithApi(createMockOctokit(), { | ||
...options, | ||
preserveGeneratedFrom: true, | ||
}); | ||
|
||
expect(mockCreateForAuthenticatedUser).not.toHaveBeenCalled(); | ||
expect(mockCreateInOrg).not.toHaveBeenCalled(); | ||
expect(mockCreateUsingTemplate).toHaveBeenCalledWith({ | ||
name: options.repository, | ||
owner: options.owner, | ||
template_owner: "JoshuaKGoldberg", | ||
template_repo: "create-typescript-app", | ||
}); | ||
}); | ||
|
||
it("creates under the user when the user is the owner", async () => { | ||
mockGetAuthenticated.mockResolvedValueOnce({ | ||
data: { | ||
login: options.owner, | ||
}, | ||
}); | ||
await createRepositoryWithApi(createMockOctokit(), options); | ||
|
||
expect(mockCreateForAuthenticatedUser).toHaveBeenCalledWith({ | ||
name: options.repository, | ||
}); | ||
expect(mockCreateInOrg).not.toHaveBeenCalled(); | ||
expect(mockCreateUsingTemplate).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("creates under an org when the user is not the owner", async () => { | ||
const login = "other-user"; | ||
mockGetAuthenticated.mockResolvedValueOnce({ data: { login } }); | ||
await createRepositoryWithApi(createMockOctokit(), options); | ||
|
||
expect(mockCreateForAuthenticatedUser).not.toHaveBeenCalled(); | ||
expect(mockCreateInOrg).toHaveBeenCalledWith({ | ||
name: options.repository, | ||
org: options.owner, | ||
}); | ||
expect(mockCreateUsingTemplate).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Octokit } from "octokit"; | ||
|
||
export interface CreateRepositoryWithApiOptions { | ||
owner: string; | ||
preserveGeneratedFrom?: boolean; | ||
repository: string; | ||
} | ||
|
||
export async function createRepositoryWithApi( | ||
octokit: Octokit, | ||
options: CreateRepositoryWithApiOptions, | ||
) { | ||
if (options.preserveGeneratedFrom) { | ||
await octokit.rest.repos.createUsingTemplate({ | ||
name: options.repository, | ||
owner: options.owner, | ||
template_owner: "JoshuaKGoldberg", | ||
template_repo: "create-typescript-app", | ||
}); | ||
return; | ||
} | ||
|
||
const currentUser = await octokit.rest.users.getAuthenticated(); | ||
|
||
if (currentUser.data.login === options.owner) { | ||
await octokit.rest.repos.createForAuthenticatedUser({ | ||
name: options.repository, | ||
}); | ||
} else { | ||
await octokit.rest.repos.createInOrg({ | ||
name: options.repository, | ||
org: options.owner, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.