From f21ba7277f29e77d21228e20b3c40acb0e07776d Mon Sep 17 00:00:00 2001 From: Mattias Kindborg Date: Fri, 6 Oct 2023 14:57:57 +0200 Subject: [PATCH] fix(scripts): bug when calculating answers Only answers in a comment was regarded, not answers in a reply to a comment. This has now been fixed. --- scripts/most-helpful-contributors.ts | 89 +++++++++++++++++----------- scripts/src/list-comments.ts | 24 +++++++- 2 files changed, 78 insertions(+), 35 deletions(-) diff --git a/scripts/most-helpful-contributors.ts b/scripts/most-helpful-contributors.ts index c9bb5a5..bac2c52 100644 --- a/scripts/most-helpful-contributors.ts +++ b/scripts/most-helpful-contributors.ts @@ -1,4 +1,4 @@ -import { Author, Comment, listComments } from "./src/list-comments" +import { Author, Comment, Reply, listComments } from "./src/list-comments" import { updateDiscussion } from "./src/update-discussion" export const ORGANIZATION_NAME = "AxisCommunications" @@ -17,7 +17,7 @@ const usage = () => { console.log() } -interface Count { +interface AuthorCount { author: Author count: number } @@ -26,50 +26,71 @@ const removeAuthor = (comments: Comment[], authorLogin: string): Comment[] => { return comments.filter((c) => c.author.login !== authorLogin) } -const filterAnswers = (comments: Comment[]): Count[] => { - return comments - .filter((c) => c.isAnswer) - .reduce((result, comment) => { - let item = result.find((i) => i.author.login === comment.author.login) - if (!item) { - item = { - author: comment.author, - count: 0, - } - result.push(item) - } +const handleComment = (comment: Comment, authorCounts: AuthorCount[]) => { + let authorCount = authorCounts.find((ac) => ac.author.login === comment.author.login) + if (!authorCount) { + authorCount = { + author: comment.author, + count: 0, + } + authorCounts.push(authorCount) + } - item.count++ + authorCount.count++ +} - return result - }, []) +const handleReply = (reply: Reply, authorCounts: AuthorCount[]) => { + let authorCount = authorCounts.find((ac) => ac.author.login === reply.author.login) + if (!authorCount) { + authorCount = { + author: reply.author, + count: 0, + } + authorCounts.push(authorCount) + } + + authorCount.count++ } -const filterInteractions = (comments: Comment[]): Count[] => { - return [...comments.map((c) => c.author), ...comments.flatMap((c) => c.replies.map((r) => r.author))].reduce( - (result, author) => { - let item = result.find((i) => i.author.login === author.login) - if (!item) { - item = { - author, - count: 0, - } - result.push(item) +const filterAnswers = (comments: Comment[]): AuthorCount[] => { + const authorCounts: AuthorCount[] = [] + + for (const comment of comments) { + if (comment.isAnswer) { + console.log(`${comment.author.login} answered using a comment in ${comment.discussion.number}`) + handleComment(comment, authorCounts) + } + + for (const reply of comment.replies) { + if (reply.isAnswer) { + console.log(`${reply.author.login} answered using a reply in ${reply.discussion.number}`) + handleReply(reply, authorCounts) } + } + } + + return authorCounts +} - item.count++ +const filterInteractions = (comments: Comment[]): AuthorCount[] => { + const authorCounts: AuthorCount[] = [] + + for (const comment of comments) { + handleComment(comment, authorCounts) + + for (const reply of comment.replies) { + handleReply(reply, authorCounts) + } + } - return result - }, - [] - ) + return authorCounts } -const byCount = (a: Count, b: Count) => { +const byCount = (a: AuthorCount, b: AuthorCount) => { return b.count - a.count || a.author.login.localeCompare(b.author.login) } -const createBody = (answers: Count[], interactions: Count[]) => { +const createBody = (answers: AuthorCount[], interactions: AuthorCount[]) => { return [ "# Let's celebrate our contributors!", "", diff --git a/scripts/src/list-comments.ts b/scripts/src/list-comments.ts index 59ce19a..96fee4f 100644 --- a/scripts/src/list-comments.ts +++ b/scripts/src/list-comments.ts @@ -2,12 +2,19 @@ import { Octokit } from "octokit" import { PageInfo, processPagedQuery } from "./paging" export interface Comment { + discussion: { + number: number + } author: Author isAnswer: boolean replies: Reply[] } export interface Reply { + discussion: { + number: number + } + isAnswer: boolean author: Author } @@ -35,8 +42,9 @@ export const listComments = async (organizationName: string, repositoryName: str const comments: Comment[] = [] for (const d of discussions) { - for (const { author, isAnswer, replies} of d.comments.nodes) { + for (const { discussion, isAnswer, author, replies} of d.comments.nodes) { comments.push({ + discussion, isAnswer, author, replies: replies.nodes, @@ -50,10 +58,17 @@ export const listComments = async (organizationName: string, repositoryName: str interface Discussion { comments: { nodes: { + discussion: { + number: number + } isAnswer: boolean author: Author replies: { nodes: { + discussion: { + number: number + } + isAnswer: boolean author: Author }[] pageInfo: PageInfo @@ -71,6 +86,9 @@ const createQuery = (organizationName: string, repositoryName: string): string = nodes { comments(first: 20) { nodes { + discussion { + number + } isAnswer author { login @@ -78,6 +96,10 @@ const createQuery = (organizationName: string, repositoryName: string): string = } replies(first: 20) { nodes { + discussion { + number + } + isAnswer author { login resourcePath