Skip to content

Commit

Permalink
Merge pull request #135 from RajuGangitla/main
Browse files Browse the repository at this point in the history
fix: fixed leaderboard and comments
  • Loading branch information
jobenjada authored Oct 11, 2024
2 parents bd3c434 + ab9f647 commit e02af57
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 43 additions & 7 deletions lib/github/hooks/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -80,12 +81,22 @@ 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) {
Expand All @@ -98,6 +109,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](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 [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! `;
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;
Expand Down Expand Up @@ -157,24 +193,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,
Expand Down
35 changes: 19 additions & 16 deletions lib/points/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,31 +192,34 @@ export interface LeaderboardEntry {

export const getAllUserPointsList = async (): Promise<LeaderboardEntry[]> => {
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");
}
};

0 comments on commit e02af57

Please sign in to comment.