-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.test.js
249 lines (198 loc) · 7.23 KB
/
main.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const core = require('@actions/core')
const github = require('@actions/github')
const FogBugzClient = require('../src/fogbugz-client')
jest.mock('../src/fogbugz-client', () => jest.fn())
const createCase = jest.fn()
const PlanviewClient = require('../src/planview-client')
jest.mock('../src/planview-client', () => jest.fn())
const createCard = jest.fn()
const dependabotPr = require('./dependabot-pr-sample.json')
const regularPr = require('./regular-pr-sample.json')
const syncPr = require('./sync-pr-sample.json')
const notAPr = require('./not-a-pr-sample.json')
const main = require('../src/main')
const debugMock = jest.spyOn(core, 'debug').mockImplementation()
const getInputMock = jest.spyOn(core, 'getInput').mockImplementation()
const setFailedMock = jest.spyOn(core, 'setFailed').mockImplementation()
const setOutputMock = jest.spyOn(core, 'setOutput').mockImplementation()
const invalidFogBugzResponse = { success: false, error: 'hi' }
const validFogBugzResponse = { success: true, case: { ixBug: 123 } }
const invalidPlanviewResponse = { success: false, result: 'hi' }
const validPlanviewResponse = { success: true, data: { id: 42 } }
describe('action', () => {
let debugMessages = []
beforeEach(() => {
jest.clearAllMocks()
debugMessages = []
debugMock.mockImplementation(msg => {
debugMessages.push(msg)
})
getInputMock.mockImplementation(name => {
switch (name) {
case 'fogbugz_api_url':
return 'https://my.fb.com/api'
case 'fogbugz_token':
return 'myfbtoken'
case 'fogbugz_project':
return 'My Project'
case 'fogbugz_subproject':
return 'My Subproject'
case 'fogbugz_category':
return 'My Category'
case 'planview_api_url':
return 'https://my.pv.com/io'
case 'planview_auth':
return 'myplanviewauth'
case 'planview_board_id':
return 123456
case 'planview_lane_id':
return 654321
case 'planview_type_id':
return 987654
case 'users':
return 'dependabot[bot]'
default:
return ''
}
})
FogBugzClient.mockImplementation((_baseUrl, _token) => {
return { createCase }
})
PlanviewClient.mockImplementation((_baseUrl, _auth) => {
return { createCard }
})
})
it('ignores non-opened PRs', async () => {
github.context.payload = syncPr
await main.run()
expect(debugMessages.length).toBe(3)
expect(debugMessages).toContain('Running action')
expect(debugMessages).toContain(syncPr)
expect(debugMessages).toContain('pr was synchronize so not running')
})
it("ignores actions that aren't prs", async () => {
github.context.payload = notAPr
await main.run()
expect(debugMessages.length).toBe(3)
expect(debugMessages).toContain('Running action')
expect(debugMessages).toContain(notAPr)
expect(debugMessages).toContain('not a pr so not running')
})
it('ignores PRs not opened by dependabot', async () => {
github.context.payload = regularPr
await main.run()
expect(debugMessages.length).toBe(3)
expect(debugMessages).toContain('Running action')
expect(debugMessages).toContain(regularPr)
expect(debugMessages).toContain('pr was not opened by Dependabot so not running')
})
it('creates the FogBugz client', async () => {
github.context.payload = dependabotPr
await main.run()
expect(FogBugzClient).toHaveBeenCalledWith('https://my.fb.com/api', 'myfbtoken')
})
it('tries to create the FogBugz case', async () => {
github.context.payload = dependabotPr
createCase.mockReturnValue('myresult')
await main.run()
expect(createCase).toHaveBeenCalledWith(
'My Subproject - Dependabot PR title',
'My Project',
'Dependabot PR body',
'My Category'
)
expect(debugMessages).toContain('Creating FB case for My Subproject - Dependabot PR title')
expect(debugMessages).toContain('fbt_result: "myresult"')
})
it("fails if the FogBugz case isn't created", async () => {
github.context.payload = dependabotPr
createCase.mockReturnValue(invalidFogBugzResponse)
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(invalidFogBugzResponse)
expect(debugMessages).toContain(`fbt_result: ${JSON.stringify(invalidFogBugzResponse)}`)
})
it('adds the FogBugz id to the output if the FogBugz case is created', async () => {
github.context.payload = dependabotPr
createCase.mockReturnValue(validFogBugzResponse)
await main.run()
expect(FogBugzClient).toHaveBeenCalledWith('https://my.fb.com/api', 'myfbtoken')
expect(setOutputMock).toHaveBeenCalledWith('fogbugz_id', 123)
})
it('creates the Planview client if the FogBugz case is created', async () => {
github.context.payload = dependabotPr
await main.run()
expect(PlanviewClient).toHaveBeenCalledWith('https://my.pv.com/io', 'myplanviewauth')
expect(debugMessages).toContain(`Creating Planview card for 123`)
})
it('tries to create the Planview card', async () => {
github.context.payload = dependabotPr
createCard.mockReturnValue('myresult')
await main.run()
expect(createCard).toHaveBeenCalledWith(
123456,
654321,
987654,
'My Subproject - Dependabot PR title',
123,
'https://github.com/MyOrg/my_repo/pull/123'
)
expect(debugMessages).toContain('pvc_result: "myresult"')
})
it("fails if the card can't be created", async () => {
github.context.payload = dependabotPr
createCard.mockReturnValue(invalidPlanviewResponse)
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(invalidPlanviewResponse)
expect(debugMessages).toContain(`pvc_result: ${JSON.stringify(invalidPlanviewResponse)}`)
})
it('succeeds if the card can be created', async () => {
github.context.payload = dependabotPr
createCard.mockReturnValue(validPlanviewResponse)
await main.run()
expect(setOutputMock).toHaveBeenCalledWith('planview_id', 42)
})
it('handles and unecpected exception', async () => {
const error = new Error('just no')
getInputMock.mockImplementation(_name => {
throw error
})
await main.run()
expect(setFailedMock).toHaveBeenCalledWith(error)
})
it('just uses the PR title if there is no subproject', async () => {
github.context.payload = dependabotPr
getInputMock.mockImplementation(name => {
switch (name) {
case 'fogbugz_api_url':
return 'https://my.fb.com/api'
case 'fogbugz_token':
return 'myfbtoken'
case 'fogbugz_project':
return 'My Project'
case 'fogbugz_category':
return 'My Category'
case 'planview_api_url':
return 'https://my.pv.com/io'
case 'planview_auth':
return 'myplanviewauth'
case 'planview_board_id':
return 123456
case 'planview_lane_id':
return 654321
case 'planview_type_id':
return 987654
case 'users':
return 'dependabot[bot]'
default:
return ''
}
})
await main.run()
expect(createCase).toHaveBeenCalledWith(
'Dependabot PR title',
'My Project',
'Dependabot PR body',
'My Category'
)
})
})