2019-01-15 23:41:18 +00:00
|
|
|
/* eslint-disable */
|
|
|
|
|
|
|
|
const { execFile, execFileSync } = require('child_process');
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const URL = require('url');
|
2019-01-17 00:03:36 +00:00
|
|
|
const octokit = require('@octokit/rest')();
|
2019-01-15 23:41:18 +00:00
|
|
|
|
|
|
|
const Mercury = require('../dist/mercury');
|
|
|
|
|
|
|
|
// get all fixtures
|
|
|
|
execFile('find', ['fixtures', '-type', 'f'], (err, stdout) => {
|
|
|
|
const fixtures = stdout.split('\n');
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const twoWeeks = 2 * 7 * 24 * 60 * 60 * 1000;
|
|
|
|
|
|
|
|
// iterate through fixtures for fixtures older than 2 weeks
|
|
|
|
console.log('Finding fixtures to update...');
|
2019-01-17 00:03:36 +00:00
|
|
|
const fixturesToUpdate = fixtures
|
|
|
|
.filter(fixture => {
|
|
|
|
const timestamp = path
|
|
|
|
.basename(fixture)
|
|
|
|
.split(/\.html$/)[0]
|
|
|
|
.trim();
|
|
|
|
try {
|
|
|
|
const date = new Date(parseInt(timestamp, 10));
|
|
|
|
return now - date > twoWeeks;
|
|
|
|
} catch (e) {
|
|
|
|
// if fixture isn't a timestamp, ignore it
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.slice(0, 1);
|
2019-01-15 23:41:18 +00:00
|
|
|
console.log(`${fixturesToUpdate.length} fixtures are out of date`);
|
|
|
|
|
|
|
|
// iterate through fixtures and extract their URLs.
|
|
|
|
console.log('Extracting urls...');
|
2019-01-17 00:03:36 +00:00
|
|
|
const baseDomains = fixturesToUpdate.map(fixture => fixture.split('/')[1]);
|
|
|
|
Promise.all(
|
|
|
|
fixturesToUpdate.map((fixture, i) => {
|
|
|
|
const html = fs.readFileSync(fixture);
|
2019-02-08 00:48:13 +00:00
|
|
|
return Mercury.parse(`http://${baseDomains[i]}`, { html });
|
2019-01-17 00:03:36 +00:00
|
|
|
})
|
|
|
|
).then(parsedFixture => {
|
|
|
|
const fixturesAndUrls = fixturesToUpdate.reduce(
|
|
|
|
(acc, fixture, i) =>
|
|
|
|
acc.concat({
|
|
|
|
fixture,
|
|
|
|
url: parsedFixture[i].url,
|
|
|
|
baseDomain: baseDomains[i],
|
|
|
|
}),
|
|
|
|
[]
|
|
|
|
);
|
2019-01-15 23:41:18 +00:00
|
|
|
|
|
|
|
console.log('Updating all fixtures');
|
2019-01-17 00:03:36 +00:00
|
|
|
const fns = fixturesAndUrls
|
|
|
|
.map(fixtureAndUrl => {
|
|
|
|
return () => {
|
|
|
|
// console.log('Updating fixture for', fixtureAndUrl);
|
|
|
|
return updateFixture(fixtureAndUrl);
|
|
|
|
};
|
2019-01-15 23:41:18 +00:00
|
|
|
})
|
2019-01-17 00:03:36 +00:00
|
|
|
.concat(() => {
|
|
|
|
return new Promise(res => {
|
|
|
|
console.log('changed bases', changeBase);
|
|
|
|
console.log(`otherMess`, otherMess);
|
|
|
|
res();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
promiseSerial(fns);
|
|
|
|
});
|
2019-01-15 23:41:18 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const changeBase = [];
|
|
|
|
const otherMess = [];
|
2019-01-17 00:03:36 +00:00
|
|
|
const updateFixture = ({ fixture, url, baseDomain }) => {
|
2019-01-15 23:41:18 +00:00
|
|
|
return new Promise(res => {
|
2019-01-17 00:03:36 +00:00
|
|
|
Mercury.parse(url)
|
|
|
|
.then(({ url: updatedUrl }) => {
|
|
|
|
if (!updatedUrl) {
|
|
|
|
otherMess.push({ updatedUrl, url, fixture, baseDomain });
|
|
|
|
return res();
|
|
|
|
}
|
2019-01-15 23:41:18 +00:00
|
|
|
console.log(`updatedUrl`, updatedUrl);
|
2019-01-17 00:03:36 +00:00
|
|
|
const { hostname } = URL.parse(updatedUrl);
|
|
|
|
if (hostname !== baseDomain) {
|
|
|
|
console.log('Base URL has changed!!! Do something different');
|
|
|
|
console.log(`url`, url);
|
|
|
|
console.log(`updatedUrl`, updatedUrl);
|
|
|
|
console.log(`hostname`, hostname);
|
|
|
|
changeBase.push({
|
|
|
|
fixture,
|
|
|
|
url,
|
|
|
|
baseDomain,
|
|
|
|
newBaseDomain: hostname,
|
|
|
|
updatedUrl,
|
|
|
|
});
|
|
|
|
return res();
|
|
|
|
}
|
|
|
|
execFile('yarn', ['generate-parser', url], (err, stdout) => {
|
|
|
|
// console.log(`stdout`, stdout);
|
|
|
|
const dirRe = new RegExp(`(${path.dirname(fixture)}\/\\d+\.html)`);
|
|
|
|
const newFixture = stdout.match(dirRe)[0];
|
|
|
|
|
|
|
|
console.log(`newFixture`, newFixture);
|
|
|
|
// replace old fixture with new fixture in tests
|
|
|
|
execFile(
|
|
|
|
'./scripts/find-and-replace.sh',
|
|
|
|
[fixture, newFixture, 'src/extractors/custom/**/*.test.js'],
|
|
|
|
(err, stdout) => {
|
|
|
|
// remove old fixture
|
|
|
|
fs.unlinkSync(fixture);
|
|
|
|
const { branchName, commitMessage } = doTestsPass(baseDomain)
|
|
|
|
? {
|
|
|
|
branchName: `chore-update-${baseDomain}-fixture`,
|
|
|
|
commitMessage: `chore: update ${baseDomain} fixture`,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
branchName: `fix-update-${baseDomain}-extractor`,
|
|
|
|
commitMessage: `fix: update ${baseDomain} extractor`,
|
|
|
|
};
|
|
|
|
|
|
|
|
createAndPushBranch({ branchName, commitMessage });
|
|
|
|
createPR({ branchName, title: commitMessage });
|
|
|
|
}
|
|
|
|
);
|
2019-01-15 23:41:18 +00:00
|
|
|
});
|
2019-01-17 00:03:36 +00:00
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
otherMess.push({ fixture, url, baseDomain, e });
|
2019-01-15 23:41:18 +00:00
|
|
|
});
|
|
|
|
});
|
2019-01-17 00:03:36 +00:00
|
|
|
};
|
2019-01-15 23:41:18 +00:00
|
|
|
|
2019-01-17 00:03:36 +00:00
|
|
|
const doTestsPass = site => {
|
2019-01-15 23:41:18 +00:00
|
|
|
try {
|
|
|
|
execFileSync('yarn', ['test:node', site]);
|
2019-01-17 00:03:36 +00:00
|
|
|
return true;
|
2019-01-15 23:41:18 +00:00
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-17 00:03:36 +00:00
|
|
|
};
|
2019-01-15 23:41:18 +00:00
|
|
|
|
|
|
|
const promiseSerial = funcs =>
|
2019-01-17 00:03:36 +00:00
|
|
|
funcs.reduce(
|
|
|
|
(promise, func) =>
|
|
|
|
promise.then(result => func().then(Array.prototype.concat.bind(result))),
|
|
|
|
Promise.resolve([])
|
|
|
|
);
|
2019-01-15 23:41:18 +00:00
|
|
|
|
|
|
|
const createAndPushBranch = ({ branchName, commitMessage }) => {
|
2019-01-17 00:03:36 +00:00
|
|
|
execFileSync('git', [
|
|
|
|
'config',
|
|
|
|
'user.email',
|
|
|
|
'adam.pash+postlight-bot@postlight.com',
|
|
|
|
]);
|
2019-01-15 23:41:18 +00:00
|
|
|
execFileSync('git', ['config', 'user.name', 'Postlight Bot']);
|
|
|
|
execFileSync('git', ['checkout', '-b', branchName]);
|
|
|
|
execFileSync('git', ['add', '.']);
|
|
|
|
execFileSync('git', ['commit', '-m', commitMessage]);
|
|
|
|
execFileSync('git', [
|
|
|
|
'push',
|
|
|
|
'-q',
|
2019-01-17 00:03:36 +00:00
|
|
|
`https://${
|
|
|
|
process.env.GH_AUTH_TOKEN
|
|
|
|
}@github.com/postlight/mercury-parser.git`,
|
2019-01-15 23:41:18 +00:00
|
|
|
]);
|
2019-01-17 00:03:36 +00:00
|
|
|
};
|
2019-01-15 23:41:18 +00:00
|
|
|
|
|
|
|
const createPR = ({ branchName, title, body = '' }) => {
|
|
|
|
octokit.authenticate({
|
|
|
|
type: 'token',
|
2019-01-17 00:03:36 +00:00
|
|
|
token: process.env.GH_AUTH_TOKEN,
|
|
|
|
});
|
2019-01-15 23:41:18 +00:00
|
|
|
|
|
|
|
octokit.pulls.create({
|
2019-01-17 00:03:36 +00:00
|
|
|
owner: 'postlight',
|
2019-01-15 23:41:18 +00:00
|
|
|
repo: 'mercury-parser',
|
2019-01-17 00:03:36 +00:00
|
|
|
title,
|
|
|
|
head: branchName,
|
|
|
|
base: 'master',
|
|
|
|
body,
|
|
|
|
maintainer_can_modify: true,
|
|
|
|
});
|
|
|
|
};
|