Dangerfile: Fixed how changed files are determined (#2757)
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
diff --git a/dangerfile.js b/dangerfile.js
index 02d8df8..23e1075 100644
--- a/dangerfile.js
+++ b/dangerfile.js
@@ -24,6 +24,25 @@ function readPRFile(path) {
return git.show([`pr:${path}`]);
}
+/**
+ * Returns the relative paths of all files changed in the PR.
+ *
+ * @returns {Promise<string[]>}
+ */
+const getChangedFiles = async () => {
+ // Determine the merge base between master and the PR branch.
+ // If files changed in master since PR was branched they would show in the diff otherwise.
+ // https://stackoverflow.com/questions/25071579/list-all-files-changed-in-a-pull-request-in-git-github
+ const mergeBase = await git.raw(['merge-base', 'pr', 'HEAD']);
+ const result = await git.diff(['--name-only', '--no-renames', 'pr', mergeBase]);
+ return (result || '').split(/\r?\n/g);
+};
+
+const getChangedMinifiedFiles = async () => {
+ const changed = await getChangedFiles();
+ return changed.filter(file => file.endsWith('.min.js'));
+};
+
// 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';
@@ -64,11 +83,6 @@ const getSummary = (rows, totalMasterFileSize, totalFileSize) => {
return `A total of ${numFiles} file${maybeS} have changed, with a combined diff of ${byteDiff} (${percentDiff}).`;
}
-const getChangedMinifiedFiles = async () => {
- 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 () => {
const minified = await getChangedMinifiedFiles();