Skip to content

Commit 9eef9f6

Browse files
feat: use get-github-auth-token instead of gh auth token (#1533)
## PR Checklist - [x] Addresses an existing open issue: fixes #1532 - [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 Swaps out `gh auth token` with `await getGitHubAuthToken()`. Doesn't fully remove the dependency on `gh`. 💖
1 parent 834a44d commit 9eef9f6

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"all-contributors-for-repository": "^0.2.1",
4646
"chalk": "^5.3.0",
4747
"execa": "^9.0.0",
48+
"get-github-auth-token": "^0.1.0",
4849
"git-remote-origin-url": "^4.0.0",
4950
"git-url-parse": "^14.0.0",
5051
"js-yaml": "^4.1.0",

pnpm-lock.yaml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/shared/options/getGitHub.test.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import { describe, expect, it, vi } from "vitest";
22

33
import { getGitHub } from "./getGitHub.js";
44

5-
const mock$ = vi.fn();
5+
const mockGetGitHubAuthToken = vi.fn();
66

7-
vi.mock("execa", () => ({
8-
get $() {
9-
return mock$;
7+
vi.mock("get-github-auth-token", () => ({
8+
get getGitHubAuthToken() {
9+
return mockGetGitHubAuthToken;
1010
},
1111
}));
1212

@@ -25,17 +25,24 @@ vi.mock("octokit", () => ({
2525
}));
2626

2727
describe("getOctokit", () => {
28-
it("throws an error when gh auth status fails", async () => {
29-
mock$.mockRejectedValueOnce(new Error("Oh no!"));
28+
it("throws an error when getGitHubAuthToken fails", async () => {
29+
mockGetGitHubAuthToken.mockResolvedValue({
30+
error: "Oh no!",
31+
succeeded: false,
32+
});
3033

3134
await expect(getGitHub).rejects.toMatchInlineSnapshot(
3235
"[Error: GitHub authentication failed.]",
3336
);
3437
});
3538

36-
it("returns a new Octokit when gh auth status succeeds", async () => {
39+
it("returns a new Octokit when getGitHubAuthToken succeeds", async () => {
3740
const auth = "abc123";
38-
mock$.mockResolvedValueOnce({}).mockResolvedValueOnce({ stdout: auth });
41+
42+
mockGetGitHubAuthToken.mockResolvedValue({
43+
succeeded: true,
44+
token: auth,
45+
});
3946

4047
const actual = await getGitHub();
4148

src/shared/options/getGitHub.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { $ } from "execa";
1+
import { getGitHubAuthToken } from "get-github-auth-token";
22
import { Octokit } from "octokit";
33

44
export interface GitHub {
@@ -7,16 +7,15 @@ export interface GitHub {
77
}
88

99
export async function getGitHub(): Promise<GitHub | undefined> {
10-
try {
11-
await $`gh auth status`;
12-
} catch (error) {
10+
const auth = await getGitHubAuthToken();
11+
12+
if (!auth.succeeded) {
1313
throw new Error("GitHub authentication failed.", {
14-
cause: (error as Error).message,
14+
cause: auth.error,
1515
});
1616
}
1717

18-
const auth = (await $`gh auth token`).stdout.trim();
19-
const octokit = new Octokit({ auth });
18+
const octokit = new Octokit({ auth: auth.token });
2019

21-
return { auth, octokit };
20+
return { auth: auth.token, octokit };
2221
}

src/steps/uninstallPackages.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export async function uninstallPackages(offline: boolean | undefined) {
1111
"all-contributors-for-repository",
1212
"chalk",
1313
"execa",
14+
"get-github-auth-token",
1415
"git-remote-origin-url",
1516
"git-url-parse",
1617
"lazy-value",

0 commit comments

Comments
 (0)