From dc147efeb5d8ecb4ec4fa0b21e745c566511e301 Mon Sep 17 00:00:00 2001 From: Ashutosh8215 Date: Wed, 21 Feb 2024 16:46:27 +0530 Subject: [PATCH 1/2] feat: add the code to remove github-actions[bot] --- updateProject.mjs | 160 +++++++++++++++++++++++++++++----------------- 1 file changed, 100 insertions(+), 60 deletions(-) diff --git a/updateProject.mjs b/updateProject.mjs index 7f70e08c..de82015f 100644 --- a/updateProject.mjs +++ b/updateProject.mjs @@ -28,15 +28,16 @@ async function fetchCollaborators(url, githubToken) { // Function to get collaborators of a repository async function getCollaborators(repoData, githubToken) { - if (repoData.fork && repoData.parent?.contributors_url) { // If the repository is a fork and has a parent, fetch collaborators from both const [c1, c2] = await Promise.all([ fetchCollaborators(repoData.contributors_url, githubToken), - fetchCollaborators(repoData.parent.contributors_url, githubToken) + fetchCollaborators(repoData.parent.contributors_url, githubToken), ]); // Filter out collaborators from the repository who are also in the parent - return c1.filter(collab1 => !c2.some(collab2 => collab1.login === collab2.login)); + return c1.filter( + (collab1) => !c2.some((collab2) => collab1.login === collab2.login) + ); } // Otherwise, fetch collaborators directly from the repository return fetchCollaborators(repoData.contributors_url, githubToken); @@ -44,18 +45,21 @@ async function getCollaborators(repoData, githubToken) { // Main function to update projects data async function updateProjects() { - const githubToken = process.env.GITHUB_TOKEN; + // const githubToken = process.env.GITHUB_TOKEN; + const githubToken = + "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"; try { process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; // Fetch data for current projects and upcoming projects - const [currentProjectsData, upcomingProjectsData, repositories] = await Promise.all([ - // Fetch current projects data - fetchData("https://directus.ourgoalplan.co.in/graphql", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - query: `query getCurrentProjects { + const [currentProjectsData, upcomingProjectsData, repositories] = + await Promise.all([ + // Fetch current projects data + fetchData("https://directus.ourgoalplan.co.in/graphql", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + query: `query getCurrentProjects { foss_projects(filter: {project_type: { _eq: "current" }}) { id, title, @@ -67,15 +71,15 @@ async function updateProjects() { date_updated, status, } - }` + }`, + }), }), - }), - // Fetch upcoming projects data - fetchData("https://directus.ourgoalplan.co.in/graphql", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - query: `query getUpcomingProjects { + // Fetch upcoming projects data + fetchData("https://directus.ourgoalplan.co.in/graphql", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + query: `query getUpcomingProjects { foss_projects(filter: {project_type: { _eq: "upcoming" }}) { id, title, @@ -87,66 +91,102 @@ async function updateProjects() { date_updated, status, } - }` + }`, + }), }), - }), - // Fetch repositories data from GitHub - fetchData("https://api.github.com/users/mindfiredigital/repos", { - headers: { - Authorization: `token ${githubToken}`, - Accept: "application/vnd.github.v3+json", - }, - }) - ]); + // Fetch repositories data from GitHub + fetchData("https://api.github.com/users/mindfiredigital/repos", { + headers: { + Authorization: `token ${githubToken}`, + Accept: "application/vnd.github.v3+json", + }, + }), + ]); // Process and write data for current projects - const currentProjects = currentProjectsData.data.foss_projects.map(entry => ({ - ...entry, - id: parseInt(entry.id), - shortDescription: entry.short_description, - githubUrl: entry.github_repository_link, - documentationUrl: entry.documentation_link, - })); - fs.writeFileSync(path.join(__dirname, "src/app/projects/assets/projects.json"), JSON.stringify(currentProjects, null, 2)); + const currentProjects = currentProjectsData.data.foss_projects.map( + (entry) => ({ + ...entry, + id: parseInt(entry.id), + shortDescription: entry.short_description, + githubUrl: entry.github_repository_link, + documentationUrl: entry.documentation_link, + }) + ); + fs.writeFileSync( + path.join(__dirname, "src/app/projects/assets/projects.json"), + JSON.stringify(currentProjects, null, 2) + ); console.log("Current projects updated successfully."); // Process and write data for upcoming projects - const upcomingProjects = upcomingProjectsData.data.foss_projects.map(entry => ({ - ...entry, - id: parseInt(entry.id), - shortDescription: entry.short_description, - githubUrl: entry.github_repository_link, - documentationUrl: entry.documentation_link, - })); - fs.writeFileSync(path.join(__dirname, "src/app/projects/assets/upcomingProjects.json"), JSON.stringify(upcomingProjects, null, 2)); + const upcomingProjects = upcomingProjectsData.data.foss_projects.map( + (entry) => ({ + ...entry, + id: parseInt(entry.id), + shortDescription: entry.short_description, + githubUrl: entry.github_repository_link, + documentationUrl: entry.documentation_link, + }) + ); + fs.writeFileSync( + path.join(__dirname, "src/app/projects/assets/upcomingProjects.json"), + JSON.stringify(upcomingProjects, null, 2) + ); console.log("Upcoming projects updated successfully."); // Fetch and process contributors data for repositories - const repoNames = repositories.map(repo => repo.name); + const repoNames = repositories.map((repo) => repo.name); const contributorsObject = {}; for (const repoName of repoNames) { - const repoData = await fetchData(`https://api.github.com/repos/mindfiredigital/${repoName}`, { - headers: { - Authorization: `token ${githubToken}`, - Accept: "application/vnd.github.v3+json", - }, - }) - contributorsObject[repoName] = await getCollaborators(repoData, githubToken); + const repoData = await fetchData( + `https://api.github.com/repos/mindfiredigital/${repoName}`, + { + headers: { + Authorization: `token ${githubToken}`, + Accept: "application/vnd.github.v3+json", + }, + } + ); + contributorsObject[repoName] = await getCollaborators( + repoData, + githubToken + ); } // Aggregate contributor from contributors const contributionsMap = {}; + for (const repo in contributorsObject) { - contributorsObject[repo].forEach(contributor => { - const { login, contributions, id, avatar_url, html_url } = contributor; - contributionsMap[login] = contributionsMap[login] || { id, contributions: 0, html_url, avatar_url, login }; - contributionsMap[login].contributions += contributions; - }); + if (contributorsObject.hasOwnProperty(repo)) { + contributorsObject[repo].forEach((contributor) => { + if (contributor.login === "github-actions[bot]") { + // Skip processing GitHub Actions bot contributions + return; + } + const { login, contributions, id, avatar_url, html_url } = + contributor; + // Update contributions map + contributionsMap[login] = { + id, + contributions: + (contributionsMap[login]?.contributions || 0) + contributions, + html_url, + avatar_url, + login, + }; + }); + } } // Sort contributions and write data to file - const sortedContributions = Object.values(contributionsMap).sort((a, b) => b.contributions - a.contributions); - fs.writeFileSync(path.join(__dirname, "src/app/projects/assets/contributors.json"), JSON.stringify(sortedContributions, null, 2)); + const sortedContributions = Object.values(contributionsMap).sort( + (a, b) => b.contributions - a.contributions + ); + fs.writeFileSync( + path.join(__dirname, "src/app/projects/assets/contributors.json"), + JSON.stringify(sortedContributions, null, 2) + ); console.log("Contributors list updated successfully."); } catch (error) { console.error("An error occurred:", error); From 08eea9daa0458069e450734a4b7fb03a6d78ee01 Mon Sep 17 00:00:00 2001 From: Ashutosh8215 Date: Wed, 21 Feb 2024 16:50:55 +0530 Subject: [PATCH 2/2] fix: set the git token --- updateProject.mjs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/updateProject.mjs b/updateProject.mjs index de82015f..fc27f251 100644 --- a/updateProject.mjs +++ b/updateProject.mjs @@ -45,9 +45,7 @@ async function getCollaborators(repoData, githubToken) { // Main function to update projects data async function updateProjects() { - // const githubToken = process.env.GITHUB_TOKEN; - const githubToken = - "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"; + const githubToken = process.env.GITHUB_TOKEN; try { process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;