diff --git a/README.md b/README.md index eadaf87..75f32df 100644 --- a/README.md +++ b/README.md @@ -324,7 +324,8 @@ jobs: runs-on: ubuntu-latest # Required permissions permissions: - pull-requests: read + contents: read # required by actions/checkout + pull-requests: read # required by dorny/paths-filter steps: - uses: actions/checkout@v4 - uses: dorny/paths-filter@v3 diff --git a/__tests__/outputs.test.ts b/__tests__/outputs.test.ts new file mode 100644 index 0000000..cd8269d --- /dev/null +++ b/__tests__/outputs.test.ts @@ -0,0 +1,46 @@ +import {ChangeStatus} from '../src/file' +import {exportResults} from '../src/main' +import * as core from '@actions/core' + +jest.mock('@actions/core', () => ({ + setOutput: jest.fn(), + startGroup: jest.fn(), + endGroup: jest.fn(), + info: jest.fn() +})) + +describe('all_changed and any_changed outputs', () => { + beforeEach(() => { + jest.clearAllMocks() + }) + + test('reports true when all filters changed', () => { + const results = { + src: [{filename: 'src/file.ts', status: ChangeStatus.Modified}], + docs: [{filename: 'docs/readme.md', status: ChangeStatus.Added}] + } + exportResults(results, 'none') + expect(core.setOutput).toHaveBeenCalledWith('all_changed', true) + expect(core.setOutput).toHaveBeenCalledWith('any_changed', true) + }) + + test('reports false for all_changed when some filters unchanged', () => { + const results = { + src: [{filename: 'src/file.ts', status: ChangeStatus.Modified}], + docs: [] + } + exportResults(results, 'none') + expect(core.setOutput).toHaveBeenCalledWith('all_changed', false) + expect(core.setOutput).toHaveBeenCalledWith('any_changed', true) + }) + + test('reports false for any_changed when no filter changed', () => { + const results = { + src: [], + docs: [] + } + exportResults(results, 'none') + expect(core.setOutput).toHaveBeenCalledWith('all_changed', true) + expect(core.setOutput).toHaveBeenCalledWith('any_changed', false) + }) +}) diff --git a/action.yml b/action.yml index 8513f57..14ba243 100644 --- a/action.yml +++ b/action.yml @@ -47,9 +47,19 @@ inputs: predicate-quantifier: description: | Optional parameter to override the default behavior of file matching algorithm. - By default, files that match at least one pattern defined by the filters will be included. - This parameter allows overriding the "at least one" behavior so that all patterns must match. - Supported values are 'some' and 'every'. + By default files that match at least one pattern defined by the filters will be included. + This parameter allows to override the "at least one pattern" behavior to make it so that + all of the patterns have to match or otherwise the file is excluded. + An example scenario where this is useful if you would like to match all + .ts files in a sub-directory but not .md files. + The filters below will match markdown files despite the exclusion syntax UNLESS + you specify 'every' as the predicate-quantifier parameter. When you do that, + it will only match the .ts files in the subdirectory as expected. + + backend: + - 'pkg/a/b/c/**' + - '!**/*.jpeg' + - '!**/*.md' required: false default: 'some' outputs: diff --git a/dist/index.js b/dist/index.js index b136b8c..d39ee04 100644 --- a/dist/index.js +++ b/dist/index.js @@ -564,6 +564,7 @@ var __importStar = (this && this.__importStar) || function (mod) { return result; }; Object.defineProperty(exports, "__esModule", ({ value: true })); +exports.run = exports.exportResults = void 0; const fs = __importStar(__nccwpck_require__(7147)); const core = __importStar(__nccwpck_require__(2186)); const github = __importStar(__nccwpck_require__(5438)); @@ -606,6 +607,7 @@ async function run() { core.setFailed(getErrorMessage(error)); } } +exports.run = run; function isPathInput(text) { return !(text.includes('\n') || text.includes(':')); } @@ -793,6 +795,7 @@ function exportResults(results, format) { core.info('Cannot set changes output variable - name already used by filter output'); } } +exports.exportResults = exportResults; function serializeExport(files, format) { const fileNames = files.map(file => file.filename); switch (format) { @@ -816,7 +819,9 @@ function getErrorMessage(error) { return error.message; return String(error); } -run(); +if (require.main === require.cache[eval('__filename')]) { + run(); +} /***/ }), diff --git a/src/main.ts b/src/main.ts index f620560..1288206 100644 --- a/src/main.ts +++ b/src/main.ts @@ -228,7 +228,7 @@ async function getChangedFilesFromApi(token: string, pullRequest: PullRequestEve } } -function exportResults(results: FilterResults, format: ExportFormat): void { +export function exportResults(results: FilterResults, format: ExportFormat): void { core.info('Results:') const changes: string[] = [] let anyChanged = false @@ -295,4 +295,8 @@ function getErrorMessage(error: unknown): string { return String(error) } -run() +if (require.main === module) { + run() +} + +export {run}