Skip to content

Commit

Permalink
Fixed Danger CI for forks (#2638)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Dec 22, 2020
1 parent 990f48f commit 7f23ef3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/danger.yml
@@ -1,8 +1,7 @@
name: Danger

on:
pull_request:
branches: [ master ]
on:
pull_request_target:

jobs:
run:
Expand All @@ -13,6 +12,11 @@ jobs:
- uses: actions/checkout@v1
with:
fetch-depth: 0
# create a new branch called pr from the remote PR branch
- run: git remote add pr_repo $PR_URL && git fetch pr_repo $PR_REF && git branch pr pr_repo/$PR_REF
env:
PR_URL: ${{github.event.pull_request.head.repo.clone_url}}
PR_REF: ${{github.event.pull_request.head.ref}}
- run: npm ci
- name: Danger
run: npx danger ci
Expand Down
46 changes: 28 additions & 18 deletions dangerfile.js
Expand Up @@ -3,6 +3,27 @@ const fs = require('fs').promises;
const gzipSize = require('gzip-size');
const git = require('simple-git/promise')(__dirname).silent(true);

/**
* Returns the contents of a text file in the base of the PR.
*
* The base is usually PrismJS/prism/master.
*
* @param {string} path
* @returns {Promise<string>}
*/
function readBaseFile(path) {
return fs.readFile(path, 'utf-8');
}
/**
* Returns the contents of a text file in the pull request branch.
*
* @param {string} path
* @returns {Promise<string>}
*/
function readPRFile(path) {
return git.show([`pr:${path}`]);
}

// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
const formatBytes = (bytes, decimals = 2) => {
if (bytes === 0) return '0 Bytes';
Expand All @@ -26,14 +47,12 @@ const absDiff = (from, to) => {
return `${maybePlus(from, to)}${formatBytes(to - from)}`;
}

const percDiff = (from, to) => {
const percDiff = (from, to) => {
if (from === to) {
return '0%';
}

return `${maybePlus(from, to)}${
(100 * (to - from) / (from || to)).toFixed(1)
}%`;
return `${maybePlus(from, to)}${(100 * (to - from) / (from || to)).toFixed(1)}%`;
}

const getSummary = (rows, totalMasterFileSize, totalFileSize) => {
Expand All @@ -46,20 +65,11 @@ const getSummary = (rows, totalMasterFileSize, totalFileSize) => {
}

const getChangedMinifiedFiles = async () => {
const result = await git.diff(['--name-only', '--no-renames', 'master...']);

return result
? result.split(/\r?\n/g).filter(file => file.endsWith('.min.js'))
: [];
const result = await git.diff(['--name-only', '--no-renames', 'pr', 'HEAD']);
return (result || '').split(/\r?\n/g).filter(file => file.endsWith('.min.js'));
};

const run = async () => {
// Check if master exists & check it out if not.
const result = await git.branch(['--list', 'master']);
if (result.all.length === 0) {
await git.branch(['master', 'origin/master']);
}

const minified = await getChangedMinifiedFiles();

if (minified.length === 0) {
Expand All @@ -73,8 +83,8 @@ const run = async () => {

for (const file of minified) {
const [fileContents, fileMasterContents] = await Promise.all([
fs.readFile(file, 'utf-8').catch(() => ''),
git.show([`master:${file}`]).catch(() => ''),
readPRFile(file).catch(() => ''),
readBaseFile(file).catch(() => ''),
]);

const [fileSize, fileMasterSize] = await Promise.all([
Expand All @@ -83,7 +93,7 @@ const run = async () => {
]);

totalFileSize += fileSize;
totalMasterFileSize +=fileMasterSize
totalMasterFileSize += fileMasterSize

rows.push([
file,
Expand Down

0 comments on commit 7f23ef3

Please sign in to comment.