| 1 | import path from 'node:path'; |
| 2 | import { createDatabase, type |
| 3 | import { and, eq |
| 4 | import { migrate } |
| 5 | import { afterAll, beforeAll |
| 6 | |
| 7 | import { PullRequestReopenError } |
| 8 | import { IssuesService } |
| 9 | import { RepositoryAccessService } |
| 10 | import { UsersService } |
| 11 | import { IssueReferencesService } |
| 12 | |
| 13 | const CONNECTION = process |
| 14 | const MIGRATIONS = path |
| 15 | import.meta.dirname, |
| 16 | '../../../../../packages/db/drizzle', |
| 17 | ); |
| 18 | |
| 19 | const OWNER = ' |
| 20 | const STRANGER = ' |
| 21 | |
| 22 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 23 | describe.skipIf(!CONNECTION |
| 24 | let db: |
| 25 | let pool: |
| 26 | let references: |
| 27 | let issues: |
| 28 | let repository: |
| 29 | let secret: |
| 30 | |
| 31 | const open = |
| 32 | issues.open( |
| 33 | { repository, title |
| 34 | async () => |
| 35 | ); |
| 36 | |
| 37 | const referencesTo = |
| 38 | db |
| 39 | .select() |
| 40 | .from(schema.issueReference |
| 41 | .where(eq(schema |
| 42 | |
| 43 | beforeAll(async () |
| 44 | ({ db, pool |
| 45 | await migrate(db |
| 46 | |
| 47 | references = new |
| 48 | issues = new |
| 49 | db, |
| 50 | new UsersService(db |
| 51 | new RepositoryAccessService(db |
| 52 | references, |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | beforeEach(async () |
| 57 | await db |
| 58 | .delete(schema.user |
| 59 | .where(inArray(schema |
| 60 | await db.insert |
| 61 | { |
| 62 | id: OWNER, |
| 63 | name: 'Owner', |
| 64 | email: 'refspec-owner@example.com', |
| 65 | username: 'refspec-owner', |
| 66 | }, |
| 67 | { |
| 68 | id: STRANGER, |
| 69 | name: 'Stranger', |
| 70 | email: 'refspec-stranger@example.com', |
| 71 | username: 'refspec-stranger', |
| 72 | }, |
| 73 | ]); |
| 74 | [repository, secret] |
| 75 | .insert(schema.repository |
| 76 | .values([ |
| 77 | { name: 'app', |
| 78 | { name: 'secret', |
| 79 | ]) |
| 80 | .returning(); |
| 81 | }); |
| 82 | |
| 83 | afterAll(async () |
| 84 | await db |
| 85 | .delete(schema.user |
| 86 | .where(inArray(schema |
| 87 | await pool?.end |
| 88 | }); |
| 89 | |
| 90 | it('numbers issues and pull requests from one sequence', |
| 91 | const issue = |
| 92 | const pull = |
| 93 | |
| 94 | expect([issue.number |
| 95 | }); |
| 96 | |
| 97 | it('records same-repository and cross-repository references, skipping itself and unknown numbers', |
| 98 | const target = |
| 99 | const elsewhere = |
| 100 | { |
| 101 | repository: secret, |
| 102 | title: 'secret target', |
| 103 | body: null, |
| 104 | authorId: OWNER, |
| 105 | isPullRequest: false, |
| 106 | }, |
| 107 | async () => |
| 108 | ); |
| 109 | const source = |
| 110 | 'source', |
| 111 | 'fixes #1, see #2 and #99, and refspec-owner/secret#1', |
| 112 | ); |
| 113 | |
| 114 | const rows = |
| 115 | .select() |
| 116 | .from(schema.issueReference |
| 117 | .where(eq(schema |
| 118 | expect(rows.map |
| 119 | [ |
| 120 | [target.id, |
| 121 | [elsewhere.id, |
| 122 | ].sort(), |
| 123 | ); |
| 124 | }); |
| 125 | |
| 126 | it('resolves nothing in a repository the author cannot read', |
| 127 | const hidden = |
| 128 | { |
| 129 | repository: secret, |
| 130 | title: 'hidden', |
| 131 | body: null, |
| 132 | authorId: OWNER, |
| 133 | isPullRequest: false, |
| 134 | }, |
| 135 | async () => |
| 136 | ); |
| 137 | await references.record |
| 138 | db, |
| 139 | { |
| 140 | type: 'comment', |
| 141 | id: 'ic_stranger', |
| 142 | repository, |
| 143 | issueId: null, |
| 144 | actorId: STRANGER, |
| 145 | }, |
| 146 | 'refspec-owner/secret#1', |
| 147 | ); |
| 148 | |
| 149 | expect(await referencesTo |
| 150 | }); |
| 151 | |
| 152 | it('keeps a surviving reference on edit, updates its closing flag and drops removed ones', |
| 153 | const first = |
| 154 | const second = |
| 155 | const source = |
| 156 | const [before] |
| 157 | |
| 158 | await issues.updateIssue |
| 159 | username: 'refspec-owner', |
| 160 | repo: 'app', |
| 161 | number: source.number |
| 162 | requesterId: OWNER, |
| 163 | body: { body: ' |
| 164 | }); |
| 165 | |
| 166 | const [after] |
| 167 | expect(after).toMatchObject |
| 168 | expect(await referencesTo |
| 169 | }); |
| 170 | |
| 171 | it('forgets a deleted comment’s references', |
| 172 | const target = |
| 173 | const source = |
| 174 | const ref = |
| 175 | username: 'refspec-owner', |
| 176 | repo: 'app', |
| 177 | number: source.number |
| 178 | }; |
| 179 | const comment = |
| 180 | ...ref, |
| 181 | requesterId: OWNER, |
| 182 | body: 'relates to #1', |
| 183 | }); |
| 184 | expect(await referencesTo |
| 185 | |
| 186 | await issues.deleteComment |
| 187 | ...ref, |
| 188 | requesterId: OWNER, |
| 189 | commentId: comment.id |
| 190 | }); |
| 191 | expect(await referencesTo |
| 192 | }); |
| 193 | |
| 194 | it('shows one mention per source issue, and hides private sources from other viewers', |
| 195 | const target = |
| 196 | const source = |
| 197 | await issues.createComment |
| 198 | username: 'refspec-owner', |
| 199 | repo: 'app', |
| 200 | number: source.number |
| 201 | requesterId: OWNER, |
| 202 | body: 'again #1', |
| 203 | }); |
| 204 | await references.record |
| 205 | db, |
| 206 | { |
| 207 | type: 'issue', |
| 208 | id: 'issue_private_source', |
| 209 | repository: secret, |
| 210 | issueId: null, |
| 211 | actorId: OWNER, |
| 212 | }, |
| 213 | 'refspec-owner/app#1', |
| 214 | ); |
| 215 | |
| 216 | const owner = |
| 217 | const stranger = |
| 218 | userId: STRANGER, |
| 219 | }); |
| 220 | |
| 221 | expect(owner).toHaveLength |
| 222 | expect(stranger).toEqual |
| 223 | expect.objectContaining({ |
| 224 | sourceType: 'issue', |
| 225 | repository: { username: ' |
| 226 | source: { |
| 227 | number: source.number |
| 228 | title: 'source', |
| 229 | state: 'open', |
| 230 | isPullRequest: false, |
| 231 | }, |
| 232 | }), |
| 233 | ]); |
| 234 | }); |
| 235 | |
| 236 | it('closes issues a commit promises to, but never a pull request or a repository the pusher cannot write to', |
| 237 | const issue = |
| 238 | const pull = |
| 239 | |
| 240 | await references.closeFromCommits |
| 241 | repository, |
| 242 | actorId: STRANGER, |
| 243 | commits: [commit(' |
| 244 | }); |
| 245 | expect(await stateOf |
| 246 | |
| 247 | await references.closeFromCommits |
| 248 | repository, |
| 249 | actorId: OWNER, |
| 250 | commits: [commit(' |
| 251 | }); |
| 252 | expect(await stateOf |
| 253 | expect(await stateOf |
| 254 | |
| 255 | const [event] |
| 256 | .select() |
| 257 | .from(schema.issueEvent |
| 258 | .where( |
| 259 | and( |
| 260 | eq(schema.issueEvent |
| 261 | eq(schema.issueEvent |
| 262 | ), |
| 263 | ); |
| 264 | expect(event).toMatchObject |
| 265 | |
| 266 | expect(await references |
| 267 | expect.objectContaining({ |
| 268 | sourceType: 'commit', |
| 269 | commitSha: 'aaaa', |
| 270 | source: null, |
| 271 | }), |
| 272 | expect.objectContaining({ |
| 273 | sourceType: 'commit', |
| 274 | commitSha: 'bbbb', |
| 275 | source: null, |
| 276 | }), |
| 277 | ]); |
| 278 | }); |
| 279 | |
| 280 | it('keeps a pull request in step when it is closed as an issue, and refuses to reopen it', |
| 281 | const pull = |
| 282 | await db.insert |
| 283 | issueId: pull.id |
| 284 | baseRepositoryId: repository.id |
| 285 | baseRef: 'main', |
| 286 | headRepositoryId: repository.id |
| 287 | headRef: 'feature', |
| 288 | headSha: 'cccc', |
| 289 | }); |
| 290 | const ref = |
| 291 | username: 'refspec-owner', |
| 292 | repo: 'app', |
| 293 | number: pull.number |
| 294 | requesterId: OWNER, |
| 295 | }; |
| 296 | |
| 297 | await issues.closeIssue |
| 298 | const [row] |
| 299 | .select({ state: |
| 300 | .from(schema.pullRequest |
| 301 | .where(eq(schema |
| 302 | expect(row.state |
| 303 | |
| 304 | await expect(issues |
| 305 | PullRequestReopenError, |
| 306 | ); |
| 307 | }); |
| 308 | |
| 309 | it('lists issues without pull requests', |
| 310 | await open('issue |
| 311 | await open('pr |
| 312 | |
| 313 | const { issues: |
| 314 | username: 'refspec-owner', |
| 315 | repo: 'app', |
| 316 | requesterId: OWNER, |
| 317 | query: {}, |
| 318 | }); |
| 319 | expect(listed.map |
| 320 | }); |
| 321 | |
| 322 | it('puts mentions into the timeline', |
| 323 | await open('target |
| 324 | await open('source |
| 325 | |
| 326 | const { timeline |
| 327 | username: 'refspec-owner', |
| 328 | repo: 'app', |
| 329 | number: 1, |
| 330 | requesterId: OWNER, |
| 331 | }); |
| 332 | expect(timeline.map |
| 333 | }); |
| 334 | |
| 335 | function stateOf(issueId |
| 336 | return db |
| 337 | .select({ state: |
| 338 | .from(schema.issue |
| 339 | .where(eq(schema |
| 340 | .then(([row |
| 341 | } |
| 342 | }); |
| 343 | |
| 344 | function commit(sha: |
| 345 | return { |
| 346 | sha, |
| 347 | subject, |
| 348 | body: '', |
| 349 | authorName: 'a', |
| 350 | authorEmail: 'a@example.com', |
| 351 | committedAt: new Date |
| 352 | }; |
| 353 | } |