Commit f649c59

Anton Medvedev <anton@medv.io>
2024-07-07 00:35:33
Add issue-comments.ts
1 parent cce5715
Changed files (3)
src/collect/collect.ts
@@ -5,6 +5,7 @@ import { issuesQuery, IssuesQuery } from './issues.js'
 import { userQuery, UserQuery } from './user.js'
 import { IssueTimelineQuery, issueTimelineQuery } from './issue-timeline.js'
 import { Data } from './types.js'
+import { issueCommentsQuery, IssueCommentsQuery } from './issue-comments.js'
 
 export async function collect(
   octokit: Octokit,
@@ -19,6 +20,7 @@ export async function collect(
     repos: [],
     pulls: [],
     issues: [],
+    issueComments: [],
   }
 
   const repos = octokit.paginate.iterator('GET /users/{username}/repos', {
@@ -151,5 +153,25 @@ export async function collect(
     }
   }
 
+  const issueComments = octokit.graphql.paginate.iterator<IssueCommentsQuery>(
+    issueCommentsQuery,
+    {
+      login: username,
+    },
+  )
+  try {
+    for await (const resp of issueComments) {
+      for (const comment of resp.user.issueComments.nodes) {
+        data.issueComments.push(comment)
+      }
+      console.log(
+        `| issue comments ${data.issueComments.length}/${resp.user.issueComments.totalCount} (cost: ${resp.rateLimit.cost}, remaining: ${resp.rateLimit.remaining})`,
+      )
+    }
+  } catch (err) {
+    console.error(`Failed to load issue comments`)
+    console.error(err)
+  }
+
   return data
 }
src/collect/issue-comments.ts
@@ -0,0 +1,97 @@
+export const issueCommentsQuery = `#graphql
+query IssueCommentsQuery($login: String!, $num: Int = 100, $cursor: String) {
+  user(login: $login) {
+    issueComments(first: $num, after: $cursor) {
+      totalCount
+      nodes {
+        repository {
+          nameWithOwner
+        }
+        issue {
+          number
+          author {
+            login
+          }
+        }
+        body
+        createdAt
+        updatedAt
+        editor {
+          login
+        }
+        reactions(first: 100) {
+          totalCount
+          nodes {
+            content
+            user {
+              login
+            }
+          }
+        }
+      }
+      pageInfo {
+        hasNextPage
+        endCursor
+      }
+    }
+  }
+  rateLimit {
+    limit
+    cost
+    remaining
+    resetAt
+  }
+}
+`
+
+export type IssueCommentsQuery = {
+  user: {
+    issueComments: {
+      totalCount: number
+      nodes: Array<{
+        repository: {
+          nameWithOwner: string
+        }
+        issue: {
+          number: number
+          author: {
+            login: string
+          }
+        }
+        body: string
+        createdAt: string
+        updatedAt: string
+        editor: {
+          login: string
+        } | null
+        reactions: {
+          totalCount: number
+          nodes: Array<{
+            content:
+              | 'CONFUSED'
+              | 'EYES'
+              | 'HEART'
+              | 'HOORAY'
+              | 'LAUGH'
+              | 'ROCKET'
+              | 'THUMBS_DOWN'
+              | 'THUMBS_UP'
+            user: {
+              login: string
+            }
+          }>
+        }
+      }>
+      pageInfo: {
+        hasNextPage: boolean
+        endCursor: string
+      }
+    }
+  }
+  rateLimit: {
+    limit: number
+    cost: number
+    remaining: number
+    resetAt: string
+  }
+}
src/collect/types.ts
@@ -3,7 +3,10 @@ import { CommitsQuery } from './commits.js'
 import { IssuesQuery } from './issues.js'
 import { UserQuery } from './user.js'
 import { PullsQuery } from './pulls.js'
+import { IssueCommentsQuery } from './issue-comments.js'
 
+// Extra<T> represents additional data that is not returned by the GraphQL API,
+// but enriched by some other means (e.g., separate queries).
 export type Extra<T> = T | undefined
 
 export type Data = {
@@ -11,6 +14,7 @@ export type Data = {
   repos: Repo[]
   pulls: Pull[]
   issues: Issue[]
+  issueComments: IssueComment[]
 }
 
 export type User = UserQuery['user']
@@ -26,3 +30,6 @@ export type Commit =
 export type Pull = PullsQuery['user']['pullRequests']['nodes'][0]
 
 export type Issue = IssuesQuery['user']['issues']['nodes'][0]
+
+export type IssueComment =
+  IssueCommentsQuery['user']['issueComments']['nodes'][0]