From 8e9748af400cc7456dd9436869116850d6a83cdd Mon Sep 17 00:00:00 2001 From: Gonzalo Peci Date: Wed, 15 Jan 2025 10:47:43 +0100 Subject: [PATCH] Allow input of files --- src/main.ts | 49 +++++++++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/src/main.ts b/src/main.ts index 8320287..5f0757c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,9 @@ -import * as fs from 'fs' import * as core from '@actions/core' +import * as fs from 'fs' import * as github from '@actions/github' -import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types' -import {PushEvent, PullRequestEvent} from '@octokit/webhooks-types' +import * as jsyaml from 'js-yaml' +import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types' +import { PushEvent, PullRequestEvent } from '@octokit/webhooks-types' import { isPredicateQuantifier, @@ -12,28 +13,29 @@ import { PredicateQuantifier, SUPPORTED_PREDICATE_QUANTIFIERS } from './filter' -import {File, ChangeStatus} from './file' +import { File, ChangeStatus } from './file' import * as git from './git' -import {backslashEscape, shellEscape} from './list-format/shell-escape' -import {csvEscape} from './list-format/csv-escape' +import { backslashEscape, shellEscape } from './list-format/shell-escape' +import { csvEscape } from './list-format/csv-escape' type ExportFormat = 'none' | 'csv' | 'json' | 'shell' | 'escape' async function run(): Promise { try { - const workingDirectory = core.getInput('working-directory', {required: false}) + const workingDirectory = core.getInput('working-directory', { required: false }) if (workingDirectory) { process.chdir(workingDirectory) } - const token = core.getInput('token', {required: false}) - const ref = core.getInput('ref', {required: false}) - const base = core.getInput('base', {required: false}) - const filtersInput = core.getInput('filters', {required: true}) + const token = core.getInput('token', { required: false }) + const ref = core.getInput('ref', { required: false }) + const base = core.getInput('base', { required: false }) + const filesInput = core.getInput('files', { 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' - const initialFetchDepth = parseInt(core.getInput('initial-fetch-depth', {required: false})) || 10 - const predicateQuantifier = core.getInput('predicate-quantifier', {required: false}) || PredicateQuantifier.SOME + const listFiles = core.getInput('list-files', { required: false }).toLowerCase() || 'none' + const initialFetchDepth = parseInt(core.getInput('initial-fetch-depth', { required: false })) || 10 + const predicateQuantifier = core.getInput('predicate-quantifier', { required: false }) || PredicateQuantifier.SOME if (!isExportFormat(listFiles)) { core.setFailed(`Input parameter 'list-files' is set to invalid value '${listFiles}'`) @@ -46,10 +48,10 @@ async function run(): Promise { `'${predicateQuantifier}'. Valid values: ${SUPPORTED_PREDICATE_QUANTIFIERS.join(', ')}` throw new Error(predicateQuantifierInvalidErrorMsg) } - const filterConfig: FilterConfig = {predicateQuantifier} + const filterConfig: FilterConfig = { predicateQuantifier } const filter = new Filter(filtersYaml, filterConfig) - const files = await getChangedFiles(token, base, ref, initialFetchDepth) + const files = await getChangedFiles(filesInput, token, base, ref, initialFetchDepth) core.info(`Detected ${files.length} changed files`) const results = filter.match(files) exportResults(results, listFiles) @@ -71,10 +73,21 @@ function getConfigFileContent(configPath: string): string { throw new Error(`'${configPath}' is not a file.`) } - return fs.readFileSync(configPath, {encoding: 'utf8'}) + return fs.readFileSync(configPath, { encoding: 'utf8' }) } -async function getChangedFiles(token: string, base: string, ref: string, initialFetchDepth: number): Promise { +async function getChangedFiles( + files: string, + token: string, + base: string, + ref: string, + initialFetchDepth: number +): Promise { + if (files) { + const doc = jsyaml.load(files) as string[] + return doc.map(filename => ({ filename, status: ChangeStatus.Modified })) + } + // if base is 'HEAD' only local uncommitted changes will be detected // This is the simplest case as we don't need to fetch more commits or evaluate current/before refs if (base === git.HEAD) {