Skip to content

Commit 3a36518

Browse files
authored
fix: race conditions in fetch-team-data (#808)
1 parent 43d50c0 commit 3a36518

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

src/_data/all_authors.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"nzakas": {
33
"username": "nzakas",
44
"name": "Nicholas C. Zakas",
5-
"title": "Guest Author",
5+
"title": "ESLint Technical Steering Committee",
66
"website": "https://humanwhocodes.com",
77
"avatar_url": "https://avatars.githubusercontent.com/u/38546",
88
"bio": "Creator of ESLint, independent software developer, consultant, coach, and author.",
@@ -104,7 +104,7 @@
104104
"hzoo": {
105105
"username": "hzoo",
106106
"name": "Henry",
107-
"title": "Guest Author",
107+
"title": "ESLint Alumnus",
108108
"website": "https://henryzoo.com",
109109
"avatar_url": "https://avatars.githubusercontent.com/u/588473",
110110
"bio": "Learning to maintain stuff",

tools/fetch-team-data.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,41 +74,44 @@ const octokit = new Octokit({
7474
auth: ESLINT_GITHUB_TOKEN,
7575
});
7676

77-
async function fetchUserProfile(username) {
78-
// check cache first
79-
if (users.has(username)) {
80-
return users.get(username);
77+
function fetchUserProfile(username) {
78+
async function doFetch() {
79+
const { data: profile } = await octokit.users.getByUsername({
80+
username,
81+
});
82+
const { data: social } = await octokit.request(
83+
"GET /users/{username}/social_accounts",
84+
{ username },
85+
);
86+
const avatarURL = `https://avatars.githubusercontent.com/u/${profile.id}`;
87+
88+
return {
89+
username: profile.login,
90+
name: profile.name,
91+
title: "Guest Author",
92+
// eslint-disable-next-line no-nested-ternary -- add https if missing
93+
website: profile.blog.match(/http(s)?:\/\//u)
94+
? profile.blog
95+
: profile.blog
96+
? `https://${profile.blog}`
97+
: profile.blog,
98+
avatar_url: avatarURL,
99+
bio: profile.bio,
100+
twitter_username: profile.twitter_username,
101+
github_username: profile.login,
102+
mastodon_url: social.find(
103+
account => account.provider === "mastodon",
104+
)?.url,
105+
location: profile.location,
106+
};
81107
}
82108

83-
const { data: profile } = await octokit.users.getByUsername({ username });
84-
const { data: social } = await octokit.request(
85-
"GET /users/{username}/social_accounts",
86-
{ username },
87-
);
88-
const avatarURL = `https://avatars.githubusercontent.com/u/${profile.id}`;
89-
90-
const result = {
91-
username: profile.login,
92-
name: profile.name,
93-
title: "Guest Author",
94-
// eslint-disable-next-line no-nested-ternary -- add https if missing
95-
website: profile.blog.match(/http(s)?:\/\//u)
96-
? profile.blog
97-
: profile.blog
98-
? `https://${profile.blog}`
99-
: profile.blog,
100-
avatar_url: avatarURL,
101-
bio: profile.bio,
102-
twitter_username: profile.twitter_username,
103-
github_username: profile.login,
104-
mastodon_url: social.find(account => account.provider === "mastodon")
105-
?.url,
106-
location: profile.location,
107-
};
109+
// Immediately store Promise in the cache to avoid race conditions
110+
if (!users.has(username)) {
111+
users.set(username, doFetch());
112+
}
108113

109-
// cache the result
110-
users.set(username, result);
111-
return result;
114+
return users.get(username);
112115
}
113116

114117
async function fetchTeamMembers() {

0 commit comments

Comments
 (0)