| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { and, asc |
| 3 | |
| 4 | import { isoTimestamp } |
| 5 | import type { UserRow |
| 6 | |
| 7 | /** The repository and number behind an issue's Ghost id, or null. */ |
| 8 | export async function issueRefOf |
| 9 | const [row] |
| 10 | .select({ |
| 11 | repositoryId: schema.issue |
| 12 | number: schema.issue |
| 13 | }) |
| 14 | .from(schema.issue |
| 15 | .where(eq(schema |
| 16 | return row ?? |
| 17 | } |
| 18 | |
| 19 | /** A comment with its author's username and the issue it belongs to, or null. */ |
| 20 | export async function commentOf |
| 21 | const [row] |
| 22 | .select({ |
| 23 | issueId: schema.issueComment |
| 24 | id: schema.issueComment |
| 25 | body: schema.issueComment |
| 26 | createdAt: isoTimestamp(schema |
| 27 | updatedAt: isoTimestamp(schema |
| 28 | authorUsername: sql<string |
| 29 | }) |
| 30 | .from(schema.issueComment |
| 31 | .innerJoin(schema.user |
| 32 | .where(eq(schema |
| 33 | return row ?? |
| 34 | } |
| 35 | |
| 36 | /** Label names by id, for ids within one repository; ids from another repository are left out. */ |
| 37 | export async function labelNamesOf |
| 38 | db: Database |
| 39 | repositoryId: string, |
| 40 | labelIds: string[] |
| 41 | ) { |
| 42 | if (labelIds.length |
| 43 | const rows = |
| 44 | .select({ id: |
| 45 | .from(schema.label |
| 46 | .where( |
| 47 | and( |
| 48 | eq(schema.label |
| 49 | inArray(schema.label |
| 50 | ), |
| 51 | ); |
| 52 | return new Map |
| 53 | } |
| 54 | |
| 55 | /** Usernames by user id; unknown ids, and users without a username, are left out. */ |
| 56 | export async function usernamesOf |
| 57 | if (userIds.length |
| 58 | const rows = |
| 59 | .select({ id: |
| 60 | .from(schema.user |
| 61 | .where(inArray(schema |
| 62 | return new Map |
| 63 | rows.flatMap((row |
| 64 | row.username ? |
| 65 | ), |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** The pull_request rows behind issue ids, keyed by issue id; issues that are not pull requests are left out. */ |
| 70 | export async function pullRequestRowsOf |
| 71 | if (issueIds.length |
| 72 | const rows = |
| 73 | .select({ |
| 74 | issueId: schema.pullRequest |
| 75 | id: schema.pullRequest |
| 76 | state: schema.pullRequest |
| 77 | draft: schema.pullRequest |
| 78 | baseRepositoryId: schema.pullRequest |
| 79 | baseRef: schema.pullRequest |
| 80 | headRepositoryId: schema.pullRequest |
| 81 | headRef: schema.pullRequest |
| 82 | headSha: schema.pullRequest |
| 83 | mergeCommitSha: schema.pullRequest |
| 84 | mergedAt: schema.pullRequest |
| 85 | }) |
| 86 | .from(schema.pullRequest |
| 87 | .where(inArray(schema |
| 88 | return new Map |
| 89 | } |
| 90 | |
| 91 | export type PullRequestRow |
| 92 | typeof schema.pullRequest |
| 93 | | 'issueId' |
| 94 | | 'id' |
| 95 | | 'state' |
| 96 | | 'draft' |
| 97 | | 'baseRepositoryId' |
| 98 | | 'baseRef' |
| 99 | | 'headRepositoryId' |
| 100 | | 'headRef' |
| 101 | | 'headSha' |
| 102 | | 'mergeCommitSha' |
| 103 | | 'mergedAt' |
| 104 | >; |
| 105 | |
| 106 | /** The accounts behind commit author emails, keyed by the lowercased email. Only verified addresses count, as on GitHub, so nobody claims another's commits by adding their address. */ |
| 107 | export async function usersByEmailOf |
| 108 | const wanted = |
| 109 | if (wanted.length |
| 110 | const [primaries, |
| 111 | db |
| 112 | .select() |
| 113 | .from(schema.user |
| 114 | .where( |
| 115 | and( |
| 116 | inArray(sql`lower(${ |
| 117 | eq(schema.user |
| 118 | ), |
| 119 | ), |
| 120 | db |
| 121 | .select({ email: |
| 122 | .from(schema.userEmail |
| 123 | .innerJoin(schema.user |
| 124 | .where( |
| 125 | and( |
| 126 | inArray(sql`lower(${ |
| 127 | eq(schema.userEmail |
| 128 | ), |
| 129 | ), |
| 130 | ]); |
| 131 | return new Map |
| 132 | ...extras.map( |
| 133 | ...primaries.map( |
| 134 | ]); |
| 135 | } |
| 136 | |
| 137 | /** Submitted reviews of pull_request ids, oldest first; a pending review is its author's alone. */ |
| 138 | export function reviewsOf(db |
| 139 | return db |
| 140 | .select({ |
| 141 | id: schema.pullRequestReview |
| 142 | numericId: schema.pullRequestReview |
| 143 | pullRequestId: schema.pullRequestReview |
| 144 | authorLogin: sql<string |
| 145 | state: schema.pullRequestReview |
| 146 | body: schema.pullRequestReview |
| 147 | commitSha: schema.pullRequestReview |
| 148 | dismissed: sql<boolean |
| 149 | createdAt: schema.pullRequestReview |
| 150 | submittedAt: schema.pullRequestReview |
| 151 | }) |
| 152 | .from(schema.pullRequestReview |
| 153 | .innerJoin( |
| 154 | schema.user, |
| 155 | eq(schema.user |
| 156 | ) |
| 157 | .where( |
| 158 | and( |
| 159 | inArray(schema.pullRequestReview |
| 160 | isNotNull(schema.pullRequestReview |
| 161 | ), |
| 162 | ) |
| 163 | .orderBy( |
| 164 | asc(schema.pullRequestReview |
| 165 | asc(schema.pullRequestReview |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /** The integer ids GitHub's release shapes need, which ReleasesService leaves out, keyed by Ghost id. */ |
| 170 | export async function releaseNumericIdsOf |
| 171 | const rows = |
| 172 | releaseIds.length > 0 |
| 173 | ? await db |
| 174 | .select({ |
| 175 | id: schema.release |
| 176 | numericId: schema.release |
| 177 | }) |
| 178 | .from(schema.release |
| 179 | .where(inArray(schema |
| 180 | : []; |
| 181 | return new Map |
| 182 | } |
| 183 | |
| 184 | /** What GitHub's release asset has and ReleasesService's asset leaves out, keyed by Ghost id. */ |
| 185 | export async function releaseAssetExtrasOf |
| 186 | const rows = |
| 187 | assetIds.length > 0 |
| 188 | ? await db |
| 189 | .select({ |
| 190 | id: schema.releaseAsset |
| 191 | numericId: schema.releaseAsset |
| 192 | label: schema.releaseAsset |
| 193 | updatedAt: isoTimestamp(schema |
| 194 | uploaderLogin: schema.user |
| 195 | }) |
| 196 | .from(schema.releaseAsset |
| 197 | .leftJoin( |
| 198 | schema.user, |
| 199 | eq(schema.user |
| 200 | ) |
| 201 | .where(inArray(schema |
| 202 | : []; |
| 203 | return new Map |
| 204 | } |
| 205 | |
| 206 | export type ReleaseAssetExtras |
| 207 | Awaited<ReturnType |
| 208 | ? T |
| 209 | : never; |