diff --git a/.github/workflows/update-contributors.yml b/.github/workflows/update-contributors.yml index 0cf25bc4..5972075e 100644 --- a/.github/workflows/update-contributors.yml +++ b/.github/workflows/update-contributors.yml @@ -4,6 +4,8 @@ on: pull_request_target: types: - closed + repository_dispatch: + types: [trigger-workflow] jobs: update-contributors: diff --git a/packages/frontendmu-data/scripts/update-contributors.config.json b/packages/frontendmu-data/scripts/update-contributors.config.json new file mode 100644 index 00000000..49b1bde1 --- /dev/null +++ b/packages/frontendmu-data/scripts/update-contributors.config.json @@ -0,0 +1,13 @@ +{ + "includedRepositories": [ + "frontend.mu", + "video", + "google-photos-sync", + "conference-2024", + "frontendmu-ticket" + ], + "excludedContributors": [ + "actions-user", + "github-actions[bot]" + ] +} \ No newline at end of file diff --git a/packages/frontendmu-data/scripts/update-contributors.js b/packages/frontendmu-data/scripts/update-contributors.js index f0cc2509..cb0a27f3 100644 --- a/packages/frontendmu-data/scripts/update-contributors.js +++ b/packages/frontendmu-data/scripts/update-contributors.js @@ -1,33 +1,53 @@ import fs from "fs"; import { execSync } from "child_process"; -const owner = "Front-End-Coders-Mauritius"; -const repo = "frontend.mu"; +const owner = "frontendmu"; const branch = "main"; // Replace with the default branch of your repository const contributorsFile = "./data/contributors.json"; +const configSource = `https://raw.githubusercontent.com/${owner}/frontend.mu/${branch}/packages/frontendmu-data/scripts/update-contributors.config.json` async function updateContributors() { try { - const response = await fetch( - `https://api.github.com/repos/${owner}/${repo}/contributors` + const config = await loadConfig(); + const includedRepositories = config.includedRepositories; + const excludedContributors = config.excludedContributors; + + const allPublicRepositoriesList = await fetch( + `https://api.github.com/users/${owner}/repos` + ).then((response) => response.json()); + + const allPublicRepositories = allPublicRepositoriesList.map( + (repo) => repo.name ); - const result = await response.json(); - const contributors = result - .map((contributor) => { - return { - username: contributor.login, - contributions: contributor.contributions, - }; - }) - .filter((contributor) => { - // Exclude the following contributors from the list - const excludedContributors = ["actions-user", "github-actions[bot]"]; - return !excludedContributors.includes(contributor.username); + const contributorsMap = {}; + + for (const repo of allPublicRepositories) { + if (!includedRepositories.includes(repo)) { + continue; + } + const contributorsList = await fetch( + `https://api.github.com/repos/${owner}/${repo}/contributors` + ).then((response) => response.json()); + + contributorsList.forEach((contributor) => { + if (!excludedContributors.includes(contributor.login)) { + if (contributorsMap[contributor.login]) { + contributorsMap[contributor.login] += contributor.contributions; + } else { + contributorsMap[contributor.login] = contributor.contributions; + } + } }); + } + const updatedContributors = Object.entries(contributorsMap).map(([username, contributions]) => ({ + username, + contributions + })); + const contributorsData = JSON.stringify(updatedContributors, null, 2); - const updatedContributors = [...new Set(contributors)]; + console.log(contributorsData) if ( JSON.stringify(updatedContributors) !== @@ -35,9 +55,8 @@ async function updateContributors() { ) { fs.writeFileSync( contributorsFile, - JSON.stringify(updatedContributors, null, 2) + contributorsData ); - console.log("Contributors file updated."); // Configure Git user and email for the commit execSync('git config user.name "GitHub Action"'); @@ -70,4 +89,8 @@ function getExistingContributors() { return []; } +async function loadConfig() { + return await fetch(configSource).then((response) => response.json()); +} + updateContributors();