diff --git a/dist/index.js b/dist/index.js index 39f974d..8844285 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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': @@ -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; @@ -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; @@ -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); }); diff --git a/index.js b/index.js index f051a0e..f43dd1a 100644 --- a/index.js +++ b/index.js @@ -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': @@ -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; @@ -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; @@ -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); }); diff --git a/package.json b/package.json index 623e1fd..92ba5a2 100644 --- a/package.json +++ b/package.json @@ -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":{