Skip to content

Commit

Permalink
Handle push events in git/git by triggering the sync-ref workflow
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Aug 31, 2023
1 parent da326af commit 8747931
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions GitGitGadget/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const { validateGitHubWebHook } = require('./validate-github-webhook');

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

const { triggerWorkflowDispatch } = require('./trigger-workflow-dispatch')

module.exports = async (context, req) => {
try {
validateGitHubWebHook(context);
Expand Down Expand Up @@ -55,6 +57,22 @@ module.exports = async (context, req) => {
context.res = {
body: `Ignored event type: ${eventType}`,
};
} else if (eventType === 'push') {
if (req.body.repository.full_name !== 'git/git') {
context.res = { body: `Ignoring pushes to ${req.body.repository.full_name}` }
} else {
const run = await triggerWorkflowDispatch(
context,
undefined,
'gitgitgadget',
'gitgitgadget-workflows',
'sync-ref.yml',
'main', {
ref: req.body.ref
}
)
context.res = { body: `push(${req.body.ref}): triggered ${run.html_url}` }
}
} else if (eventType === 'issue_comment') {
const triggerToken = process.env['GITGITGADGET_TRIGGER_TOKEN'];
if (!triggerToken) {
Expand Down
53 changes: 53 additions & 0 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const mockTriggerWorkflowDispatch = jest.fn(async (_context, _token, owner, repo, workflow_id, ref, inputs) => {
expect(`${owner}/${repo}`).toEqual('gitgitgadget/gitgitgadget-workflows')
expect(workflow_id).toEqual('sync-ref.yml')
expect(ref).toEqual('main')
expect(inputs).toEqual({ ref: 'refs/heads/next' })
return { html_url: '<the URL to the workflow run>'}
})
jest.mock('../GitGitGadget/trigger-workflow-dispatch', () => ({
triggerWorkflowDispatch: mockTriggerWorkflowDispatch
}))

const index = require('../GitGitGadget/index')
const crypto = require('crypto')
const stream = require('stream')
Expand Down Expand Up @@ -157,4 +168,46 @@ testIssueComment('/verify-repository', 'nope', (context) => {
})
expect(mockRequest.write).not.toHaveBeenCalled()
expect(mockRequest.end).not.toHaveBeenCalled()
})

const testWebhookPayload = (testLabel, gitHubEvent, payload, fn) => {
const context = makeContext(payload, {
'x-github-event': gitHubEvent
})

test(testLabel, async () => {
try {
expect(await index(context, context.req)).toBeUndefined()
await fn(context)
expect(context.done).toHaveBeenCalledTimes(1)
} catch (e) {
context.log.mock.calls.forEach(e => console.log(e[0]))
throw e;
}
})
}

testWebhookPayload('react to `next` being pushed to git/git', 'push', {
ref: 'refs/heads/next',
repository: {
full_name: 'git/git',
owner: {
login: 'git'
}
}
}, (context) => {
expect(context.res).toEqual({
body: 'push(refs/heads/next): triggered <the URL to the workflow run>'
})
expect(mockTriggerWorkflowDispatch).toHaveBeenCalledTimes(1)
expect(mockTriggerWorkflowDispatch.mock.calls[0]).toEqual([
context,
undefined,
'gitgitgadget',
'gitgitgadget-workflows',
'sync-ref.yml',
'main', {
ref: 'refs/heads/next'
}
])
})

0 comments on commit 8747931

Please sign in to comment.