| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { parseReferences } |
| 3 | import { Inject, Injectable |
| 4 | import { and, asc |
| 5 | import { alias } |
| 6 | |
| 7 | import { DATABASE } |
| 8 | import type { Commit |
| 9 | import { closeIssue, type |
| 10 | import { isoTimestamp, excluded |
| 11 | import { |
| 12 | acceptedCollaboration, |
| 13 | type Actor, |
| 14 | canAccess, |
| 15 | organizationMembership, |
| 16 | ownerNameOf, |
| 17 | repositoryFullNameOf, |
| 18 | type Repository, |
| 19 | basePermissionOf, |
| 20 | roleOf, |
| 21 | teamRoleOf, |
| 22 | } from '../../lib/git/repository-access/repository-access.js'; |
| 23 | |
| 24 | type SourceType |
| 25 | |
| 26 | export interface ReferenceSource |
| 27 | type: SourceType |
| 28 | /** The issue id, comment id or commit sha the text lives in. */ |
| 29 | id: string; |
| 30 | repository: Pick |
| 31 | /** The issue or pull request the text belongs to; null for a commit. */ |
| 32 | issueId: string |
| 33 | actorId: string |
| 34 | } |
| 35 | |
| 36 | const sourceIssue = alias |
| 37 | const sourceRepository = alias |
| 38 | const sourceOwner = alias |
| 39 | const sourceOrganization = alias |
| 40 | const actor = alias |
| 41 | |
| 42 | @Injectable() |
| 43 | export class IssueReferencesService |
| 44 | constructor(@Inject( |
| 45 | |
| 46 | /** Makes the references `source` holds match what `text` says now. A reference that survives an edit keeps its original timestamp. */ |
| 47 | async record(db |
| 48 | const targets = |
| 49 | const targetIds = |
| 50 | |
| 51 | await db |
| 52 | .delete(schema.issueReference |
| 53 | .where( |
| 54 | and( |
| 55 | eq(schema.issueReference |
| 56 | eq(schema.issueReference |
| 57 | targetIds.length > 0 |
| 58 | ? notInArray(schema. |
| 59 | : undefined, |
| 60 | ), |
| 61 | ); |
| 62 | if (targetIds.length |
| 63 | |
| 64 | await db |
| 65 | .insert(schema.issueReference |
| 66 | .values( |
| 67 | targetIds.map((targetIssueId |
| 68 | sourceType: source.type |
| 69 | sourceId: source.id |
| 70 | sourceRepositoryId: source.repository |
| 71 | sourceIssueId: source.issueId |
| 72 | targetIssueId, |
| 73 | closing: targets.get |
| 74 | actorId: source.actorId |
| 75 | })), |
| 76 | ) |
| 77 | .onConflictDoUpdate({ |
| 78 | target: [ |
| 79 | schema.issueReference.sourceType |
| 80 | schema.issueReference.sourceId |
| 81 | schema.issueReference.targetIssueId |
| 82 | ], |
| 83 | set: { closing: excluded |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | /** Records what each commit message references, attributed to whoever pushed or merged it. */ |
| 88 | async recordCommits( |
| 89 | db: Executor |
| 90 | { |
| 91 | repository, |
| 92 | actorId, |
| 93 | commits, |
| 94 | }: { |
| 95 | repository: Pick |
| 96 | actorId: string |
| 97 | commits: Commit |
| 98 | }, |
| 99 | ) { |
| 100 | for (const commit |
| 101 | await this.record |
| 102 | db, |
| 103 | { type: 'commit', |
| 104 | `${commit.subject} |
| 105 | ); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** Records what pushed commits reference and closes what each promises to, crediting the commit on the closed issue. */ |
| 110 | async closeFromCommits(params |
| 111 | repository: Pick |
| 112 | actorId: string |
| 113 | commits: Commit |
| 114 | }) { |
| 115 | await this.db |
| 116 | await this.recordCommits |
| 117 | for (const commit |
| 118 | await this.closeReferenced |
| 119 | sources: [{ type: |
| 120 | actorId: params.actorId |
| 121 | commitSha: commit.sha |
| 122 | }); |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | /** Drops every reference a deleted comment made. */ |
| 128 | async forget(db |
| 129 | await db |
| 130 | .delete(schema.issueReference |
| 131 | .where( |
| 132 | and( |
| 133 | eq(schema.issueReference |
| 134 | eq(schema.issueReference |
| 135 | ), |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** Where `targetIssueId` was mentioned, one entry per issue or commit, limited to sources `viewer` can read. */ |
| 140 | async mentionsOf(targetIssueId |
| 141 | const rows = |
| 142 | .selectDistinctOn( |
| 143 | [ |
| 144 | sql`coalesce(${schema. |
| 145 | ], |
| 146 | { |
| 147 | id: schema.issueReference |
| 148 | sourceType: schema.issueReference |
| 149 | commitSha: sql< |
| 150 | string | null |
| 151 | >`case when ${schema.issueReference |
| 152 | actorUsername: sql<string |
| 153 | createdAt: isoTimestamp(schema |
| 154 | repository: { |
| 155 | ownerId: sourceRepository.ownerId |
| 156 | organizationId: sourceRepository.organizationId |
| 157 | visibility: sourceRepository.visibility |
| 158 | collaboratorRole: schema.repositoryCollaborator |
| 159 | memberRole: schema.member |
| 160 | teamRole: teamRoleOf(sourceRepository |
| 161 | basePermission: basePermissionOf(sourceRepository |
| 162 | username: sql<string |
| 163 | slug: sourceRepository.slug |
| 164 | }, |
| 165 | source: { |
| 166 | number: sourceIssue.number |
| 167 | title: sourceIssue.title |
| 168 | state: sourceIssue.state |
| 169 | isPullRequest: sourceIssue.isPullRequest |
| 170 | }, |
| 171 | }, |
| 172 | ) |
| 173 | .from(schema.issueReference |
| 174 | .innerJoin( |
| 175 | sourceRepository, |
| 176 | eq(sourceRepository.id |
| 177 | ) |
| 178 | .innerJoin(sourceOwner, |
| 179 | .leftJoin( |
| 180 | sourceOrganization, |
| 181 | eq(sourceOrganization.id |
| 182 | ) |
| 183 | .leftJoin( |
| 184 | schema.repositoryCollaborator, |
| 185 | acceptedCollaboration(sourceRepository.id |
| 186 | ) |
| 187 | .leftJoin( |
| 188 | schema.member, |
| 189 | organizationMembership(sourceRepository.organizationId |
| 190 | ) |
| 191 | .leftJoin( |
| 192 | sourceIssue, |
| 193 | eq(sourceIssue.id |
| 194 | ) |
| 195 | .leftJoin(actor, |
| 196 | .where(eq(schema |
| 197 | .orderBy( |
| 198 | sql`coalesce(${schema. |
| 199 | asc(schema.issueReference |
| 200 | ); |
| 201 | |
| 202 | return rows |
| 203 | .filter(({ repository |
| 204 | canAccess(repository, roleOf |
| 205 | ) |
| 206 | .map(({ repository |
| 207 | ...row, |
| 208 | repository: { username, |
| 209 | source, |
| 210 | })); |
| 211 | } |
| 212 | |
| 213 | /** Closes the open issues these sources promise to close. Pull requests are never closed by keyword, and a target in a repository `actorId` cannot write to stays open. */ |
| 214 | async closeReferenced( |
| 215 | db: Executor |
| 216 | { |
| 217 | sources, |
| 218 | actorId, |
| 219 | sourceIssueId, |
| 220 | commitSha, |
| 221 | }: { |
| 222 | sources: Array |
| 223 | actorId: string |
| 224 | sourceIssueId?: string |
| 225 | commitSha?: string |
| 226 | }, |
| 227 | ) { |
| 228 | if (sources.length |
| 229 | |
| 230 | const writer = |
| 231 | const targets = |
| 232 | .select({ |
| 233 | issueId: schema.issue |
| 234 | repository: { |
| 235 | ownerId: schema.repository |
| 236 | organizationId: schema.repository |
| 237 | visibility: schema.repository |
| 238 | collaboratorRole: schema.repositoryCollaborator |
| 239 | memberRole: schema.member |
| 240 | teamRole: teamRoleOf(schema |
| 241 | basePermission: basePermissionOf(schema |
| 242 | }, |
| 243 | }) |
| 244 | .from(schema.issueReference |
| 245 | .innerJoin( |
| 246 | schema.issue, |
| 247 | eq(schema.issue |
| 248 | ) |
| 249 | .innerJoin( |
| 250 | schema.repository, |
| 251 | eq(schema.repository |
| 252 | ) |
| 253 | .leftJoin( |
| 254 | schema.repositoryCollaborator, |
| 255 | acceptedCollaboration(schema.repository |
| 256 | ) |
| 257 | .leftJoin( |
| 258 | schema.member, |
| 259 | organizationMembership(schema.repository |
| 260 | ) |
| 261 | .where( |
| 262 | and( |
| 263 | eq(schema.issueReference |
| 264 | eq(schema.issue |
| 265 | eq(schema.issue |
| 266 | or( |
| 267 | ...sources.map( |
| 268 | and( |
| 269 | eq(schema.issueReference |
| 270 | eq(schema.issueReference |
| 271 | ), |
| 272 | ), |
| 273 | ), |
| 274 | ), |
| 275 | ); |
| 276 | |
| 277 | const closed: |
| 278 | for (const issueId |
| 279 | targets |
| 280 | .filter(({ repository |
| 281 | canAccess( |
| 282 | repository, |
| 283 | roleOf(repository, repository |
| 284 | 'write', |
| 285 | ), |
| 286 | ) |
| 287 | .map((target) |
| 288 | )) { |
| 289 | if ( |
| 290 | await closeIssue(db |
| 291 | ) { |
| 292 | closed.push(issueId |
| 293 | } |
| 294 | } |
| 295 | return closed; |
| 296 | } |
| 297 | |
| 298 | /** Issue ids `text` points at, each with whether any mention of it closes it. Repositories the author cannot read resolve to nothing, and an issue never references itself. */ |
| 299 | private async resolve |
| 300 | const wanted = |
| 301 | string, |
| 302 | { owner: string |
| 303 | >(); |
| 304 | const closing = |
| 305 | for (const reference |
| 306 | if (reference.kind |
| 307 | const key = |
| 308 | const entry = |
| 309 | owner: reference.owner |
| 310 | repo: reference.repo |
| 311 | numbers: new Set |
| 312 | }; |
| 313 | entry.numbers.add |
| 314 | wanted.set(key |
| 315 | if (reference.closing |
| 316 | } |
| 317 | |
| 318 | const targets = |
| 319 | if (wanted.size |
| 320 | |
| 321 | const named = |
| 322 | (entry): entry |
| 323 | entry.owner !== |
| 324 | ); |
| 325 | const author = |
| 326 | const repositories = |
| 327 | ? await db |
| 328 | .select({ |
| 329 | id: schema.repository |
| 330 | ownerId: schema.repository |
| 331 | organizationId: schema.repository |
| 332 | visibility: schema.repository |
| 333 | collaboratorRole: schema.repositoryCollaborator |
| 334 | memberRole: schema.member |
| 335 | teamRole: teamRoleOf(schema |
| 336 | basePermission: basePermissionOf(schema |
| 337 | key: repositoryFullNameOf( |
| 338 | schema.user, |
| 339 | schema.organization, |
| 340 | schema.repository, |
| 341 | ), |
| 342 | }) |
| 343 | .from(schema.repository |
| 344 | .innerJoin(schema.user |
| 345 | .leftJoin( |
| 346 | schema.organization, |
| 347 | eq(schema.organization |
| 348 | ) |
| 349 | .leftJoin( |
| 350 | schema.repositoryCollaborator, |
| 351 | acceptedCollaboration(schema.repository |
| 352 | ) |
| 353 | .leftJoin( |
| 354 | schema.member, |
| 355 | organizationMembership(schema.repository |
| 356 | ) |
| 357 | .where( |
| 358 | or( |
| 359 | ...named.map( |
| 360 | and( |
| 361 | eq( |
| 362 | ownerNameOf(schema.user |
| 363 | entry.owner, |
| 364 | ), |
| 365 | eq(schema.repository |
| 366 | ), |
| 367 | ), |
| 368 | ), |
| 369 | ) |
| 370 | : []; |
| 371 | |
| 372 | const repositoryIdOf = |
| 373 | repositories |
| 374 | .filter((repository) |
| 375 | canAccess(repository, roleOf |
| 376 | ) |
| 377 | .map((repository) |
| 378 | ); |
| 379 | repositoryIdOf.set('/ |
| 380 | |
| 381 | const scopes = |
| 382 | const repositoryId = |
| 383 | return repositoryId |
| 384 | ? [{ key, |
| 385 | : []; |
| 386 | }); |
| 387 | if (scopes.length |
| 388 | |
| 389 | const issues = |
| 390 | .select({ |
| 391 | id: schema.issue |
| 392 | repositoryId: schema.issue |
| 393 | number: schema.issue |
| 394 | }) |
| 395 | .from(schema.issue |
| 396 | .where( |
| 397 | or( |
| 398 | ...scopes.map( |
| 399 | and( |
| 400 | eq(schema.issue |
| 401 | inArray(schema.issue |
| 402 | ), |
| 403 | ), |
| 404 | ), |
| 405 | ); |
| 406 | |
| 407 | for (const issue |
| 408 | if (issue.id |
| 409 | // The same issue can be written as `#1` and `me/repo#1`; either spelling closing it is enough. |
| 410 | const isClosing = |
| 411 | (scope) => |
| 412 | scope.repositoryId === |
| 413 | closing.has(`${ |
| 414 | ); |
| 415 | targets.set(issue |
| 416 | } |
| 417 | return targets; |
| 418 | } |
| 419 | } |