Skip to content

Commit

Permalink
Return non-zero code if CDN check failed
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenjoezhang committed Dec 6, 2023
1 parent 6683aae commit 5496232
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
13 changes: 12 additions & 1 deletion .github/workflows/commenter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,19 @@ jobs:
- name: Use Node.js
uses: actions/setup-node@v4
- run: npm install
- run: node lib/comment.js | tee -a results.txt
- run: |
node lib/comment.js --log
if [ $? -ne 0 ]; then
echo "cdn_check=fail" >> "$GITHUB_ENV"
else
echo "cdn_check=pass" >> "$GITHUB_ENV"
fi
- name: Comment PR
uses: marocchino/sticky-pull-request-comment@v2
with:
path: results.txt
- if: ${{ env.cdn_check }} == "fail"
uses: actions/github-script@v3
with:
script: |
core.setFailed('CDN check failed!')
26 changes: 17 additions & 9 deletions lib/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ const newPlugins = require('../package.json').dependencies;
const oldPlugins = JSON.parse(spawnSync('curl', ['https://raw.githubusercontent.com/next-theme/plugins/main/package.json']).stdout).dependencies;

const diff = [];
let writeBuffer = '';
Object.keys(newPlugins).forEach(key => {
if (!(key in oldPlugins) || newPlugins[key] !== oldPlugins[key]) {
diff.push(key);
}
});
if (diff.length > 0) {
console.log('The following packages are updated in the Pull Request.');
console.log(diff.map(item => '- ' + item).join('\n'));
console.log('\nThe affected CDN links are listed below.');
writeBuffer += 'The following packages are updated in the Pull Request.';
writeBuffer += diff.map(item => '- ' + item).join('\n');
writeBuffer += '\nThe affected CDN links are listed below.';
printCDNTable();
} else {
console.log('No packages are updated in the Pull Request.');
writeBuffer += 'No packages are updated in the Pull Request.';
}

function request(url) {
Expand Down Expand Up @@ -67,12 +68,13 @@ async function formatTable(name, links, groundTruth) {
}
content += `| [${key}](${url}) | ${statusCode === 200 ? '✅' : '❌'} ${statusCode} | ${integrityMatch} |\n`;
}
console.log(`
writeBuffer += `
## ${name}
| CDN Provider | Status | Integrity |
| - | - | - |
${content}`);
${content}`;
return !content.includes('❌');
}

async function printCDNTable() {
Expand All @@ -82,8 +84,14 @@ async function printCDNTable() {
const version = newPlugins[name];
const links = getVendors({ ...value, version, minified: file });
const integrity = await ssri
.fromStream(fs.createReadStream(`./node_modules/${name}/${file}`), { algorithms: ['sha256'] })
.toString();
await formatTable(key, links, integrity);
.fromStream(fs.createReadStream(`./node_modules/${name}/${file}`), { algorithms: ['sha256'] });
const fail = await formatTable(key, links, integrity.toString());
console.log(writeBuffer);
if (process.argv.includes('--log')) {
fs.writeFileSync('results.txt', writeBuffer);
}
if (fail) {
process.exit(1);
}
}
}

0 comments on commit 5496232

Please sign in to comment.