Skip to content

Commit 5aebcfd

Browse files
committed
Add a function to list GitHub workflow runs
This function will be used to determine whether a workflow run is already queued, to prevent concurrent runs of the upcoming GitHub workflows that replace GitGitGadget's Azure Pipelines. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 1665a87 commit 5aebcfd

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

GitGitGadget/trigger-workflow-dispatch.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,34 @@ const triggerWorkflowDispatch = async (context, token, owner, repo, workflow_id,
5656
return runs[0]
5757
}
5858

59+
const listWorkflowRuns = async (context, token, owner, repo, workflow_id, branch, status) => {
60+
if (token === undefined) {
61+
const { getInstallationIdForRepo } = require('./get-installation-id-for-repo')
62+
const installationID = await getInstallationIdForRepo(context, owner, repo)
63+
64+
const { getInstallationAccessToken } = require('./get-installation-access-token')
65+
token = await getInstallationAccessToken(context, installationID)
66+
}
67+
68+
const query = [
69+
branch && `branch=${branch}`,
70+
status && `status=${status}`,
71+
]
72+
.filter((e) => e)
73+
.map((e, i) => `${i === 0 ? '?' : '&'}${e}`)
74+
.join('')
75+
76+
const result = await gitHubAPIRequest(
77+
context,
78+
token,
79+
'GET',
80+
`/repos/${owner}/${repo}/actions/workflows/${workflow_id}/runs${query}`,
81+
)
82+
return result.workflow_runs
83+
}
84+
5985
module.exports = {
6086
triggerWorkflowDispatch,
61-
waitForWorkflowRun
87+
waitForWorkflowRun,
88+
listWorkflowRuns,
6289
}

__tests__/trigger-workflow-dispatch.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,19 @@ const mockHTTPSRequest = jest.fn(async (_context, _hostname, method, requestPath
1616
]
1717
}
1818
}
19+
if (method === 'GET' && requestPath === '/repos/hello/world/actions/workflows/the-workflow.yml/runs?branch=main&status=queued') {
20+
return {
21+
workflow_runs: [
22+
{ id: 1, head_branch: 'main', status: 'queued' },
23+
{ id: 2, head_branch: 'main', status: 'queued' },
24+
]
25+
}
26+
}
1927
throw new Error(`Unexpected requestPath: ${method} '${requestPath}'`)
2028
})
2129
jest.mock('../GitGitGadget/https-request', () => { return { httpsRequest: mockHTTPSRequest } })
2230

23-
const { triggerWorkflowDispatch } = require('../GitGitGadget/trigger-workflow-dispatch')
31+
const { triggerWorkflowDispatch, listWorkflowRuns } = require('../GitGitGadget/trigger-workflow-dispatch')
2432

2533
const { generateKeyPairSync } = require('crypto')
2634

@@ -44,4 +52,10 @@ test('trigger a workflow_dispatch event and wait for workflow run', async () =>
4452
path: '.github/workflows/the-workflow.yml',
4553
breadcrumb: true
4654
})
55+
})
56+
57+
test('list workflow runs', async () => {
58+
const context = {}
59+
const runs = await listWorkflowRuns(context, 'my-token', 'hello', 'world', 'the-workflow.yml', 'main', 'queued')
60+
expect(runs.length).toEqual(2)
4761
})

0 commit comments

Comments
 (0)