Skip to content

Commit e086537

Browse files
fix: don't crash reading contributors when not logged into GitHub (#1972)
## PR Checklist - [x] Addresses an existing open issue: fixes #1971 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/create-typescript-app/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/create-typescript-app/blob/main/.github/CONTRIBUTING.md) were taken ## Overview The `GET /user` result can be `undefined` if the user isn't logged in. 🎁
1 parent e6402c6 commit e086537

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/options/readAllContributors.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ describe(readAllContributors, () => {
3333
expect(mockInputFromOctokit).not.toHaveBeenCalled();
3434
});
3535

36-
it("returns the current user as a contributor when .all-contributorsrc cannot be read", async () => {
36+
it("returns undefined when .all-contributorsrc cannot be read and GET /user resolves undefined", async () => {
37+
mockInputFromFileJSON.mockResolvedValueOnce(new Error("Oh no!"));
38+
mockInputFromOctokit.mockResolvedValueOnce(undefined);
39+
40+
const actual = await readAllContributors(take);
41+
42+
expect(actual).toBeUndefined();
43+
});
44+
45+
it("returns the current user as a contributor when .all-contributorsrc cannot be read and GET /user resolves a user", async () => {
3746
mockInputFromFileJSON.mockResolvedValueOnce(new Error("Oh no!"));
3847
mockInputFromOctokit.mockResolvedValueOnce({
3948
avatar_url: "avatar_url",

src/options/readAllContributors.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,19 @@ export async function readAllContributors(take: TakeInput) {
1616

1717
const user = (await take(inputFromOctokit, {
1818
endpoint: "GET /user",
19-
})) as { avatar_url: string; blog: string; login: string; name: string };
19+
})) as
20+
| undefined
21+
| { avatar_url: string; blog: string; login: string; name: string };
2022

21-
return [
22-
{
23-
avatar_url: user.avatar_url,
24-
contributions: ownerContributions,
25-
login: user.login,
26-
name: user.name,
27-
profile: user.blog,
28-
},
29-
];
23+
return (
24+
user && [
25+
{
26+
avatar_url: user.avatar_url,
27+
contributions: ownerContributions,
28+
login: user.login,
29+
name: user.name,
30+
profile: user.blog,
31+
},
32+
]
33+
);
3034
}

0 commit comments

Comments
 (0)