snowballtools-base-mirror/packages/backend/src/routes/github.ts
Nabarun Gogoi c4ba59d97e
Create deployments on push events in GitHub repo (#69)
* Create repo webhook and express handler for webhook

* Create deployments from commits in GitHub

* Update isCurrent in previous production deployment

* Create script for setting authority

* Update README for initialize registry script

* Handle review changes

* Use correct repo URL in record data

* Handle github unique webhook error

* Handle async execution of publishing records

* Update readme with ngrok setup

* Review changes

* Add logs for GitHub webhooks

---------

Co-authored-by: neeraj <neeraj.rtly@gmail.com>
2024-02-15 17:24:57 +05:30

27 lines
936 B
TypeScript

import { Router } from 'express';
import debug from 'debug';
import { Service } from '../service';
const log = debug('snowball:routes-github');
const router = Router();
/* POST GitHub webhook handler */
// https://docs.github.com/en/webhooks/using-webhooks/handling-webhook-deliveries#javascript-example
router.post('/webhook', async (req, res) => {
// Server should respond with a 2XX response within 10 seconds of receiving a webhook delivery
// If server takes longer than that to respond, then GitHub terminates the connection and considers the delivery a failure
res.status(202).send('Accepted');
const service = req.app.get('service') as Service;
const githubEvent = req.headers['x-github-event'];
log(`Received GitHub webhook for event ${githubEvent}`);
if (githubEvent === 'push') {
// Create deployments using push event data
await service.handleGitHubPush(req.body);
}
});
export default router;