Skip to content

Commit 5ae167a

Browse files
committed
tests: also test /open pr
This one is a lot more involved than the `/hi` command. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent d155922 commit 5ae167a

1 file changed

Lines changed: 84 additions & 0 deletions

File tree

__tests__/index.test.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,63 @@ jest.mock('../GitForWindowsHelper/github-api-request-as-app', () => {
5454
return mockGitHubApiRequestAsApp
5555
})
5656

57+
const dispatchedWorkflows = []
5758
let mockGitHubApiRequest = jest.fn((_context, _token, method, requestPath, payload) => {
5859
if (method === 'POST' && requestPath.endsWith('/comments')) return {
5960
id: -124,
6061
html_url: `new-comment-url-${payload.body}`
6162
}
63+
if (method === 'GET' && requestPath.endsWith('/comments/0')) return {
64+
body: `existing comment body`
65+
}
66+
if (method === 'PATCH' && requestPath.endsWith('/comments/0')) return {
67+
id: 0,
68+
html_url: `appended-comment-body-${payload.body}`
69+
}
70+
if (method === 'POST' && requestPath.endsWith('/reactions')) return {
71+
id: `new-reaction-${payload.content}`
72+
}
73+
if (method === 'POST' && requestPath === '/graphql') {
74+
if (payload.query.startsWith('query CollaboratorPermission')) return {
75+
data: {
76+
repository:{
77+
collaborators: {
78+
edges: [{ permission: 'WRITE'}]
79+
}
80+
}
81+
}
82+
}
83+
}
84+
let match
85+
if (method === 'POST' && (match = requestPath.match(/([^/]+)\/dispatches$/))) {
86+
dispatchedWorkflows.unshift({
87+
html_url: `dispatched-workflow-${match[1]}`,
88+
path: `.github/workflows/${match[1]}`,
89+
payload
90+
})
91+
return {
92+
headers: {
93+
date: (new Date()).toISOString()
94+
}
95+
}
96+
}
97+
if (method === 'GET' && requestPath.indexOf('/actions/runs?') > 0) return {
98+
workflow_runs: dispatchedWorkflows
99+
}
100+
if (method === 'GET' && requestPath === '/user') return {
101+
login: 'cheers'
102+
}
103+
throw new Error(`Unhandled ${method}-${requestPath}-${JSON.stringify(payload)}`)
62104
})
63105
jest.mock('../GitForWindowsHelper/github-api-request', () => {
64106
return mockGitHubApiRequest
65107
})
66108

109+
afterEach(() => {
110+
jest.clearAllMocks()
111+
dispatchedWorkflows.splice(0, dispatchedWorkflows.length) // empty the array
112+
})
113+
67114
const makeContext = (body, headers) => {
68115
const rawBody = JSON.stringify(body)
69116
const sha256 = crypto.createHmac('sha256', process.env['GITHUB_WEBHOOK_SECRET']).update(rawBody).digest('hex')
@@ -154,3 +201,40 @@ testIssueComment('/hi', async (context) => {
154201
{"body": "Hi @statler and waldorf!" }
155202
])
156203
})
204+
205+
let mockGetInstallationIDForRepo = jest.fn(() => 'installation-id')
206+
jest.mock('../GitForWindowsHelper/get-installation-id-for-repo', () => {
207+
return mockGetInstallationIDForRepo
208+
})
209+
210+
let mockSearchIssues = jest.fn(() => [])
211+
jest.mock('../GitForWindowsHelper/search', () => {
212+
return {
213+
searchIssues: mockSearchIssues
214+
}
215+
})
216+
217+
testIssueComment('/open pr', {
218+
issue: {
219+
number: 4281,
220+
title: '[New gnutls version] GnuTLS 3.8.0',
221+
body: `Released a bug-fix and enhancement release on the 3.8.x branch.[GnuTLS 3.8.0](https://lists.gnupg.org/pipermail/gnutls-help/2023-February/004816.html)
222+
223+
Added the security advisory.[GNUTLS-SA-2020-07-14](security-new.html#GNUTLS-SA-2020-07-14)
224+
225+
http://www.gnutls.org/news.html#2023-02-10`
226+
}
227+
}, async (context) => {
228+
expect(await index(context, context.req)).toBeUndefined()
229+
expect(context.res).toEqual({
230+
body: `I edited the comment: appended-comment-body-existing comment body
231+
232+
The MINGW workflow run [was started](dispatched-workflow-open-pr.yml)`,
233+
headers: undefined,
234+
status: undefined
235+
})
236+
expect(mockGetInstallationAccessToken).toHaveBeenCalledTimes(1)
237+
expect(mockGitHubApiRequestAsApp).not.toHaveBeenCalled()
238+
expect(dispatchedWorkflows).toHaveLength(2)
239+
expect(dispatchedWorkflows.map(e => e.payload.inputs.package)).toEqual(['mingw-w64-gnutls', 'gnutls'])
240+
})

0 commit comments

Comments
 (0)