Skip to content

Commit 7400342

Browse files
committed
Handle push events in git/git by triggering the sync-ref workflow
We added the `sync-ref` GitHub workflow in gitgitgadget/gitgitgadget-workflows#1 specifically to let it be triggered by `push` webhook events delivered to GitGitGadget's GitHub App. And this is the commit that teaches the GitHub App that trick. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 97718cd commit 7400342

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

GitGitGadget/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const { validateGitHubWebHook } = require('./validate-github-webhook');
1212

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

15+
const { triggerWorkflowDispatch } = require('./trigger-workflow-dispatch')
16+
1517
module.exports = async (context, req) => {
1618
try {
1719
validateGitHubWebHook(context);
@@ -55,6 +57,22 @@ module.exports = async (context, req) => {
5557
context.res = {
5658
body: `Ignored event type: ${eventType}`,
5759
};
60+
} else if (eventType === 'push') {
61+
if (req.body.repository.full_name !== 'git/git') {
62+
context.res = { body: `Ignoring pushes to ${req.body.repository.full_name}` }
63+
} else {
64+
const run = await triggerWorkflowDispatch(
65+
context,
66+
undefined,
67+
'gitgitgadget',
68+
'gitgitgadget-workflows',
69+
'sync-ref.yml',
70+
'main', {
71+
ref: req.body.ref
72+
}
73+
)
74+
context.res = { body: `push(${req.body.ref}): triggered ${run.html_url}` }
75+
}
5876
} else if (eventType === 'issue_comment') {
5977
const triggerToken = process.env['GITGITGADGET_TRIGGER_TOKEN'];
6078
if (!triggerToken) {

__tests__/index.test.js

+53
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
const mockTriggerWorkflowDispatch = jest.fn(async (_context, _token, owner, repo, workflow_id, ref, inputs) => {
2+
expect(`${owner}/${repo}`).toEqual('gitgitgadget/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>'}
7+
})
8+
jest.mock('../GitGitGadget/trigger-workflow-dispatch', () => ({
9+
triggerWorkflowDispatch: mockTriggerWorkflowDispatch
10+
}))
11+
112
const index = require('../GitGitGadget/index')
213
const crypto = require('crypto')
314
const stream = require('stream')
@@ -157,4 +168,46 @@ testIssueComment('/verify-repository', 'nope', (context) => {
157168
})
158169
expect(mockRequest.write).not.toHaveBeenCalled()
159170
expect(mockRequest.end).not.toHaveBeenCalled()
171+
})
172+
173+
const testWebhookPayload = (testLabel, gitHubEvent, payload, fn) => {
174+
const context = makeContext(payload, {
175+
'x-github-event': gitHubEvent
176+
})
177+
178+
test(testLabel, async () => {
179+
try {
180+
expect(await index(context, context.req)).toBeUndefined()
181+
await fn(context)
182+
expect(context.done).toHaveBeenCalledTimes(1)
183+
} catch (e) {
184+
context.log.mock.calls.forEach(e => console.log(e[0]))
185+
throw e;
186+
}
187+
})
188+
}
189+
190+
testWebhookPayload('react to `next` being pushed to git/git', 'push', {
191+
ref: 'refs/heads/next',
192+
repository: {
193+
full_name: 'git/git',
194+
owner: {
195+
login: 'git'
196+
}
197+
}
198+
}, (context) => {
199+
expect(context.res).toEqual({
200+
body: 'push(refs/heads/next): triggered <the URL to the workflow run>'
201+
})
202+
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(1)
203+
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
204+
context,
205+
undefined,
206+
'gitgitgadget',
207+
'gitgitgadget-workflows',
208+
'sync-ref.yml',
209+
'main', {
210+
ref: 'refs/heads/next'
211+
}
212+
])
160213
})

0 commit comments

Comments
 (0)