From 0feb266f4695810317b4f0fbafc810623df07023 Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Fri, 12 Feb 2021 15:08:57 +0100 Subject: [PATCH] Dangerfile: Fixed how changed files are determined (#2757) --- dangerfile.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/dangerfile.js b/dangerfile.js index 02d8df8156..23e10750da 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} + */ +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();