/** * Snapshot collection: pulls open PRs/issues/discussions (with timelines) and * recently closed items from the GitHub GraphQL API and normalizes them into * the shapes in types.ts. Read-only — never mutates anything. */ import { isBotLogin } from './config.ts' import type { GitHubClient } from './github.ts' import type { CIState, ClosedItem, DiscussionItem, IssueItem, PRItem, RepoSnapshot, TimelineEvent, ToolsetConfig, } from './types.ts' const CLOSED_LOOKBACK_DAYS = 14 interface GqlActor { login?: string __typename?: string createdAt?: string } interface GqlTimelineNode { __typename: string actor?: GqlActor | null assignee?: GqlActor | null requestedReviewer?: GqlActor | null author?: GqlActor | null createdAt?: string submittedAt?: string state?: string body?: string commit?: { committedDate: string author?: { user?: { login: string } | null } | null } | null } function actorLogin(actor: GqlActor | null | undefined): string | null { return actor?.login ?? null } function actorIsBot( actor: GqlActor | null | undefined, config: ToolsetConfig, ): boolean { if (actor?.__typename === 'Bot') return true return isBotLogin(actorLogin(actor), config) } function normalizeTimeline( nodes: Array, fallbackAuthor: string | null, config: ToolsetConfig, ): Array { const events: Array = [] for (const node of nodes) { if (!node) continue if ( (node.__typename === 'AssignedEvent' || node.__typename === 'ReviewRequestedEvent') && node.createdAt ) { events.push({ kind: node.__typename === 'AssignedEvent' ? 'assigned' : 'review-requested', actor: actorLogin(node.actor), subject: actorLogin(node.assignee ?? node.requestedReviewer), isBot: actorIsBot(node.actor, config), at: node.createdAt, }) } else if (node.__typename === 'IssueComment' && node.createdAt) { events.push({ kind: 'comment', actor: actorLogin(node.author), isBot: actorIsBot(node.author, config), at: node.createdAt, body: node.body ?? '', }) } else if (node.__typename === 'PullRequestReview' && node.submittedAt) { events.push({ kind: 'review', actor: actorLogin(node.author), isBot: actorIsBot(node.author, config), at: node.submittedAt, reviewState: node.state, }) } else if (node.__typename === 'PullRequestCommit' && node.commit) { const login = node.commit.author?.user?.login ?? fallbackAuthor events.push({ kind: 'commit', actor: login, isBot: isBotLogin(login, config), at: node.commit.committedDate, }) } } return events } function normalizeCIState(state: string | null | undefined): CIState { switch (state) { case 'SUCCESS': return 'success' case 'FAILURE': case 'ERROR': return 'failure' case 'PENDING': case 'EXPECTED': return 'pending' default: return 'unknown' } } const COMMENT_FRAGMENT = ` __typename ... on IssueComment { author { login __typename } createdAt body } ` const PR_TIMELINE_FRAGMENT = ` ${COMMENT_FRAGMENT} ... on AssignedEvent { actor { login __typename } createdAt assignee { ... on User { login } } } ... on ReviewRequestedEvent { actor { login __typename } createdAt requestedReviewer { ... on User { login } } } ... on PullRequestReview { author { login __typename } submittedAt state } ... on PullRequestCommit { commit { committedDate author { user { login } } } } ` const OPEN_PRS_QUERY = ` query($owner: String!, $name: String!, $cursor: String) { repository(owner: $owner, name: $name) { pullRequests(states: OPEN, first: 20, after: $cursor, orderBy: {field: CREATED_AT, direction: DESC}) { pageInfo { hasNextPage endCursor } nodes { number title url createdAt updatedAt isDraft mergeable reviewDecision additions deletions changedFiles authorAssociation author { login __typename ... on User { createdAt } } labels(first: 20) { nodes { name } } assignees(first: 10) { nodes { login } } files(first: 100) { nodes { path } pageInfo { hasNextPage endCursor } } reviewRequests(first: 100) { nodes { requestedReviewer { ... on User { login } } } pageInfo { hasNextPage endCursor } } closingIssuesReferences(first: 10) { nodes { number } } commits(last: 1) { nodes { commit { statusCheckRollup { state } } } } timelineItems(first: 100, itemTypes: [ISSUE_COMMENT, PULL_REQUEST_REVIEW, PULL_REQUEST_COMMIT, ASSIGNED_EVENT, REVIEW_REQUESTED_EVENT]) { pageInfo { hasNextPage endCursor } nodes { ${PR_TIMELINE_FRAGMENT} } } } } } }` const OPEN_ISSUES_QUERY = ` query($owner: String!, $name: String!, $cursor: String) { repository(owner: $owner, name: $name) { issues(states: OPEN, first: 40, after: $cursor, orderBy: {field: CREATED_AT, direction: DESC}) { pageInfo { hasNextPage endCursor } nodes { number title url createdAt updatedAt body authorAssociation author { login __typename } labels(first: 20) { nodes { name } } assignees(first: 10) { nodes { login } } timelineItems(last: 100, itemTypes: [ISSUE_COMMENT]) { nodes { ${COMMENT_FRAGMENT} } } } } } }` const DISCUSSIONS_QUERY = ` query($owner: String!, $name: String!, $cursor: String) { repository(owner: $owner, name: $name) { discussions(first: 50, after: $cursor, states: OPEN, orderBy: {field: UPDATED_AT, direction: DESC}) { pageInfo { hasNextPage endCursor } nodes { number title url createdAt updatedAt isAnswered upvoteCount category { name } author { login __typename } comments(last: 20) { totalCount nodes { author { login __typename } createdAt } } } } } }` const CLOSED_SEARCH_QUERY = ` query($query: String!, $cursor: String) { search(query: $query, type: ISSUE, first: 50, after: $cursor) { pageInfo { hasNextPage endCursor } nodes { __typename ... on PullRequest { number title url createdAt closedAt mergedAt author { login __typename } files(first: 100) { nodes { path } pageInfo { hasNextPage endCursor } } timelineItems(first: 100, itemTypes: [ISSUE_COMMENT, PULL_REQUEST_REVIEW, PULL_REQUEST_COMMIT, ASSIGNED_EVENT, REVIEW_REQUESTED_EVENT]) { pageInfo { hasNextPage endCursor } nodes { ${PR_TIMELINE_FRAGMENT} } } } ... on Issue { number title url createdAt closedAt author { login __typename } timelineItems(first: 30, itemTypes: [ISSUE_COMMENT]) { nodes { ${COMMENT_FRAGMENT} } } } } } }` async function paginate( fetchPage: (cursor: string | null) => Promise<{ nodes: Array pageInfo: { hasNextPage: boolean; endCursor: string | null } }>, maxPages = Infinity, ): Promise> { const all: Array = [] let cursor: string | null = null for (let page = 0; page < maxPages; page++) { const result = await fetchPage(cursor) for (const node of result.nodes) if (node !== null) all.push(node) if (!result.pageInfo.hasNextPage) break cursor = result.pageInfo.endCursor } return all } export async function collectSnapshot( client: GitHubClient, config: ToolsetConfig, now: Date = new Date(), ): Promise { const [owner, repo] = config.repo.split('/') as [string, string] const vars = { owner, name: repo } interface Connection { nodes: Array | null> pageInfo: { hasNextPage: boolean; endCursor: string | null } } const ownership = await client.graphql<{ repository: { object: { text: string } | null } }>( ` query($owner: String!, $name: String!) { repository(owner: $owner, name: $name) { object(expression: "HEAD:.github/CODEOWNERS") { ... on Blob { text } } } }`, vars, ) if (!ownership.repository.object) throw new Error('Default-branch CODEOWNERS is missing') const prNodes = await paginate((cursor) => client .graphql<{ repository: { pullRequests: Connection } }>(OPEN_PRS_QUERY, { ...vars, cursor, }) .then((d) => d.repository.pullRequests), ) const issueNodes = await paginate((cursor) => client .graphql<{ repository: { issues: Connection } }>(OPEN_ISSUES_QUERY, { ...vars, cursor, }) .then((d) => d.repository.issues), ) const discussionNodes = await paginate( (cursor) => client .graphql<{ repository: { discussions: Connection } }>(DISCUSSIONS_QUERY, { ...vars, cursor }) .then((d) => d.repository.discussions), 4, ) const sinceDate = new Date( now.getTime() - CLOSED_LOOKBACK_DAYS * 24 * 60 * 60 * 1000, ) .toISOString() .slice(0, 10) const closedNodes = await paginate((cursor) => client .graphql<{ search: Connection }>(CLOSED_SEARCH_QUERY, { query: `repo:${config.repo} closed:>=${sinceDate}`, cursor, }) .then((d) => d.search), ) // Page nested connections too: a core-owned path or review may be beyond page one. for (const node of [ ...prNodes, ...closedNodes.filter((n) => n.__typename === 'PullRequest'), ]) { for (const [field, selection] of [ ['files', 'nodes { path }'], [ 'reviewRequests', 'nodes { requestedReviewer { ... on User { login } } }', ], ['timelineItems', `nodes { ${PR_TIMELINE_FRAGMENT} }`], ]) { const connection = node[field!] if (!connection?.pageInfo?.hasNextPage) continue const remaining = await paginate((cursor) => client .graphql<{ repository: { pullRequest: Record } }>( ` query($owner: String!, $name: String!, $number: Int!, $cursor: String) { repository(owner: $owner, name: $name) { pullRequest(number: $number) { ${field}(first: 100, after: $cursor${field === 'timelineItems' ? ', itemTypes: [ISSUE_COMMENT, PULL_REQUEST_REVIEW, PULL_REQUEST_COMMIT, ASSIGNED_EVENT, REVIEW_REQUESTED_EVENT]' : ''}) { ${selection} pageInfo { hasNextPage endCursor } } } } }`, { ...vars, number: node.number, cursor: cursor ?? connection.pageInfo.endCursor, }, ) .then((d) => d.repository.pullRequest[field!]!), ) connection.nodes.push(...remaining) } } const prs: Array = prNodes.map((n) => { const author = actorLogin(n.author) return { type: 'pr', number: n.number, title: n.title, url: n.url, author, authorIsBot: actorIsBot(n.author, config), authorAccountCreatedAt: n.author?.createdAt ?? null, authorAssociation: n.authorAssociation ?? 'NONE', createdAt: n.createdAt, updatedAt: n.updatedAt, isDraft: Boolean(n.isDraft), mergeable: n.mergeable ?? 'UNKNOWN', reviewDecision: n.reviewDecision ?? '', additions: n.additions ?? 0, deletions: n.deletions ?? 0, changedFiles: n.changedFiles ?? 0, files: (n.files?.nodes ?? []) .filter(Boolean) .map((f: { path: string }) => f.path), labels: (n.labels?.nodes ?? []) .filter(Boolean) .map((l: { name: string }) => l.name), assignees: (n.assignees?.nodes ?? []) .filter(Boolean) .map((a: { login: string }) => a.login), requestedReviewers: (n.reviewRequests?.nodes ?? []).flatMap( (r: { requestedReviewer?: { login?: string } | null }) => r?.requestedReviewer?.login ? [r.requestedReviewer.login] : [], ), ciState: normalizeCIState( n.commits?.nodes?.[0]?.commit?.statusCheckRollup?.state, ), linkedIssues: (n.closingIssuesReferences?.nodes ?? []) .filter(Boolean) .map((i: { number: number }) => i.number), timeline: normalizeTimeline(n.timelineItems?.nodes ?? [], author, config), } }) const issues: Array = issueNodes.map((n) => ({ type: 'issue', number: n.number, title: n.title, url: n.url, author: actorLogin(n.author), authorIsBot: actorIsBot(n.author, config), authorAssociation: n.authorAssociation ?? 'NONE', createdAt: n.createdAt, updatedAt: n.updatedAt, body: n.body ?? '', labels: (n.labels?.nodes ?? []) .filter(Boolean) .map((l: { name: string }) => l.name), assignees: (n.assignees?.nodes ?? []) .filter(Boolean) .map((a: { login: string }) => a.login), timeline: normalizeTimeline( n.timelineItems?.nodes ?? [], actorLogin(n.author), config, ), })) const discussions: Array = discussionNodes.map((n) => ({ type: 'discussion', number: n.number, title: n.title, url: n.url, author: actorLogin(n.author), createdAt: n.createdAt, updatedAt: n.updatedAt, isAnswered: Boolean(n.isAnswered), category: n.category?.name ?? '', upvotes: n.upvoteCount ?? 0, commentCount: n.comments?.totalCount ?? 0, comments: (n.comments?.nodes ?? []) .filter(Boolean) .map((c: { author: GqlActor | null; createdAt: string }) => ({ actor: actorLogin(c.author), isBot: actorIsBot(c.author, config), at: c.createdAt, })), })) const recentlyClosed: Array = closedNodes .filter((n) => n.closedAt) .map((n) => ({ type: n.__typename === 'PullRequest' ? ('pr' as const) : ('issue' as const), number: n.number, title: n.title, url: n.url, author: actorLogin(n.author), authorIsBot: actorIsBot(n.author, config), createdAt: n.createdAt, closedAt: n.closedAt, mergedAt: n.mergedAt ?? null, files: (n.files?.nodes ?? []) .filter(Boolean) .map((f: { path: string }) => f.path), timeline: normalizeTimeline( n.timelineItems?.nodes ?? [], actorLogin(n.author), config, ), })) return { owner, repo, codeowners: ownership.repository.object.text, takenAt: now.toISOString(), prs, issues, discussions, recentlyClosed, } }