Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dangerfile: Fixed how changed files are determined #2757

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions dangerfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();

Expand Down