UNPKG

1.28 kBJavaScriptView Raw
1import R from "ramda";
2import log from "./log";
3
4
5/**
6 * Handles web-hook calls from Github.
7 */
8export default (apps, manifest) => {
9 return {
10 post(req, res) {
11 // Extract data.
12 const data = req.body;
13 const branch = (data.ref && R.last(data.ref.split("/"))) || data.repository.default_branch;
14 const repo = data.repository.full_name;
15 const modified = (data.head_commit && data.head_commit.modified) || [];
16
17 const isManifestMatch = R.any(path => path === manifest.repo.path, modified);
18 if (isManifestMatch) {
19 manifest.update()
20 .catch(err => {
21 log.error(`Failed while updating manifest.`);
22 log.error((err));
23 });
24 } else {
25 // Match apps that reside within the repo that Github posted.
26 const isRepoMatch = (app) => app.repo.name === repo && app.branch === branch;
27 const matchingApps = R.filter(isRepoMatch, apps);
28
29 // Update any matching apps.
30 if (matchingApps.length > 0) {
31 log.info(`Github webhook for '${ repo }:${ branch }' => Checking for updates: ${ matchingApps.map(item => item.id) }`);
32 matchingApps.forEach(app => app.update());
33 }
34 }
35
36 // Finish up.
37 res.status(200).send();
38 }
39 };
40};