Skip to content

Commit

Permalink
Added [logic to count PRs for each user]
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam-Amin committed Oct 21, 2024
1 parent 38a8b0e commit fc9644c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
3 changes: 3 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
}

Expand Down
33 changes: 29 additions & 4 deletions src/api/users/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 => ({
Expand Down
27 changes: 27 additions & 0 deletions src/webhook/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down

0 comments on commit fc9644c

Please sign in to comment.