Skip to content

Commit

Permalink
Fix bug where deleted files were not properly detected because the te…
Browse files Browse the repository at this point in the history
…rm used by the api is actually "removed". Replaced usage of "deleted" with "removed" while also retaining previous output using "deleted" for backwards compatability.
  • Loading branch information
lots0logs committed Jun 5, 2020
1 parent 1317d3f commit 14772bf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 51 deletions.
12 changes: 6 additions & 6 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ inputs:
required: true
outputs:
all:
description: Array of all changed files (JSON string)
description: JSON array of all changed files that weren't removed
added:
description: Array of added files (JSON string)
deleted:
description: Array of deleted files (JSON string)
description: JSON array of added files
modified:
description: Array of modified files (JSON string)
description: JSON array of modified files
renamed:
description: Array of renamed files (JSON string)
description: JSON array of renamed files
removed:
description: JSON array of removed files
branding:
icon: 'search'
color: 'gray-dark'
50 changes: 28 additions & 22 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ const repo = context.payload.repository;
const owner = repo.owner;

const FILES = new Set();
const FILES_MODIFIED = new Set();
const FILES_ADDED = new Set();
const FILES_DELETED = new Set();
const FILES_MODIFIED = new Set();
const FILES_REMOVED = new Set();
const FILES_RENAMED = new Set();

const gh = github.getOctokit(core.getInput('token'));
Expand Down Expand Up @@ -347,14 +347,14 @@ function isAdded(file) {
return 'added' === file.status;
}

function isDeleted(file) {
return 'deleted' === file.status;
}

function isModified(file) {
return 'modified' === file.status;
}

function isRemoved(file) {
return 'removed' === file.status;
}

