From fc9644c6f8ed2a1398e87b8c4efccb6628ff5705 Mon Sep 17 00:00:00 2001 From: Shivam-Amin <106419448+Shivam-Amin@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:34:53 +0530 Subject: [PATCH] Added [logic to count PRs for each user] --- prisma/schema.prisma | 3 +++ src/api/users/controller.js | 33 +++++++++++++++++++++++++++++---- src/webhook/github.js | 27 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index cecee5e..00bf4b7 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -22,6 +22,9 @@ model users { createdAt DateTime @default(now()) updatedAt DateTime @updatedAt points Int @default(0) + openPRs Int @default(0) + mergedPRs Int @default(0) + closedPRs Int @default(0) prs pullRequests[] } diff --git a/src/api/users/controller.js b/src/api/users/controller.js index f821ffe..a11e035 100644 --- a/src/api/users/controller.js +++ b/src/api/users/controller.js @@ -52,9 +52,9 @@ const getUserStats = async (req, res) => { const stats = { totalPRs: user.prs.length, points: user.points, - mergedPRs: user.prs.filter(pr => pr.state === 'merged').length, - openPRs: user.prs.filter(pr => pr.state === 'open').length, - closedPRs: user.prs.filter(pr => pr.state === 'closed').length, + mergedPRs: user.mergedPRs, + openPRs: user.openPRs, + closedPRs: user.closedPRs, repositoryBreakdown: {}, prs: [], }; @@ -122,7 +122,14 @@ const getUserStats = async (req, res) => { }; const repositories = [ - 'yogi-coder-boy/github' + 'clubgamma/club-gamma-frontend', + 'clubgamma/club-gamma-backend', + 'clubgamma/Internet-Speed-Tester', + 'clubgamma/Weather-Web-App-2024', + 'clubgamma/Air-Quality-Index-Analysis', + 'clubgamma/Summarize-papers', + 'clubgamma/Sudoku', + 'clubgamma/Ticket-Booking' ]; const prPoints = { "documentation": 1, @@ -219,6 +226,24 @@ const syncPullRequests = async (req, res) => { await Promise.all(prSavePromises); + // Lastly update user PR count. + const userPRs = await prisma.pullRequests.findMany({ + where: { authorId: githubId }, + }); + + const openPRs = userPRs.filter(pr => pr.state === 'open').length; + const closedPRs = userPRs.filter(pr => pr.state === 'closed').length; + const mergedPRs = userPRs.filter(pr => pr.state === 'merged').length; + + await prisma.users.update({ + where: { githubId }, + data: { + openPRs, + closedPRs, + mergedPRs, + }, + }); + res.json({ message: 'Synchronization complete', syncedRepos: reposDetailedPRs.map(r => ({ diff --git a/src/webhook/github.js b/src/webhook/github.js index 0fe175a..d2f08f6 100644 --- a/src/webhook/github.js +++ b/src/webhook/github.js @@ -76,6 +76,15 @@ module.exports = async (req, res) => { if (action === 'opened') { console.log(`PR #${prNumber} opened on main branch`); + await prisma.users.update({ + where: { + githubId: author.githubId + }, + data: { + openPRs: { increment: 1 } + } + }); + await prisma.pullRequests.create({ data: { prNumber: prNumber, @@ -94,6 +103,16 @@ module.exports = async (req, res) => { console.log(`PR #${prNumber} closed`); const isMerged = prData.merged; + await prisma.users.update({ + where: { + githubId: author.githubId + }, + data: { + openPRs: { decrement: 1 }, + [isMerged ? mergedPRs : closedPRs]: { increment: 1 } + } + }); + await prisma.pullRequests.upsert({ where: { prNumber_repository: { @@ -166,6 +185,14 @@ module.exports = async (req, res) => { } else if (action === 'reopened') { console.log(`PR #${prNumber} reopened`); + await prisma.users.update({ + where: { githubId: author.githubId }, + data: { + opnePRs: { increment: 1 }, + closedPRs: { decrement: 1 } + } + }); + await prisma.pullRequests.update({ where: { prNumber_repository: {