|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { tmpdir } from 'node:os' |
| 3 | +import path from 'node:path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +import { attachSboms } from './attach-sboms.mjs' |
| 7 | + |
| 8 | +const TAG = 'v4.0.0-beta.4' |
| 9 | +const ZIP = 'payload-sboms-4.0.0-beta.4.zip' |
| 10 | + |
| 11 | +const release = (overrides = {}) => ({ |
| 12 | + draft: true, |
| 13 | + html_url: `https://github.com/payloadcms/payload/releases/tag/${TAG}`, |
| 14 | + id: 42, |
| 15 | + tag_name: TAG, |
| 16 | + ...overrides, |
| 17 | +}) |
| 18 | + |
| 19 | +const makeGithub = ({ existingAssets = [], releases = [release()] } = {}) => { |
| 20 | + const rest = { |
| 21 | + repos: { |
| 22 | + deleteReleaseAsset: vi.fn(async () => ({})), |
| 23 | + listReleaseAssets: vi.fn(), |
| 24 | + listReleases: vi.fn(), |
| 25 | + uploadReleaseAsset: vi.fn(async () => ({})), |
| 26 | + }, |
| 27 | + } |
| 28 | + const results = new Map([ |
| 29 | + [rest.repos.listReleaseAssets, existingAssets], |
| 30 | + [rest.repos.listReleases, releases], |
| 31 | + ]) |
| 32 | + const paginate = vi.fn(async (method) => { |
| 33 | + if (!results.has(method)) { |
| 34 | + throw new Error('unexpected paginate call') |
| 35 | + } |
| 36 | + return results.get(method) |
| 37 | + }) |
| 38 | + return { paginate, rest } |
| 39 | +} |
| 40 | + |
| 41 | +const makeCore = () => { |
| 42 | + const summary = { addRaw: vi.fn(() => summary), write: vi.fn(async () => summary) } |
| 43 | + return { info: vi.fn(), summary, warning: vi.fn() } |
| 44 | +} |
| 45 | + |
| 46 | +describe('attachSboms', () => { |
| 47 | + let dir |
| 48 | + |
| 49 | + beforeEach(() => { |
| 50 | + dir = mkdtempSync(path.join(tmpdir(), 'attach-sboms-')) |
| 51 | + writeFileSync(path.join(dir, ZIP), 'PK') |
| 52 | + }) |
| 53 | + |
| 54 | + afterEach(() => { |
| 55 | + rmSync(dir, { force: true, recursive: true }) |
| 56 | + }) |
| 57 | + |
| 58 | + const run = ({ core = makeCore(), github = makeGithub(), tag = TAG } = {}) => |
| 59 | + attachSboms({ core, dir, github, owner: 'payloadcms', repo: 'payload', tag }) |
| 60 | + |
| 61 | + it('uploads the SBOM zip for the tag with its label to the matching release', async () => { |
| 62 | + const github = makeGithub() |
| 63 | + |
| 64 | + const result = await run({ github }) |
| 65 | + |
| 66 | + expect(result).toEqual({ assetName: ZIP, releaseUrl: release().html_url }) |
| 67 | + expect(github.rest.repos.uploadReleaseAsset).toHaveBeenCalledOnce() |
| 68 | + expect(github.rest.repos.uploadReleaseAsset).toHaveBeenCalledWith( |
| 69 | + expect.objectContaining({ |
| 70 | + label: 'SBOMs', |
| 71 | + name: ZIP, |
| 72 | + owner: 'payloadcms', |
| 73 | + release_id: 42, |
| 74 | + repo: 'payload', |
| 75 | + }), |
| 76 | + ) |
| 77 | + }) |
| 78 | + |
| 79 | + it('sends the zip bytes with content type and length', async () => { |
| 80 | + const github = makeGithub() |
| 81 | + |
| 82 | + await run({ github }) |
| 83 | + |
| 84 | + const [[args]] = github.rest.repos.uploadReleaseAsset.mock.calls |
| 85 | + |
| 86 | + expect(args.data.toString()).toBe('PK') |
| 87 | + expect(args.headers).toEqual({ 'content-length': 2, 'content-type': 'application/zip' }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('picks the release that matches the tag', async () => { |
| 91 | + const github = makeGithub({ |
| 92 | + releases: [ |
| 93 | + release({ id: 1, tag_name: 'v4.0.0-beta.3' }), |
| 94 | + release(), |
| 95 | + release({ id: 3, tag_name: 'v4.0.0-beta.2' }), |
| 96 | + ], |
| 97 | + }) |
| 98 | + |
| 99 | + await run({ github }) |
| 100 | + |
| 101 | + expect(github.rest.repos.uploadReleaseAsset).toHaveBeenCalledWith( |
| 102 | + expect.objectContaining({ release_id: 42 }), |
| 103 | + ) |
| 104 | + }) |
| 105 | + |
| 106 | + it('deletes an existing asset with the same name before upload', async () => { |
| 107 | + const github = makeGithub({ |
| 108 | + existingAssets: [ |
| 109 | + { id: 6, name: 'notes.txt' }, |
| 110 | + { id: 7, name: ZIP }, |
| 111 | + ], |
| 112 | + }) |
| 113 | + |
| 114 | + await run({ github }) |
| 115 | + |
| 116 | + expect(github.rest.repos.deleteReleaseAsset).toHaveBeenCalledOnce() |
| 117 | + expect(github.rest.repos.deleteReleaseAsset).toHaveBeenCalledWith( |
| 118 | + expect.objectContaining({ asset_id: 7 }), |
| 119 | + ) |
| 120 | + expect(github.rest.repos.deleteReleaseAsset.mock.invocationCallOrder[0]).toBeLessThan( |
| 121 | + github.rest.repos.uploadReleaseAsset.mock.invocationCallOrder[0] ?? 0, |
| 122 | + ) |
| 123 | + }) |
| 124 | + |
| 125 | + it('throws with the tag when no release matches, and uploads nothing', async () => { |
| 126 | + const github = makeGithub() |
| 127 | + writeFileSync(path.join(dir, 'payload-sboms-9.9.9.zip'), 'PK') |
| 128 | + |
| 129 | + await expect(run({ github, tag: 'v9.9.9' })).rejects.toThrow('v9.9.9') |
| 130 | + expect(github.rest.repos.uploadReleaseAsset).not.toHaveBeenCalled() |
| 131 | + }) |
| 132 | + |
| 133 | + it('uploads when the release is published', async () => { |
| 134 | + const github = makeGithub({ releases: [release({ draft: false })] }) |
| 135 | + |
| 136 | + await run({ github }) |
| 137 | + |
| 138 | + expect(github.rest.repos.uploadReleaseAsset).toHaveBeenCalledOnce() |
| 139 | + }) |
| 140 | + |
| 141 | + it('throws before any API call when the zip for the tag is missing', async () => { |
| 142 | + rmSync(path.join(dir, ZIP)) |
| 143 | + writeFileSync(path.join(dir, 'payload-sboms-4.0.0-beta.3.zip'), 'PK') |
| 144 | + const github = makeGithub() |
| 145 | + |
| 146 | + await expect(run({ github })).rejects.toThrow(ZIP) |
| 147 | + expect(github.paginate).not.toHaveBeenCalled() |
| 148 | + expect(github.rest.repos.uploadReleaseAsset).not.toHaveBeenCalled() |
| 149 | + }) |
| 150 | + |
| 151 | + it('writes the zip name and release URL to the job summary', async () => { |
| 152 | + const core = makeCore() |
| 153 | + |
| 154 | + await run({ core }) |
| 155 | + |
| 156 | + expect(core.summary.addRaw).toHaveBeenCalledWith(`Attached ${ZIP} to ${release().html_url}`) |
| 157 | + expect(core.summary.write).toHaveBeenCalled() |
| 158 | + }) |
| 159 | +}) |
0 commit comments