Skip to content

Commit 8580d33

Browse files
committed
Trigger update-prs and update-mail-to-commit-notes on seen updates
This is part of my ongoing effort to replace GitGitGadget's Azure Pipelines by GitHub workflows. Currently, the Azure Pipelines have the upstream Git repository as "source" (even if they don't check that out), i.e. Azure Pipelines poll for changes. In GitHub workflows, such a thing is no longer possible: There is no way to trigger a GitHub workflow on updates in _another_ repository than the workflow definition resides (a rather vexing limitation of GitHub Actions' architecture, to be sure). In this instance, we can avoid resorting to the ugly, ugly, yet all-too-common workaround of polling (which we do in `sync-git-mailing-list-mirror`) but can at least react to the respective webhook event (at least we _can_, what with the `gitgitgadget-git` app being installed on `git/git`). Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 31edbe5 commit 8580d33

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

GitGitGadget/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const { validateGitHubWebHook } = require('./validate-github-webhook');
1212

1313
const { triggerAzurePipeline } = require('./trigger-azure-pipeline');
1414

15-
const { triggerWorkflowDispatch } = require('./trigger-workflow-dispatch')
15+
const { triggerWorkflowDispatch, listWorkflowRuns } = require('./trigger-workflow-dispatch')
1616

1717
module.exports = async (context, req) => {
1818
try {
@@ -69,7 +69,16 @@ module.exports = async (context, req) => {
6969
ref: req.body.ref
7070
}
7171
)
72-
context.res = { body: `push(${req.body.ref}): triggered ${run.html_url}` }
72+
const extra = []
73+
if (req.body.ref === 'refs/heads/seen') {
74+
for (const workflow of ['update-prs.yml', 'update-mail-to-commit-notes.yml']) {
75+
if ((await listWorkflowRuns(...a, workflow, 'main', 'queued')).length === 0) {
76+
const run = await triggerWorkflowDispatch(...a, workflow, 'main')
77+
extra.push(` and ${run.html_url}`)
78+
}
79+
}
80+
}
81+
context.res = { body: `push(${req.body.ref}): triggered ${run.html_url}${extra.join('')}` }
7382
}
7483
} else if (eventType === 'issue_comment') {
7584
const triggerToken = process.env['GITGITGADGET_TRIGGER_TOKEN'];

__tests__/index.test.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@ const mockTriggerWorkflowDispatch = jest.fn(async (_context, _token, _owner, _re
22
return { html_url: `<the URL to the workflow ${workflow_id} run on ${ref} with inputs ${JSON.stringify(inputs)}>` }
33
})
44
jest.mock('../GitGitGadget/trigger-workflow-dispatch', () => ({
5-
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+
})
616
}))
717

818
const index = require('../GitGitGadget/index')
@@ -206,4 +216,43 @@ testWebhookPayload('react to `next` being pushed to git/git', 'push', {
206216
ref: 'refs/heads/next'
207217
}
208218
])
219+
})
220+
221+
testWebhookPayload('react to `seen` being pushed to git/git', 'push', {
222+
ref: 'refs/heads/seen',
223+
repository: {
224+
full_name: 'git/git',
225+
owner: {
226+
login: 'git'
227+
}
228+
}
229+
}, (context) => {
230+
expect(context.res).toEqual({
231+
body: [
232+
'push(refs/heads/seen):',
233+
'triggered <the URL to the workflow sync-ref.yml run on main with inputs {"ref":"refs/heads/seen"}>',
234+
'and <the URL to the workflow update-mail-to-commit-notes.yml run on main with inputs undefined>'
235+
].join(' ')
236+
})
237+
// we expect `update-prs` _not_ to be triggered here because we pretend that it is already queued
238+
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(2)
239+
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
240+
context,
241+
undefined,
242+
'gitgitgadget-workflows',
243+
'gitgitgadget-workflows',
244+
'sync-ref.yml',
245+
'main', {
246+
ref: 'refs/heads/seen'
247+
}
248+
])
249+
expect(mockTriggerWorkflowDispatch.mock.calls[1]).toEqual([
250+
context,
251+
undefined,
252+
'gitgitgadget-workflows',
253+
'gitgitgadget-workflows',
254+
'update-mail-to-commit-notes.yml',
255+
'main',
256+
undefined
257+
])
209258
})

0 commit comments

Comments
 (0)