Skip to content

Commit a4d7696

Browse files
committed
fix: set failed action on errors
1 parent 9a73cff commit a4d7696

File tree

3 files changed

+69
-49
lines changed

3 files changed

+69
-49
lines changed

dist/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3594,6 +3594,7 @@ function run() {
35943594
}
35953595
catch (error) {
35963596
core.error(error);
3597+
core.setFailed(`Create deployment failed: ${JSON.stringify(error, null, 2)}`);
35973598
throw error;
35983599
}
35993600
break;
@@ -3603,6 +3604,7 @@ function run() {
36033604
}
36043605
catch (error) {
36053606
core.error(error);
3607+
core.setFailed(`Finish deployment failed: ${JSON.stringify(error, null, 2)}`);
36063608
throw error;
36073609
}
36083610
break;
@@ -3612,6 +3614,7 @@ function run() {
36123614
}
36133615
catch (error) {
36143616
core.error(error);
3617+
core.setFailed(`Delete deployment failed: ${JSON.stringify(error, null, 2)}`);
36153618
throw error;
36163619
}
36173620
break;
@@ -3621,6 +3624,7 @@ function run() {
36213624
}
36223625
catch (error) {
36233626
core.error(error);
3627+
core.setFailed(`Delete all deployments failed: ${JSON.stringify(error, null, 2)}`);
36243628
throw error;
36253629
}
36263630
break;

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export async function run (): Promise<void> {
8888
core.setOutput('deployment_id', deploymentId)
8989
} catch (error) {
9090
core.error(error)
91+
core.setFailed(`Create deployment failed: ${JSON.stringify(error, null, 2)}`)
9192
throw error
9293
}
9394
break
@@ -102,6 +103,7 @@ export async function run (): Promise<void> {
102103
)
103104
} catch (error) {
104105
core.error(error)
106+
core.setFailed(`Finish deployment failed: ${JSON.stringify(error, null, 2)}`)
105107
throw error
106108
}
107109
break
@@ -113,6 +115,7 @@ export async function run (): Promise<void> {
113115
)
114116
} catch (error) {
115117
core.error(error)
118+
core.setFailed(`Delete deployment failed: ${JSON.stringify(error, null, 2)}`)
116119
throw error
117120
}
118121
break
@@ -124,6 +127,7 @@ export async function run (): Promise<void> {
124127
)
125128
} catch (error) {
126129
core.error(error)
130+
core.setFailed(`Delete all deployments failed: ${JSON.stringify(error, null, 2)}`)
127131
throw error
128132
}
129133
break

tests/main.test.ts

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,30 @@ const postDeploymentReply = { id: 42 } as any
1212
const postStatusReply = {} as any
1313

