Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #515 from Financial-Times/rebuild-command-circle2-…
Browse files Browse the repository at this point in the history
…compatibility

Make rebuild command compatible with CircleCI 2.0
  • Loading branch information
simonplend authored Aug 14, 2018
2 parents 30f0735 + e544017 commit eb84f64
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions tasks/rebuild.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fetchres = require('fetchres');
const keys = require('../lib/keys');

const DEFAULT_REGISTRY_URI = 'https://next-registry.ft.com/services.json';
const DEFAULT_REGISTRY_URI = 'https://next-registry.ft.com/v2/services.json';

const getCircleToken = () =>
process.env.CIRCLECI_REBUILD_KEY
Expand All @@ -19,7 +19,8 @@ async function circleFetch (path, opts) {

const circleToken = await getCircleToken();
const options = Object.assign(defaultOptions, opts);
const url = `https://circleci.com/api/v1${path}?circle-token=${circleToken}`;
const url = `https://circleci.com/api/v1.1/project/github/Financial-Times${path}?circle-token=${circleToken}`;

const res = await fetch(url, options);

if (res.ok) {
Expand All @@ -30,24 +31,23 @@ async function circleFetch (path, opts) {
}
}

const clearCache = (project) => circleFetch(`/project/Financial-Times/${project}/build-cache`, { method: 'DELETE' });
const clearCache = (project) => circleFetch(`/${project}/build-cache`, { method: 'DELETE' });

const rebuildMasterBuild = (project) => circleFetch(`/${project}/tree/master`, { method: 'POST' });

const rebuildMasterBuild = (project) => circleFetch(`/project/Financial-Times/${project}/tree/master`, { method: 'POST' });
const triggerMasterBuild = (project) => circleFetch(`/${project}/build`, { method: 'POST', body: JSON.stringify({ branch: 'master' }) });

const lastMasterBuild = (project) => circleFetch(`/project/Financial-Times/${project}/tree/master`);
const lastMasterBuild = (project) => circleFetch(`/${project}/tree/master`);

const getRepoName = (app) => {
const repoUrl = Object.values(app.versions).find(version => version.repo).repo;
if (/https?:\/\/github\.com\/Financial-Times\//.test(repoUrl)) {
return repoUrl
const getRepoName = ({ repository }) => {
if (/https?:\/\/github\.com\/Financial-Times\//.test(repository)) {
return repository
.replace(/https?:\/\/github\.com\/Financial-Times\//, '')
.replace(/\/$/, ''); // trim trailing "/"
}
};

const hasVersions = (app) => app.versions['1'] || app.versions['2'];

const serves = type => app => type ? app.serves && app.serves.includes(type) : true;
const serves = type => app => type ? app.types && app.types.includes(type) : true;

async function task (options) {
const apps = options.apps;
Expand All @@ -67,7 +67,6 @@ async function task (options) {
const registryData = await fetch(registry).then(fetchres.json);
appsToRebuild = registryData
.filter(serves(options.serves))
.filter(hasVersions)
.map(getRepoName)
.filter(repo => repo);
}
Expand All @@ -77,13 +76,25 @@ async function task (options) {
try {
const [lastBuild] = await lastMasterBuild(app);

if (lastBuild.status !== 'running' && lastBuild.status !== 'not_running') {
console.log(`Clearing cache and triggering rebuild of last master build of ${app} ( ${lastBuild.committer_name}: ${lastBuild.subject ? lastBuild.subject.replace(/\n/g, ' ') : 'No subject'})`); // eslint-disable-line no-console
await clearCache(app);
await rebuildMasterBuild(app);
} else {
console.log(`Skipping rebuild of ${app} because job already exists.`); // eslint-disable-line no-console
switch (lastBuild.platform) {
case '1.0':
if (lastBuild.status !== 'running' && lastBuild.status !== 'not_running') {
console.log(`Clearing cache and triggering rebuild of last master build of ${app} (${lastBuild.committer_name}: ${lastBuild.subject ? lastBuild.subject.replace(/\n/g, ' ') : 'No subject'})`); // eslint-disable-line no-console
await clearCache(app);
await rebuildMasterBuild(app);
} else {
console.log(`Skipping rebuild of ${app} because job already exists.`); // eslint-disable-line no-console
}
break;
case '2.0':
console.log(`Triggering master build for ${app} (git commit: ${lastBuild.vcs_revision})`); // eslint-disable-line no-console
await triggerMasterBuild(app);
break;
default:
throw new Error(`CircleCI platform version ${lastBuild.platform} is not supported by this command`);
break;
}

} catch (error) {
console.log(`Skipped rebuild of ${app} probably because Circle CI not set up for this repo`); // eslint-disable-line no-console
}
Expand Down

0 comments on commit eb84f64

Please sign in to comment.