Skip to content

feat: use get-github-auth-token instead of gh auth token #1533

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

Merged
merged 2 commits into from
May 26, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
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
@@ -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;
},
}));

@@ -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();

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 {
@@ -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
@@ -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",