1414
describe('create', () => {
15-
beforeEach(() => {
16-
process.env['GITHUB_REPOSITORY'] = 'owner/repo'
1715

18-
let inputs = {} as any
19-
let inputSpy: jest.SpyInstance;
2016

21-
// @actions/core
22-
inputs = {
23-
'token': 'fake-token',
24-
'type': 'create',
25-
}
26-
inputSpy = jest.spyOn(core, 'getInput');
27-
inputSpy.mockImplementation(name => inputs[name]);
17+
beforeEach(() => {
18+
process.env['GITHUB_REPOSITORY'] = 'owner/repo'
2819

2920
// @actions/github
3021
Object.defineProperty(github.context, 'actor', { get: () => 'fake-actor' })
3122
Object.defineProperty(github.context, 'ref', { get: () => 'refs/heads/master' })
3223
})
3324

3425
afterEach(() => {
35-
jest.resetAllMocks();
36-
jest.clearAllMocks();
26+
jest.resetAllMocks()
27+
jest.clearAllMocks()
3728
})
3829

3930
it('200', async () => {
4031
// arrange
32+
const inputs = {
33+
'token': 'fake-token',
34+
'type': 'create',
35+
} as any
36+
const inputSpy = jest.spyOn(core, 'getInput')
37+
inputSpy.mockImplementation(name => inputs[name])
38+
4139
const getListDeployments = nock('https://api.github.com')
4240
.get('/repos/owner/repo/deployments?ref=refs%2Fheads%2Fmaster&environment=master')
4341
.reply(200, listDeploymentsReply)
@@ -50,18 +48,47 @@ describe('create', () => {
5048
.post('/repos/owner/repo/deployments/42/statuses')
5149
.reply(200, postStatusReply)
5250

51+
// act
52+
await main.run()
53+
54+
// assert
55+
getListDeployments.done()
56+
postDeployment.done()
57+
postStatus.done()
58+
})
59+
60+
it('400 when environment_url has no https:// prefix', async () => {
61+
// arrange
62+
const inputs = {
63+
'token': 'fake-token',
64+
'type': 'create',
65+
'environment_url': 'test.app'
66+
} as any
67+
const inputSpy = jest.spyOn(core, 'getInput')
68+
inputSpy.mockImplementation(name => inputs[name])
69+
70+
const setFailedSpy = jest.spyOn(core, 'setFailed')
71+
72+
const getListDeployments = nock('https://api.github.com')
73+
.get('/repos/owner/repo/deployments?ref=refs%2Fheads%2Fmaster&environment=master')
74+
.reply(200, listDeploymentsReply)
75+
76+
const postDeployment = nock('https://api.github.com')
77+
.post('/repos/owner/repo/deployments')
78+
.reply(400, {"resource":"DeploymentStatus","code":"custom","field":"environment_url","message":"environment_url must use http(s) scheme"})
79+
5380
// act
5481
try {
5582
await main.run()
83+
expect('this should not be reached').toEqual('')
5684
} catch (error) {
57-
console.error(JSON.stringify(error.toString(), null, 2))
58-
console.error(JSON.stringify(error.stack, null, 2))
85+
expect(error.message).toEqual("{\"resource\":\"DeploymentStatus\",\"code\":\"custom\",\"field\":\"environment_url\",\"message\":\"environment_url must use http(s) scheme\"}")
5986
}
6087

6188
// assert
6289
getListDeployments.done()
6390
postDeployment.done()
64-
postStatus.done()
91+
expect(setFailedSpy.mock.calls).toHaveLength(1)
6592
})
6693
})
6794

@@ -70,24 +97,24 @@ describe('finish', () => {
7097
process.env['GITHUB_REPOSITORY'] = 'owner/repo'
7198

7299
let inputs = {} as any
73-
let inputSpy: jest.SpyInstance;
100+
let inputSpy: jest.SpyInstance
74101

75102
// @actions/core
76103
inputs = {
77104
'token': 'fake-token',
78105
'type': 'finish',
79106
}
80-
inputSpy = jest.spyOn(core, 'getInput');
81-
inputSpy.mockImplementation(name => inputs[name]);
107+
inputSpy = jest.spyOn(core, 'getInput')
108+
inputSpy.mockImplementation(name => inputs[name])
82109

83110
// @actions/github
84111
Object.defineProperty(github.context, 'actor', { get: () => 'fake-actor' })
85112
Object.defineProperty(github.context, 'ref', { get: () => 'refs/heads/master' })
86113
})
87114

88115
afterEach(() => {
89-
jest.resetAllMocks();
90-
jest.clearAllMocks();
116+
jest.resetAllMocks()
117+
jest.clearAllMocks()
91118
})
92119

93120
it('200', async () => {
@@ -97,12 +124,7 @@ describe('finish', () => {
97124
.reply(200, postStatusReply)
98125

99126
// act
100-
try {
101-
await main.run()
102-
} catch (error) {
103-
console.error(JSON.stringify(error.toString(), null, 2))
104-
console.error(JSON.stringify(error.stack, null, 2))
105-
}
127+
await main.run()
106128

107129
// assert
108130
postDeploymentStatus.done()
@@ -114,25 +136,25 @@ describe('delete-all', () => {
114136
process.env['GITHUB_REPOSITORY'] = 'owner/repo'
115137

116138
let inputs = {} as any
117-
let inputSpy: jest.SpyInstance;
139+
let inputSpy: jest.SpyInstance
118140

119141
// @actions/core
120142
inputs = {
121143
'token': 'fake-token',
122144
'type': 'delete-all',
123145
'environment': 'staging'
124146
}
125-
inputSpy = jest.spyOn(core, 'getInput');
126-
inputSpy.mockImplementation(name => inputs[name]);
147+
inputSpy = jest.spyOn(core, 'getInput')
148+
inputSpy.mockImplementation(name => inputs[name])
127149

128150
// @actions/github
129151
Object.defineProperty(github.context, 'actor', { get: () => 'fake-actor' })
130152
Object.defineProperty(github.context, 'ref', { get: () => 'refs/heads/master' })
131153
})
132154

133155
afterEach(() => {
134-
jest.resetAllMocks();
135-
jest.clearAllMocks();
156+
jest.resetAllMocks()
157+
jest.clearAllMocks()
136158
})
137159

138160
it('200', async () => {
@@ -150,12 +172,7 @@ describe('delete-all', () => {
150172
.reply(200)
151173

152174
// 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-
}
175+
await main.run()
159176

160177
// assert
161178
getListDeployments.done()
@@ -169,25 +186,25 @@ describe('delete', () => {
169186
process.env['GITHUB_REPOSITORY'] = 'owner/repo'
170187

171188
let inputs = {} as any
172-
let inputSpy: jest.SpyInstance;
189+
let inputSpy: jest.SpyInstance
173190

174191
// @actions/core
175192
inputs = {
176193
'token': 'fake-token',
177194
'type': 'delete',
178195
'deployment_id': '42'
179196
}
180-
inputSpy = jest.spyOn(core, 'getInput');
181-
inputSpy.mockImplementation(name => inputs[name]);
197+
inputSpy = jest.spyOn(core, 'getInput')
198+
inputSpy.mockImplementation(name => inputs[name])
182199

183200
// @actions/github
184201
Object.defineProperty(github.context, 'actor', { get: () => 'fake-actor' })
185202
Object.defineProperty(github.context, 'ref', { get: () => 'refs/heads/master' })
186203
})
187204

188205
afterEach(() => {
189-
jest.resetAllMocks();
190-
jest.clearAllMocks();
206+
jest.resetAllMocks()
207+
jest.clearAllMocks()
191208
})
192209

193210
it('200', async () => {
@@ -201,12 +218,7 @@ describe('delete', () => {
201218
.reply(200)
202219

203220
// 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-
}
221+
await main.run()
210222

211223
// assert
212224
postStatus.done()

0 commit comments

Comments
 (0)