Skip to content

Commit

Permalink
chore: use octokit-from-auth package (#437)
Browse files Browse the repository at this point in the history
## 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.

💖
  • Loading branch information
JoshuaKGoldberg authored Dec 15, 2024
1 parent 6279758 commit 3f666be
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 74 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 13 additions & 4 deletions pnpm-lock.yaml

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

50 changes: 6 additions & 44 deletions src/getGitHubUsernameEmails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => ({
Expand All @@ -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(`
Expand Down
26 changes: 3 additions & 23 deletions src/getGitHubUsernameEmails.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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<GitHubUsernameEmails> {
const auth = await retrieveAuth(providedAuth);
const octokit = await octokitFromAuth({ auth });

const octokit = new Octokit({ auth });
const options = { ...defaultOptions, ...rawOptions };

return {
Expand Down

0 comments on commit 3f666be

Please sign in to comment.