mirror of
https://github.com/dorny/paths-filter.git
synced 2026-06-30 09:51:40 +00:00
Compare commits
4 Commits
a2f17f45cf
...
3a17995bc0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a17995bc0 | ||
|
|
f3ceefdc7e | ||
|
|
61f87a10cd | ||
|
|
8ec3b44912 |
38
README.md
38
README.md
@ -151,6 +151,13 @@ For more information, see [CHANGELOG](https://github.com/dorny/paths-filter/blob
|
||||
# Default: none
|
||||
list-files: ''
|
||||
|
||||
# Enables writing the lists of matching files to a corresponding file.
|
||||
# If set, the action will create the specified file with the list of matching files.
|
||||
# The file will be written in the format specified by the `list-files` option and named
|
||||
# after the filter. The path to the file will be relative to the working directory and
|
||||
# exported as an output variable named `<filter-name>_files_path`.
|
||||
write-to-files: ''
|
||||
|
||||
# Relative path under $GITHUB_WORKSPACE where the repository was checked out.
|
||||
working-directory: ''
|
||||
|
||||
@ -520,6 +527,37 @@ jobs:
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>Handle large change sets (2000+ files)</summary>
|
||||
|
||||
```yaml
|
||||
- uses: dorny/paths-filter@v3
|
||||
id: changed
|
||||
with:
|
||||
# Enable writing the files matching each filter to the disk in addition to the output '<filter_name>_files'.
|
||||
# The path for each filter's file is output in the format '<filter_name>_files_path'.
|
||||
write-to-files: true
|
||||
list-files: json
|
||||
filters: |
|
||||
content:
|
||||
- 'content/**'
|
||||
|
||||
|
||||
- name: List changed directories relative to the base directory
|
||||
shell: bash
|
||||
env:
|
||||
BASE_DIR: ${{ inputs.base-directory }}
|
||||
CHANGED_CONTENT_FILES_PATH: ${{ steps.changed.outputs.content_files_path }}
|
||||
run: |
|
||||
CHANGED_CONTENT_DIRECTORIES=$(cat "${CHANGED_CONTENT_FILES_PATH}" | xargs -n1 realpath -m --relative-to=${BASE_DIR} | cut -f1 -d / | sort -u)
|
||||
for d in $CHANGED_CONTENT_DIRECTORIES
|
||||
do
|
||||
echo "Content directory change detected: ${d}"
|
||||
done
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
### Custom processing of changed files
|
||||
|
||||
<details>
|
||||
|
||||
@ -36,6 +36,13 @@ inputs:
|
||||
Backslash escapes every potentially unsafe character.
|
||||
required: false
|
||||
default: none
|
||||
write-to-files:
|
||||
description: |
|
||||
Enables writing the lists of matching files to a corresponding file in addition to the output '<filter-name>_files'.
|
||||
If set, the action will create the specified file with the list of matching files.
|
||||
The file will be written in the format specified by the `list-files` option and named
|
||||
after the filter. The path to the file will be output as a variable named `<filter-name>_files_path`.
|
||||
required: false
|
||||
initial-fetch-depth:
|
||||
description: |
|
||||
How many commits are initially fetched from base branch.
|
||||
|
||||
34
dist/index.js
vendored
34
dist/index.js
vendored
@ -363,8 +363,8 @@ exports.listAllFilesAsAdded = listAllFilesAsAdded;
|
||||
async function getCurrentRef() {
|
||||
core.startGroup(`Get current git ref`);
|
||||
try {
|
||||
const branch = (await (0, exec_1.getExecOutput)('git', ['branch', '--show-current'])).stdout.trim();
|
||||
if (branch) {
|
||||
const branch = (await (0, exec_1.getExecOutput)('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).stdout.trim();
|
||||
if (branch && branch !== 'HEAD') {
|
||||
return branch;
|
||||
}
|
||||
const describe = await (0, exec_1.getExecOutput)('git', ['describe', '--tags', '--exact-match'], { ignoreReturnCode: true });
|
||||
@ -552,15 +552,20 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
||||
__setModuleDefault(result, mod);
|
||||
return result;
|
||||
};
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", ({ value: true }));
|
||||
const fs = __importStar(__nccwpck_require__(7147));
|
||||
const core = __importStar(__nccwpck_require__(2186));
|
||||
const github = __importStar(__nccwpck_require__(5438));
|
||||
const path_1 = __importDefault(__nccwpck_require__(1017));
|
||||
const filter_1 = __nccwpck_require__(3707);
|
||||
const file_1 = __nccwpck_require__(4014);
|
||||
const git = __importStar(__nccwpck_require__(3374));
|
||||
const shell_escape_1 = __nccwpck_require__(4613);
|
||||
const csv_escape_1 = __nccwpck_require__(7402);
|
||||
const fs_1 = __nccwpck_require__(7147);
|
||||
async function run() {
|
||||
try {
|
||||
const workingDirectory = core.getInput('working-directory', { required: false });
|
||||
@ -573,6 +578,7 @@ async function run() {
|
||||
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 writeToFiles = core.getInput('write-to-files', { required: false }).toLowerCase() === 'true';
|
||||
const initialFetchDepth = parseInt(core.getInput('initial-fetch-depth', { required: false })) || 10;
|
||||
const predicateQuantifier = core.getInput('predicate-quantifier', { required: false }) || filter_1.PredicateQuantifier.SOME;
|
||||
if (!isExportFormat(listFiles)) {
|
||||
@ -589,7 +595,7 @@ async function run() {
|
||||
const files = await getChangedFiles(token, base, ref, initialFetchDepth);
|
||||
core.info(`Detected ${files.length} changed files`);
|
||||
const results = filter.match(files);
|
||||
exportResults(results, listFiles);
|
||||
exportResults(results, listFiles, writeToFiles);
|
||||
}
|
||||
catch (error) {
|
||||
core.setFailed(getErrorMessage(error));
|
||||
@ -640,7 +646,7 @@ async function getChangedFiles(token, base, ref, initialFetchDepth) {
|
||||
// At the same time we don't want to fetch any code from forked repository
|
||||
throw new Error(`'token' input parameter is required if action is triggered by 'pull_request_target' event`);
|
||||
}
|
||||
core.info('Github token is not available - changes will be detected using git diff');
|
||||
core.info('GitHub token is not available - changes will be detected using git diff');
|
||||
const baseSha = (_a = github.context.payload.pull_request) === null || _a === void 0 ? void 0 : _a.base.sha;
|
||||
const defaultBranch = (_b = github.context.payload.repository) === null || _b === void 0 ? void 0 : _b.default_branch;
|
||||
const currentRef = await git.getCurrentRef();
|
||||
@ -710,7 +716,7 @@ async function getChangedFilesFromGit(base, head, initialFetchDepth) {
|
||||
}
|
||||
// Uses github REST api to get list of files changed in PR
|
||||
async function getChangedFilesFromApi(token, pullRequest) {
|
||||
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`);
|
||||
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from GitHub API`);
|
||||
try {
|
||||
const client = github.getOctokit(token);
|
||||
const per_page = 100;
|
||||
@ -758,13 +764,14 @@ async function getChangedFilesFromApi(token, pullRequest) {
|
||||
core.endGroup();
|
||||
}
|
||||
}
|
||||
function exportResults(results, format) {
|
||||
function exportResults(results, format, writeToFiles) {
|
||||
const tempDir = (0, fs_1.mkdtempSync)(path_1.default.join(process.cwd(), 'paths-filter-'));
|
||||
core.info('Results:');
|
||||
const changes = [];
|
||||
for (const [key, files] of Object.entries(results)) {
|
||||
const value = files.length > 0;
|
||||
core.startGroup(`Filter ${key} = ${value}`);
|
||||
if (files.length > 0) {
|
||||
const match = files.length > 0;
|
||||
core.startGroup(`Filter ${key} = ${match}`);
|
||||
if (match) {
|
||||
changes.push(key);
|
||||
core.info('Matching files:');
|
||||
for (const file of files) {
|
||||
@ -774,11 +781,18 @@ function exportResults(results, format) {
|
||||
else {
|
||||
core.info('Matching files: none');
|
||||
}
|
||||
core.setOutput(key, value);
|
||||
core.setOutput(key, match);
|
||||
core.setOutput(`${key}_count`, files.length);
|
||||
if (format !== 'none') {
|
||||
const filesValue = serializeExport(files, format);
|
||||
core.setOutput(`${key}_files`, filesValue);
|
||||
if (writeToFiles) {
|
||||
const ext = format === 'json' ? 'json' : 'txt';
|
||||
const filePath = path_1.default.join(tempDir, `${key}-files.${ext}`);
|
||||
fs.writeFileSync(filePath, filesValue);
|
||||
core.info(`Matching files list for filter '${key}' written to '${filePath}'`);
|
||||
core.setOutput(`${key}_files_path`, filePath);
|
||||
}
|
||||
}
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
@ -166,8 +166,8 @@ export async function listAllFilesAsAdded(): Promise<File[]> {
|
||||
export async function getCurrentRef(): Promise<string> {
|
||||
core.startGroup(`Get current git ref`)
|
||||
try {
|
||||
const branch = (await getExecOutput('git', ['branch', '--show-current'])).stdout.trim()
|
||||
if (branch) {
|
||||
const branch = (await getExecOutput('git', ['rev-parse', '--abbrev-ref', 'HEAD'])).stdout.trim()
|
||||
if (branch && branch !== 'HEAD') {
|
||||
return branch
|
||||
}
|
||||
|
||||
|
||||
30
src/main.ts
30
src/main.ts
@ -1,6 +1,7 @@
|
||||
import * as fs from 'fs'
|
||||
import * as core from '@actions/core'
|
||||
import * as github from '@actions/github'
|
||||
import path from 'path'
|
||||
import {GetResponseDataTypeFromEndpointMethod} from '@octokit/types'
|
||||
import {MergeGroupEvent, PullRequest, PushEvent} from '@octokit/webhooks-types'
|
||||
|
||||
@ -16,6 +17,7 @@ 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 {mkdtempSync} from 'fs'
|
||||
|
||||
type ExportFormat = 'none' | 'csv' | 'json' | 'shell' | 'escape'
|
||||
|
||||
@ -32,6 +34,7 @@ async function run(): Promise<void> {
|
||||
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 writeToFiles = core.getInput('write-to-files', {required: false}).toLowerCase() === 'true'
|
||||
const initialFetchDepth = parseInt(core.getInput('initial-fetch-depth', {required: false})) || 10
|
||||
const predicateQuantifier = core.getInput('predicate-quantifier', {required: false}) || PredicateQuantifier.SOME
|
||||
|
||||
@ -52,7 +55,7 @@ async function run(): Promise<void> {
|
||||
const files = await getChangedFiles(token, base, ref, initialFetchDepth)
|
||||
core.info(`Detected ${files.length} changed files`)
|
||||
const results = filter.match(files)
|
||||
exportResults(results, listFiles)
|
||||
exportResults(results, listFiles, writeToFiles)
|
||||
} catch (error) {
|
||||
core.setFailed(getErrorMessage(error))
|
||||
}
|
||||
@ -107,7 +110,7 @@ async function getChangedFiles(token: string, base: string, ref: string, initial
|
||||
// At the same time we don't want to fetch any code from forked repository
|
||||
throw new Error(`'token' input parameter is required if action is triggered by 'pull_request_target' event`)
|
||||
}
|
||||
core.info('Github token is not available - changes will be detected using git diff')
|
||||
core.info('GitHub token is not available - changes will be detected using git diff')
|
||||
const baseSha = github.context.payload.pull_request?.base.sha
|
||||
const defaultBranch = github.context.payload.repository?.default_branch
|
||||
const currentRef = await git.getCurrentRef()
|
||||
@ -194,7 +197,7 @@ async function getChangedFilesFromGit(base: string, head: string, initialFetchDe
|
||||
|
||||
// Uses github REST api to get list of files changed in PR
|
||||
async function getChangedFilesFromApi(token: string, pullRequest: PullRequest): Promise<File[]> {
|
||||
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from Github API`)
|
||||
core.startGroup(`Fetching list of changed files for PR#${pullRequest.number} from GitHub API`)
|
||||
try {
|
||||
const client = github.getOctokit(token)
|
||||
const per_page = 100
|
||||
@ -246,13 +249,15 @@ async function getChangedFilesFromApi(token: string, pullRequest: PullRequest):
|
||||
}
|
||||
}
|
||||
|
||||
function exportResults(results: FilterResults, format: ExportFormat): void {
|
||||
function exportResults(results: FilterResults, format: ExportFormat, writeToFiles: boolean): void {
|
||||
const tempDir = mkdtempSync(path.join(process.cwd(), 'paths-filter-'))
|
||||
|
||||
core.info('Results:')
|
||||
const changes = []
|
||||
for (const [key, files] of Object.entries(results)) {
|
||||
const value = files.length > 0
|
||||
core.startGroup(`Filter ${key} = ${value}`)
|
||||
if (files.length > 0) {
|
||||
const match = files.length > 0
|
||||
core.startGroup(`Filter ${key} = ${match}`)
|
||||
if (match) {
|
||||
changes.push(key)
|
||||
core.info('Matching files:')
|
||||
for (const file of files) {
|
||||
@ -262,12 +267,21 @@ function exportResults(results: FilterResults, format: ExportFormat): void {
|
||||
core.info('Matching files: none')
|
||||
}
|
||||
|
||||
core.setOutput(key, value)
|
||||
core.setOutput(key, match)
|
||||
core.setOutput(`${key}_count`, files.length)
|
||||
if (format !== 'none') {
|
||||
const filesValue = serializeExport(files, format)
|
||||
core.setOutput(`${key}_files`, filesValue)
|
||||
|
||||
if (writeToFiles) {
|
||||
const ext = format === 'json' ? 'json' : 'txt'
|
||||
const filePath = path.join(tempDir, `${key}-files.${ext}`)
|
||||
fs.writeFileSync(filePath, filesValue)
|
||||
core.info(`Matching files list for filter '${key}' written to '${filePath}'`)
|
||||
core.setOutput(`${key}_files_path`, filePath)
|
||||
}
|
||||
}
|
||||
|
||||
core.endGroup()
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user