Skip to content

Commit 0feb266

Browse files
Dangerfile: Fixed how changed files are determined (#2757)
1 parent cf354ef commit 0feb266

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

dangerfile.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ function readPRFile(path) {
2424
return git.show([`pr:${path}`]);
2525
}
2626

27+
/**
28+
* Returns the relative paths of all files changed in the PR.
29+
*
30+
* @returns {Promise<string[]>}
31+
*/
32+
const getChangedFiles = async () => {
33+
// Determine the merge base between master and the PR branch.
34+
// If files changed in master since PR was branched they would show in the diff otherwise.
35+
// https://stackoverflow.com/questions/25071579/list-all-files-changed-in-a-pull-request-in-git-github
36+
const mergeBase = await git.raw(['merge-base', 'pr', 'HEAD']);
37+
const result = await git.diff(['--name-only', '--no-renames', 'pr', mergeBase]);
38+
return (result || '').split(/\r?\n/g);
39+
};
40+
41+
const getChangedMinifiedFiles = async () => {
42+
const changed = await getChangedFiles();
43+
return changed.filter(file => file.endsWith('.min.js'));
44+
};
45+
2746
// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
2847
const formatBytes = (bytes, decimals = 2) => {
2948
if (bytes === 0) return '0 Bytes';
@@ -64,11 +83,6 @@ const getSummary = (rows, totalMasterFileSize, totalFileSize) => {
6483
return `A total of ${numFiles} file${maybeS} have changed, with a combined diff of ${byteDiff} (${percentDiff}).`;
6584
}
6685

67-
const getChangedMinifiedFiles = async () => {
68-
const result = await git.diff(['--name-only', '--no-renames', 'pr', 'HEAD']);
69-
return (result || '').split(/\r?\n/g).filter(file => file.endsWith('.min.js'));
70-
};
71-
7286
const run = async () => {
7387
const minified = await getChangedMinifiedFiles();
7488

0 commit comments

Comments
 (0)