Skip to content

Commit

Permalink
- added ability to filter and return object of files
Browse files Browse the repository at this point in the history
  • Loading branch information
maZahaca committed Feb 16, 2021
1 parent b17fbb0 commit 68f05dd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: >
Expand Down
23 changes: 21 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`);
Expand Down Expand Up @@ -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(' ')) {
Expand Down Expand Up @@ -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}`);
Expand Down
25 changes: 22 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
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<void> {
try {
// 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.
Expand Down Expand Up @@ -90,8 +91,12 @@ async function run(): Promise<void> {
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(' ')) {
Expand Down Expand Up @@ -162,6 +167,20 @@ async function run(): Promise<void> {
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.
Expand Down

0 comments on commit 68f05dd

Please sign in to comment.