Skip to content

Commit 50cd308

Browse files
authored
Merge pull request #2 from tivv/add_filter
Add filter
2 parents 6b5da6f + cfb6d45 commit 50cd308

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ Optional. Title for the check run to create. Defaults to `Checkstyle Source Code
2727
### `token`
2828
Optional. GitHub API access token. Defaults to `${{ github.token }}`, which is set by `actions/checkout@v2` minimally.
2929

30+
### `changed-since`
31+
Optional. This option allow uploading only violations in files that were changed
32+
between the current commit and the base commit. Set this parameter to base branch name or commit to
33+
skip marking violations on unchanged files. This is especially useful when project code style is changed
34+
and iterative clean-up is performed.
35+
3036
## Example usage
3137

3238
```yaml

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ inputs:
2222
Also when generating a new PAT, select the least scopes necessary.
2323
[Learn more about creating and using encrypted secrets](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/creating-and-using-encrypted-secrets)
2424
default: ${{ github.token }}
25+
changed-since:
26+
description: >
27+
This option allow uploading only violations in files that were changed
28+
between the current commit and the base commit. Set this parameter to base branch name or commit to
29+
skip marking violations on unchanged files. This is especially useful when project code style is changed
30+
and iterative clean-up is performed.
2531
runs:
2632
using: 'node12'
2733
main: 'dist/index.js'

dist/index.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ var Inputs;
113113
Inputs["Title"] = "title";
114114
Inputs["Path"] = "path";
115115
Inputs["Token"] = "token";
116+
Inputs["ChangedSince"] = "changed-since";
116117
})(Inputs = exports.Inputs || (exports.Inputs = {}));
117118

118119

@@ -184,15 +185,20 @@ function run() {
184185
const name = core.getInput(constants_1.Inputs.Name);
185186
const title = core.getInput(constants_1.Inputs.Title);
186187
const commit = core.getInput(constants_1.Inputs.Commit);
188+
const changedSince = core.getInput(constants_1.Inputs.ChangedSince);
189+
const filter = yield getFilter(commit, changedSince);
190+
core.debug(`Got the filter of ${filter} files, e.g. ${filter.slice(0, 3)}`);
187191
const searchResult = yield search_1.findResults(path);
188192
if (searchResult.filesToUpload.length === 0) {
189193
core.warning(`No files were found for the provided path: ${path}. No results will be uploaded.`);
190194
}
191195
else {
192196
core.info(`With the provided path, there will be ${searchResult.filesToUpload.length} results uploaded`);
193197
core.debug(`Root artifact directory is ${searchResult.rootDirectory}`);
194-
const annotations = ramda_1.chain(annotations_1.annotationsForPath, searchResult.filesToUpload);
195-
core.debug(`Grouping ${annotations.length} annotations into chunks of ${MAX_ANNOTATIONS_PER_REQUEST}`);
198+
const allAnnotations = ramda_1.chain(annotations_1.annotationsForPath, searchResult.filesToUpload);
199+
const annotations = filter.length == 0 ? allAnnotations :
200+
allAnnotations.filter(annotation => filter.includes(annotation.path));
201+
core.debug(`Grouping ${annotations.length} filtered out of ${allAnnotations.length} annotations into chunks of ${MAX_ANNOTATIONS_PER_REQUEST}`);
196202
const groupedAnnotations = annotations.length > MAX_ANNOTATIONS_PER_REQUEST
197203
? ramda_1.splitEvery(MAX_ANNOTATIONS_PER_REQUEST, annotations)
198204
: [annotations];
@@ -225,6 +231,20 @@ function getConclusion(annotations) {
225231
}
226232
return 'success';
227233
}
234+
function getFilter(commit, changedSince) {
235+
return __awaiter(this, void 0, void 0, function* () {
236+
if (changedSince == "") {
237+
return [];
238+
}
239+
const octokit = github_2.getOctokit(core.getInput(constants_1.Inputs.Token));
240+
const head_sha = commit ||
241+
(github_2.context.payload.pull_request && github_2.context.payload.pull_request.head.sha) ||
242+
github_2.context.sha;
243+
const req = Object.assign(Object.assign({}, github_2.context.repo), { head: head_sha, base: changedSince });
244+
const compare = yield octokit.repos.compareCommits(req);
245+
return compare.data.files.map(file => file.filename);
246+
});
247+
}
228248
function createCheck(name, commit, title, annotations, processedErrors, numErrors, conclusion) {
229249
return __awaiter(this, void 0, void 0, function* () {
230250
const head_sha = commit ||

src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export enum Inputs {
33
Commit = 'commit',
44
Title = 'title',
55
Path = 'path',
6-
Token = 'token'
6+
Token = 'token',
7+
ChangedSince = 'changed-since'
78
}

src/main.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ async function run(): Promise<void> {
1414
const name = core.getInput(Inputs.Name)
1515
const title = core.getInput(Inputs.Title)
1616
const commit = core.getInput(Inputs.Commit)
17+
const changedSince = core.getInput(Inputs.ChangedSince)
1718

19+
const filter = await getFilter(commit, changedSince)
20+
core.debug(`Got the filter of ${filter} files, e.g. ${filter.slice(0, 3)}`)
1821
const searchResult = await findResults(path)
1922
if (searchResult.filesToUpload.length === 0) {
2023
core.warning(
@@ -26,12 +29,15 @@ async function run(): Promise<void> {
2629
)
2730
core.debug(`Root artifact directory is ${searchResult.rootDirectory}`)
2831

29-
const annotations: Annotation[] = chain(
32+
const allAnnotations: Annotation[] = chain(
3033
annotationsForPath,
3134
searchResult.filesToUpload
3235
)
36+
const annotations = filter.length == 0 ? allAnnotations :
37+
allAnnotations.filter(annotation => filter.includes(annotation.path))
38+
3339
core.debug(
34-
`Grouping ${annotations.length} annotations into chunks of ${MAX_ANNOTATIONS_PER_REQUEST}`
40+
`Grouping ${annotations.length} filtered out of ${allAnnotations.length} annotations into chunks of ${MAX_ANNOTATIONS_PER_REQUEST}`
3541
)
3642

3743
const groupedAnnotations: Annotation[][] =
@@ -89,6 +95,29 @@ function getConclusion(
8995
return 'success'
9096
}
9197

98+
async function getFilter(
99+
commit: string,
100+
changedSince: string
101+
): Promise<string[]> {
102+
if (changedSince == "") {
103+
return [];
104+
}
105+
106+
const octokit = getOctokit(core.getInput(Inputs.Token))
107+
108+
const head_sha = commit ||
109+
(context.payload.pull_request && context.payload.pull_request.head.sha) ||
110+
context.sha;
111+
112+
const req = {
113+
...context.repo,
114+
head: head_sha,
115+
base: changedSince
116+
}
117+
const compare = await octokit.repos.compareCommits(req)
118+
return compare.data.files.map(file => file.filename)
119+
}
120+
92121
async function createCheck(
93122
name: string,
94123
commit: string,

0 commit comments

Comments
 (0)