-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fetch all organization contributors for home page
- Loading branch information
Aleksandr Burobin
committed
Jun 28, 2024
1 parent
8ac022f
commit d59e1ea
Showing
5 changed files
with
100 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,6 @@ yarn-error.log* | |
|
||
/src/libs-data.json | ||
/src/packages-versions.json | ||
/src/contributors.json | ||
|
||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* This script is used to prefetch all organization contributors. | ||
*/ | ||
|
||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import {fileURLToPath} from 'node:url'; | ||
|
||
import * as dotenv from 'dotenv'; | ||
|
||
import {getOrganizationRepositories, getRepositoryContributors} from './github.mjs'; | ||
|
||
dotenv.config(); | ||
|
||
const LIBS_DATA_FILENAME = 'libs-data.json'; | ||
const CONTRIB_FILENAME = 'contributors.json'; | ||
|
||
const contribDataPath = path.resolve( | ||
path.dirname(fileURLToPath(import.meta.url)), | ||
'../src', | ||
CONTRIB_FILENAME, | ||
); | ||
|
||
const readLibsData = async () => { | ||
const libsDataPath = path.resolve( | ||
path.dirname(fileURLToPath(import.meta.url)), | ||
'../src', | ||
LIBS_DATA_FILENAME, | ||
); | ||
|
||
const libsDataContent = await fs.promises.readFile(libsDataPath, 'utf-8'); | ||
return JSON.parse(libsDataContent); | ||
}; | ||
|
||
const start = async () => { | ||
if (fs.existsSync(contribDataPath) && !process.env.GITHUB_TOKEN) { | ||
console.error( | ||
`The ${CONTRIB_FILENAME} file exists. You can delete it manually for refetch. | ||
Learn more about the limitations of the GitHub API... | ||
https://docs.github.com/ru/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#rate-limiting`, | ||
); | ||
|
||
return; | ||
} | ||
|
||
const libsData = await readLibsData(); | ||
const repos = await getOrganizationRepositories('gravity-ui'); | ||
|
||
const rawContributors = await Promise.all( | ||
repos.map(async (repo) => { | ||
if (libsData[repo.name]) { | ||
return libsData[repo.name].contributors; | ||
} | ||
|
||
return await getRepositoryContributors(repo.owner.login, repo.name); | ||
}), | ||
); | ||
|
||
const contributors = {}; | ||
|
||
for (const list of rawContributors) { | ||
for (const contributor of list) { | ||
const {login, contributions} = contributor; | ||
|
||
if (contributors[login]) { | ||
contributors[login].contributions += contributions; | ||
} else { | ||
contributors[login] = contributor; | ||
} | ||
} | ||
} | ||
|
||
const sortedContributors = Object.values(contributors).sort( | ||
(a, b) => b.contributions - a.contributions, | ||
); | ||
|
||
fs.writeFileSync(contribDataPath, JSON.stringify(sortedContributors, null, 4), 'utf8'); | ||
}; | ||
|
||
start().catch((err) => { | ||
console.error(err.message); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters