You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.6 KiB
JavaScript
88 lines
2.6 KiB
JavaScript
6 years ago
|
/* eslint-disable */
|
||
6 years ago
|
const bot = require('@jesses/circle-github-bot').default.create();
|
||
6 years ago
|
const Mercury = require('../dist/mercury.js');
|
||
6 years ago
|
const fs = require('fs');
|
||
6 years ago
|
const getTestReport = require('./get-test-report');
|
||
6 years ago
|
const execSync = require('child_process').execSync;
|
||
6 years ago
|
|
||
6 years ago
|
const run = () => {
|
||
|
const screenshotPath = process.argv[2];
|
||
6 years ago
|
|
||
6 years ago
|
const fixture = screenshotPath.split('tmp/artifacts/')[1].slice(0, -4);
|
||
6 years ago
|
|
||
|
const html = fs.readFileSync(`${fixture}`);
|
||
|
|
||
6 years ago
|
// first parse is just to get the url
|
||
6 years ago
|
Mercury.parse('http://example.com', { html, fallback: false }).then(
|
||
6 years ago
|
({ url, domain, excerpt, word_count, direction }) => {
|
||
|
// with the url, second pass will test the correct parser
|
||
6 years ago
|
Mercury.parse(url, { html, fallback: false }).then(json => {
|
||
6 years ago
|
// removing excerpt b/c this comes from content, not necessary
|
||
|
delete json.excerpt;
|
||
|
|
||
|
// adding items that aren't pulled in custom parser w/out fallback
|
||
|
Object.assign(json, { url, domain, word_count, direction });
|
||
|
|
||
|
// a quick preview of the parsed content in an html file
|
||
|
const previewHtml = `<h1>${json.title}</h1><img src=${
|
||
|
json.lead_image_url
|
||
|
} /><p>${json.author}</p>${json.content}`;
|
||
|
|
||
|
const jsonPath = `${screenshotPath}-parsed.json`;
|
||
|
const fixtureArtifactPath = `tmp/artifacts/${fixture}`;
|
||
|
const previewPath = `tmp/artifacts/${fixture}.preview.html`;
|
||
|
|
||
|
fs.writeFileSync(previewPath, previewHtml);
|
||
|
fs.writeFileSync(jsonPath, JSON.stringify(json));
|
||
|
fs.writeFileSync(fixtureArtifactPath, html);
|
||
|
|
||
6 years ago
|
const testReport =
|
||
|
getTestReport('./test-output.json') || '✅ All tests passed';
|
||
|
|
||
6 years ago
|
const comment = `### 🤖 Automated Parsing Preview 🤖
|
||
6 years ago
|
**Commit:** \`${bot.env.commitMessage}\`
|
||
|
|
||
6 years ago
|
![Screenshot of fixture (this embed should work after repo is public)](${bot.artifactUrl(
|
||
6 years ago
|
screenshotPath
|
||
|
)})
|
||
6 years ago
|
|
||
6 years ago
|
[Original Article](${url}) | ${bot.artifactLink(
|
||
6 years ago
|
fixtureArtifactPath,
|
||
|
'HTML Fixture'
|
||
|
)} | ${bot.artifactLink(previewPath, 'Parsed Content Preview')}
|
||
6 years ago
|
|
||
|
<details>
|
||
|
<summary><b>Parsed JSON</b></summary>
|
||
|
|
||
|
\`\`\`json
|
||
|
${JSON.stringify(json, null, 2)}
|
||
|
\`\`\`
|
||
|
|
||
|
</details>
|
||
|
<br />
|
||
|
|
||
|
|
||
|
**\`null\` fields**
|
||
|
|
||
6 years ago
|
${Object.keys(json)
|
||
6 years ago
|
.map(key => (json[key] !== null ? '' : ` * \`${key}\n\``))
|
||
|
.join('\n\n') || 'None'}
|
||
6 years ago
|
|
||
6 years ago
|
|
||
6 years ago
|
${testReport}
|
||
6 years ago
|
`;
|
||
|
const commentPath = 'tmp/artifacts/comment.json';
|
||
|
fs.writeFileSync(
|
||
|
commentPath,
|
||
|
JSON.stringify({
|
||
|
body: comment,
|
||
|
issue: process.env.CIRCLE_PULL_REQUEST,
|
||
|
})
|
||
6 years ago
|
);
|
||
|
});
|
||
|
}
|
||
|
);
|
||
|
};
|
||
6 years ago
|
|
||
|
run();
|