Skip to content

Commit 512b1a9

Browse files
authored
Merge pull request #11990 from ethereum/fix-github-contributors
Fix GitHub contributors history
2 parents b4106cd + 4daba77 commit 512b1a9

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

src/hooks/useClientSideGitHubContributors.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ import { join } from "path"
22

33
import { useEffect, useState } from "react"
44

5-
import type { Author, FileContributorsState } from "@/lib/types"
5+
import type { Author, Commit, FileContributorsState } from "@/lib/types"
66

7-
import { CONTENT_DIR,GITHUB_COMMITS_URL } from "@/lib/constants"
7+
import {
8+
CONTENT_DIR,
9+
GITHUB_COMMITS_URL,
10+
OLD_CONTENT_DIR,
11+
} from "@/lib/constants"
812

913
export const gitHubAuthHeaders = {
1014
headers: new Headers({
@@ -13,42 +17,19 @@ export const gitHubAuthHeaders = {
1317
}),
1418
}
1519

16-
const fetchGitHubContributors = async (
17-
relativePath: string
18-
): Promise<FileContributorsState> => {
20+
const fetchGitHubCommits = async (filePath: string): Promise<Commit[]> => {
1921
const url = new URL(GITHUB_COMMITS_URL)
20-
const filePath = join(CONTENT_DIR, relativePath, "index.md")
2122
url.searchParams.set("path", filePath)
2223

2324
try {
2425
const response = await fetch(url, gitHubAuthHeaders)
2526
if (!response.ok) throw new Error(response.statusText)
26-
const commits = await response.json()
27-
const authorSet = new Set<string>()
28-
commits
29-
.filter(({ author }) => author)
30-
.forEach(({ author, commit }) => {
31-
const entry: Author = {
32-
name: commit.author.name,
33-
email: commit.author.email,
34-
avatarUrl: author.avatar_url,
35-
user: {
36-
login: author.login,
37-
url: author.html_url,
38-
},
39-
}
40-
// Unique authors only
41-
authorSet.add(JSON.stringify(entry))
42-
})
43-
const authors = Array.from(authorSet).map(
44-
JSON.parse as (entry: string) => Author
45-
)
46-
return { loading: false, data: authors }
27+
return (await response.json()) as Commit[]
4728
} catch (error: unknown) {
4829
if (error instanceof Error) {
4930
console.error(filePath, error.message)
5031
}
51-
return { loading: false, error }
32+
throw error
5233
}
5334
}
5435
/**
@@ -63,7 +44,41 @@ export const useClientSideGitHubContributors = (
6344
const [state, setState] = useState<FileContributorsState>({ loading: true })
6445
useEffect(() => {
6546
;(async () => {
66-
setState(await fetchGitHubContributors(relativePath))
47+
const oldFilePath = join(OLD_CONTENT_DIR, relativePath, "index.md")
48+
const filePath = join(CONTENT_DIR, relativePath, "index.md")
49+
50+
try {
51+
const oldCommits = await fetchGitHubCommits(oldFilePath)
52+
const newCommits = await fetchGitHubCommits(filePath)
53+
54+
const authorSet = new Set<string>()
55+
56+
;[...oldCommits, ...newCommits]
57+
.filter(({ author }) => author)
58+
.forEach(({ author, commit }) => {
59+
const entry: Author = {
60+
name: commit.author.name,
61+
email: commit.author.email,
62+
avatarUrl: author.avatar_url,
63+
user: {
64+
login: author.login,
65+
url: author.html_url,
66+
},
67+
}
68+
// Unique authors only
69+
authorSet.add(JSON.stringify(entry))
70+
})
71+
const authors = Array.from(authorSet).map(
72+
JSON.parse as (entry: string) => Author
73+
)
74+
75+
setState({
76+
loading: false,
77+
data: authors,
78+
})
79+
} catch (error: unknown) {
80+
setState({ loading: false, error })
81+
}
6782
})()
6883
}, [relativePath])
6984
return state

src/lib/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ReportsModel } from "@crowdin/crowdin-api-client"
22

33
import i18nConfig from "../../i18n.config.json"
44

5+
export const OLD_CONTENT_DIR = "src/content"
56
export const CONTENT_DIR = "public/content"
67
export const TRANSLATIONS_DIR = "public/content/translations"
78
export const TRANSLATED_IMAGES_DIR = "/content/translations"

src/lib/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ export type LocaleContributions = {
188188
}
189189

190190
// GitHub contributors
191+
export type Commit = {
192+
commit: {
193+
author: {
194+
name: string
195+
email: string
196+
}
197+
}
198+
author: {
199+
avatar_url: string
200+
login: string
201+
html_url: string
202+
}
203+
}
204+
191205
export type Author = {
192206
name: string
193207
email: string

0 commit comments

Comments
 (0)