Skip to content

Commit ffc1f76

Browse files
authored
Merge pull request #6 from worksome/feature/ignored-files
feat: add support to filter specific files
2 parents cfb195f + ca8d07e commit ffc1f76

6 files changed

Lines changed: 94 additions & 9 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Enable checkboxes when listing the files in the comment. The default value is `f
2424

2525
Enable the inclusion of deleted files when checking for code owners. The default value is `false`.
2626

27+
### `ignoredFiles`
28+
29+
Ignore specific files using a new-line-separated list of files.
30+
2731
### `token`
2832

2933
The token used to authenticate with GitHub and post the comments on the PR. Defaults to `github.token`.
@@ -49,6 +53,9 @@ The output comment that should be posted on the pull request.
4953
commentSuffix: ''
5054
checkboxes: true
5155
includeDeleted: false
56+
ignoredFiles: |
57+
README.md
58+
composer.json
5259
token: ${{ github.token }}
5360
```
5461
@@ -65,5 +72,8 @@ The output comment that should be posted on the pull request.
6572
commentSuffix: ''
6673
checkboxes: true
6774
includeDeleted: false
75+
ignoredFiles: |
76+
README.md
77+
composer.json
6878
token: ${{ github.token }}
6979
```

__tests__/enforcer.test.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { checkFiles, generateIgnore } from '../src/enforcer'
1+
import { checkFiles, generateIgnore, getIgnoredFiles } from '../src/enforcer'
22

33
import * as core from '@actions/core'
44
import ignore, { Ignore } from 'ignore'
@@ -33,3 +33,14 @@ describe('checkFiles', () => {
3333
expect(result).toEqual(['bar.txt'])
3434
})
3535
})
36+
37+
describe('ignoredFiles', () => {
38+
it('returns an empty array when all files have a code owner', async () => {
39+
const result = await getIgnoredFiles(`
40+
foo.txt
41+
bar.txt
42+
`)
43+
44+
expect(result).toEqual(['foo.txt', 'bar.txt'])
45+
})
46+
})

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ inputs:
1818
includeDeleted:
1919
description: 'Include deleted files.'
2020
default: false
21+
ignoredFiles:
22+
description: 'A new-line-separated list of ignored files.'
23+
default: ''
2124
token:
2225
description: 'The token used to authenticate with GitHub and post the comments on the PR.'
2326
default: ${{ github.server_url == 'https://github.com' && github.token || '' }}

composite/with-comment/action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ inputs:
1818
includeDeleted:
1919
description: 'Include deleted files.'
2020
default: false
21+
ignoredFiles:
22+
description: 'A new-line-separated list of ignored files.'
23+
default: ''
2124
custom-comment-identifier:
2225
description: 'A string that will be used to identify existing comments to update.'
2326
default: 'CODEOWNERS ACTION'
@@ -39,6 +42,9 @@ runs:
3942
commentSuffix: |
4043
${{ inputs.commentSuffix }}
4144
checkboxes: ${{ inputs.checkboxes }}
45+
includeDeleted: ${{ inputs.includeDeleted }}
46+
ignoredFiles: |
47+
${{ inputs.ignoredFiles }}
4248
token: ${{ inputs.token }}
4349

4450
- name: Find Existing Comment

dist/index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4242
return (mod && mod.__esModule) ? mod : { "default": mod };
4343
};
4444
Object.defineProperty(exports, "__esModule", ({ value: true }));
45-
exports.postComment = exports.checkFiles = exports.generateIgnore = exports.run = void 0;
45+
exports.postComment = exports.checkFiles = exports.getIgnoredFiles = exports.generateIgnore = exports.run = void 0;
4646
const fs_1 = __nccwpck_require__(7147);
4747
const core = __importStar(__nccwpck_require__(2186));
4848
const github = __importStar(__nccwpck_require__(5438));
@@ -56,6 +56,7 @@ function run() {
5656
const commentSuffix = core.getInput('commentSuffix') || '';
5757
const checkboxes = core.getBooleanInput('checkboxes') || false;
5858
const includeDeleted = core.getBooleanInput('includeDeleted') || false;
59+
const ignoredFiles = yield getIgnoredFiles(core.getInput('ignoredFiles') || '');
5960
const prNumber = getPrNumber();
6061
if (!prNumber) {
6162
console.log('Could not get pull request number from context, exiting');
@@ -66,6 +67,7 @@ function run() {
6667
const changedFiles = yield getChangedFiles(client, prNumber, includeDeleted);
6768
const ignored = (0, ignore_1.default)();
6869
generateIgnore(ignored, codeOwnersPath);
70+
ignoredFiles.forEach((ignoredFile) => ignored.add(ignoredFile));
6971
const result = yield checkFiles(ignored, changedFiles);
7072
if (result.length !== 0) {
7173
yield postComment(result, checkboxes, commentPrefix, commentSuffix);
@@ -103,13 +105,36 @@ function getChangedFiles(client, prNumber, includeDeleted) {
103105
const changedFiles = listFilesResponse
104106
.filter((f) => includeDeleted || f.status !== 'deleted')
105107
.map((f) => f.filename);
106-
core.debug('Found changed files:');
107-
for (const file of changedFiles) {
108-
core.debug(` ${file}`);
108+
if (changedFiles.length > 0) {
109+
core.debug('Found changed files:');
110+
for (const file of changedFiles) {
111+
core.debug(` ${file}`);
112+
}
113+
}
114+
else {
115+
core.debug('No changed files were found.');
109116
}
110117
return changedFiles;
111118
});
112119
}
120+
function getIgnoredFiles(ignoredLinesList) {
121+
return __awaiter(this, void 0, void 0, function* () {
122+
const ignoredFiles = ignoredLinesList
123+
.split('\n')
124+
.filter((ignoredFile) => ignoredFile.trim().length > 0);
125+
if (ignoredFiles.length > 0) {
126+
core.debug('Ignoring files:');
127+
for (const file of ignoredFiles) {
128+
core.debug(` ${file}`);
129+
}
130+
}
131+
else {
132+
core.debug('No ignored files were found.');
133+
}
134+
return ignoredFiles;
135+
});
136+
}
137+
exports.getIgnoredFiles = getIgnoredFiles;
113138
function checkFiles(ig, changedFiles) {
114139
return __awaiter(this, void 0, void 0, function* () {
115140
const failedList = [];

src/enforcer.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { existsSync, readFileSync } from 'fs'
1+
import { existsSync, readFileSync, createReadStream } from 'fs'
22
import * as core from '@actions/core'
33
import * as github from '@actions/github'
44
import ignore, { Ignore } from 'ignore'
5+
import * as readline from 'readline'
56

67
type ClientType = ReturnType<typeof github.getOctokit>
78

@@ -16,6 +17,10 @@ export async function run(): Promise<void> {
1617
const includeDeleted: boolean =
1718
core.getBooleanInput('includeDeleted') || false
1819

20+
const ignoredFiles: string[] = await getIgnoredFiles(
21+
core.getInput('ignoredFiles') || ''
22+
)
23+
1924
const prNumber = getPrNumber()
2025
if (!prNumber) {
2126
console.log('Could not get pull request number from context, exiting')
@@ -35,6 +40,8 @@ export async function run(): Promise<void> {
3540

3641
generateIgnore(ignored, codeOwnersPath)
3742

43+
ignoredFiles.forEach((ignoredFile: string) => ignored.add(ignoredFile))
44+
3845
const result = await checkFiles(ignored, changedFiles)
3946

4047
if (result.length !== 0) {
@@ -79,14 +86,37 @@ async function getChangedFiles(
7986
.filter((f: any) => includeDeleted || f.status !== 'deleted')
8087
.map((f: any) => f.filename)
8188

82-
core.debug('Found changed files:')
83-
for (const file of changedFiles) {
84-
core.debug(` ${file}`)
89+
if (changedFiles.length > 0) {
90+
core.debug('Found changed files:')
91+
for (const file of changedFiles) {
92+
core.debug(` ${file}`)
93+
}
94+
} else {
95+
core.debug('No changed files were found.')
8596
}
8697

8798
return changedFiles
8899
}
89100

101+
export async function getIgnoredFiles(
102+
ignoredLinesList: string
103+
): Promise<string[]> {
104+
const ignoredFiles: string[] = ignoredLinesList
105+
.split('\n')
106+
.filter((ignoredFile: string) => ignoredFile.trim().length > 0)
107+
108+
if (ignoredFiles.length > 0) {
109+
core.debug('Ignoring files:')
110+
for (const file of ignoredFiles) {
111+
core.debug(` ${file}`)
112+
}
113+
} else {
114+
core.debug('No ignored files were found.')
115+
}
116+
117+
return ignoredFiles
118+
}
119+
90120
export async function checkFiles(
91121
ig: Ignore,
92122
changedFiles: string[]

0 commit comments

Comments
 (0)