Fixed Danger CI for forks (#2638)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
diff --git a/.github/workflows/danger.yml b/.github/workflows/danger.yml
index d9b1d97..eda9686 100644
--- a/.github/workflows/danger.yml
+++ b/.github/workflows/danger.yml
@@ -1,8 +1,7 @@
name: Danger
-on:
- pull_request:
- branches: [ master ]
+on:
+ pull_request_target:
jobs:
run:
@@ -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
diff --git a/dangerfile.js b/dangerfile.js
index 0a6a777..02d8df8 100644
--- a/dangerfile.js
+++ b/dangerfile.js
@@ -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';
@@ -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) => {
@@ -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) {
@@ -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([
@@ -83,7 +93,7 @@ const run = async () => {
]);
totalFileSize += fileSize;
- totalMasterFileSize +=fileMasterSize
+ totalMasterFileSize += fileMasterSize
rows.push([
file,