( store: GraphStore, pr: PRDetail, meta: RepoMeta, )
| 60 | } |
| 61 | |
| 62 | export async function indexPRIntoGraph( |
| 63 | store: GraphStore, |
| 64 | pr: PRDetail, |
| 65 | meta: RepoMeta, |
| 66 | ): Promise<{ nodes_created: number; relationships_created: number }> { |
| 67 | const repoId = `${meta.owner}/${meta.repo}`; |
| 68 | const prId = `${repoId}/pr/${pr.number}`; |
| 69 | |
| 70 | const batch: ImportBatchRequest = { |
| 71 | nodes: [ |
| 72 | { |
| 73 | id: prId, |
| 74 | type: 'PullRequest', |
| 75 | name: `#${pr.number}: ${pr.title}`, |
| 76 | properties: { |
| 77 | number: pr.number, |
| 78 | title: pr.title, |
| 79 | state: pr.state, |
| 80 | author: pr.author, |
| 81 | url: pr.url, |
| 82 | createdAt: pr.created_at, |
| 83 | baseBranch: pr.base_branch, |
| 84 | headBranch: pr.head_branch, |
| 85 | additions: pr.additions, |
| 86 | deletions: pr.deletions, |
| 87 | filesChanged: pr.files.length, |
| 88 | }, |
| 89 | }, |
| 90 | ], |
| 91 | relationships: [ |
| 92 | { |
| 93 | id: `${prId}->repo:${repoId}`, |
| 94 | type: 'TARGETS_REPO', |
| 95 | source_id: prId, |
| 96 | target_id: repoId, |
| 97 | }, |
| 98 | ], |
| 99 | }; |
| 100 | |
| 101 | // Build the set of File/Directory IDs we need, then check which already |
| 102 | // exist in the store so we only create genuinely new nodes. The store's |
| 103 | // COPY FROM does not support MERGE, so duplicate primary keys throw. |
| 104 | const ensuredDirs = new Set<string>(); |
| 105 | const neededIds = new Set<string>(); |
| 106 | |
| 107 | for (const file of pr.files) { |
| 108 | neededIds.add(`${repoId}/${file.path}`); |
| 109 | const parts = file.path.split('/'); |
| 110 | for (let i = parts.length - 1; i >= 1; i--) { |
| 111 | neededIds.add(`${repoId}/${parts.slice(0, i).join('/')}`); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Probe the store for existing nodes — getNode returns null for missing |
| 116 | const existingIds = new Set<string>(); |
| 117 | await Promise.all( |
| 118 | [...neededIds].map(async (id) => { |
| 119 | try { |
no test coverage detected