This repository has been archived by the owner on Nov 18, 2024. It is now read-only.
generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfs.ts
94 lines (79 loc) · 2.16 KB
/
fs.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// import { promises as fs } from 'fs';
import * as core from '@actions/core';
import micromatch from 'micromatch';
import { fetchFilesBatchPR, fetchFilesBatchCommit } from './api';
import { Octokit, PrResponse, ActionData, ActionDataWithPR } from './types';
export async function filterFiles(
files: string[],
data: ActionData,
unused?: any
): Promise<string[]> {
const { extensions } = data.eslint;
// const result: string[] = [];
const matches = micromatch(files, [`**{${extensions.join(',')}}`]);
const include: string[] =
data.includeGlob.length > 0
? micromatch(matches, data.includeGlob)
: matches;
const ignore: string[] =
data.ignoreGlob.length > 0 ? micromatch(include, data.ignoreGlob) : [];
if (ignore.length === 0) {
return include;
}
return include.filter((file) => !ignore.includes(file));
}
async function* getFilesFromPR(
client: Octokit,
data: Omit<ActionData, 'prID'> & { prID: number },
): AsyncGenerator<string[]> {
let cursor: string | undefined = undefined;
while (true) {
try {
const result: PrResponse = await fetchFilesBatchPR(
client,
data.prID,
cursor,
);
if (!result || !result.files.length) {
break;
}
const files = await filterFiles(result.files, data);
yield files;
if (!result.hasNextPage) break;
cursor = result.endCursor;
} catch (err) {
core.error(err);
throw err;
}
}
}
async function* getFilesFromCommit(
client: Octokit,
data: ActionData,
): AsyncGenerator<string[]> {
try {
const files = await fetchFilesBatchCommit(client, data);
const filtered = await filterFiles(files, data);
while (filtered.length > 0) {
yield filtered.splice(0, 50);
}
} catch (err) {
core.error(err);
throw err;
}
}
function hasPR(data: ActionData | ActionDataWithPR): data is ActionDataWithPR {
if (data.prID) {
return true;
}
return false;
}
export async function getChangedFiles(
client: Octokit,
data: ActionData,
): Promise<AsyncGenerator<string[]>> {
if (hasPR(data)) {
return getFilesFromPR(client, data);
}
return getFilesFromCommit(client, data);
}