|
| 1 | +import { writeFileSync } from 'fs' |
| 2 | +import { tmpdir } from 'os' |
| 3 | +import { resolve } from 'path' |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +type CoreMock = { |
| 7 | + getInput: ReturnType<typeof vi.fn> |
| 8 | + info: ReturnType<typeof vi.fn> |
| 9 | + warning: ReturnType<typeof vi.fn> |
| 10 | + setFailed: ReturnType<typeof vi.fn> |
| 11 | +} |
| 12 | + |
| 13 | +const core: CoreMock = { |
| 14 | + getInput: vi.fn(), |
| 15 | + info: vi.fn(), |
| 16 | + warning: vi.fn(), |
| 17 | + setFailed: vi.fn(), |
| 18 | +} |
| 19 | + |
| 20 | +vi.mock('@actions/core', () => core) |
| 21 | + |
| 22 | +const createTempEventFile = (payload: unknown) => { |
| 23 | + const filePath = resolve(tmpdir(), `deploy-production-failure-${Date.now()}-${Math.random().toString(16).slice(2)}.json`) |
| 24 | + writeFileSync(filePath, JSON.stringify(payload), 'utf8') |
| 25 | + return filePath |
| 26 | +} |
| 27 | + |
| 28 | +const createMockResponse = (params: { ok: boolean; status: number; json?: unknown }) => { |
| 29 | + return { |
| 30 | + ok: params.ok, |
| 31 | + status: params.status, |
| 32 | + json: async () => params.json, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +describe('deploy-production-failure action', () => { |
| 37 | + const originalEnv = process.env |
| 38 | + |
| 39 | + beforeEach(() => { |
| 40 | + vi.clearAllMocks() |
| 41 | + process.env = { ...originalEnv } |
| 42 | + delete process.env['GITHUB_EVENT_PATH'] |
| 43 | + |
| 44 | + core.getInput = vi.fn() |
| 45 | + core.info = vi.fn() |
| 46 | + core.warning = vi.fn() |
| 47 | + core.setFailed = vi.fn() |
| 48 | + }) |
| 49 | + |
| 50 | + afterEach(() => { |
| 51 | + process.env = originalEnv |
| 52 | + vi.unstubAllGlobals() |
| 53 | + }) |
| 54 | + |
| 55 | + it('skips when head_sha is missing', async () => { |
| 56 | + const { run } = await import('../src/index') |
| 57 | + |
| 58 | + const eventPath = createTempEventFile({ |
| 59 | + 'workflow_run': {}, |
| 60 | + repository: { owner: { login: 'webstackdev' }, name: 'astro.webstackbuilders.com' }, |
| 61 | + }) |
| 62 | + |
| 63 | + process.env['GITHUB_EVENT_PATH'] = eventPath |
| 64 | + |
| 65 | + core.getInput.mockImplementation((name: string, options?: { required?: boolean }) => { |
| 66 | + if (name === 'github-token') return 'ghs_test' |
| 67 | + if (options?.required) throw new Error(`Missing required input: ${name}`) |
| 68 | + return '' |
| 69 | + }) |
| 70 | + |
| 71 | + const fetchMock = vi.fn(async () => createMockResponse({ ok: true, status: 200, json: {} })) |
| 72 | + vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch) |
| 73 | + |
| 74 | + await run() |
| 75 | + |
| 76 | + expect(core.warning).toHaveBeenCalledWith('Missing workflow_run.head_sha; skipping production failure comment.') |
| 77 | + expect(fetchMock).not.toHaveBeenCalled() |
| 78 | + expect(core.setFailed).not.toHaveBeenCalled() |
| 79 | + }) |
| 80 | + |
| 81 | + it('creates commit comment with production url when provided', async () => { |
| 82 | + const { run } = await import('../src/index') |
| 83 | + |
| 84 | + const eventPath = createTempEventFile({ |
| 85 | + 'workflow_run': { 'head_sha': '0123456789abcdef' }, |
| 86 | + repository: { owner: { login: 'webstackdev' }, name: 'astro.webstackbuilders.com' }, |
| 87 | + }) |
| 88 | + |
| 89 | + process.env['GITHUB_EVENT_PATH'] = eventPath |
| 90 | + |
| 91 | + const inputs: Record<string, string> = { |
| 92 | + 'github-token': 'ghs_test', |
| 93 | + 'preview-url': 'https://example.vercel.app', |
| 94 | + } |
| 95 | + |
| 96 | + core.getInput.mockImplementation((name: string, options?: { required?: boolean }) => { |
| 97 | + const value = (inputs[name] ?? '').trim() |
| 98 | + if (options?.required && !value) { |
| 99 | + throw new Error(`Missing required input: ${name}`) |
| 100 | + } |
| 101 | + return value |
| 102 | + }) |
| 103 | + |
| 104 | + const fetchMock = vi.fn(async (url: string, init?: RequestInit) => { |
| 105 | + if (String(url).includes('/commits/0123456789abcdef/comments') && init?.method === 'POST') { |
| 106 | + return createMockResponse({ ok: true, status: 201, json: { id: 1 } }) |
| 107 | + } |
| 108 | + return createMockResponse({ ok: false, status: 404 }) |
| 109 | + }) |
| 110 | + |
| 111 | + vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch) |
| 112 | + |
| 113 | + await run() |
| 114 | + |
| 115 | + const createCall = fetchMock.mock.calls.find( |
| 116 | + ([url, init]) => String(url).includes('/commits/0123456789abcdef/comments') && (init as RequestInit | undefined)?.method === 'POST', |
| 117 | + ) |
| 118 | + expect(createCall).toBeTruthy() |
| 119 | + |
| 120 | + const [, init] = createCall as [string, RequestInit] |
| 121 | + const body = JSON.parse(String(init.body)) as { body?: string } |
| 122 | + |
| 123 | + expect(body.body).toContain('❌ Production deployment failed.') |
| 124 | + expect(body.body).toContain('🔗 https://example.vercel.app') |
| 125 | + expect(core.setFailed).not.toHaveBeenCalled() |
| 126 | + }) |
| 127 | + |
| 128 | + it('creates commit comment without url when none provided', async () => { |
| 129 | + const { run } = await import('../src/index') |
| 130 | + |
| 131 | + const eventPath = createTempEventFile({ |
| 132 | + 'workflow_run': { 'head_sha': '0123456789abcdef' }, |
| 133 | + repository: { owner: { login: 'webstackdev' }, name: 'astro.webstackbuilders.com' }, |
| 134 | + }) |
| 135 | + |
| 136 | + process.env['GITHUB_EVENT_PATH'] = eventPath |
| 137 | + |
| 138 | + core.getInput.mockImplementation((name: string, options?: { required?: boolean }) => { |
| 139 | + if (name === 'github-token') return 'ghs_test' |
| 140 | + if (options?.required) throw new Error(`Missing required input: ${name}`) |
| 141 | + return '' |
| 142 | + }) |
| 143 | + |
| 144 | + const fetchMock = vi.fn(async (url: string, init?: RequestInit) => { |
| 145 | + if (String(url).includes('/commits/0123456789abcdef/comments') && init?.method === 'POST') { |
| 146 | + return createMockResponse({ ok: true, status: 201, json: { id: 1 } }) |
| 147 | + } |
| 148 | + return createMockResponse({ ok: false, status: 404 }) |
| 149 | + }) |
| 150 | + |
| 151 | + vi.stubGlobal('fetch', fetchMock as unknown as typeof fetch) |
| 152 | + |
| 153 | + await run() |
| 154 | + |
| 155 | + const createCall = fetchMock.mock.calls.find( |
| 156 | + ([url, init]) => String(url).includes('/commits/0123456789abcdef/comments') && (init as RequestInit | undefined)?.method === 'POST', |
| 157 | + ) |
| 158 | + expect(createCall).toBeTruthy() |
| 159 | + |
| 160 | + const [, init] = createCall as [string, RequestInit] |
| 161 | + const body = JSON.parse(String(init.body)) as { body?: string } |
| 162 | + |
| 163 | + expect(body.body).toBe('❌ Production deployment failed. Please review the Vercel logs.') |
| 164 | + expect(core.setFailed).not.toHaveBeenCalled() |
| 165 | + }) |
| 166 | +}) |
0 commit comments