1- const mockTriggerWorkflowDispatch = jest . fn ( async ( _context , _token , owner , repo , workflow_id , ref , inputs ) => {
2- expect ( `${ owner } /${ repo } ` ) . toEqual ( 'gitgitgadget-workflows/gitgitgadget-workflows' )
3- expect ( workflow_id ) . toEqual ( 'sync-ref.yml' )
4- expect ( ref ) . toEqual ( 'main' )
5- expect ( inputs ) . toEqual ( { ref : 'refs/heads/next' } )
6- return { html_url : '<the URL to the workflow run>' }
1+ const mockTriggerWorkflowDispatch = jest . fn ( async ( _context , _token , _owner , _repo , workflow_id , ref , inputs ) => {
2+ return { html_url : `<the URL to the workflow ${ workflow_id } run on ${ ref } with inputs ${ JSON . stringify ( inputs ) } >` }
73} )
84jest . mock ( '../GitGitGadget/trigger-workflow-dispatch' , ( ) => ( {
9- triggerWorkflowDispatch : mockTriggerWorkflowDispatch
5+ triggerWorkflowDispatch : mockTriggerWorkflowDispatch ,
6+ listWorkflowRuns : jest . fn ( async ( _context , _token , _owner , _repo , workflow_id , branch , status ) => {
7+ if ( workflow_id === 'update-prs.yml' && branch === 'main' && status === 'queued' ) {
8+ // pretend that `update-prs` is clogged up, for whatever reason
9+ return [
10+ { id : 1 , head_branch : 'main' , status : 'queued' , html_url : '<the URL to the workflow run>' } ,
11+ { id : 2 , head_branch : 'main' , status : 'queued' , html_url : '<another URL to the workflow run>' }
12+ ]
13+ }
14+ return [ ]
15+ } )
1016} ) )
1117
1218const index = require ( '../GitGitGadget/index' )
@@ -147,17 +153,10 @@ const testIssueComment = (comment, repoOwner, fn) => {
147153testIssueComment ( '/test' , async ( context ) => {
148154 expect ( context . done ) . toHaveBeenCalledTimes ( 1 )
149155 expect ( context . res ) . toEqual ( {
150- body : 'Okay!'
151- } )
152- expect ( mockRequest . write ) . toHaveBeenCalledTimes ( 1 )
153- expect ( JSON . parse ( mockRequest . write . mock . calls [ 0 ] [ 0 ] ) ) . toEqual ( {
154- definition : {
155- id : 3
156- } ,
157- sourceBranch : 'refs/pull/1886743660/head' ,
158- parameters : '{"pr.comment.id":27988538471837300}'
156+ body : 'Okay, triggered <the URL to the workflow handle-pr-comment.yml run on main with inputs {"pr-comment-url":"https://github.com/gitgitgadget/git/pull/1886743660"}>!'
159157 } )
160- expect ( mockRequest . end ) . toHaveBeenCalledTimes ( 1 )
158+ expect ( mockRequest . write ) . not . toHaveBeenCalled ( )
159+ expect ( mockRequest . end ) . not . toHaveBeenCalled ( )
161160} )
162161
163162testIssueComment ( '/verify-repository' , 'nope' , ( context ) => {
@@ -197,7 +196,7 @@ testWebhookPayload('react to `next` being pushed to git/git', 'push', {
197196 }
198197} , ( context ) => {
199198 expect ( context . res ) . toEqual ( {
200- body : 'push(refs/heads/next): triggered <the URL to the workflow run>'
199+ body : 'push(refs/heads/next): triggered <the URL to the workflow sync-ref.yml run on main with inputs {"ref":"refs/heads/next"} >'
201200 } )
202201 expect ( mockTriggerWorkflowDispatch ) . toHaveBeenCalledTimes ( 1 )
203202 expect ( mockTriggerWorkflowDispatch . mock . calls [ 0 ] ) . toEqual ( [
@@ -210,4 +209,100 @@ testWebhookPayload('react to `next` being pushed to git/git', 'push', {
210209 ref : 'refs/heads/next'
211210 }
212211 ] )
212+ } )
213+
214+ testWebhookPayload ( 'react to `seen` being pushed to git/git' , 'push' , {
215+ ref : 'refs/heads/seen' ,
216+ repository : {
217+ full_name : 'git/git' ,
218+ owner : {
219+ login : 'git'
220+ }
221+ }
222+ } , ( context ) => {
223+ expect ( context . res ) . toEqual ( {
224+ body : [
225+ 'push(refs/heads/seen):' ,
226+ 'triggered <the URL to the workflow sync-ref.yml run on main with inputs {"ref":"refs/heads/seen"}>' ,
227+ 'and <the URL to the workflow update-mail-to-commit-notes.yml run on main with inputs undefined>'
228+ ] . join ( ' ' )
229+ } )
230+ // we expect `update-prs` _not_ to be triggered here because we pretend that it is already queued
231+ expect ( mockTriggerWorkflowDispatch ) . toHaveBeenCalledTimes ( 2 )
232+ expect ( mockTriggerWorkflowDispatch . mock . calls [ 0 ] ) . toEqual ( [
233+ context ,
234+ undefined ,
235+ 'gitgitgadget-workflows' ,
236+ 'gitgitgadget-workflows' ,
237+ 'sync-ref.yml' ,
238+ 'main' , {
239+ ref : 'refs/heads/seen'
240+ }
241+ ] )
242+ expect ( mockTriggerWorkflowDispatch . mock . calls [ 1 ] ) . toEqual ( [
243+ context ,
244+ undefined ,
245+ 'gitgitgadget-workflows' ,
246+ 'gitgitgadget-workflows' ,
247+ 'update-mail-to-commit-notes.yml' ,
248+ 'main' ,
249+ undefined
250+ ] )
251+ } )
252+
253+ testWebhookPayload ( 'react to `lore-1` being pushed to https://github.com/gitgitgadget/git-mailing-list-mirror' , 'push' , {
254+ ref : 'refs/heads/lore-1' ,
255+ repository : {
256+ full_name : 'gitgitgadget/git-mailing-list-mirror' ,
257+ owner : {
258+ login : 'gitgitgadget'
259+ }
260+ }
261+ } , ( context ) => {
262+ expect ( context . res ) . toEqual ( {
263+ body : [
264+ 'push(refs/heads/lore-1 in gitgitgadget/git-mailing-list-mirror):' ,
265+ 'triggered <the URL to the workflow handle-new-mails.yml run on main with inputs undefined>'
266+ ] . join ( ' ' )
267+ } )
268+ expect ( mockTriggerWorkflowDispatch ) . toHaveBeenCalledTimes ( 1 )
269+ expect ( mockTriggerWorkflowDispatch . mock . calls [ 0 ] ) . toEqual ( [
270+ context ,
271+ undefined ,
272+ 'gitgitgadget-workflows' ,
273+ 'gitgitgadget-workflows' ,
274+ 'handle-new-mails.yml' ,
275+ 'main' ,
276+ undefined
277+ ] )
278+ } )
279+ testWebhookPayload ( 'react to PR push' , 'pull_request' , {
280+ action : 'synchronize' ,
281+ pull_request : {
282+ html_url : 'https://github.com/gitgitgadget/git/pull/1956' ,
283+ } ,
284+ repository : {
285+ full_name : 'gitgitgadget/git' ,
286+ owner : {
287+ login : 'gitgitgadget'
288+ }
289+ }
290+ } , ( context ) => {
291+ expect ( context . res ) . toEqual ( {
292+ body : [
293+ 'Okay, triggered <the URL to the workflow handle-pr-push.yml run on main with inputs' ,
294+ '{"pr-url":"https://github.com/gitgitgadget/git/pull/1956"}>!'
295+ ] . join ( ' ' )
296+ } )
297+ expect ( mockTriggerWorkflowDispatch ) . toHaveBeenCalledTimes ( 1 )
298+ expect ( mockTriggerWorkflowDispatch . mock . calls [ 0 ] ) . toEqual ( [
299+ context ,
300+ undefined ,
301+ 'gitgitgadget-workflows' ,
302+ 'gitgitgadget-workflows' ,
303+ 'handle-pr-push.yml' ,
304+ 'main' , {
305+ 'pr-url' : 'https://github.com/gitgitgadget/git/pull/1956' ,
306+ }
307+ ] )
213308} )
0 commit comments