Skip to content

Commit 931b6e5

Browse files
committed
feat: delete all deployments
1 parent 185cfc4 commit 931b6e5

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ jobs:
1818
runs-on: ubuntu-latest
1919
steps:
2020
- uses: actions/checkout@v1
21+
- uses: ./
22+
with:
23+
token: ${{github.token}}
24+
type: delete-all
2125
- uses: ./
2226
id: create
2327
with:

src/delete-all.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { context, GitHub } from '@actions/github'
2+
3+
export async function deleteAll (
4+
client: GitHub,
5+
environment: string
6+
): Promise<void> {
7+
const deployments = await client.repos.listDeployments({
8+
...context.repo,
9+
environment
10+
})
11+
12+
await Promise.all(deployments.data.map(async (deployment) => {
13+
// invalidate deployment first
14+
// since we can't delete active deployment
15+
console.log(`invalidate deployment: ${deployment.id}`)
16+
await client.repos.createDeploymentStatus({
17+
...context.repo,
18+
deployment_id: deployment.id,
19+
state: 'failure'
20+
});
21+
22+
// then delete it
23+
console.log(`delete deployment: ${deployment.url}`)
24+
await client.request(deployment.url, { "method": "DELETE" })
25+
}))
26+
}

src/finish.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { DeploymentStatus } from './deployment-status'
33

44
export async function finish (
55
client: GitHub,
6-
deploymentId: string,
6+
deploymentId: number,
77
status: DeploymentStatus,
88
logUrl: string,
99
environmentUrl: string
1010
): Promise<void> {
1111
const statusResult = await client.repos.createDeploymentStatus({
1212
...context.repo,
13-
deployment_id: Number(deploymentId),
13+
deployment_id: deploymentId,
1414
state: status,
1515
log_url: logUrl,
1616
environment_url: environmentUrl

src/main.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as core from '@actions/core'
22
import * as github from '@actions/github'
33
import { create } from './create'
44
import { finish } from './finish'
5+
import { deleteAll } from './delete-all'
56
import { DeploymentStatus } from './deployment-status'
67

78
type ActionType = 'create' | 'delete' | 'delete-all' | 'finish'
@@ -80,28 +81,35 @@ export async function run (): Promise<void> {
8081
core.setOutput('deployment_id', deploymentId)
8182
} catch (error) {
8283
core.error(error)
83-
// core.setFailed(error)
8484
throw error
8585
}
8686
break
8787
case 'finish':
8888
try {
8989
await finish(
9090
client,
91-
deploymentId,
91+
Number(deploymentId),
9292
status,
9393
logsUrl,
9494
environmentUrl
9595
)
9696
} catch (error) {
9797
core.error(error)
98-
// core.setFailed(`Could not finish a deployment: ${JSON.stringify(error, null, 2)}`)
9998
throw error
10099
}
101100
break
102101
case 'delete':
103102
break
104103
case 'delete-all':
104+
try {
105+
await deleteAll(
106+
client,
107+
environment
108+
)
109+
} catch (error) {
110+
core.error(error)
111+
throw error
112+
}
105113
break
106114
}
107115
}

tests/main.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,58 @@ describe('finish', () => {
108108
postDeploymentStatus.done()
109109
})
110110
})
111+
112+
describe('delete-all', () => {
113+
beforeEach(() => {
114+
process.env['GITHUB_REPOSITORY'] = 'owner/repo'
115+
116+
let inputs = {} as any
117+
let inputSpy: jest.SpyInstance;
118+
119+
// @actions/core
120+
inputs = {
121+
'token': 'fake-token',
122+
'type': 'delete-all',
123+
'environment': 'staging'
124+
}
125+
inputSpy = jest.spyOn(core, 'getInput');
126+
inputSpy.mockImplementation(name => inputs[name]);
127+
128+
// @actions/github
129+
Object.defineProperty(github.context, 'actor', { get: () => 'fake-actor' })
130+
Object.defineProperty(github.context, 'ref', { get: () => 'refs/heads/master' })
131+
})
132+
133+
afterEach(() => {
134+
jest.resetAllMocks();
135+
jest.clearAllMocks();
136+
})
137+
138+
it('200', async () => {
139+
// arrange
140+
const getListDeployments = nock('https://api.github.com')
141+
.get('/repos/owner/repo/deployments?environment=staging')
142+
.reply(200, [{ id: 42, url: 'https://api.github.com/repos/owner/repo/deployments/42' }])
143+
144+
const postStatus = nock('https://api.github.com')
145+
.post('/repos/owner/repo/deployments/42/statuses')
146+
.reply(200, postStatusReply)
147+
148+
const deleteDeployment = nock('https://api.github.com')
149+
.delete('/repos/owner/repo/deployments/42')
150+
.reply(200)
151+
152+
// act
153+
try {
154+
await main.run()
155+
} catch (error) {
156+
console.error(JSON.stringify(error.toString(), null, 2))
157+
console.error(JSON.stringify(error.stack, null, 2))
158+
}
159+
160+
// assert
161+
getListDeployments.done()
162+
postStatus.done()
163+
deleteDeployment.done()
164+
})
165+
})

0 commit comments

Comments
 (0)