forked from tinovyatkin/action-php-codesniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-changed-file.ts
80 lines (75 loc) · 2.02 KB
/
get-changed-file.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { spawn } from 'child_process';
import { createInterface } from 'readline';
import { existsSync } from 'fs';
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as Webhooks from '@octokit/webhooks';
import picomatch from 'picomatch';
interface ChangedFiles {
added: string[];
modified: string[];
}
export async function getChangedFiles(): Promise<ChangedFiles> {
const pattern = core.getInput('files', {
required: false,
});
const globs = pattern.length ? pattern.split(',') : ['**.php'];
const isMatch = picomatch(globs);
console.log('Filter patterns:', globs, isMatch('src/test.php'));
const payload = github.context
.payload as Webhooks.EventPayloads.WebhookPayloadPullRequest;
/*
getting them from Git
git diff-tree --no-commit-id --name-status --diff-filter=d -r ${{ github.event.pull_request.base.sha }}..${{ github.event.after }}
*/
try {
const git = spawn(
'git',
[
'--no-pager',
'diff-tree',
'--no-commit-id',
'--name-status',
'--diff-filter=d', // we don't need deleted files
'-r',
`${payload.pull_request.base.sha}..`,
],
{
windowsHide: true,
timeout: 5000,
}
);
const readline = createInterface({
input: git.stdout,
});
const result: ChangedFiles = {
added: [],
modified: [],
};
for await (const line of readline) {
const parsed = /^(?<status>[ACMR])[\s\t]+(?<file>\S+)$/.exec(line);
if (parsed?.groups) {
const { status, file } = parsed.groups;
// ensure file exists
if (isMatch(file) && existsSync(file)) {
switch (status) {
case 'A':
case 'C':
case 'R':
result.added.push(file);
break;
case 'M':
result.modified.push(file);
}
}
}
}
return result;
} catch (err) {
console.error(err);
return {
added: [],
modified: [],
};
}
}