| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { promisify } |
| 6 | |
| 7 | import type { INestApplication |
| 8 | import request from 'supertest |
| 9 | import { afterAll, beforeAll |
| 10 | |
| 11 | import { hasBackends, signUp |
| 12 | |
| 13 | /** The release routes and fields gh's own suite (gh-cli.e2e-spec.ts) does not reach: paging, uploads of any type, labels, renames, refusals and the GraphQL release fields. */ |
| 14 | describe.skipIf(!hasBackends |
| 15 | let app: |
| 16 | let work: |
| 17 | let owner: |
| 18 | let reader: |
| 19 | const username = |
| 20 | const repo = |
| 21 | const http = |
| 22 | const get = |
| 23 | http().get(` |
| 24 | const send = |
| 25 | method: 'post |
| 26 | path: string, |
| 27 | body: object |
| 28 | token = owner. |
| 29 | ) => |
| 30 | http() |
| 31 | [method](`/api/v3${path |
| 32 | .set('authorization', |
| 33 | .send(body); |
| 34 | const upload = |
| 35 | url: string, |
| 36 | bytes: string, |
| 37 | type: string, |
| 38 | token = owner. |
| 39 | ) => |
| 40 | http() |
| 41 | .post(new URL |
| 42 | .set('authorization', |
| 43 | .set('content-type', |
| 44 | .send(bytes); |
| 45 | /** A release's upload_url with its template filled in. */ |
| 46 | const uploadUrl = |
| 47 | `${template.replace( |
| 48 | const graphql = |
| 49 | const { body |
| 50 | .post('/api/graphql') |
| 51 | .set('authorization', |
| 52 | .send({ query |
| 53 | .expect(200); |
| 54 | return body; |
| 55 | }; |
| 56 | |
| 57 | beforeAll(async () |
| 58 | let origin: |
| 59 | ({ app, origin |
| 60 | owner = await |
| 61 | reader = await |
| 62 | await http() |
| 63 | .post('/api/repositories') |
| 64 | .set('cookie', |
| 65 | .send({ name: |
| 66 | .expect(201); |
| 67 | |
| 68 | work = mkdtempSync( |
| 69 | const git = |
| 70 | execFileSync( |
| 71 | 'git', |
| 72 | ['-c', 'user.name=E2E' |
| 73 | { cwd: work |
| 74 | ); |
| 75 | git('init', |
| 76 | writeFileSync(path.join |
| 77 | git('add', |
| 78 | git('commit', |
| 79 | git('checkout', |
| 80 | // The server runs in this process, so the push must not block the event loop. |
| 81 | await promisify(execFile |
| 82 | 'git', |
| 83 | [ |
| 84 | 'push', |
| 85 | '-q', |
| 86 | `${origin.replace( |
| 87 | 'main', |
| 88 | 'feature', |
| 89 | ], |
| 90 | { cwd: work, |
| 91 | ); |
| 92 | }, 120_000); |
| 93 | |
| 94 | afterAll(async () |
| 95 | await app?.close |
| 96 | if (work) rmSync |
| 97 | }); |
| 98 | |
| 99 | it('creates a release with its tag and answers it in GitHub shape', |
| 100 | const { body, |
| 101 | tag_name: 'v1.0', |
| 102 | name: 'First', |
| 103 | body: 'Notes', |
| 104 | make_latest: 'legacy', |
| 105 | }).expect(201) |
| 106 | expect(headers.location |
| 107 | expect(body).toMatchObject |
| 108 | id: expect.any |
| 109 | node_id: expect.stringMatching |
| 110 | tag_name: 'v1.0', |
| 111 | target_commitish: expect.stringMatching |
| 112 | name: 'First', |
| 113 | body: 'Notes', |
| 114 | draft: false, |
| 115 | prerelease: false, |
| 116 | immutable: false, |
| 117 | tarball_url: null, |
| 118 | zipball_url: null, |
| 119 | published_at: expect.any |
| 120 | author: expect.objectContaining |
| 121 | assets: [], |
| 122 | }); |
| 123 | expect(body.url |
| 124 | new RegExp(`/api/v3/repos/ |
| 125 | ); |
| 126 | expect(body.html_url |
| 127 | expect(body.upload_url |
| 128 | new RegExp( |
| 129 | `/api/uploads/repos/${username}/tools/releases/${ |
| 130 | ), |
| 131 | ); |
| 132 | expect((await get |
| 133 | body, |
| 134 | ); |
| 135 | expect((await get |
| 136 | body.id, |
| 137 | ); |
| 138 | expect((await get |
| 139 | body.id, |
| 140 | ); |
| 141 | }); |
| 142 | |
| 143 | it('answers what Ghost cannot do, and a duplicate tag, as GitHub does', |
| 144 | expect( |
| 145 | (await send(' |
| 146 | .body, |
| 147 | ).toMatchObject({ |
| 148 | message: 'Validation Failed', |
| 149 | errors: [ |
| 150 | { resource: 'Release', |
| 151 | ], |
| 152 | }); |
| 153 | expect( |
| 154 | ( |
| 155 | await send('post |
| 156 | tag_name: 'v9', |
| 157 | target_commitish: 'nope', |
| 158 | }).expect(422) |
| 159 | ).body.errors, |
| 160 | ).toEqual([ |
| 161 | { resource: 'Release', |
| 162 | ]); |
| 163 | expect( |
| 164 | ( |
| 165 | await send('post |
| 166 | tag_name: 'bad..tag', |
| 167 | }).expect(422) |
| 168 | ).body.errors, |
| 169 | ).toEqual([{ resource |
| 170 | await send('post |
| 171 | tag_name: 'v9', |
| 172 | generate_release_notes: true, |
| 173 | }).expect(422) |
| 174 | await send('post |
| 175 | tag_name: 'v9', |
| 176 | discussion_category_name: 'Announcements', |
| 177 | }).expect(404) |
| 178 | await send( |
| 179 | 'post', |
| 180 | `${repo}/releases`, |
| 181 | { tag_name: 'v9' |
| 182 | reader.key, |
| 183 | ).expect(403); |
| 184 | await http() |
| 185 | .post(`/api/v3${repo |
| 186 | .send({ tag_name: |
| 187 | .expect(401); |
| 188 | }); |
| 189 | |
| 190 | it('keeps a draft out of the tag route and from readers, but not from GraphQL', |
| 191 | const { body: |
| 192 | tag_name: 'v2.0', |
| 193 | target_commitish: 'feature', |
| 194 | draft: true, |
| 195 | }).expect(201) |
| 196 | expect(draft).toMatchObject |
| 197 | await get(`${ |
| 198 | await get(`${ |
| 199 | expect( |
| 200 | (await get(` |
| 201 | ).toBe(draft. |
| 202 | |
| 203 | const { data |
| 204 | repository(owner: "${username}", name: "tools") { |
| 205 | release(tagName: "v2.0") { databaseId isDraft tagName } |
| 206 | missing: release(tagName: "v8") { id } |
| 207 | } |
| 208 | }`); |
| 209 | expect(data.repository |
| 210 | release: { databaseId: draft |
| 211 | missing: null, |
| 212 | }); |
| 213 | }); |
| 214 | |
| 215 | it('lists releases over REST with paging, and over GraphQL in either order', |
| 216 | const all = |
| 217 | expect( |
| 218 | all.body.map |
| 219 | ).toEqual(['v2.0' |
| 220 | const first = |
| 221 | expect(first.body |
| 222 | expect(first.headers |
| 223 | expect( |
| 224 | (await get(` |
| 225 | ).toHaveLength(1); |
| 226 | |
| 227 | const list = |
| 228 | graphql(`{ |
| 229 | repository(owner: "${username}", name: "tools") { |
| 230 | releases(first: 1, orderBy: ${orderBy}${after && |
| 231 | totalCount |
| 232 | nodes { tagName isLatest isDraft isPrerelease name publishedAt createdAt url } |
| 233 | pageInfo { hasNextPage endCursor } |
| 234 | } |
| 235 | } |
| 236 | }`); |
| 237 | const page = |
| 238 | .repository.releases; |
| 239 | expect(page).toMatchObject |
| 240 | totalCount: 2, |
| 241 | nodes: [{ tagName: |
| 242 | pageInfo: { hasNextPage: true |
| 243 | }); |
| 244 | const next = |
| 245 | await list( |
| 246 | '{ field: CREATED_AT, direction: DESC }', |
| 247 | page.pageInfo.endCursor |
| 248 | ) |
| 249 | ).data.repository. |
| 250 | expect(next).toMatchObject |
| 251 | nodes: [{ tagName: |
| 252 | pageInfo: { hasNextPage: false |
| 253 | }); |
| 254 | expect(next.nodes |
| 255 | // a nameless release sorts by its tag, so `First` comes before `v2.0` |
| 256 | expect( |
| 257 | (await list(' |
| 258 | .nodes, |
| 259 | ).toEqual([expect. |
| 260 | }); |
| 261 | |
| 262 | it('uploads assets of any type with a label, serves, renames and deletes them', |
| 263 | const { body: |
| 264 | 200, |
| 265 | ); |
| 266 | const json = |
| 267 | uploadUrl(release.upload_url |
| 268 | '{"a":1}', |
| 269 | 'application/json', |
| 270 | ).expect(201); |
| 271 | expect(json.body |
| 272 | id: expect.any |
| 273 | node_id: expect.stringMatching |
| 274 | name: 'data.json', |
| 275 | label: 'Data', |
| 276 | state: 'uploaded', |
| 277 | content_type: 'application/json', |
| 278 | size: 7, |
| 279 | digest: null, |
| 280 | download_count: 0, |
| 281 | uploader: expect.objectContaining |
| 282 | }); |
| 283 | expect(json.body |
| 284 | new RegExp(`/releases/assets/ |
| 285 | ); |
| 286 | expect(json.body |
| 287 | /\/tools\/releases\/ |
| 288 | ); |
| 289 | await upload( |
| 290 | uploadUrl(release.upload_url |
| 291 | 'hello\n', |
| 292 | 'text/plain; charset=utf-8', |
| 293 | ).expect(201); |
| 294 | |
| 295 | expect( |
| 296 | ( |
| 297 | await upload( |
| 298 | uploadUrl(release.upload_url |
| 299 | 'again', |
| 300 | 'application/json', |
| 301 | ).expect(422) |
| 302 | ).body.errors, |
| 303 | ).toEqual([ |
| 304 | { resource: 'ReleaseAsset', |
| 305 | ]); |
| 306 | await upload( |
| 307 | uploadUrl(release.upload_url |
| 308 | 'x', |
| 309 | 'not a type', |
| 310 | ).expect(422); |
| 311 | await upload( |
| 312 | uploadUrl(release.upload_url |
| 313 | 'x', |
| 314 | 'application/octet-stream', |
| 315 | reader.key, |
| 316 | ).expect(403); |
| 317 | |
| 318 | const assets = |
| 319 | 200, |
| 320 | ); |
| 321 | expect(assets.body |
| 322 | 'data.json', |
| 323 | 'notes.txt', |
| 324 | ]); |
| 325 | expect( |
| 326 | ( |
| 327 | await get(`${ |
| 328 | 200, |
| 329 | ) |
| 330 | ).headers.link, |
| 331 | ).toContain('rel="next"' |
| 332 | |
| 333 | const bytes = |
| 334 | .set('accept', |
| 335 | .buffer(true) |
| 336 | .parse((response, |
| 337 | const chunks: |
| 338 | response.on('data |
| 339 | response.on('end |
| 340 | }) |
| 341 | .expect(200); |
| 342 | expect(bytes.body |
| 343 | expect( |
| 344 | (await get(` |
| 345 | ).toMatchObject({ name |
| 346 | |
| 347 | const renamed = |
| 348 | 'patch', |
| 349 | `${repo}/releases/assets/${json |
| 350 | { name: 'data-v1.json', |
| 351 | ).expect(200); |
| 352 | expect(renamed.body |
| 353 | id: json.body |
| 354 | name: 'data-v1.json', |
| 355 | label: null, |
| 356 | }); |
| 357 | expect( |
| 358 | ( |
| 359 | await send('patch |
| 360 | name: 'notes.txt', |
| 361 | }).expect(422) |
| 362 | ).body.errors, |
| 363 | ).toEqual([ |
| 364 | { resource: 'ReleaseAsset', |
| 365 | ]); |
| 366 | |
| 367 | await send('delete |
| 368 | await get(`${ |
| 369 | await get(`${ |
| 370 | }); |
| 371 | |
| 372 | it('edits a release, refusing to move it to another tag', |
| 373 | const { body: |
| 374 | 200, |
| 375 | ); |
| 376 | const { body |
| 377 | tag_name: 'v1.0', |
| 378 | name: 'First (edited)', |
| 379 | body: null, |
| 380 | prerelease: true, |
| 381 | }).expect(200) |
| 382 | expect(body).toMatchObject |
| 383 | name: 'First (edited)', |
| 384 | body: null, |
| 385 | prerelease: true, |
| 386 | }); |
| 387 | // a prerelease is never the latest |
| 388 | await get(`${ |
| 389 | expect( |
| 390 | ( |
| 391 | await send('patch |
| 392 | tag_name: 'v1.1', |
| 393 | }).expect(422) |
| 394 | ).body, |
| 395 | ).toMatchObject({ |
| 396 | message: 'Ghost cannot move a release to another tag', |
| 397 | }); |
| 398 | await send('patch |
| 399 | discussion_category_name: 'Announcements', |
| 400 | }).expect(404) |
| 401 | }); |
| 402 | |
| 403 | it('deletes a release, then its tag through git/refs, and refuses a missing ref', |
| 404 | const { body: |
| 405 | 200, |
| 406 | ); |
| 407 | await send('delete |
| 408 | await get(`${ |
| 409 | |
| 410 | await send('delete |
| 411 | expect( |
| 412 | (await send(' |
| 413 | ).toMatchObject({ message |
| 414 | await send('delete |
| 415 | await send('delete |
| 416 | await send('delete |
| 417 | await send('delete |
| 418 | 403, |
| 419 | ); |
| 420 | }); |
| 421 | }); |