Skip to content

Commit

Permalink
fix(scripts): bug when calculating answers
Browse files Browse the repository at this point in the history
Only answers in a comment was regarded, not answers in a reply to a
comment. This has now been fixed.
  • Loading branch information
mattias-kindborg-at-work committed Oct 6, 2023
1 parent 58e3731 commit f21ba72
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 35 deletions.
89 changes: 55 additions & 34 deletions scripts/most-helpful-contributors.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -17,7 +17,7 @@ const usage = () => {
console.log()
}

interface Count {
interface AuthorCount {
author: Author
count: number
}
Expand All @@ -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<Count[]>((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<Count[]>(
(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!",
"",
Expand Down
24 changes: 23 additions & 1 deletion scripts/src/list-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -71,13 +86,20 @@ const createQuery = (organizationName: string, repositoryName: string): string =
nodes {
comments(first: 20) {
nodes {
discussion {
number
}
isAnswer
author {
login
resourcePath
}
replies(first: 20) {
nodes {
discussion {
number
}
isAnswer
author {
login
resourcePath
Expand Down

0 comments on commit f21ba72

Please sign in to comment.