Skip to content

Commit

Permalink
Add debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
lots0logs committed Jun 3, 2020
1 parent 8dcee54 commit a2501ff
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 59 deletions.
80 changes: 51 additions & 29 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,26 +305,19 @@ const FILES_RENAMED = new Set();
const gh = github.getOctokit(core.getInput('token'));
const args = { owner: owner.name, repo: repo.name };

function isAdded(file) {
return 'added' === file.status;
}

function isDeleted(file) {
return 'deleted' === file.status;
function debug(msg, obj = null) {
core.debug(formatLogMessage(msg, obj));
}

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

function isRenamed(file) {
return 'renamed' === file.status;
function formatLogMessage(msg, obj = null) {
return obj ? `${msg}: ${toJSON(obj)}` : msg;
}

async function getCommits() {
let commits;

core.debug('Getting commits...');
debug('Getting commits...');

switch(context.eventName) {
case 'push':
Expand All @@ -338,7 +331,7 @@ async function getCommits() {
break;

default:
core.info('You are using this action on an event for which it has not been tested. Only the "push" and "pull_request" events are officially supported.');
info('You are using this action on an event for which it has not been tested. Only the "push" and "pull_request" events are officially supported.');

commits = [];
break;
Expand All @@ -347,14 +340,36 @@ async function getCommits() {
return commits;
}

function info(msg, obj = null) {
core.info(formatLogMessage(msg, obj));
}

function isAdded(file) {
return 'added' === file.status;
}

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

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

function isRenamed(file) {
return 'renamed' === file.status;
}

async function processCommit(commit) {
core.debug(`Processing commit: ${JSON.stringify(commit, 4)}`);
debug('Processing commit', commit);

args.ref = commit.id;

debug('Calling gh.repos.getCommit() with args', args)

let result = await gh.repos.getCommit(args);

core.debug(`API Response: ${JSON.stringify(result, 4)}`);
debug('API Response', result);

if (result && result.data) {
const files = result.data.files;
Expand All @@ -372,25 +387,32 @@ async function processCommit(commit) {
}
}

function toJSON(value) {
return JSON.stringify(value, null, 4);
}

debug('context', context);
debug('args', args);

getCommits().then(commits => {
commits = commits.filter(c => c.distinct);

core.debug(`All Distinct Commits: ${JSON.stringify(commits, 4)}`);
debug('All Distinct Commits', commits);

Promise.all(commits.map(processCommit)).then(() => {
core.debug(JSON.stringify(FILES, 4));

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

fs.writeFileSync(`${process.env.HOME}/files.json`, JSON.stringify(Array.from(FILES.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_modified.json`, JSON.stringify(Array.from(FILES_MODIFIED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_added.json`, JSON.stringify(Array.from(FILES_ADDED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_deleted.json`, JSON.stringify(Array.from(FILES_DELETED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_renamed.json`, JSON.stringify(Array.from(FILES_RENAMED.values())), 'utf-8');
debug('FILES', FILES);

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');

process.exit(0);
});
Expand Down
80 changes: 51 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,19 @@ const FILES_RENAMED = new Set();
const gh = github.getOctokit(core.getInput('token'));
const args = { owner: owner.name, repo: repo.name };

function isAdded(file) {
return 'added' === file.status;
}

function isDeleted(file) {
return 'deleted' === file.status;
function debug(msg, obj = null) {
core.debug(formatLogMessage(msg, obj));
}

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

function isRenamed(file) {
return 'renamed' === file.status;
function formatLogMessage(msg, obj = null) {
return obj ? `${msg}: ${toJSON(obj)}` : msg;
}

async function getCommits() {
let commits;

core.debug('Getting commits...');
debug('Getting commits...');

switch(context.eventName) {
case 'push':
Expand All @@ -50,7 +43,7 @@ async function getCommits() {
break;

default:
core.info('You are using this action on an event for which it has not been tested. Only the "push" and "pull_request" events are officially supported.');
info('You are using this action on an event for which it has not been tested. Only the "push" and "pull_request" events are officially supported.');

commits = [];
break;
Expand All @@ -59,14 +52,36 @@ async function getCommits() {
return commits;
}

function info(msg, obj = null) {
core.info(formatLogMessage(msg, obj));
}

function isAdded(file) {
return 'added' === file.status;
}

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

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

function isRenamed(file) {
return 'renamed' === file.status;
}

async function processCommit(commit) {
core.debug(`Processing commit: ${JSON.stringify(commit, 4)}`);
debug('Processing commit', commit);

args.ref = commit.id;

debug('Calling gh.repos.getCommit() with args', args)

let result = await gh.repos.getCommit(args);

core.debug(`API Response: ${JSON.stringify(result, 4)}`);
debug('API Response', result);

if (result && result.data) {
const files = result.data.files;
Expand All @@ -84,25 +99,32 @@ async function processCommit(commit) {
}
}

function toJSON(value) {
return JSON.stringify(value, null, 4);
}

debug('context', context);
debug('args', args);

getCommits().then(commits => {
commits = commits.filter(c => c.distinct);

core.debug(`All Distinct Commits: ${JSON.stringify(commits, 4)}`);
debug('All Distinct Commits', commits);

Promise.all(commits.map(processCommit)).then(() => {
core.debug(JSON.stringify(FILES, 4));

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

fs.writeFileSync(`${process.env.HOME}/files.json`, JSON.stringify(Array.from(FILES.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_modified.json`, JSON.stringify(Array.from(FILES_MODIFIED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_added.json`, JSON.stringify(Array.from(FILES_ADDED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_deleted.json`, JSON.stringify(Array.from(FILES_DELETED.values())), 'utf-8');
fs.writeFileSync(`${process.env.HOME}/files_renamed.json`, JSON.stringify(Array.from(FILES_RENAMED.values())), 'utf-8');
debug('FILES', FILES);

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');

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.0rc3",
"version": "2.1.0rc4",
"private": true,
"main": "dist/index.js",
"scripts":{
Expand Down

0 comments on commit a2501ff

Please sign in to comment.