From 68f05ddcc28f953cda39a5e05b2b816da8f25cf4 Mon Sep 17 00:00:00 2001 From: Andrew Red Date: Tue, 16 Feb 2021 11:03:40 +0300 Subject: [PATCH] - added ability to filter and return object of files --- action.yml | 7 ++++++- dist/index.js | 23 +++++++++++++++++++++-- src/main.ts | 25 ++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/action.yml b/action.yml index 2536c037..e7d02bb0 100644 --- a/action.yml +++ b/action.yml @@ -17,9 +17,14 @@ inputs: format: description: > Format of the steps output context. - Can be 'space-delimited', 'csv', or 'json'. + Can be 'space-delimited', 'csv', 'json' or 'object'. required: true default: space-delimited + filter: + description: > + Regex for filtering files, for example to get files of specific type. + required: false + default: '' outputs: all: description: > diff --git a/dist/index.js b/dist/index.js index 72c16629..e41ff84f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3517,9 +3517,10 @@ function run() { // Create GitHub client with the API token. const client = new github_1.GitHub(core.getInput('token', { required: true })); const format = core.getInput('format', { required: true }); + const filter = core.getInput('filter', { required: false }); // Ensure that the format parameter is set properly. - if (format !== 'space-delimited' && format !== 'csv' && format !== 'json') { - core.setFailed(`Format must be one of 'string-delimited', 'csv', or 'json', got '${format}'.`); + if (format !== 'space-delimited' && format !== 'csv' && format !== 'json' && format !== 'object') { + core.setFailed(`Format must be one of 'string-delimited', 'csv', 'json', or 'object', got '${format}'.`); } // Debug log the payload. core.debug(`Payload keys: ${Object.keys(github_1.context.payload)}`); @@ -3573,8 +3574,12 @@ function run() { // Get the changed files from the response payload. const files = response.data.files; const all = [], added = [], modified = [], removed = [], renamed = [], addedModified = []; + const re = filter ? new RegExp(filter, 'i') : false; for (const file of files) { const filename = file.filename; + if (re && !filename.match(re)) { + continue; + } // If we're using the 'space-delimited' format and any of the filenames have a space in them, // then fail the step. if (format === 'space-delimited' && filename.includes(' ')) { @@ -3633,6 +3638,20 @@ function run() { renamedFormatted = JSON.stringify(renamed); addedModifiedFormatted = JSON.stringify(addedModified); break; + case 'object': + // eslint-disable-next-line github/array-foreach + all.forEach((f, i) => core.setOutput(`all${i}`, f)); + // eslint-disable-next-line github/array-foreach + added.forEach((f, i) => core.setOutput(`added${i}`, f)); + // eslint-disable-next-line github/array-foreach + modified.forEach((f, i) => core.setOutput(`modified${i}`, f)); + // eslint-disable-next-line github/array-foreach + removed.forEach((f, i) => core.setOutput(`removed${i}`, f)); + // eslint-disable-next-line github/array-foreach + renamed.forEach((f, i) => core.setOutput(`renamed${i}`, f)); + // eslint-disable-next-line github/array-foreach + addedModified.forEach((f, i) => core.setOutput(`addedModified${i}`, f)); + return; } // Log the output values. core.info(`All: ${allFormatted}`); diff --git a/src/main.ts b/src/main.ts index 0f0fdd61..b6247428 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ import * as core from '@actions/core' import {context, GitHub} from '@actions/github' -type Format = 'space-delimited' | 'csv' | 'json' +type Format = 'space-delimited' | 'csv' | 'json' | 'object' type FileStatus = 'added' | 'modified' | 'removed' | 'renamed' async function run(): Promise { @@ -9,10 +9,11 @@ async function run(): Promise { // Create GitHub client with the API token. const client = new GitHub(core.getInput('token', {required: true})) const format = core.getInput('format', {required: true}) as Format + const filter = core.getInput('filter', {required: false}) // Ensure that the format parameter is set properly. - if (format !== 'space-delimited' && format !== 'csv' && format !== 'json') { - core.setFailed(`Format must be one of 'string-delimited', 'csv', or 'json', got '${format}'.`) + if (format !== 'space-delimited' && format !== 'csv' && format !== 'json' && format !== 'object') { + core.setFailed(`Format must be one of 'string-delimited', 'csv', 'json', or 'object', got '${format}'.`) } // Debug log the payload. @@ -90,8 +91,12 @@ async function run(): Promise { removed = [] as string[], renamed = [] as string[], addedModified = [] as string[] + const re = filter ? new RegExp(filter, 'i') : false for (const file of files) { const filename = file.filename + if (re && !filename.match(re)) { + continue + } // If we're using the 'space-delimited' format and any of the filenames have a space in them, // then fail the step. if (format === 'space-delimited' && filename.includes(' ')) { @@ -162,6 +167,20 @@ async function run(): Promise { renamedFormatted = JSON.stringify(renamed) addedModifiedFormatted = JSON.stringify(addedModified) break + case 'object': + // eslint-disable-next-line github/array-foreach + all.forEach((f, i) => core.setOutput(`all${i}`, f)) + // eslint-disable-next-line github/array-foreach + added.forEach((f, i) => core.setOutput(`added${i}`, f)) + // eslint-disable-next-line github/array-foreach + modified.forEach((f, i) => core.setOutput(`modified${i}`, f)) + // eslint-disable-next-line github/array-foreach + removed.forEach((f, i) => core.setOutput(`removed${i}`, f)) + // eslint-disable-next-line github/array-foreach + renamed.forEach((f, i) => core.setOutput(`renamed${i}`, f)) + // eslint-disable-next-line github/array-foreach + addedModified.forEach((f, i) => core.setOutput(`addedModified${i}`, f)) + return } // Log the output values.