From d3e3aa5811651066a137de139e773a9636d3080d Mon Sep 17 00:00:00 2001 From: RajuGangitla Date: Fri, 11 Oct 2024 09:21:05 +0530 Subject: [PATCH 1/4] fixed leaderboard --- lib/github/hooks/issue.ts | 28 +++++++++++++++++++++++----- lib/points/service.ts | 35 +++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/lib/github/hooks/issue.ts b/lib/github/hooks/issue.ts index 91825cf..92d0157 100644 --- a/lib/github/hooks/issue.ts +++ b/lib/github/hooks/issue.ts @@ -71,6 +71,7 @@ export const onIssueOpened = async (webhooks: Webhooks) => { export const onAssignCommented = async (webhooks: Webhooks) => { webhooks.on(EVENT_TRIGGERS.ISSUE_COMMENTED, async (context) => { try { + const issueCommentBody = context.payload.comment.body; const [identifier, points] = issueCommentBody.split(" "); const issueNumber = context.payload.issue.number; @@ -84,6 +85,22 @@ export const onAssignCommented = async (webhooks: Webhooks) => { if (issueCommentBody.trim() === ASSIGN_IDENTIFIER) { if (!isOssGgLabel) return; + const isPullRequestComment = !!context.payload.issue.pull_request; + + if (isPullRequestComment) { + // Handle pull request comments + const pullRequestNumber = context.payload.issue.number; + + await octokit.issues.createComment({ + owner, + repo, + issue_number: pullRequestNumber, + body: `😂 Oh no, @${commenter}! You can't assign a pull request! PRs are for solving issues, not assigning them! 🚧 + Feel free to head over to the issues section and find a nice juicy issue to work on instead! đŸ•ĩī¸â€â™‚ī¸đŸ” Let's stick to the program and leave the PRs for the finishing touches! 🎨🎉`, + }); + return + } + const isAssigned = context.payload.issue.assignees.length > 0; if (isAssigned) { const assignee = context.payload.issue.assignees[0].login; @@ -100,6 +117,7 @@ export const onAssignCommented = async (webhooks: Webhooks) => { return; } + //users who haven't linked the issue to the PR will be able to assign themselves again even if their pr was rejected, because their names won't be added to the "Attempted:user1" comment in the issue. const allCommentsInTheIssue = await octokit.issues.listComments({ owner, @@ -143,24 +161,24 @@ export const onAssignCommented = async (webhooks: Webhooks) => { //checking if the current level of user has the power to solve the issue on which the /assign comment was made. const currentRepo = await getRepositoryByGithubId(context.payload.repository.id); const user = await getUserByGithubId(context.payload.comment.user.id); - + if (currentRepo && user) { const userTotalPoints = await getPointsForPlayerInRepoByRepositoryId(currentRepo.id, user.id); const { currentLevelOfUser } = await findCurrentAndNextLevelOfCurrentUser( currentRepo.id, userTotalPoints ); //this just has tags that limit the user to take on task of higher level but misses out on tags of lower levels. - + const levels = currentRepo?.levels as TLevel[]; const modifiedTagsArray = calculateAssignabelNonAssignableIssuesForUserInALevel(levels); //gets all assignable tags be it from the current level and from lower levels. - + const labels = context.payload.issue.labels; const tags = modifiedTagsArray.find((item) => item.levelId === currentLevelOfUser?.id); //finds the curent level in the modifiedTagsArray. - + const isAssignable = labels.some((label) => { return tags?.assignableIssues.includes(label.name); }); - + if (!isAssignable) { await octokit.issues.createComment({ owner, diff --git a/lib/points/service.ts b/lib/points/service.ts index 926d1db..d028052 100644 --- a/lib/points/service.ts +++ b/lib/points/service.ts @@ -192,31 +192,34 @@ export interface LeaderboardEntry { export const getAllUserPointsList = async (): Promise => { try { - // Fetch users and their points in a single query - const leaderboard = await db.user.findMany({ + // Fetch users (id, login, avatarUrl) without point transactions initially + const users = await db.user.findMany({ select: { id: true, login: true, avatarUrl: true, - pointTransactions: { - select: { - points: true, - }, - }, }, }); - // Process the results - return leaderboard - .map((user) => ({ - userId: user.id, - login: user.login, - avatarUrl: user.avatarUrl, - totalPoints: user.pointTransactions.reduce((sum, transaction) => sum + (transaction.points || 0), 0), - })) - .sort((a, b) => b.totalPoints - a.totalPoints); + // Fetch total points and rank for each user in parallel using Promise.all + const leaderboard = await Promise.all( + users.map(async (user) => { + const { totalPoints } = await getTotalPointsAndGlobalRank(user.id); // Fetch total points for the user + return { + userId: user.id, + login: user.login, + avatarUrl: user.avatarUrl, + totalPoints, // Assign fetched total points + }; + }) + ); + + // Sort the leaderboard by totalPoints in descending order + return leaderboard.sort((a, b) => b.totalPoints - a.totalPoints); + } catch (error) { console.error("Error fetching leaderboard:", error); throw new Error("Failed to fetch leaderboard"); } }; + From 3b9c4db6e7f8463eaec9e59291a2618674a18954 Mon Sep 17 00:00:00 2001 From: RajuGangitla Date: Fri, 11 Oct 2024 23:23:53 +0530 Subject: [PATCH 2/4] latest code --- lib/github/hooks/issue.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/github/hooks/issue.ts b/lib/github/hooks/issue.ts index 92d0157..95e9e51 100644 --- a/lib/github/hooks/issue.ts +++ b/lib/github/hooks/issue.ts @@ -82,25 +82,23 @@ export const onAssignCommented = async (webhooks: Webhooks) => { const octokit = getOctokitInstance(installationId); const isOssGgLabel = context.payload.issue.labels.some((label) => label.name === OSS_GG_LABEL); + // Check if this is a pull request + const isPullRequest = !!context.payload.issue.pull_request; if (issueCommentBody.trim() === ASSIGN_IDENTIFIER) { if (!isOssGgLabel) return; - const isPullRequestComment = !!context.payload.issue.pull_request; - - if (isPullRequestComment) { - // Handle pull request comments - const pullRequestNumber = context.payload.issue.number; - + // If it's a pull request, don't allow assignment + if (isPullRequest) { await octokit.issues.createComment({ owner, repo, - issue_number: pullRequestNumber, - body: `😂 Oh no, @${commenter}! You can't assign a pull request! PRs are for solving issues, not assigning them! 🚧 - Feel free to head over to the issues section and find a nice juicy issue to work on instead! đŸ•ĩī¸â€â™‚ī¸đŸ” Let's stick to the program and leave the PRs for the finishing touches! 🎨🎉`, + issue_number: issueNumber, + body: "The /assign command can only be used on issues, not on pull requests.", }); - return + return; } + const isAssigned = context.payload.issue.assignees.length > 0; if (isAssigned) { const assignee = context.payload.issue.assignees[0].login; From 2a8ddf21125a498486bca4ddac12775a8ba69b5f Mon Sep 17 00:00:00 2001 From: RajuGangitla Date: Fri, 11 Oct 2024 23:53:55 +0530 Subject: [PATCH 3/4] improved comments --- lib/constants.ts | 1 + lib/github/hooks/issue.ts | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/constants.ts b/lib/constants.ts index 7adda27..be18fbb 100644 --- a/lib/constants.ts +++ b/lib/constants.ts @@ -13,6 +13,7 @@ export const ASSIGN_IDENTIFIER = "/assign" as const; export const CREATE_IDENTIFIER = "/oss.gg" as const; export const UNASSIGN_IDENTIFIER = "/unassign" as const; export const REJECT_IDENTIFIER = "/reject" as const; +export const PLAYER_SUBMISSION = "player submission" as const; export enum EVENT_TRIGGERS { ISSUE_OPENED = "issues.opened", INSTALLATION_CREATED = "installation.created", diff --git a/lib/github/hooks/issue.ts b/lib/github/hooks/issue.ts index 3ceff3c..6b6e125 100644 --- a/lib/github/hooks/issue.ts +++ b/lib/github/hooks/issue.ts @@ -7,6 +7,7 @@ import { ON_NEW_ISSUE, ON_USER_NOT_REGISTERED, OSS_GG_LABEL, + PLAYER_SUBMISSION, POINT_IS_NOT_A_NUMBER, REJECTION_MESSAGE_TEMPLATE, REJECT_IDENTIFIER, @@ -81,11 +82,20 @@ export const onAssignCommented = async (webhooks: Webhooks) => { const installationId = context.payload.installation?.id!; const octokit = getOctokitInstance(installationId); const isOssGgLabel = context.payload.issue.labels.some((label) => label.name === OSS_GG_LABEL); + const isPlayerSubmission = context.payload.issue.labels.some((label) => label.name === PLAYER_SUBMISSION); // Check if this is a pull request const isPullRequest = !!context.payload.issue.pull_request; if (issueCommentBody.trim() === ASSIGN_IDENTIFIER) { - if (!isOssGgLabel) return; + if (!isOssGgLabel) { + await octokit.issues.createComment({ + owner, + repo, + issue_number: issueNumber, + body: "This issue is not part of oss.gg hackathon. Please pick a different one or start with a side quest (https://oss.gg/side-quests)", + }); + return; + } // If it's a pull request, don't allow assignment if (isPullRequest) { @@ -98,6 +108,31 @@ export const onAssignCommented = async (webhooks: Webhooks) => { return; } + //If issue has a label player submission + if (isPlayerSubmission) { + const comment = ` This is a submission of a different player for a side quest, you cannot get assigned to that. + +If you also want to complete it, here is the list of all side quests (link to oss.gg/side-quests) + +If you want to make e.g. 1050 points in 5 minutes without touching code, do the Starry-eyed Supporter quest (link to https://formbricks.notion.site/How-to-make-1050-points-without-touching-code-in-5-minutes-e71e624b5b9b487bbac28030d142438a?pvs=74 ) + +As a reminder, this is how side quest submissions work: +1. Complete the quest, gather proof (as described above) +2. Open a oss.gg submission issue in the respective repository +3. Wait for a maintainer to review, award points and close the issue +4. In the meanwhile, you can work on all other side quests :rocket: + + +Thanks for playing! OPEN SOURCE LETS GOOOO! ` + await octokit.issues.createComment({ + owner, + repo, + issue_number: issueNumber, + body: comment, + }); + return; + } + const isAssigned = context.payload.issue.assignees.length > 0; if (isAssigned) { const assignee = context.payload.issue.assignees[0].login; From ab9f647af23e288cbf9a8a49d4ac3f95ad95b983 Mon Sep 17 00:00:00 2001 From: Johannes Date: Fri, 11 Oct 2024 15:11:37 -0700 Subject: [PATCH 4/4] fix the comment --- lib/github/hooks/issue.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/github/hooks/issue.ts b/lib/github/hooks/issue.ts index 6b6e125..cc86039 100644 --- a/lib/github/hooks/issue.ts +++ b/lib/github/hooks/issue.ts @@ -72,7 +72,6 @@ export const onIssueOpened = async (webhooks: Webhooks) => { export const onAssignCommented = async (webhooks: Webhooks) => { webhooks.on(EVENT_TRIGGERS.ISSUE_COMMENTED, async (context) => { try { - const issueCommentBody = context.payload.comment.body; const [identifier, points] = issueCommentBody.split(" "); const issueNumber = context.payload.issue.number; @@ -82,7 +81,9 @@ export const onAssignCommented = async (webhooks: Webhooks) => { const installationId = context.payload.installation?.id!; const octokit = getOctokitInstance(installationId); const isOssGgLabel = context.payload.issue.labels.some((label) => label.name === OSS_GG_LABEL); - const isPlayerSubmission = context.payload.issue.labels.some((label) => label.name === PLAYER_SUBMISSION); + const isPlayerSubmission = context.payload.issue.labels.some( + (label) => label.name === PLAYER_SUBMISSION + ); // Check if this is a pull request const isPullRequest = !!context.payload.issue.pull_request; @@ -92,7 +93,7 @@ export const onAssignCommented = async (webhooks: Webhooks) => { owner, repo, issue_number: issueNumber, - body: "This issue is not part of oss.gg hackathon. Please pick a different one or start with a side quest (https://oss.gg/side-quests)", + body: "This issue is not part of oss.gg hackathon. Please pick a different one or start with a [side quest](https://oss.gg/side-quests)", }); return; } @@ -108,22 +109,22 @@ export const onAssignCommented = async (webhooks: Webhooks) => { return; } - //If issue has a label player submission + //If issue has a label player submission if (isPlayerSubmission) { const comment = ` This is a submission of a different player for a side quest, you cannot get assigned to that. If you also want to complete it, here is the list of all side quests (link to oss.gg/side-quests) -If you want to make e.g. 1050 points in 5 minutes without touching code, do the Starry-eyed Supporter quest (link to https://formbricks.notion.site/How-to-make-1050-points-without-touching-code-in-5-minutes-e71e624b5b9b487bbac28030d142438a?pvs=74 ) +If you want to make e.g. 1050 points in 5 minutes without touching code, do the [Starry-eyed Supporter quest](https://formbricks.notion.site/How-to-make-1050-points-without-touching-code-in-5-minutes-e71e624b5b9b487bbac28030d142438a?pvs=74) As a reminder, this is how side quest submissions work: -1. Complete the quest, gather proof (as described above) +1. Complete the quest, gather proof (as described [here](https://formbricks.notion.site/How-to-submit-a-non-code-contributions-via-GitHub-81166e8c948841d18209ac4c60280e60?pvs=74) 2. Open a oss.gg submission issue in the respective repository 3. Wait for a maintainer to review, award points and close the issue 4. In the meanwhile, you can work on all other side quests :rocket: -Thanks for playing! OPEN SOURCE LETS GOOOO! ` +Thanks for playing 🕹ī¸ OPEN SOURCE LETS GOOOO! `; await octokit.issues.createComment({ owner, repo, @@ -149,7 +150,6 @@ Thanks for playing! OPEN SOURCE LETS GOOOO! ` return; } - //users who haven't linked the issue to the PR will be able to assign themselves again even if their pr was rejected, because their names won't be added to the "Attempted:user1" comment in the issue. const allCommentsInTheIssue = await octokit.issues.listComments({ owner,