function isRenamed(file) {
return 'renamed' === file.status;
}
Expand All @@ -378,13 +378,13 @@ async function processCommit(commit) {

if (isAdded(file)) {
FILES_ADDED.add(file.filename);
FILES_DELETED.delete(file.filename);
FILES_REMOVED.delete(file.filename);

return; // continue
}

if (isDeleted(file)) {
FILES_DELETED.add(file.filename);
if (isRemoved(file)) {
FILES_REMOVED.add(file.filename);
FILES_ADDED.delete(file.filename);
FILES_MODIFIED.delete(file.filename);

Expand All @@ -404,8 +404,10 @@ async function processCommit(commit) {
}
}

function toJSON(value) {
return JSON.stringify(value, null, 4);
function toJSON(value, pretty=true) {
return pretty
? JSON.stringify(value, null, 4)
: JSON.stringify(value);
}


Expand All @@ -424,17 +426,21 @@ getCommits().then(commits => {
Promise.all(commits.map(processCommit)).then(() => {
debug('FILES', Array.from(FILES.values()));

core.setOutput('all', toJSON(Array.from(FILES.values())));
core.setOutput('added', toJSON(Array.from(FILES_ADDED.values())));
core.setOutput('deleted', toJSON(Array.from(FILES_DELETED.values())));
core.setOutput('modified', toJSON(Array.from(FILES_MODIFIED.values())));
core.setOutput('renamed', toJSON(Array.from(FILES_RENAMED.values())));

fs.writeFileSync(`${process.env.HOME}/files.json`, toJSON(Array.from(FILES.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_modified.json`, toJSON(Array.from(FILES_MODIFIED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_added.json`, toJSON(Array.from(FILES_ADDED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_deleted.json`, toJSON(Array.from(FILES_DELETED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_renamed.json`, toJSON(Array.from(FILES_RENAMED.values())), 'utf-8');
core.setOutput('all', toJSON(Array.from(FILES.values()), 0));
core.setOutput('added', toJSON(Array.from(FILES_ADDED.values()), 0));
core.setOutput('modified', toJSON(Array.from(FILES_MODIFIED.values()), 0));
core.setOutput('removed', toJSON(Array.from(FILES_REMOVED.values()), 0));
core.setOutput('renamed', toJSON(Array.from(FILES_RENAMED.values()), 0));

fs.writeFileSync(`${process.env.HOME}/files.json`, toJSON(Array.from(FILES.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_added.json`, toJSON(Array.from(FILES_ADDED.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_modified.json`, toJSON(Array.from(FILES_MODIFIED.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_removed.json`, toJSON(Array.from(FILES_REMOVED.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_renamed.json`, toJSON(Array.from(FILES_RENAMED.values()), 0), 'utf-8');

// Backwards Compatability
core.setOutput('deleted', toJSON(Array.from(FILES_REMOVED.values()), 0));
fs.writeFileSync(`${process.env.HOME}/files_deleted.json`, toJSON(Array.from(FILES_REMOVED.values()), 0), 'utf-8');

process.exit(0);
});
Expand Down
50 changes: 28 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const repo = context.payload.repository;
const owner = repo.owner;

const FILES = new Set();
const FILES_MODIFIED = new Set();
const FILES_ADDED = new Set();
const FILES_DELETED = new Set();
const FILES_MODIFIED = new Set();
const FILES_REMOVED = new Set();
const FILES_RENAMED = new Set();

const gh = github.getOctokit(core.getInput('token'));
Expand Down Expand Up @@ -59,14 +59,14 @@ function isAdded(file) {
return 'added' === file.status;
}

function isDeleted(file) {
return 'deleted' === file.status;
}

function isModified(file) {
return 'modified' === file.status;
}

function isRemoved(file) {
return 'removed' === file.status;
}

function isRenamed(file) {
return 'renamed' === file.status;
}
Expand All @@ -90,13 +90,13 @@ async function processCommit(commit) {

if (isAdded(file)) {
FILES_ADDED.add(file.filename);
FILES_DELETED.delete(file.filename);
FILES_REMOVED.delete(file.filename);

return; // continue
}

if (isDeleted(file)) {
FILES_DELETED.add(file.filename);
if (isRemoved(file)) {
FILES_REMOVED.add(file.filename);
FILES_ADDED.delete(file.filename);
FILES_MODIFIED.delete(file.filename);

Expand All @@ -116,8 +116,10 @@ async function processCommit(commit) {
}
}

function toJSON(value) {
return JSON.stringify(value, null, 4);
function toJSON(value, pretty=true) {
return pretty
? JSON.stringify(value, null, 4)
: JSON.stringify(value);
}


Expand All @@ -136,17 +138,21 @@ getCommits().then(commits => {
Promise.all(commits.map(processCommit)).then(() => {
debug('FILES', Array.from(FILES.values()));

core.setOutput('all', toJSON(Array.from(FILES.values())));
core.setOutput('added', toJSON(Array.from(FILES_ADDED.values())));
core.setOutput('deleted', toJSON(Array.from(FILES_DELETED.values())));
core.setOutput('modified', toJSON(Array.from(FILES_MODIFIED.values())));
core.setOutput('renamed', toJSON(Array.from(FILES_RENAMED.values())));

fs.writeFileSync(`${process.env.HOME}/files.json`, toJSON(Array.from(FILES.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_modified.json`, toJSON(Array.from(FILES_MODIFIED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_added.json`, toJSON(Array.from(FILES_ADDED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_deleted.json`, toJSON(Array.from(FILES_DELETED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_renamed.json`, toJSON(Array.from(FILES_RENAMED.values())), 'utf-8');
core.setOutput('all', toJSON(Array.from(FILES.values()), 0));
core.setOutput('added', toJSON(Array.from(FILES_ADDED.values()), 0));
core.setOutput('modified', toJSON(Array.from(FILES_MODIFIED.values()), 0));
core.setOutput('removed', toJSON(Array.from(FILES_REMOVED.values()), 0));
core.setOutput('renamed', toJSON(Array.from(FILES_RENAMED.values()), 0));

fs.writeFileSync(`${process.env.HOME}/files.json`, toJSON(Array.from(FILES.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_added.json`, toJSON(Array.from(FILES_ADDED.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_modified.json`, toJSON(Array.from(FILES_MODIFIED.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_removed.json`, toJSON(Array.from(FILES_REMOVED.values()), 0), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_renamed.json`, toJSON(Array.from(FILES_RENAMED.values()), 0), 'utf-8');

// Backwards Compatability
core.setOutput('deleted', toJSON(Array.from(FILES_REMOVED.values()), 0));
fs.writeFileSync(`${process.env.HOME}/files_deleted.json`, toJSON(Array.from(FILES_REMOVED.values()), 0), 'utf-8');

process.exit(0);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gh-action-get-changed-files",
"version": "2.1.1rc1",
"version": "2.1.1rc2",
"private": true,
"main": "dist/index.js",
"scripts":{
Expand Down

0 comments on commit 14772bf

Please sign in to comment.