feat: add support for complex filters (#1265)

Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: tj-actions[bot] <109116665+tj-actions-bot@users.noreply.github.com>
This commit is contained in:
Tonye Jack
2023-06-16 00:17:13 -06:00
committed by GitHub
parent ea90b5ced9
commit c25c77a67a
15 changed files with 26879 additions and 548 deletions

292
src/changedFilesOutput.ts Normal file
View File

@@ -0,0 +1,292 @@
import * as core from '@actions/core'
import {
ChangedFiles,
ChangeTypeEnum,
getAllChangeTypeFiles,
getChangeTypeFiles
} from './changedFiles'
import {Inputs} from './inputs'
import {getFilteredChangedFiles, setOutput} from './utils'
const getOutputKey = (key: string, outputPrefix: string): string => {
return outputPrefix ? `${outputPrefix}_${key}` : key
}
export const setChangedFilesOutput = async ({
allDiffFiles,
inputs,
filePatterns = [],
outputPrefix = ''
}: {
allDiffFiles: ChangedFiles
filePatterns?: string[]
inputs: Inputs
outputPrefix?: string
}): Promise<void> => {
const allFilteredDiffFiles = await getFilteredChangedFiles({
allDiffFiles,
filePatterns
})
core.debug(`All filtered diff files: ${JSON.stringify(allFilteredDiffFiles)}`)
const addedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Added]
})
core.debug(`Added files: ${addedFiles}`)
await setOutput({
key: getOutputKey('added_files', outputPrefix),
value: addedFiles,
inputs
})
const copiedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Copied]
})
core.debug(`Copied files: ${copiedFiles}`)
await setOutput({
key: getOutputKey('copied_files', outputPrefix),
value: copiedFiles,
inputs
})
const modifiedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Modified]
})
core.debug(`Modified files: ${modifiedFiles}`)
await setOutput({
key: getOutputKey('modified_files', outputPrefix),
value: modifiedFiles,
inputs
})
const renamedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Renamed]
})
core.debug(`Renamed files: ${renamedFiles}`)
await setOutput({
key: getOutputKey('renamed_files', outputPrefix),
value: renamedFiles,
inputs
})
const typeChangedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.TypeChanged]
})
core.debug(`Type changed files: ${typeChangedFiles}`)
await setOutput({
key: getOutputKey('type_changed_files', outputPrefix),
value: typeChangedFiles,
inputs
})
const unmergedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Unmerged]
})
core.debug(`Unmerged files: ${unmergedFiles}`)
await setOutput({
key: getOutputKey('unmerged_files', outputPrefix),
value: unmergedFiles,
inputs
})
const unknownFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Unknown]
})
core.debug(`Unknown files: ${unknownFiles}`)
await setOutput({
key: getOutputKey('unknown_files', outputPrefix),
value: unknownFiles,
inputs
})
const allChangedAndModifiedFiles = await getAllChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles
})
core.debug(`All changed and modified files: ${allChangedAndModifiedFiles}`)
await setOutput({
key: getOutputKey('all_changed_and_modified_files', outputPrefix),
value: allChangedAndModifiedFiles,
inputs
})
const allChangedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed
]
})
core.debug(`All changed files: ${allChangedFiles}`)
await setOutput({
key: getOutputKey('all_changed_files', outputPrefix),
value: allChangedFiles,
inputs
})
await setOutput({
key: getOutputKey('any_changed', outputPrefix),
value: allChangedFiles.length > 0 && filePatterns.length > 0,
inputs
})
const allOtherChangedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed
]
})
core.debug(`All other changed files: ${allOtherChangedFiles}`)
const otherChangedFiles = allOtherChangedFiles
.split(inputs.separator)
.filter(
(filePath: string) =>
!allChangedFiles.split(inputs.separator).includes(filePath)
)
const onlyChanged =
otherChangedFiles.length === 0 &&
allChangedFiles.length > 0 &&
filePatterns.length > 0
await setOutput({
key: getOutputKey('only_changed', outputPrefix),
value: onlyChanged,
inputs
})
await setOutput({
key: getOutputKey('other_changed_files', outputPrefix),
value: otherChangedFiles.join(inputs.separator),
inputs
})
const allModifiedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed,
ChangeTypeEnum.Deleted
]
})
core.debug(`All modified files: ${allModifiedFiles}`)
await setOutput({
key: getOutputKey('all_modified_files', outputPrefix),
value: allModifiedFiles,
inputs
})
await setOutput({
key: getOutputKey('any_modified', outputPrefix),
value: allModifiedFiles.length > 0 && filePatterns.length > 0,
inputs
})
const allOtherModifiedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed,
ChangeTypeEnum.Deleted
]
})
const otherModifiedFiles = allOtherModifiedFiles
.split(inputs.separator)
.filter(
(filePath: string) =>
!allModifiedFiles.split(inputs.separator).includes(filePath)
)
const onlyModified =
otherModifiedFiles.length === 0 &&
allModifiedFiles.length > 0 &&
filePatterns.length > 0
await setOutput({
key: getOutputKey('only_modified', outputPrefix),
value: onlyModified,
inputs
})
await setOutput({
key: getOutputKey('other_modified_files', outputPrefix),
value: otherModifiedFiles.join(inputs.separator),
inputs
})
const deletedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Deleted]
})
core.debug(`Deleted files: ${deletedFiles}`)
await setOutput({
key: getOutputKey('deleted_files', outputPrefix),
value: deletedFiles,
inputs
})
await setOutput({
key: getOutputKey('any_deleted', outputPrefix),
value: deletedFiles.length > 0 && filePatterns.length > 0,
inputs
})
const allOtherDeletedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allDiffFiles,
changeTypes: [ChangeTypeEnum.Deleted]
})
const otherDeletedFiles = allOtherDeletedFiles
.split(inputs.separator)
.filter(
filePath => !deletedFiles.split(inputs.separator).includes(filePath)
)
const onlyDeleted =
otherDeletedFiles.length === 0 &&
deletedFiles.length > 0 &&
filePatterns.length > 0
await setOutput({
key: getOutputKey('only_deleted', outputPrefix),
value: onlyDeleted,
inputs
})
await setOutput({
key: getOutputKey('other_deleted_files', outputPrefix),
value: otherDeletedFiles.join(inputs.separator),
inputs
})
}

