Skip to content

Commit

Permalink
Add capability to register webhooks if permissions allow, try caching…
Browse files Browse the repository at this point in the history
… build folder
  • Loading branch information
Ludwig Schubert committed Jan 10, 2018
1 parent 53f68b1 commit 705e541
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_js:
cache:
directories:
- node_modules
- build
script:
- npm run build
deploy:
Expand Down
46 changes: 36 additions & 10 deletions src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ const buildDir = './build';
const render = './node_modules/.bin/distill-render';
const concurrency = 10;


function registerWebhooks(repo) {
const hookURL = 'https://us-central1-distill-wrp.cloudfunctions.net/githubDraftsWebhook';
const repository = githubClient.getRepo(repo.owner.login, repo.name);
return repository.listHooks()
.then(({data: hooks}) => {
const alreadyHasHook = hooks.some((hook) => hook.config.url === hookURL);
if (alreadyHasHook) {
console.log(`${repo.name} already has hook installed, skipping.`);
} else {
const hookOptions = {
name: 'web',
events: [ 'push' ],
config: {
url: hookURL,
content_type: 'json',
secret: process.env.GITHUB_HOOK_SECRET
}
};
return repository.createHook(hookOptions)
.then(() => console.log(`Successfully registered hook for ${repo.name}.`));
}
})
.catch(() => {
console.log(`Drafts has no admin/hooks access to ${repo.name}, skipping hooks.`);
})
}

function getRepoFromURL(name, authorizedURL, targetDir) {
console.log('Cloning ' + name + ' ...');
const repoFolder = `${targetDir}/${name}`;
Expand All @@ -26,8 +54,10 @@ function getRepoFromURL(name, authorizedURL, targetDir) {
.then(exists => {
let command;
if (exists) {
return exec(`git -C ${repoFolder} pull && git -C ${repoFolder} clean -xdf`);
console.log(`${repoFolder} already exists, cleaning & pulling.`)
return exec(`git -C ${repoFolder} clean -xdf && git -C ${repoFolder} pull`);
} else {
console.log(`${repoFolder} is new, cloning repo.`)
return exec(`git clone --depth 1 ${authorizedURL} ${repoFolder}`);
}
})
Expand Down Expand Up @@ -94,22 +124,18 @@ const user = githubClient.getUser();
// -all names:
// we can't filter by 'post--' prefix as drafts may not follow naming scheme yet.
const options = {
visibility: 'private',
affiliation: 'collaborator'
};

user.listRepos(options)
.then(({data: reposJson}) => {
const repos = reposJson.map(repo => [repo.name, repo.clone_url]);
const promises = [];
for (const [name, url] of repos) {
.then(({data: repos}) => {
return Promise.all(repos.map(repo => {
const [name, url] = [repo.name, repo.clone_url];
const https = url.substring(0, 8);
const auth = process.env.GITHUB_TOKEN + ':x-oauth-basic@';
const authorizedURL = https + auth + url.substring(8, url.length);
const promise = getRepoFromURL(name, authorizedURL, buildDir);
promises.push(promise);
}
return Promise.all(promises);
return registerWebhooks(repo).then(() => getRepoFromURL(name, authorizedURL, buildDir));
}));
})
.then(() => {
console.log("...all done!");
Expand Down

0 comments on commit 705e541

Please sign in to comment.