Skip to content

Commit

Permalink
fixed leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
RajuGangitla committed Oct 11, 2024
1 parent b204365 commit d3e3aa5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
28 changes: 23 additions & 5 deletions lib/github/hooks/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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,
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 d3e3aa5

Please sign in to comment.