Skip to content

Commit f1b2161

Browse files
committed
feat: delete specific deployment
1 parent 6322af5 commit f1b2161

File tree

6 files changed

+130
-2
lines changed

6 files changed

+130
-2
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,8 @@ jobs:
3333
type: finish
3434
status: success
3535
deployment_id: ${{steps.create.outputs.deployment_id}}
36+
- uses: ./
37+
with:
38+
token: ${{github.token}}
39+
type: delete
40+
deployment_id: ${{steps.create.outputs.deployment_id}}

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ inputs:
77
description: GitHub token
88
type:
99
required: true
10-
description: a type of deployment action to perform, can be 'create', 'delete', 'delete-all' and 'status'
10+
description: a type of deployment action to perform, can be 'create', 'delete', 'delete-all' and 'finish'
1111
logs:
1212
required: false
1313
description: url to deployment logs
@@ -16,7 +16,7 @@ inputs:
1616
description: description of a deployment
1717
deployment_id:
1818
required: false
19-
description: an id for deployment if 'delete' or 'status' type is used
19+
description: an id for deployment if 'delete' or 'finish' type is used
2020
runs:
2121
using: 'node12'
2222
main: 'dist/index.js'

dist/index.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,6 +3531,7 @@ const github = __importStar(__webpack_require__(469));
35313531
const create_1 = __webpack_require__(646);
35323532
const finish_1 = __webpack_require__(209);
35333533
const delete_all_1 = __webpack_require__(832);
3534+
const delete_1 = __webpack_require__(583);
35343535
// nullify getInput empty results
35353536
// to allow coalescence ?? operator
35363537
function getInput(name, options) {
@@ -3602,6 +3603,13 @@ function run() {
36023603
}
36033604
break;
36043605
case 'delete':
3606+
try {
3607+
yield delete_1.deleteDeployment(client, Number(deploymentId));
3608+
}
3609+
catch (error) {
3610+
core.error(error);
3611+
throw error;
3612+
}
36053613
break;
36063614
case 'delete-all':
36073615
try {
@@ -8481,6 +8489,40 @@ function getPageLinks (link) {
84818489
}
84828490

84838491

8492+
/***/ }),
8493+
8494+
/***/ 583:
8495+
/***/ (function(__unusedmodule, exports, __webpack_require__) {
8496+
8497+
"use strict";
8498+
8499+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
8500+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
8501+
return new (P || (P = Promise))(function (resolve, reject) {
8502+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
8503+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8504+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8505+
step((generator = generator.apply(thisArg, _arguments || [])).next());
8506+
});
8507+
};
8508+
Object.defineProperty(exports, "__esModule", { value: true });
8509+
exports.deleteDeployment = void 0;
8510+
const github_1 = __webpack_require__(469);
8511+
function deleteDeployment(client, deploymentId) {
8512+
return __awaiter(this, void 0, void 0, function* () {
8513+
// invalidate deployment first
8514+
// since we can't delete active deployment
8515+
console.log(`invalidate deployment: ${deploymentId}`);
8516+
const status = yield client.repos.createDeploymentStatus(Object.assign(Object.assign({}, github_1.context.repo), { deployment_id: deploymentId, state: 'failure' }));
8517+
// then delete it
8518+
const deploymentUrl = status.data.deployment_url;
8519+
console.log(`delete deployment: ${deploymentUrl}`);
8520+
yield client.request(deploymentUrl, { method: 'DELETE' });
8521+
});
8522+
}
8523+
exports.deleteDeployment = deleteDeployment;
8524+
8525+
84848526
/***/ }),
84858527

84868528
/***/ 605:

src/delete.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { context, GitHub } from '@actions/github'
2+
3+
export async function deleteDeployment (
4+
client: GitHub,
5+
deploymentId: number
6+
): Promise<void> {
7+
// invalidate deployment first
8+
// since we can't delete active deployment
9+
console.log(`invalidate deployment: ${deploymentId}`)
10+
const status = await client.repos.createDeploymentStatus({
11+
...context.repo,
12+
deployment_id: deploymentId,
13+
state: 'failure'
14+
})
15+
16+
// then delete it
17+
const deploymentUrl = status.data.deployment_url
18+
console.log(`delete deployment: ${deploymentUrl}`)
19+
await client.request(deploymentUrl, { method: 'DELETE' })
20+
}

src/main.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import * as github from '@actions/github'
33
import { create } from './create'
44
import { finish } from './finish'
55
import { deleteAll } from './delete-all'
6+
import { deleteDeployment } from './delete'
7+
68
import { DeploymentStatus } from './deployment-status'
79

810
type ActionType = 'create' | 'delete' | 'delete-all' | 'finish'
@@ -99,6 +101,15 @@ export async function run (): Promise<void> {
99101
}
100102
break
101103
case 'delete':
104+
try {
105+
await deleteDeployment(
106+
client,
107+
Number(deploymentId)
108+
)
109+
} catch (error) {
110+
core.error(error)
111+
throw error
112+
}
102113
break
103114
case 'delete-all':
104115
try {

tests/main.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,53 @@ describe('delete-all', () => {
163163
deleteDeployment.done()
164164
})
165165
})
166+
167+
describe('delete-all', () => {
168+
beforeEach(() => {
169+
process.env['GITHUB_REPOSITORY'] = 'owner/repo'
170+
171+
let inputs = {} as any
172+
let inputSpy: jest.SpyInstance;
173+
174+
// @actions/core
175+
inputs = {
176+
'token': 'fake-token',
177+
'type': 'delete',
178+
'deployment_id': '42'
179+
}
180+
inputSpy = jest.spyOn(core, 'getInput');
181+
inputSpy.mockImplementation(name => inputs[name]);
182+
183+
// @actions/github
184+
Object.defineProperty(github.context, 'actor', { get: () => 'fake-actor' })
185+
Object.defineProperty(github.context, 'ref', { get: () => 'refs/heads/master' })
186+
})
187+
188+
afterEach(() => {
189+
jest.resetAllMocks();
190+
jest.clearAllMocks();
191+
})
192+
193+
it('200', async () => {
194+
// arrange
195+
const postStatus = nock('https://api.github.com')
196+
.post('/repos/owner/repo/deployments/42/statuses')
197+
.reply(200, { deployment_url: 'https://api.github.com/repos/owner/repo/deployments/42' })
198+
199+
const deleteDeployment = nock('https://api.github.com')
200+
.delete('/repos/owner/repo/deployments/42')
201+
.reply(200)
202+
203+
// act
204+
try {
205+
await main.run()
206+
} catch (error) {
207+
console.error(JSON.stringify(error.toString(), null, 2))
208+
console.error(JSON.stringify(error.stack, null, 2))
209+
}
210+
211+
// assert
212+
postStatus.done()
213+
deleteDeployment.done()
214+
})
215+
})

0 commit comments

Comments
 (0)