This commit is contained in:
Gonzalo Peci 2025-01-15 17:27:21 +01:00 committed by GitHub
commit 4183427d81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 15 deletions

View File

@ -1,13 +1,13 @@
name: 'Paths Changes Filter'
description: 'Execute your workflow steps only if relevant files are modified.'
author: 'Michal Dorner <dorner.michal@gmail.com>'
name: "Paths Changes Filter"
description: "Execute your workflow steps only if relevant files are modified."
author: "Michal Dorner <dorner.michal@gmail.com>"
inputs:
token:
description: 'GitHub Access Token'
description: "GitHub Access Token"
required: false
default: ${{ github.token }}
working-directory:
description: 'Relative path under $GITHUB_WORKSPACE where the repository was checked out.'
description: "Relative path under $GITHUB_WORKSPACE where the repository was checked out."
required: false
ref:
description: |
@ -20,8 +20,11 @@ inputs:
If it references same branch it was pushed to, changes are detected against the most recent commit before the push.
This option is ignored if action is triggered by pull_request event.
required: false
files:
description: "Manual list of files to filter"
required: false
filters:
description: 'Path to the configuration file or YAML string with filters definition'
description: "Path to the configuration file or YAML string with filters definition"
required: true
list-files:
description: |
@ -43,13 +46,13 @@ inputs:
until the merge-base is found or there are no more commits in the history.
This option takes effect only when changes are detected using git against different base branch.
required: false
default: '100'
default: "100"
outputs:
changes:
description: JSON array with names of all filters matching any of changed files
runs:
using: 'node20'
main: 'dist/index.js'
using: "node20"
main: "dist/index.js"
branding:
color: blue
icon: filter

14
dist/index.js vendored
View File

@ -553,9 +553,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
return result;
};
Object.defineProperty(exports, "__esModule", ({ value: true }));
const fs = __importStar(__nccwpck_require__(7147));
const core = __importStar(__nccwpck_require__(2186));
const fs = __importStar(__nccwpck_require__(7147));
const github = __importStar(__nccwpck_require__(5438));
const jsyaml = __importStar(__nccwpck_require__(1917));
const filter_1 = __nccwpck_require__(3707);
const file_1 = __nccwpck_require__(4014);
const git = __importStar(__nccwpck_require__(3374));
@ -570,6 +571,7 @@ async function run() {
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';
@ -586,7 +588,8 @@ async function run() {
}
const filterConfig = { predicateQuantifier };
const filter = new filter_1.Filter(filtersYaml, filterConfig);
const files = await getChangedFiles(token, base, ref, initialFetchDepth);
core.info(`Detected ${filesInput} files`);
const files = await getChangedFiles(filesInput, token, base, ref, initialFetchDepth);
core.info(`Detected ${files.length} changed files`);
const results = filter.match(files);
exportResults(results, listFiles);
@ -607,8 +610,13 @@ function getConfigFileContent(configPath) {
}
return fs.readFileSync(configPath, { encoding: 'utf8' });
}
async function getChangedFiles(token, base, ref, initialFetchDepth) {
async function getChangedFiles(files, token, base, ref, initialFetchDepth) {
var _a, _b;
if (files) {
core.info('Using list of files provided as input');
const doc = jsyaml.load(files);
return doc.map(filename => ({ filename, status: file_1.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) {

View File

@ -1,6 +1,7 @@
import * as fs from 'fs'
import * as core from '@actions/core'
import * as fs from 'fs'
import * as github from '@actions/github'
import * as jsyaml from 'js-yaml'
import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'
import {PushEvent, PullRequestEvent} from '@octokit/webhooks-types'
@ -29,6 +30,7 @@ async function run(): Promise<void> {
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'
@ -49,7 +51,8 @@ async function run(): Promise<void> {
const filterConfig: FilterConfig = {predicateQuantifier}
const filter = new Filter(filtersYaml, filterConfig)
const files = await getChangedFiles(token, base, ref, initialFetchDepth)
core.info(`Detected ${filesInput} files`)
const files = await getChangedFiles(filesInput, token, base, ref, initialFetchDepth)
core.info(`Detected ${files.length} changed files`)
const results = filter.match(files)
exportResults(results, listFiles)
@ -74,7 +77,19 @@ function getConfigFileContent(configPath: string): string {
return fs.readFileSync(configPath, {encoding: 'utf8'})
}
async function getChangedFiles(token: string, base: string, ref: string, initialFetchDepth: number): Promise<File[]> {
async function getChangedFiles(
files: string,
token: string,
base: string,
ref: string,
initialFetchDepth: number
): Promise<File[]> {
if (files) {
core.info('Using list of files provided as input')
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) {