From c8853d57844568ee7e3b1d98df7d7166905371e3 Mon Sep 17 00:00:00 2001 From: Dereck Lam Hon Wah Date: Fri, 18 Oct 2024 16:33:04 +0400 Subject: [PATCH 1/3] feat: modify the update-contributors.js to extract contribution to all public repo of frontendmu --- .github/workflows/update-contributors.yml | 2 + .../scripts/update-contributors.js | 91 +++++++++++++++---- 2 files changed, 75 insertions(+), 18 deletions(-) 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.js b/packages/frontendmu-data/scripts/update-contributors.js index f0cc2509..89943e6d 100644 --- a/packages/frontendmu-data/scripts/update-contributors.js +++ b/packages/frontendmu-data/scripts/update-contributors.js @@ -1,7 +1,7 @@ import fs from "fs"; import { execSync } from "child_process"; -const owner = "Front-End-Coders-Mauritius"; +const owner = "frontendmu"; const repo = "frontend.mu"; const branch = "main"; // Replace with the default branch of your repository @@ -9,25 +9,80 @@ const contributorsFile = "./data/contributors.json"; async function updateContributors() { try { - const response = await fetch( - `https://api.github.com/repos/${owner}/${repo}/contributors` + 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); + // console.log("All public repositories:", allPublicRepositories); + // [ + // '.github', + // 'branding', + // 'conference-2024', + // 'events', + // 'frontend.mu', + // 'frontendmu-daisy', + // 'frontendmu-nuxt', + // 'frontendmu-ticket', + // 'google-photos-sync', + // 'hacktoberfestmu-2019', + // 'meetupFEC', + // 'nuxt-workshop-devcon2024', + // 'nuxt-workshop-devcon2024-preparations', + // 'playground', + // 'vercel-og-next', + // 'video' + // ] + + // const contributors = []; + const contributorsMap = {}; + + const excludedContributors = ["actions-user", "github-actions[bot]"]; + const excludedRepositories = [".github", "google-photos-sync", "branding"]; + + for (const repo of allPublicRepositories) { + if (excludedRepositories.includes(repo)) { + continue; + } + const contributorsList = await fetch( + `https://api.github.com/repos/${owner}/${repo}/contributors` + ).then((response) => response.json()); + + // const contributorsListFiltered = contributorsList + // .map((contributor) => { + // return { + // username: contributor.login, + // contributions: contributor.contributions, + // }; + // }) + // .filter((contributor) => { + // return !excludedContributors.includes(contributor.username); + // }); + // contributors.push(...contributorsListFiltered); + // console.log(`Contributors for ${repo}:`, contributorsListFiltered); + // } + // const updatedContributors = [...new Set(contributors)]; + + 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 +90,9 @@ async function updateContributors() { ) { fs.writeFileSync( contributorsFile, - JSON.stringify(updatedContributors, null, 2) + contributorsData ); - console.log("Contributors file updated."); + // console.log("Contributors file updated."); // Configure Git user and email for the commit execSync('git config user.name "GitHub Action"'); From 209f2ce9768d706d2ef263ca58cb5ed1ba023fee Mon Sep 17 00:00:00 2001 From: Dereck Lam Hon Wah Date: Tue, 22 Oct 2024 08:49:47 +0400 Subject: [PATCH 2/3] feat: add a config file for update-contributors script --- .../scripts/update-contributors.config.json | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 packages/frontendmu-data/scripts/update-contributors.config.json 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 From 69efa77957d0c7b31047930eb268cddd29c033f5 Mon Sep 17 00:00:00 2001 From: Dereck Lam Hon Wah Date: Tue, 22 Oct 2024 09:55:48 +0400 Subject: [PATCH 3/3] feat: update the update-contributor script to use the config file --- .../scripts/update-contributors.js | 52 ++++--------------- 1 file changed, 10 insertions(+), 42 deletions(-) diff --git a/packages/frontendmu-data/scripts/update-contributors.js b/packages/frontendmu-data/scripts/update-contributors.js index 89943e6d..cb0a27f3 100644 --- a/packages/frontendmu-data/scripts/update-contributors.js +++ b/packages/frontendmu-data/scripts/update-contributors.js @@ -2,13 +2,17 @@ import fs from "fs"; import { execSync } from "child_process"; const owner = "frontendmu"; -const repo = "frontend.mu"; 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 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()); @@ -17,55 +21,16 @@ async function updateContributors() { (repo) => repo.name ); - // console.log("All public repositories:", allPublicRepositories); - // [ - // '.github', - // 'branding', - // 'conference-2024', - // 'events', - // 'frontend.mu', - // 'frontendmu-daisy', - // 'frontendmu-nuxt', - // 'frontendmu-ticket', - // 'google-photos-sync', - // 'hacktoberfestmu-2019', - // 'meetupFEC', - // 'nuxt-workshop-devcon2024', - // 'nuxt-workshop-devcon2024-preparations', - // 'playground', - // 'vercel-og-next', - // 'video' - // ] - - // const contributors = []; const contributorsMap = {}; - const excludedContributors = ["actions-user", "github-actions[bot]"]; - const excludedRepositories = [".github", "google-photos-sync", "branding"]; - for (const repo of allPublicRepositories) { - if (excludedRepositories.includes(repo)) { + if (!includedRepositories.includes(repo)) { continue; } const contributorsList = await fetch( `https://api.github.com/repos/${owner}/${repo}/contributors` ).then((response) => response.json()); - // const contributorsListFiltered = contributorsList - // .map((contributor) => { - // return { - // username: contributor.login, - // contributions: contributor.contributions, - // }; - // }) - // .filter((contributor) => { - // return !excludedContributors.includes(contributor.username); - // }); - // contributors.push(...contributorsListFiltered); - // console.log(`Contributors for ${repo}:`, contributorsListFiltered); - // } - // const updatedContributors = [...new Set(contributors)]; - contributorsList.forEach((contributor) => { if (!excludedContributors.includes(contributor.login)) { if (contributorsMap[contributor.login]) { @@ -92,7 +57,6 @@ async function updateContributors() { contributorsFile, contributorsData ); - // console.log("Contributors file updated."); // Configure Git user and email for the commit execSync('git config user.name "GitHub Action"'); @@ -125,4 +89,8 @@ function getExistingContributors() { return []; } +async function loadConfig() { + return await fetch(configSource).then((response) => response.json()); +} + updateContributors();