-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: main two places, process.env.GH_TOKEN and gh auth token
- Loading branch information
1 parent
1be4165
commit 1653257
Showing
10 changed files
with
140 additions
and
64 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,78 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
import { getGitHubAuthToken } from "./getGitHubAuthToken.js"; | ||
|
||
const mockExec = vi.fn(); | ||
|
||
vi.mock("node:util", () => ({ | ||
get promisify() { | ||
return () => mockExec; | ||
}, | ||
})); | ||
|
||
describe("getGitHubAuthToken", () => { | ||
beforeEach(() => { | ||
process.env.GH_TOKEN = ""; | ||
}); | ||
|
||
it("returns GH_TOKEN when it exists and is truthy", async () => { | ||
const GH_TOKEN = "gho_abc123"; | ||
|
||
process.env.GH_TOKEN = GH_TOKEN; | ||
|
||
const actual = await getGitHubAuthToken(); | ||
|
||
expect(actual).toEqual({ succeeded: true, token: GH_TOKEN }); | ||
expect(mockExec).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("returns the token when gh auth token prints a token", async () => { | ||
const stdout = "gho_abc123"; | ||
|
||
mockExec.mockResolvedValue({ stdout }); | ||
|
||
const actual = await getGitHubAuthToken(); | ||
|
||
expect(actual).toEqual({ succeeded: true, token: stdout }); | ||
}); | ||
|
||
it("returns the gh error when gh auth token and gh error", async () => { | ||
const stderr = "Oh no!"; | ||
|
||
mockExec.mockResolvedValueOnce({ stderr: "auth token issue" }); | ||
mockExec.mockResolvedValueOnce({ stderr }); | ||
|
||
const actual = await getGitHubAuthToken(); | ||
|
||
expect(actual).toEqual({ | ||
error: `Could not run \`gh\`: ${stderr}`, | ||
succeeded: false, | ||
}); | ||
}); | ||
|
||
it("returns the gh auth token error when only gh auth token errors", async () => { | ||
const stderr = "Oh no!"; | ||
|
||
mockExec.mockResolvedValueOnce({ stderr }); | ||
mockExec.mockResolvedValueOnce({ stdout: "" }); | ||
|
||
const actual = await getGitHubAuthToken(); | ||
|
||
expect(actual).toEqual({ | ||
error: stderr, | ||
succeeded: false, | ||
}); | ||
}); | ||
|
||
it("returns undefined when both gh and gh auth token print nothing", async () => { | ||
mockExec.mockResolvedValueOnce({ stderr: "", stdout: "" }); | ||
mockExec.mockResolvedValueOnce({ stderr: "", stdout: "" }); | ||
|
||
const actual = await getGitHubAuthToken(); | ||
|
||
expect(actual).toEqual({ | ||
error: undefined, | ||
succeeded: false, | ||
}); | ||
}); | ||
}); |
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,27 @@ | ||
import * as cp from "node:child_process"; | ||
import * as util from "node:util"; | ||
|
||
import { GitHubAuthToken } from "./types.js"; | ||
|
||
export async function getGitHubAuthToken(): Promise<GitHubAuthToken> { | ||
if (process.env.GH_TOKEN) { | ||
return { succeeded: true, token: process.env.GH_TOKEN }; | ||
} | ||
|
||
const exec = util.promisify(cp.exec); | ||
const token = await exec("gh auth token"); | ||
|
||
if (token?.stdout) { | ||
return { succeeded: true, token: token.stdout }; | ||
} | ||
|
||
const help = await exec("gh"); | ||
|
||
return { | ||
error: | ||
(help.stderr && `Could not run \`gh\`: ${help.stderr}`) || | ||
token?.stderr || | ||
undefined, | ||
succeeded: false, | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./greet.js"; | ||
export * from "./getGitHubAuthToken.js"; | ||
export * from "./types.js"; |
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
export interface GreetOptions { | ||
logger?: (message: string) => void; | ||
message: string; | ||
times?: number; | ||
export interface GitHubAuthTokenFailure { | ||
error: string | undefined; | ||
succeeded: false; | ||
} | ||
|
||
export interface GitHubAuthTokenSuccess { | ||
succeeded: true; | ||
token: string; | ||
} | ||
|
||
export type GitHubAuthToken = GitHubAuthTokenFailure | GitHubAuthTokenSuccess; |