From 51e11ffa17f42d3b53dd71c82720aff98f2e18d9 Mon Sep 17 00:00:00 2001 From: Matthew Andrews Date: Wed, 10 Jun 2015 11:23:03 +0100 Subject: [PATCH 1/4] rebuild --- bin/next-build-tools.js | 9 +++++ tasks/rebuild.js | 90 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 tasks/rebuild.js diff --git a/bin/next-build-tools.js b/bin/next-build-tools.js index da332092..e9439a23 100755 --- a/bin/next-build-tools.js +++ b/bin/next-build-tools.js @@ -19,6 +19,7 @@ var deployHashedAssets = require('../tasks/deploy-hashed-assets'); var deployStatic = require('../tasks/deploy-static'); var run = require('../tasks/run'); var about = require('../tasks/about'); +var rebuild = require('../tasks/rebuild'); function list(val) { return val.split(','); @@ -192,6 +193,14 @@ program }).catch(exit); }); +program + .command('rebuild [apps...]') + .description('Trigger a rebuild of the latest master on Travis') .action(function(apps) { + return rebuild({ + apps: apps + }).catch(exit); + }); + program .command('*') .description('') diff --git a/tasks/rebuild.js b/tasks/rebuild.js new file mode 100644 index 00000000..cc959ae7 --- /dev/null +++ b/tasks/rebuild.js @@ -0,0 +1,90 @@ +'use strict'; + +var fetchres = require('fetchres'); +var denodeify = require('denodeify'); +var fs = require('fs'); +var readFile = denodeify(fs.readFile); + +var travisToken; + +function travisFetch(path, opts) { + opts = opts || {}; + path = 'https://api.travis-ci.org' + path; + opts.timeout = opts.timeout || 3000; + opts.headers = opts.headers || {}; + opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json'; + opts.headers.Accept = opts.headers.Accept || 'application/vnd.travis-ci.2+json'; + if (travisToken && !opts.headers.Authorization) { + opts.headers.Authorization = 'token ' + travisToken; + } + return fetch(path, opts) + .then(function(res) { + var ok = res.ok; + var status = res.status; + return res.json() + .then(function(data) { + if (ok) { + return data; + } else { + console.log("Response not OK for " + path + ", got: " + status); + console.log(data); + throw new Error(status); + } + + }); + }); +} + +module.exports = function(options) { + var apps = options.apps; + + return readFile(process.env.HOME + '/.github_token', { encoding: 'UTF8' }) + .then(function(githubToken) { + return travisFetch('/auth/github', { + method: 'POST', + body: JSON.stringify({ github_token: githubToken }) + }); + }) + .then(function(data) { + travisToken = data.access_token; + + if (apps.length === 0) { + return fetch('http://next-registry.ft.com/services') + .then(fetchres.json) + .then(function(data) { + apps = data + .filter(function(apps) { return apps.versions['1']; }) + .map(function(app) { + var repo; + Object.keys(app.versions).forEach(function(version) { + version = app.versions[version]; + if (/https?:\/\/github\.com\/Financial-Times\//.test(version.repo)) { + repo = version.repo.replace(/https?:\/\/github\.com\/Financial-Times\//, ''); + } + }); + return repo; + }) + .filter(function(repo) { return repo; }); + }); + } + }) + .then(function() { + return Promise.all(apps.map(function(app) { + console.log("Considering whether to rebuild " + app); + return travisFetch('/repos/Financial-Times/' + app + '/branches/master') + .then(function(data) { + if (data.branch.state === 'passed' || data.branch.state === 'failed') { + console.log("Triggering rebuild of last master build of " + app + " (" + data.commit.author_name + ": " + data.commit.message.replace(/\n/g, " ") + ")"); + return travisFetch('/builds/' + data.branch.id + '/restart', { + method: 'POST' + }); + } else { + console.log("Skipping rebuild of " + app + " because job already exists."); + } + }) + .catch(function() { + console.log("Skipped rebuild of " + app + " probably because Travis CI not set up for this repo"); + }); + })); + }); +}; From b5b3120eac153810b2a92f3b448b6d1bc3328293 Mon Sep 17 00:00:00 2001 From: Matthew Andrews Date: Wed, 10 Jun 2015 11:23:16 +0100 Subject: [PATCH 2/4] Update docs --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 90eb2bbe..3e820938 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ run [options] Runs the local app through the router about Creates an __about.json file for the app deploy-static [options] [otherSources...] Deploys static to [destination] on S3 (where [destination] is a full S3 URL). Requires AWS_ACCESS and AWS_SECRET env vars + rebuild [apps...] Trigger a rebuild of the latest master on Travis * Options: From e27b942f42d2c4536469f41f957a858e58f07aa5 Mon Sep 17 00:00:00 2001 From: Matthew Andrews Date: Wed, 10 Jun 2015 11:42:34 +0100 Subject: [PATCH 3/4] tidy up repo name --- tasks/rebuild.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tasks/rebuild.js b/tasks/rebuild.js index cc959ae7..27068afb 100644 --- a/tasks/rebuild.js +++ b/tasks/rebuild.js @@ -60,6 +60,7 @@ module.exports = function(options) { version = app.versions[version]; if (/https?:\/\/github\.com\/Financial-Times\//.test(version.repo)) { repo = version.repo.replace(/https?:\/\/github\.com\/Financial-Times\//, ''); + repo = repo.replace(/\/$/, ''); } }); return repo; From 477b41db456fe7655895f1e11ff3074326af694a Mon Sep 17 00:00:00 2001 From: Matthew Andrews Date: Wed, 10 Jun 2015 11:50:35 +0100 Subject: [PATCH 4/4] action --- bin/next-build-tools.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/next-build-tools.js b/bin/next-build-tools.js index e9439a23..1b5aa2fc 100755 --- a/bin/next-build-tools.js +++ b/bin/next-build-tools.js @@ -195,7 +195,8 @@ program program .command('rebuild [apps...]') - .description('Trigger a rebuild of the latest master on Travis') .action(function(apps) { + .description('Trigger a rebuild of the latest master on Travis') + .action(function(apps) { return rebuild({ apps: apps }).catch(exit);