| 1 | import { schema } from '@ghost/db'; |
| 2 | import { and, eq |
| 3 | |
| 4 | import { publishEvent } |
| 5 | import type { Executor |
| 6 | |
| 7 | // Commits a push or merge scans for closing keywords. A longer range closes only through the newest ones. |
| 8 | export const MAX_CLOSING_COMMITS = |
| 9 | |
| 10 | /** Closes an issue that is still open and records why: `closed`, naming the pull request or commit that closed it when there is one, or `merged` for a pull request's own issue. Returns null when the issue was not open, so two racing closes record one event. */ |
| 11 | export async function closeIssue |
| 12 | db: Executor |
| 13 | { |
| 14 | issueId, |
| 15 | actorId, |
| 16 | type = 'closed' |
| 17 | sourceIssueId, |
| 18 | commitSha, |
| 19 | }: { |
| 20 | issueId: string |
| 21 | actorId: string |
| 22 | type?: ' |
| 23 | sourceIssueId?: string |
| 24 | commitSha?: string |
| 25 | }, |
| 26 | ) { |
| 27 | const [closed] |
| 28 | .update(schema.issue |
| 29 | .set({ state: |
| 30 | .where(and(eq |
| 31 | .returning(); |
| 32 | if (!closed) |
| 33 | |
| 34 | await db.insert |
| 35 | issueId, |
| 36 | actorId, |
| 37 | type, |
| 38 | sourceIssueId, |
| 39 | commitSha, |
| 40 | }); |
| 41 | await publishEvent(db |
| 42 | type: type === |
| 43 | repositoryId: closed.repositoryId |
| 44 | actorId, |
| 45 | payload: { issueId |
| 46 | }); |
| 47 | return closed; |
| 48 | } |