View File

@@ -379,9 +379,18 @@ export const getSHAForPullRequestEvent = async (
0)
) {
core.warning(
'Unable to locate the remote branch head sha. Falling back to the pull request base sha.'
'Unable to locate the remote branch head sha. Falling back to the previous commit in the local history.'
)
previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA
previousSha = await getParentSha({
cwd: workingDirectory
})
if (!previousSha) {
core.warning(
'Unable to locate the previous commit in the local history. Falling back to the pull request base sha.'
)
previousSha = env.GITHUB_EVENT_PULL_REQUEST_BASE_SHA
}
}
} else {
previousSha = await getRemoteBranchHeadSha({
@@ -395,12 +404,12 @@ export const getSHAForPullRequestEvent = async (
if (isShallow) {
if (
await canDiffCommits({
!(await canDiffCommits({
cwd: workingDirectory,
sha1: previousSha,
sha2: currentSha,
diff
})
}))
) {
core.debug(
'Merge base is not in the local history, fetching remote target branch...'

View File

@@ -2,23 +2,25 @@ import {promises as fs} from 'fs'
import * as core from '@actions/core'
export type Env = {
GITHUB_EVENT_PULL_REQUEST_HEAD_REF: string
GITHUB_EVENT_PULL_REQUEST_BASE_REF: string
GITHUB_EVENT_BEFORE: string
GITHUB_REF_NAME: string
GITHUB_REF: string
GITHUB_WORKSPACE: string
GITHUB_EVENT_ACTION: string
GITHUB_EVENT_NAME: string
GITHUB_EVENT_FORCED: string
GITHUB_EVENT_BEFORE: string
GITHUB_EVENT_BASE_REF: string
GITHUB_EVENT_RELEASE_TARGET_COMMITISH: string
GITHUB_EVENT_HEAD_REPO_FORK: string
GITHUB_WORKSPACE: string
GITHUB_EVENT_FORCED: string
GITHUB_EVENT_PULL_REQUEST_NUMBER: string
GITHUB_EVENT_PULL_REQUEST_BASE_SHA: string
GITHUB_EVENT_PULL_REQUEST_HEAD_SHA: string
GITHUB_EVENT_NAME: string
GITHUB_EVENT_PULL_REQUEST_HEAD_REF: string
GITHUB_EVENT_PULL_REQUEST_BASE_REF: string
}
type GithubEvent = {
action?: string
forced?: string
pull_request?: {
head: {
@@ -65,6 +67,7 @@ export const getEnv = async (): Promise<Env> => {
GITHUB_EVENT_PULL_REQUEST_BASE_SHA: eventJson.pull_request?.base?.sha || '',
GITHUB_EVENT_PULL_REQUEST_HEAD_SHA: eventJson.pull_request?.head?.sha || '',
GITHUB_EVENT_FORCED: eventJson.forced || '',
GITHUB_EVENT_ACTION: eventJson.action || '',
GITHUB_REF_NAME: process.env.GITHUB_REF_NAME || '',
GITHUB_REF: process.env.GITHUB_REF || '',
GITHUB_WORKSPACE: process.env.GITHUB_WORKSPACE || '',

View File

@@ -5,10 +5,16 @@ export type Inputs = {
filesSeparator: string
filesFromSourceFile: string
filesFromSourceFileSeparator: string
filesYaml: string
filesYamlFromSourceFile: string
filesYamlFromSourceFileSeparator: string
filesIgnore: string
filesIgnoreSeparator: string
filesIgnoreFromSourceFile: string
filesIgnoreFromSourceFileSeparator: string
filesIgnoreYaml: string
filesIgnoreYamlFromSourceFile: string
filesIgnoreYamlFromSourceFileSeparator: string
separator: string
includeAllOldNewRenamedFiles: boolean
oldNewSeparator: string
@@ -54,6 +60,17 @@ export const getInputs = (): Inputs => {
trimWhitespace: false
}
)
const filesYaml = core.getInput('files_yaml', {required: false})
const filesYamlFromSourceFile = core.getInput('files_yaml_from_source_file', {
required: false
})
const filesYamlFromSourceFileSeparator = core.getInput(
'files_yaml_from_source_file_separator',
{
required: false,
trimWhitespace: false
}
)
const filesIgnoreFromSourceFile = core.getInput(
'files_ignore_from_source_file',
{required: false}
@@ -65,6 +82,18 @@ export const getInputs = (): Inputs => {
trimWhitespace: false
}
)
const filesIgnoreYaml = core.getInput('files_ignore_yaml', {required: false})
const filesIgnoreYamlFromSourceFile = core.getInput(
'files_ignore_yaml_from_source_file',
{required: false}
)
const filesIgnoreYamlFromSourceFileSeparator = core.getInput(
'files_ignore_yaml_from_source_file_separator',
{
required: false,
trimWhitespace: false
}
)
const separator = core.getInput('separator', {
required: true,
trimWhitespace: false
@@ -122,10 +151,16 @@ export const getInputs = (): Inputs => {
filesSeparator,
filesFromSourceFile,
filesFromSourceFileSeparator,
filesYaml,
filesYamlFromSourceFile,
filesYamlFromSourceFileSeparator,
filesIgnore,
filesIgnoreSeparator,
filesIgnoreFromSourceFile,
filesIgnoreFromSourceFileSeparator,
filesIgnoreYaml,
filesIgnoreYamlFromSourceFile,
filesIgnoreYamlFromSourceFileSeparator,
separator,
includeAllOldNewRenamedFiles,
oldNewSeparator,

View File

@@ -1,12 +1,7 @@
import * as core from '@actions/core'
import path from 'path'
import {
getAllChangeTypeFiles,
getAllDiffFiles,
getChangeTypeFiles,
getRenamedFiles,
ChangeTypeEnum
} from './changedFiles'
import {getAllDiffFiles, getRenamedFiles} from './changedFiles'
import {setChangedFilesOutput} from './changedFilesOutput'
import {
DiffResult,
getSHAForPullRequestEvent,
@@ -16,8 +11,8 @@ import {getEnv} from './env'
import {getInputs} from './inputs'
import {
getFilePatterns,
getFilteredChangedFiles,
getSubmodulePath,
getYamlFilePatterns,
isRepoShallow,
setOutput,
submoduleExists,
@@ -88,7 +83,9 @@ export async function run(): Promise<void> {
)
} else {
core.info(
`Running on a ${env.GITHUB_EVENT_NAME || 'pull_request'} event...`
`Running on a ${env.GITHUB_EVENT_NAME || 'pull_request'} (${
env.GITHUB_EVENT_ACTION
}) event...`
)
diffResult = await getSHAForPullRequestEvent(
inputs,
@@ -110,12 +107,6 @@ export async function run(): Promise<void> {
`Retrieving changes between ${diffResult.previousSha} (${diffResult.targetBranch}) → ${diffResult.currentSha} (${diffResult.currentBranch})`
)
const filePatterns = await getFilePatterns({
inputs,
workingDirectory
})
core.debug(`File patterns: ${filePatterns}`)
const allDiffFiles = await getAllDiffFiles({
workingDirectory,
hasSubmodule,
@@ -124,275 +115,58 @@ export async function run(): Promise<void> {
outputRenamedFilesAsDeletedAndAdded
})
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
core.info('All Done!')
core.endGroup()
const allFilteredDiffFiles = await getFilteredChangedFiles({
allDiffFiles,
filePatterns
})
core.debug(`All filtered diff files: ${JSON.stringify(allFilteredDiffFiles)}`)
const addedFiles = await getChangeTypeFiles({
const filePatterns = await getFilePatterns({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Added]
})
core.debug(`Added files: ${addedFiles}`)
await setOutput({
key: 'added_files',
value: addedFiles,
inputs
workingDirectory
})
core.debug(`File patterns: ${filePatterns}`)
const copiedFiles = await getChangeTypeFiles({
if (filePatterns.length > 0) {
core.startGroup('changed-files-patterns')
await setChangedFilesOutput({
allDiffFiles,
filePatterns,
inputs
})
core.info('All Done!')
core.endGroup()
}
const yamlFilePatterns = await getYamlFilePatterns({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Copied]
})
core.debug(`Copied files: ${copiedFiles}`)
await setOutput({
key: 'copied_files',
value: copiedFiles,
inputs
workingDirectory
})
core.debug(`Yaml file patterns: ${JSON.stringify(yamlFilePatterns)}`)
const modifiedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Modified]
})
core.debug(`Modified files: ${modifiedFiles}`)
await setOutput({
key: 'modified_files',
value: modifiedFiles,
inputs
})
if (Object.keys(yamlFilePatterns).length > 0) {
for (const key of Object.keys(yamlFilePatterns)) {
core.startGroup(`changed-files-yaml-${key}`)
await setChangedFilesOutput({
allDiffFiles,
filePatterns: yamlFilePatterns[key],
inputs,
outputPrefix: key
})
core.info('All Done!')
core.endGroup()
}
}
const renamedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Renamed]
})
core.debug(`Renamed files: ${renamedFiles}`)
await setOutput({
key: 'renamed_files',
value: renamedFiles,
inputs
})
const typeChangedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.TypeChanged]
})
core.debug(`Type changed files: ${typeChangedFiles}`)
await setOutput({
key: 'type_changed_files',
value: typeChangedFiles,
inputs
})
const unmergedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Unmerged]
})
core.debug(`Unmerged files: ${unmergedFiles}`)
await setOutput({
key: 'unmerged_files',
value: unmergedFiles,
inputs
})
const unknownFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Unknown]
})
core.debug(`Unknown files: ${unknownFiles}`)
await setOutput({
key: 'unknown_files',
value: unknownFiles,
inputs
})
const allChangedAndModifiedFiles = await getAllChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles
})
core.debug(`All changed and modified files: ${allChangedAndModifiedFiles}`)
await setOutput({
key: 'all_changed_and_modified_files',
value: allChangedAndModifiedFiles,
inputs
})
const allChangedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed
]
})
core.debug(`All changed files: ${allChangedFiles}`)
await setOutput({
key: 'all_changed_files',
value: allChangedFiles,
inputs
})
await setOutput({
key: 'any_changed',
value: allChangedFiles.length > 0 && filePatterns.length > 0,
inputs
})
const allOtherChangedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed
]
})
core.debug(`All other changed files: ${allOtherChangedFiles}`)
const otherChangedFiles = allOtherChangedFiles
.split(inputs.separator)
.filter(
(filePath: string) =>
!allChangedFiles.split(inputs.separator).includes(filePath)
)
const onlyChanged =
otherChangedFiles.length === 0 &&
allChangedFiles.length > 0 &&
filePatterns.length > 0
await setOutput({
key: 'only_changed',
value: onlyChanged,
inputs
})
await setOutput({
key: 'other_changed_files',
value: otherChangedFiles.join(inputs.separator),
inputs
})
const allModifiedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed,
ChangeTypeEnum.Deleted
]
})
core.debug(`All modified files: ${allModifiedFiles}`)
await setOutput({
key: 'all_modified_files',
value: allModifiedFiles,
inputs
})
await setOutput({
key: 'any_modified',
value: allModifiedFiles.length > 0 && filePatterns.length > 0,
inputs
})
const allOtherModifiedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allDiffFiles,
changeTypes: [
ChangeTypeEnum.Added,
ChangeTypeEnum.Copied,
ChangeTypeEnum.Modified,
ChangeTypeEnum.Renamed,
ChangeTypeEnum.Deleted
]
})
const otherModifiedFiles = allOtherModifiedFiles
.split(inputs.separator)
.filter(
(filePath: string) =>
!allModifiedFiles.split(inputs.separator).includes(filePath)
)
const onlyModified =
otherModifiedFiles.length === 0 &&
allModifiedFiles.length > 0 &&
filePatterns.length > 0
await setOutput({
key: 'only_modified',
value: onlyModified,
inputs
})
await setOutput({
key: 'other_modified_files',
value: otherModifiedFiles.join(inputs.separator),
inputs
})
const deletedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allFilteredDiffFiles,
changeTypes: [ChangeTypeEnum.Deleted]
})
core.debug(`Deleted files: ${deletedFiles}`)
await setOutput({
key: 'deleted_files',
value: deletedFiles,
inputs
})
await setOutput({
key: 'any_deleted',
value: deletedFiles.length > 0 && filePatterns.length > 0,
inputs
})
const allOtherDeletedFiles = await getChangeTypeFiles({
inputs,
changedFiles: allDiffFiles,
changeTypes: [ChangeTypeEnum.Deleted]
})
const otherDeletedFiles = allOtherDeletedFiles
.split(inputs.separator)
.filter(
filePath => !deletedFiles.split(inputs.separator).includes(filePath)
)
const onlyDeleted =
otherDeletedFiles.length === 0 &&
deletedFiles.length > 0 &&
filePatterns.length > 0
await setOutput({
key: 'only_deleted',
value: onlyDeleted,
inputs
})
await setOutput({
key: 'other_deleted_files',
value: otherDeletedFiles.join(inputs.separator),
inputs
})
if (filePatterns.length === 0 && Object.keys(yamlFilePatterns).length === 0) {
core.startGroup('changed-files-all')
await setChangedFilesOutput({
allDiffFiles,
inputs
})
core.info('All Done!')
core.endGroup()
}
if (inputs.includeAllOldNewRenamedFiles) {
core.startGroup('changed-files-all-old-new-renamed-files')
const allOldNewRenamedFiles = await getRenamedFiles({
inputs,
workingDirectory,
@@ -406,11 +180,9 @@ export async function run(): Promise<void> {
value: allOldNewRenamedFiles,
inputs
})
core.info('All Done!')
core.endGroup()
}
core.info('All Done!')
core.endGroup()
}
/* istanbul ignore if */

