mirror of
https://github.com/dorny/paths-filter.git
synced 2025-06-08 00:59:04 +00:00
Export files matching rules (#32)
* Export files matching rules * Improve debug output * Fix PR test workflow * Always quote output path + fix PR test * Use proper single quote escaping in workflow file * Improve error handling and docs for list-files input parameter
This commit is contained in:
parent
483986d0a7
commit
3f845744aa
8 changed files with 205 additions and 125 deletions
|
@ -23,7 +23,11 @@ interface FilterRuleItem {
|
|||
matcher: minimatch.IMinimatch // Matches the filename
|
||||
}
|
||||
|
||||
export default class Filter {
|
||||
export interface FilterResults {
|
||||
[key: string]: File[]
|
||||
}
|
||||
|
||||
export class Filter {
|
||||
rules: {[key: string]: FilterRuleItem[]} = {}
|
||||
|
||||
// Creates instance of Filter and load rules from YAML if it's provided
|
||||
|
@ -49,20 +53,20 @@ export default class Filter {
|
|||
}
|
||||
}
|
||||
|
||||
// Returns dictionary with match result per rule
|
||||
match(files: File[]): {[key: string]: boolean} {
|
||||
const result: {[key: string]: boolean} = {}
|
||||
match(files: File[]): FilterResults {
|
||||
const result: FilterResults = {}
|
||||
for (const [key, patterns] of Object.entries(this.rules)) {
|
||||
const match = files.some(file =>
|
||||
patterns.some(
|
||||
rule => (rule.status === undefined || rule.status.includes(file.status)) && rule.matcher.match(file.filename)
|
||||
)
|
||||
)
|
||||
result[key] = match
|
||||
result[key] = files.filter(file => this.isMatch(file, patterns))
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
private isMatch(file: File, patterns: FilterRuleItem[]): boolean {
|
||||
return patterns.some(
|
||||
rule => (rule.status === undefined || rule.status.includes(file.status)) && rule.matcher.match(file.filename)
|
||||
)
|
||||
}
|
||||
|
||||
private parseFilterItemYaml(item: FilterItemYaml): FilterRuleItem[] {
|
||||
if (Array.isArray(item)) {
|
||||
return flat(item.map(i => this.parseFilterItemYaml(i)))
|
||||
|
|
86
src/main.ts
86
src/main.ts
|
@ -3,16 +3,12 @@ import * as core from '@actions/core'
|
|||
import * as github from '@actions/github'
|
||||
import {Webhooks} from '@octokit/webhooks'
|
||||
|
||||
import Filter from './filter'
|
||||
import {Filter, FilterResults} from './filter'
|
||||
import {File, ChangeStatus} from './file'
|
||||
import * as git from './git'
|
||||
import shellEscape from './shell-escape'
|
||||
|
||||
interface FilterResults {
|
||||
[key: string]: boolean
|
||||
}
|
||||
interface ActionOutput {
|
||||
[key: string]: string[]
|
||||
}
|
||||
type ExportFormat = 'none' | 'json' | 'shell'
|
||||
|
||||
async function run(): Promise<void> {
|
||||
try {
|
||||
|
@ -24,24 +20,23 @@ async function run(): Promise<void> {
|
|||
const token = core.getInput('token', {required: false})
|
||||
const filtersInput = core.getInput('filters', {required: true})
|
||||
const filtersYaml = isPathInput(filtersInput) ? getConfigFileContent(filtersInput) : filtersInput
|
||||
const listFiles = core.getInput('list-files', {required: false}).toLowerCase() || 'none'
|
||||
|
||||
if (!isExportFormat(listFiles)) {
|
||||
core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFiles}'`)
|
||||
return
|
||||
}
|
||||
|
||||
const filter = new Filter(filtersYaml)
|
||||
const files = await getChangedFiles(token)
|
||||
let results: FilterResults
|
||||
|
||||
if (files === null) {
|
||||
// Change detection was not possible
|
||||
core.info('All filters will be set to true.')
|
||||
results = {}
|
||||
for (const key of Object.keys(filter.rules)) {
|
||||
results[key] = true
|
||||
}
|
||||
exportNoMatchingResults(filter)
|
||||
} else {
|
||||
results = filter.match(files)
|
||||
const results = filter.match(files)
|
||||
exportResults(results, listFiles)
|
||||
}
|
||||
|
||||
exportFiles(files ?? [])
|
||||
exportResults(results)
|
||||
} catch (error) {
|
||||
core.setFailed(error.message)
|
||||
}
|
||||
|
@ -154,36 +149,45 @@ async function getChangedFilesFromApi(
|
|||
return files
|
||||
}
|
||||
|
||||
function exportFiles(files: File[]): void {
|
||||
const output: ActionOutput = {}
|
||||
output[ChangeStatus.Added] = []
|
||||
output[ChangeStatus.Deleted] = []
|
||||
output[ChangeStatus.Modified] = []
|
||||
|
||||
for (const file of files) {
|
||||
const arr = output[file.status] ?? []
|
||||
arr.push(file.filename)
|
||||
output[file.status] = arr
|
||||
}
|
||||
core.setOutput('files', output)
|
||||
|
||||
// Files grouped by status
|
||||
for (const [status, paths] of Object.entries(output)) {
|
||||
core.startGroup(`${status.toUpperCase()} files:`)
|
||||
for (const filename of paths) {
|
||||
core.info(filename)
|
||||
}
|
||||
core.endGroup()
|
||||
function exportNoMatchingResults(filter: Filter): void {
|
||||
core.info('All filters will be set to true but no matched files will be exported.')
|
||||
for (const key of Object.keys(filter.rules)) {
|
||||
core.setOutput(key, true)
|
||||
}
|
||||
}
|
||||
|
||||
function exportResults(results: FilterResults): void {
|
||||
core.startGroup('Filters results:')
|
||||
for (const [key, value] of Object.entries(results)) {
|
||||
core.info(`${key}: ${value}`)
|
||||
function exportResults(results: FilterResults, format: ExportFormat): void {
|
||||
for (const [key, files] of Object.entries(results)) {
|
||||
const value = files.length > 0
|
||||
core.startGroup(`Filter ${key} = ${value}`)
|
||||
core.info('Matching files:')
|
||||
for (const file of files) {
|
||||
core.info(`${file.filename} [${file.status}]`)
|
||||
}
|
||||
|
||||
core.setOutput(key, value)
|
||||
if (format !== 'none') {
|
||||
const filesValue = serializeExport(files, format)
|
||||
core.setOutput(`${key}_files`, filesValue)
|
||||
}
|
||||
}
|
||||
core.endGroup()
|
||||
}
|
||||
|
||||
function serializeExport(files: File[], format: ExportFormat): string {
|
||||
const fileNames = files.map(file => file.filename)
|
||||
switch (format) {
|
||||
case 'json':
|
||||
return JSON.stringify(fileNames)
|
||||
case 'shell':
|
||||
return fileNames.map(shellEscape).join(' ')
|
||||
default:
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
function isExportFormat(value: string): value is ExportFormat {
|
||||
return value === 'none' || value === 'shell' || value === 'json'
|
||||
}
|
||||
|
||||
run()
|
||||
|
|
7
src/shell-escape.ts
Normal file
7
src/shell-escape.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
// Credits to https://github.com/xxorax/node-shell-escape
|
||||
|
||||
export default function shellEscape(value: string): string {
|
||||
return `'${value.replace(/'/g, "'\\''")}'`
|
||||
.replace(/^(?:'')+/g, '') // unduplicate single-quote at the beginning
|
||||
.replace(/\\'''/g, "\\'") // remove non-escaped single-quote if there are enclosed between 2 escaped
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue