Skip to content

Commit

Permalink
feat: add support for returning YAML keys for paths that have changed (
Browse files Browse the repository at this point in the history
…#1581)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
jackton1 and actions-user committed Sep 18, 2023
1 parent a17e8c5 commit 5db7b57
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,12 @@ jobs:
echo "Expected: (test/test deleted.txt) got ${{ steps.changed-files-recover-deleted-files-with-files-yaml.outputs.test_deleted_files }}"
exit 1
- name: Verify that the modified_keys is correct
if: "!contains(steps.changed-files-recover-deleted-files-with-files-yaml.outputs.modified_keys, 'test')"
run: |
echo "Expected: (test) got ${{ steps.changed-files-recover-deleted-files-with-files-yaml.outputs.modified_keys }}"
exit 1
- name: Verify that test/test deleted.txt is restored
run: |
if [ ! -f "test/test deleted.txt" ]; then
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ outputs:
only_modified:
description: "Returns `true` when only files provided using the `files*` or `files_ignore*` inputs has been modified. (ACMRD)."
other_modified_files:
description: "Returns all other modified files not listed in the files input i.e. *a combination of all added, copied, modified, and deleted files (ACMRD)*"
description: "Returns all other modified files not listed in the files input i.e. *a combination of all added, copied, modified, and deleted files (ACMRD)*"
other_modified_files_count:
description: "Returns the number of `other_modified_files`"
any_deleted:
Expand All @@ -270,6 +270,10 @@ outputs:
description: "Returns all other deleted files not listed in the files input i.e. *a combination of all deleted files (D)*"
other_deleted_files_count:
description: "Returns the number of `other_deleted_files`"
modified_keys:
description: "Returns all modified YAML keys when the `files_yaml` input is used. i.e. *key that contains any path that has either been added, copied, modified, and deleted (ACMRD)*"
changed_keys:
description: "Returns all changed YAML keys when the `files_yaml` input is used. i.e. *key that contains any path that has either been added, copied, modified, and renamed (ACMR)*"

runs:
using: 'node20'
Expand Down
42 changes: 36 additions & 6 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

54 changes: 43 additions & 11 deletions src/changedFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type {RestEndpointMethodTypes} from '@octokit/rest'
import flatten from 'lodash/flatten'
import mm from 'micromatch'
import * as path from 'path'
import {setChangedFilesOutput} from './changedFilesOutput'
import {setOutputsAndGetModifiedAndChangedFilesStatus} from './changedFilesOutput'
import {DiffResult} from './commitSha'
import {Inputs} from './inputs'
import {
Expand All @@ -16,7 +16,8 @@ import {
gitRenamedFiles,
gitSubmoduleDiffSHA,
isWindows,
jsonOutput
jsonOutput,
setOutput
} from './utils'

export const processChangedFiles = async ({
Expand All @@ -39,7 +40,7 @@ export const processChangedFiles = async ({
core.debug(
`All filtered diff files: ${JSON.stringify(allFilteredDiffFiles)}`
)
await setChangedFilesOutput({
await setOutputsAndGetModifiedAndChangedFilesStatus({
allDiffFiles,
allFilteredDiffFiles,
inputs,
Expand All @@ -49,6 +50,9 @@ export const processChangedFiles = async ({
core.endGroup()
}

const modifiedKeys: string[] = []
const changedKeys: string[] = []

if (Object.keys(yamlFilePatterns).length > 0) {
for (const key of Object.keys(yamlFilePatterns)) {
core.startGroup(`changed-files-yaml-${key}`)
Expand All @@ -61,21 +65,49 @@ export const processChangedFiles = async ({
allFilteredDiffFiles
)}`
)
await setChangedFilesOutput({
allDiffFiles,
allFilteredDiffFiles,
inputs,
filePatterns: yamlFilePatterns[key],
outputPrefix: key
})
const {anyChanged, anyModified} =
await setOutputsAndGetModifiedAndChangedFilesStatus({
allDiffFiles,
allFilteredDiffFiles,
inputs,
filePatterns: yamlFilePatterns[key],
outputPrefix: key
})
if (anyModified) {
modifiedKeys.push(key)
}
if (anyChanged) {
changedKeys.push(key)
}

core.info('All Done!')
core.endGroup()
}

if (modifiedKeys.length > 0) {
await setOutput({
key: 'modified_keys',
value: modifiedKeys.join(inputs.separator),
writeOutputFiles: inputs.writeOutputFiles,
outputDir: inputs.outputDir,
json: inputs.json
})
}

if (changedKeys.length > 0) {
await setOutput({
key: 'changed_keys',
value: changedKeys.join(inputs.separator),
writeOutputFiles: inputs.writeOutputFiles,
outputDir: inputs.outputDir,
json: inputs.json
})
}
}

if (filePatterns.length === 0 && Object.keys(yamlFilePatterns).length === 0) {
core.startGroup('changed-files-all')
await setChangedFilesOutput({
await setOutputsAndGetModifiedAndChangedFilesStatus({
allDiffFiles,
allFilteredDiffFiles: allDiffFiles,
inputs
Expand Down
9 changes: 7 additions & 2 deletions src/changedFilesOutput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const getArrayFromPaths = (
return Array.isArray(paths) ? paths : paths.split(inputs.separator)
}

export const setChangedFilesOutput = async ({
export const setOutputsAndGetModifiedAndChangedFilesStatus = async ({
allDiffFiles,
allFilteredDiffFiles,
inputs,
Expand All @@ -31,7 +31,7 @@ export const setChangedFilesOutput = async ({
inputs: Inputs
filePatterns?: string[]
outputPrefix?: string
}): Promise<void> => {
}): Promise<{anyModified: boolean; anyChanged: boolean}> => {
const addedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
Expand Down Expand Up @@ -474,4 +474,9 @@ export const setChangedFilesOutput = async ({
writeOutputFiles: inputs.writeOutputFiles,
outputDir: inputs.outputDir
})

return {
anyModified: allModifiedFiles.paths.length > 0 && filePatterns.length > 0,
anyChanged: allChangedFiles.paths.length > 0 && filePatterns.length > 0
}
}

0 comments on commit 5db7b57

Please sign in to comment.