From 3f666be5f34446baa444ed0b815d671ba2bbc1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josh=20Goldberg=20=E2=9C=A8?= Date: Sun, 15 Dec 2024 11:34:17 -0500 Subject: [PATCH] chore: use octokit-from-auth package (#437) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## PR Checklist - [x] Addresses an existing open issue: fixes #436 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/github-username-to-emails/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/github-username-to-emails/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Replaces the equivalent logic internally. 💖 --- README.md | 2 +- package.json | 4 +-- pnpm-lock.yaml | 17 +++++++--- src/getGitHubUsernameEmails.test.ts | 50 ++++------------------------- src/getGitHubUsernameEmails.ts | 26 ++------------- 5 files changed, 25 insertions(+), 74 deletions(-) diff --git a/README.md b/README.md index 61a92fd4..e81ec442 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Only publicly visible emails can be retrieved. ### Options -`auth` is retrieved with [`get-github-auth-token`](https://github.com/JoshuaKGoldberg/get-github-auth-token), which defaults to `process.env.GH_TOKEN`, or failing that, [`gh auth token`](https://cli.github.com/manual/gh_auth_token). +`auth` is retrieved with [`octokit-from-auth`](https://github.com/JoshuaKGoldberg/octokit-from-auth), which defaults to `process.env.GH_TOKEN`, or failing that, [`gh auth token`](https://cli.github.com/manual/gh_auth_token). If neither is available then an auth token must be provided as an option. | Option | Type | Description | Default | diff --git a/package.json b/package.json index fb5d46c7..17f0a7bc 100644 --- a/package.json +++ b/package.json @@ -39,8 +39,8 @@ "*": "prettier --ignore-unknown --write" }, "dependencies": { - "get-github-auth-token": "^0.1.0", - "octokit": "^4.0.0" + "octokit": "^4.0.2", + "octokit-from-auth": "^0.2.0" }, "devDependencies": { "@release-it/conventional-changelog": "^9.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2ac91ee4..213751ab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,12 +8,12 @@ importers: .: dependencies: - get-github-auth-token: - specifier: ^0.1.0 - version: 0.1.0 octokit: - specifier: ^4.0.0 + specifier: ^4.0.2 version: 4.0.2 + octokit-from-auth: + specifier: ^0.2.0 + version: 0.2.0 devDependencies: '@release-it/conventional-changelog': specifier: ^9.0.0 @@ -2965,6 +2965,10 @@ packages: resolution: {integrity: sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==} engines: {node: '>=0.10.0'} + octokit-from-auth@0.2.0: + resolution: {integrity: sha512-UMo+WQkWi1PS/HZ15SMM1sgPhhXykqPJKCCAi6R0+JAhTbLBcfeKCJ3AwUVdJBJQKSQ37BUqPpME4iGAjZQtkw==} + engines: {node: '>=18.3.0'} + octokit@4.0.2: resolution: {integrity: sha512-wbqF4uc1YbcldtiBFfkSnquHtECEIpYD78YUXI6ri1Im5OO2NLo6ZVpRdbJpdnpZ05zMrVPssNiEo6JQtea+Qg==} engines: {node: '>= 18'} @@ -6981,6 +6985,11 @@ snapshots: object-assign@4.1.1: {} + octokit-from-auth@0.2.0: + dependencies: + get-github-auth-token: 0.1.0 + octokit: 4.0.2 + octokit@4.0.2: dependencies: '@octokit/app': 15.0.1 diff --git a/src/getGitHubUsernameEmails.test.ts b/src/getGitHubUsernameEmails.test.ts index 9181f76d..4ee5c6cd 100644 --- a/src/getGitHubUsernameEmails.test.ts +++ b/src/getGitHubUsernameEmails.test.ts @@ -2,18 +2,11 @@ import { describe, expect, it, vi } from "vitest"; import { getGitHubUsernameEmails } from "./getGitHubUsernameEmails.js"; -const mockGetGitHubAuthToken = vi.fn(); - -vi.mock("get-github-auth-token", () => ({ - get getGitHubAuthToken() { - return mockGetGitHubAuthToken; - }, -})); - -vi.mock("octokit", () => ({ - Octokit: class MockOctokit { - request = vi.fn(); - }, +vi.mock("octokit-from-auth", () => ({ + octokitFromAuth: () => + Promise.resolve({ + request: vi.fn(), + }), })); vi.mock("./getAccountEmail.js", () => ({ @@ -27,38 +20,7 @@ vi.mock("./getEventsEmails.js", () => ({ const username = "abc123"; describe("getGitHubUsernameEmails", () => { - it("throws an error when no auth is provided or available", async () => { - mockGetGitHubAuthToken.mockResolvedValue({ - succeeded: false, - token: "gho_def456", - }); - - await expect( - async () => await getGitHubUsernameEmails({ username }), - ).rejects.toMatchInlineSnapshot( - `[Error: Please provide an auth token (process.env.GH_TOKEN) or log in with the GitHub CLI (gh).]`, - ); - }); - - it("throws an error an empty auth is provided", async () => { - mockGetGitHubAuthToken.mockResolvedValue({ - succeeded: true, - token: "gho_def456", - }); - - await expect( - async () => await getGitHubUsernameEmails({ auth: "", username }), - ).rejects.toMatchInlineSnapshot( - `[Error: Invalid auth provided: an empty string ('').]`, - ); - }); - - it("returns account and events emails when getGitHubAuthToken auth is valid", async () => { - mockGetGitHubAuthToken.mockResolvedValue({ - succeeded: true, - token: "gho_def456", - }); - + it("returns account and events emails", async () => { const actual = await getGitHubUsernameEmails({ username }); expect(actual).toMatchInlineSnapshot(` diff --git a/src/getGitHubUsernameEmails.ts b/src/getGitHubUsernameEmails.ts index e9e58cca..74762e4b 100644 --- a/src/getGitHubUsernameEmails.ts +++ b/src/getGitHubUsernameEmails.ts @@ -1,5 +1,4 @@ -import { getGitHubAuthToken } from "get-github-auth-token"; -import { Octokit } from "octokit"; +import { octokitFromAuth } from "octokit-from-auth"; import { getAccountEmail } from "./getAccountEmail.js"; import { getEventsEmails } from "./getEventsEmails.js"; @@ -15,31 +14,12 @@ export interface GitHubUsernameEmails { events: EmailsToNames; } -async function retrieveAuth(provided: string | undefined) { - if (provided === "") { - throw new Error("Invalid auth provided: an empty string ('')."); - } - - const auth = await getGitHubAuthToken(); - if (auth.succeeded) { - return auth.token; - } - - throw new Error( - "Please provide an auth token (process.env.GH_TOKEN) or log in with the GitHub CLI (gh).", - { - cause: auth.error, - }, - ); -} - export async function getGitHubUsernameEmails({ - auth: providedAuth, + auth, ...rawOptions }: GitHubUsernameEmailsOptions): Promise { - const auth = await retrieveAuth(providedAuth); + const octokit = await octokitFromAuth({ auth }); - const octokit = new Octokit({ auth }); const options = { ...defaultOptions, ...rawOptions }; return {