Skip to content

Commit

Permalink
feat: use get-github-auth-token instead of gh auth token (#1533)
Browse files Browse the repository at this point in the history
## 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`.

💖
  • Loading branch information
JoshuaKGoldberg committed May 26, 2024
1 parent 834a44d commit 9eef9f6
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"all-contributors-for-repository": "^0.2.1",
"chalk": "^5.3.0",
"execa": "^9.0.0",
"get-github-auth-token": "^0.1.0",
"git-remote-origin-url": "^4.0.0",
"git-url-parse": "^14.0.0",
"js-yaml": "^4.1.0",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 15 additions & 8 deletions src/shared/options/getGitHub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { describe, expect, it, vi } from "vitest";

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

const mock$ = vi.fn();
const mockGetGitHubAuthToken = vi.fn();

vi.mock("execa", () => ({
get $() {
return mock$;
vi.mock("get-github-auth-token", () => ({
get getGitHubAuthToken() {
return mockGetGitHubAuthToken;
},
}));

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

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

await expect(getGitHub).rejects.toMatchInlineSnapshot(
"[Error: GitHub authentication failed.]",
);
});

it("returns a new Octokit when gh auth status succeeds", async () => {
it("returns a new Octokit when getGitHubAuthToken succeeds", async () => {
const auth = "abc123";
mock$.mockResolvedValueOnce({}).mockResolvedValueOnce({ stdout: auth });

mockGetGitHubAuthToken.mockResolvedValue({
succeeded: true,
token: auth,
});

const actual = await getGitHub();

Expand Down
15 changes: 7 additions & 8 deletions src/shared/options/getGitHub.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { $ } from "execa";
import { getGitHubAuthToken } from "get-github-auth-token";
import { Octokit } from "octokit";

export interface GitHub {
Expand All @@ -7,16 +7,15 @@ export interface GitHub {
}

export async function getGitHub(): Promise<GitHub | undefined> {
try {
await $`gh auth status`;
} catch (error) {
const auth = await getGitHubAuthToken();

if (!auth.succeeded) {
throw new Error("GitHub authentication failed.", {
cause: (error as Error).message,
cause: auth.error,
});
}

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

return { auth, octokit };
return { auth: auth.token, octokit };
}
1 change: 1 addition & 0 deletions src/steps/uninstallPackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export async function uninstallPackages(offline: boolean | undefined) {
"all-contributors-for-repository",
"chalk",
"execa",
"get-github-auth-token",
"git-remote-origin-url",
"git-url-parse",
"lazy-value",
Expand Down

0 comments on commit 9eef9f6

Please sign in to comment.