|
| 1 | +import { beforeAll, describe, expect, it } from 'vitest' |
| 2 | +import { createOctokitClient, TEST_OWNER, TEST_REPO } from './helpers' |
| 3 | + |
| 4 | +describe('github deployments API', () => { |
| 5 | + let commitSha: string |
| 6 | + let deploymentsSupported = true |
| 7 | + |
| 8 | + beforeAll(async () => { |
| 9 | + const octokit = createOctokitClient() |
| 10 | + |
| 11 | + const { data: blob } = await octokit.rest.git.createBlob({ |
| 12 | + owner: TEST_OWNER, |
| 13 | + repo: TEST_REPO, |
| 14 | + content: 'deployment test', |
| 15 | + encoding: 'utf-8', |
| 16 | + }) |
| 17 | + |
| 18 | + const { data: tree } = await octokit.rest.git.createTree({ |
| 19 | + owner: TEST_OWNER, |
| 20 | + repo: TEST_REPO, |
| 21 | + tree: [{ path: 'deploy.txt', mode: '100644', type: 'blob', sha: blob.sha }], |
| 22 | + }) |
| 23 | + |
| 24 | + const { data: commit } = await octokit.rest.git.createCommit({ |
| 25 | + owner: TEST_OWNER, |
| 26 | + repo: TEST_REPO, |
| 27 | + message: 'deploy commit', |
| 28 | + tree: tree.sha, |
| 29 | + parents: [], |
| 30 | + }) |
| 31 | + |
| 32 | + commitSha = commit.sha |
| 33 | + |
| 34 | + try { |
| 35 | + await octokit.rest.repos.createDeployment({ |
| 36 | + owner: TEST_OWNER, |
| 37 | + repo: TEST_REPO, |
| 38 | + ref: commitSha, |
| 39 | + auto_merge: false, |
| 40 | + required_contexts: [], |
| 41 | + }) |
| 42 | + } |
| 43 | + catch { |
| 44 | + deploymentsSupported = false |
| 45 | + } |
| 46 | + }) |
| 47 | + |
| 48 | + it('should create a deployment', async () => { |
| 49 | + if (!deploymentsSupported) { |
| 50 | + console.log('Skipping: GitHub Deployments API not supported by emulator') |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + const octokit = createOctokitClient() |
| 55 | + |
| 56 | + const { data: deployment } = await octokit.rest.repos.createDeployment({ |
| 57 | + owner: TEST_OWNER, |
| 58 | + repo: TEST_REPO, |
| 59 | + ref: commitSha, |
| 60 | + auto_merge: false, |
| 61 | + required_contexts: [], |
| 62 | + }) |
| 63 | + |
| 64 | + expect(deployment).toBeDefined() |
| 65 | + expect('id' in deployment).toBe(true) |
| 66 | + }) |
| 67 | + |
| 68 | + it('should create deployment statuses with state transitions', async () => { |
| 69 | + if (!deploymentsSupported) { |
| 70 | + console.log('Skipping: GitHub Deployments API not supported by emulator') |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + const octokit = createOctokitClient() |
| 75 | + |
| 76 | + const { data: deployment } = await octokit.rest.repos.createDeployment({ |
| 77 | + owner: TEST_OWNER, |
| 78 | + repo: TEST_REPO, |
| 79 | + ref: commitSha, |
| 80 | + auto_merge: false, |
| 81 | + required_contexts: [], |
| 82 | + }) |
| 83 | + |
| 84 | + if (!('id' in deployment)) { |
| 85 | + throw new Error('Expected deployment to have an id') |
| 86 | + } |
| 87 | + |
| 88 | + const { data: pendingStatus } = await octokit.rest.repos.createDeploymentStatus({ |
| 89 | + owner: TEST_OWNER, |
| 90 | + repo: TEST_REPO, |
| 91 | + deployment_id: deployment.id, |
| 92 | + state: 'pending', |
| 93 | + }) |
| 94 | + expect(pendingStatus.state).toBe('pending') |
| 95 | + |
| 96 | + const { data: successStatus } = await octokit.rest.repos.createDeploymentStatus({ |
| 97 | + owner: TEST_OWNER, |
| 98 | + repo: TEST_REPO, |
| 99 | + deployment_id: deployment.id, |
| 100 | + state: 'success', |
| 101 | + }) |
| 102 | + expect(successStatus.state).toBe('success') |
| 103 | + }) |
| 104 | + |
| 105 | + it('should list deployment statuses', async () => { |
| 106 | + if (!deploymentsSupported) { |
| 107 | + console.log('Skipping: GitHub Deployments API not supported by emulator') |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + const octokit = createOctokitClient() |
| 112 | + |
| 113 | + const { data: deployment } = await octokit.rest.repos.createDeployment({ |
| 114 | + owner: TEST_OWNER, |
| 115 | + repo: TEST_REPO, |
| 116 | + ref: commitSha, |
| 117 | + auto_merge: false, |
| 118 | + required_contexts: [], |
| 119 | + }) |
| 120 | + |
| 121 | + if (!('id' in deployment)) { |
| 122 | + throw new Error('Expected deployment to have an id') |
| 123 | + } |
| 124 | + |
| 125 | + await octokit.rest.repos.createDeploymentStatus({ |
| 126 | + owner: TEST_OWNER, |
| 127 | + repo: TEST_REPO, |
| 128 | + deployment_id: deployment.id, |
| 129 | + state: 'success', |
| 130 | + }) |
| 131 | + |
| 132 | + const { data: statuses } = await octokit.rest.repos.listDeploymentStatuses({ |
| 133 | + owner: TEST_OWNER, |
| 134 | + repo: TEST_REPO, |
| 135 | + deployment_id: deployment.id, |
| 136 | + }) |
| 137 | + |
| 138 | + expect(statuses.length).toBeGreaterThan(0) |
| 139 | + const found = statuses.find(s => s.state === 'success') |
| 140 | + expect(found).toBeDefined() |
| 141 | + }) |
| 142 | +}) |
0 commit comments