Skip to content

Commit

Permalink
feat: use octokit-from-auth for optional auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaKGoldberg committed Dec 16, 2024
1 parent 563d6ad commit c863390
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"co-author-to-username": "^0.1.1",
"conventional-commits-parser": "^6.0.0",
"description-to-co-authors": "^0.3.0",
"octokit": "^4.0.0"
"octokit": "^4.0.0",
"octokit-from-auth": "^0.3.0"
},
"devDependencies": {
"@release-it/conventional-changelog": "^9.0.0",
Expand Down
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

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

10 changes: 7 additions & 3 deletions src/collect/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Octokit } from "octokit";
import { octokitFromAuthSafe } from "octokit-from-auth";

const perPage = 100;

Expand All @@ -7,13 +8,16 @@ export interface RequestDefaults {
repo: string;
}

export function createOctokit(auth: string | undefined): Octokit {
return new (Octokit.defaults({
export async function createOctokit(
auth: string | undefined,
): Promise<Octokit> {
return await octokitFromAuthSafe({
auth,
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
per_page: perPage,
}))({ auth });
});
}

export interface RequestOptionsWithPage extends RequestDefaults {
Expand Down
2 changes: 1 addition & 1 deletion src/collect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function collect(
options: AllContributorsForRepositoryOptions,
): Promise<ContributorsContributions> {
const defaults = { owner: options.owner, repo: options.repo };
const octokit = createOctokit(options.auth);
const octokit = await createOctokit(options.auth);

// 1. Collect event data from the GitHub API
const [acceptedIssues, issueEvents, mergedPulls, repoEvents] =
Expand Down
10 changes: 6 additions & 4 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ const mockRequest = (url: string) => {
}
};

vi.mock("octokit", () => ({
get Octokit() {
return class MockOctokit {
vi.mock("octokit-from-auth", () => ({
octokitFromAuthSafe() {
class MockOctokit {
static defaults = () => MockOctokit;

request = mockRequest;
};
}

return Promise.resolve(new MockOctokit());
},
}));

Expand Down

0 comments on commit c863390

Please sign in to comment.