View File

@@ -2,9 +2,12 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import {createReadStream, promises as fs} from 'fs'
import {readFile} from 'fs/promises'
import {flattenDeep} from 'lodash'
import mm from 'micromatch'
import * as path from 'path'
import {createInterface} from 'readline'
import {parseDocument} from 'yaml'
import {ChangedFiles, ChangeTypeEnum} from './changedFiles'
import {Inputs} from './inputs'
@@ -157,7 +160,7 @@ const getFilesFromSourceFile = async ({
filePaths: string[]
excludedFiles?: boolean
}): Promise<string[]> => {
const lines = []
const lines: string[] = []
for (const filePath of filePaths) {
for await (const line of lineOfFileGenerator({filePath, excludedFiles})) {
lines.push(line)
@@ -808,10 +811,10 @@ export const getFilePatterns = async ({
if (pattern.endsWith('/')) {
return `${pattern}**`
} else {
const pathParts = pattern.split('/')
const pathParts = pattern.split(path.sep)
const lastPart = pathParts[pathParts.length - 1]
if (!lastPart.includes('.')) {
return `${pattern}/**`
return `${pattern}${path.sep}**`
} else {
return pattern
}
@@ -819,6 +822,193 @@ export const getFilePatterns = async ({
})
}
// Example YAML input:
// filesYaml: |
// frontend:
// - frontend/**
// backend:
// - backend/**
// test: test/**
// shared: &shared
// - common/**
// lib:
// - *shared
// - lib/**
// Return an Object:
// {
// frontend: ['frontend/**'],
// backend: ['backend/**'],
// test: ['test/**'],
// shared: ['common/**'],
// lib: ['common/**', 'lib/**']
// }
type YamlObject = {
[key: string]: string | string[] | [string[], string]
}
const getYamlFilePatternsFromContents = async ({
content = '',
filePath = '',
excludedFiles = false
}: {
content?: string
filePath?: string
excludedFiles?: boolean
}): Promise<Record<string, string[]>> => {
const filePatterns: Record<string, string[]> = {}
let source = ''
if (filePath) {
if (!(await exists(filePath))) {
core.error(`File does not exist: ${filePath}`)
throw new Error(`File does not exist: ${filePath}`)
}
source = await readFile(filePath, 'utf8')
} else {
source = content
}
const doc = parseDocument(source, {merge: true, schema: 'failsafe'})
if (doc.errors.length > 0) {
if (filePath) {
core.warning(`YAML errors in ${filePath}: ${doc.errors}`)
} else {
core.warning(`YAML errors: ${doc.errors}`)
}
}
if (doc.warnings.length > 0) {
if (filePath) {
core.warning(`YAML warnings in ${filePath}: ${doc.warnings}`)
} else {
core.warning(`YAML warnings: ${doc.warnings}`)
}
}
const yamlObject = doc.toJS() as YamlObject
for (const key in yamlObject) {
let value = yamlObject[key]
if (typeof value === 'string' && value.includes('\n')) {
value = value.split('\n')
}
if (typeof value === 'string') {
value = value.trim()
if (value) {
filePatterns[key] = [
excludedFiles && !value.startsWith('!') ? `!${value}` : value
]
}
} else if (Array.isArray(value)) {
filePatterns[key] = flattenDeep(value)
.filter(v => v.trim() !== '')
.map(v => {
if (excludedFiles && !v.startsWith('!')) {
v = `!${v}`
}
return v
})
}
}
return filePatterns
}
export const getYamlFilePatterns = async ({
inputs,
workingDirectory
}: {
inputs: Inputs
workingDirectory: string
}): Promise<Record<string, string[]>> => {
let filePatterns: Record<string, string[]> = {}
if (inputs.filesYaml) {
filePatterns = {
...(await getYamlFilePatternsFromContents({content: inputs.filesYaml}))
}
}
if (inputs.filesYamlFromSourceFile) {
const inputFilesYamlFromSourceFile = inputs.filesYamlFromSourceFile
.split(inputs.filesYamlFromSourceFileSeparator)
.filter(p => p !== '')
.map(p => path.join(workingDirectory, p))
core.debug(`files yaml from source file: ${inputFilesYamlFromSourceFile}`)
for (const filePath of inputFilesYamlFromSourceFile) {
const newFilePatterns = await getYamlFilePatternsFromContents({filePath})
for (const key in newFilePatterns) {
if (key in filePatterns) {
core.warning(
`files_yaml_from_source_file: Duplicated key ${key} detected in ${filePath}, the ${filePatterns[key]} will be overwritten by ${newFilePatterns[key]}.`
)
}
}
filePatterns = {
...filePatterns,
...newFilePatterns
}
}
}
if (inputs.filesIgnoreYaml) {
const newIgnoreFilePatterns = await getYamlFilePatternsFromContents({
content: inputs.filesIgnoreYaml,
excludedFiles: true
})
for (const key in newIgnoreFilePatterns) {
if (key in filePatterns) {
core.warning(
`files_ignore_yaml: Duplicated key ${key} detected, the ${filePatterns[key]} will be overwritten by ${newIgnoreFilePatterns[key]}.`
)
}
}
}
if (inputs.filesIgnoreYamlFromSourceFile) {
const inputFilesIgnoreYamlFromSourceFile =
inputs.filesIgnoreYamlFromSourceFile
.split(inputs.filesIgnoreYamlFromSourceFileSeparator)
.filter(p => p !== '')
.map(p => path.join(workingDirectory, p))
core.debug(
`files ignore yaml from source file: ${inputFilesIgnoreYamlFromSourceFile}`
)
for (const filePath of inputFilesIgnoreYamlFromSourceFile) {
const newIgnoreFilePatterns = await getYamlFilePatternsFromContents({
filePath,
excludedFiles: true
})
for (const key in newIgnoreFilePatterns) {
if (key in filePatterns) {
core.warning(
`files_ignore_yaml_from_source_file: Duplicated key ${key} detected in ${filePath}, the ${filePatterns[key]} will be overwritten by ${newIgnoreFilePatterns[key]}.`
)
}
}
filePatterns = {
...filePatterns,
...newIgnoreFilePatterns
}
}
}
return filePatterns
}
export const setOutput = async ({
key,
value,
@@ -832,7 +1022,7 @@ export const setOutput = async ({
core.setOutput(key, cleanedValue)
if (inputs.writeOutputFiles) {
const outputDir = inputs.outputDir || '.github/outputs'
const outputDir = inputs.outputDir
const extension = inputs.json ? 'json' : 'txt'
const outputFilePath = path.join(outputDir, `${key}.${extension}`)