-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice.ts
224 lines (193 loc) · 6.05 KB
/
service.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import { defineProxyService } from "@webext-core/proxy-service";
import type { GithubApi } from "./api";
import type { DiffEntry } from "./types";
import { minimatch } from "minimatch";
import { GitAttributes } from "../gitattributes";
import { logger } from "../logger";
import { customListsStorage } from "../storage";
import { commitHashDiffsCache } from "../global-cache";
import { HOUR } from "../time";
export const [registerGithubService, getGithubService] = defineProxyService(
"GithubService",
createGithubService,
);
function createGithubService(api: GithubApi) {
/**
* Returns a list of generated files that should be excluded from diff counts.
*
* Eventually, this will be based on your .gitattributes file.
*/
async function getGitAttributes(options: {
ref: string;
repo: string;
owner: string;
}): Promise<GitAttributes | undefined> {
const text = await api.getGitAttributesFile(options);
if (text === undefined) {
logger.debug("No .gitattributes file for this repo and commit");
return undefined;
}
const gitAttributes = new GitAttributes(text);
logger.debug("Git Attributes:");
logger.debug(gitAttributes.text);
logger.debug(gitAttributes.ast);
return gitAttributes;
}
async function getPatternsFromSettings(): Promise<
Array<{ source: string; pattern: string }>
> {
const res = await customListsStorage.getValue();
const { all } = res;
return all.split("\n").map((line) => ({
pattern: line,
source: "Options: All Repos",
}));
}
/**
* Get the commit hash for the current page. Used to look up gitattributes.
*/
async function getCurrentCommit(
options: RecalculateOptions,
): Promise<string> {
if (options.type === "pr") {
const fullPr = await api.getPr(options);
logger.debug("Full PR:", fullPr);
return fullPr.head.sha;
}
if (options.type === "commit") {
const fullCommit = await api.getCommit(options);
logger.debug("Full commit:", fullCommit);
return fullCommit.sha;
}
if (options.type === "compare") {
const fullCommit = await api.getCommit({
...options,
ref: options.commitRefs[0],
});
logger.debug("Full base commit:", fullCommit);
return fullCommit.sha;
}
throw Error(
`Not implemented: getCurrentCommit(${JSON.stringify(options)})`,
);
}
function calculateDiffForFiles(files: DiffEntry[]): DiffSummary {
let changes = 0,
additions = 0,
deletions = 0;
for (const file of files) {
changes += file.changes;
additions += file.additions;
deletions += file.deletions;
}
return { changes, additions, deletions };
}
async function getChangedFiles(
options: RecalculateOptions,
): Promise<DiffEntry[]> {
if (options.type === "pr") return api.getAllPrFiles(options);
if (options.type === "commit") {
const commit = await api.getCommit(options);
return commit.files;
}
if (options.type === "compare") {
const comparison = await api.compareCommits(options);
return comparison.files;
}
throw Error(`Not implemented: getChangedFiles(${JSON.stringify(options)})`);
}
function getCacheKey(
currentRef: string,
options: RecalculateOptions,
): string {
if (options.type === "compare") {
return `${options.commitRefs[0]}...${options.commitRefs[1]}`;
}
return currentRef;
}
return {
async recalculateDiff(
options: RecalculateOptions,
): Promise<RecalculateResult> {
const ref = await getCurrentCommit(options);
const cacheKey = getCacheKey(ref, options);
const cached = await commitHashDiffsCache.get(cacheKey);
if (cached) {
logger.debug("[recalculateDiff] Using cached result");
return cached;
}
// 10s sleep for testing loading UI
// await sleep(10e3);
const [gitAttributes, changedFiles, settingsPatterns] = await Promise.all(
[
getGitAttributes({ ...options, ref }),
getChangedFiles(options),
getPatternsFromSettings(),
],
);
logger.debug(`Found ${changedFiles.length} files`);
const include: DiffEntry[] = [];
const exclude: DiffEntry[] = [];
changedFiles.forEach((diff) => {
const gitAttributesEval = gitAttributes?.evaluate(diff.filename);
const matchingSettings = settingsPatterns.filter(({ pattern }) =>
minimatch(diff.filename, pattern),
);
const isGitAttributesGenerated =
!!gitAttributesEval?.attributes["linguist-generated"];
const isSettingsGenerated = matchingSettings.length > 0;
const isGenerated = isGitAttributesGenerated || isSettingsGenerated;
logger.debug("Is generated?", diff.filename, {
isGitAttributesGenerated,
isSettingsGenerated,
isGenerated,
});
logger.debug("Git attributes evaluation:", gitAttributesEval);
logger.debug("Matched settings:", isSettingsGenerated);
if (isGenerated) exclude.push(diff);
else include.push(diff);
});
const result: RecalculateResult = {
all: calculateDiffForFiles(changedFiles),
exclude: calculateDiffForFiles(exclude),
include: calculateDiffForFiles(include),
};
await commitHashDiffsCache.set(cacheKey, result, 2 * HOUR);
return result;
},
getUser: api.getUser,
};
}
// Types
export type RecalculateOptions =
| RecalculatePrOptions
| RecalculateCommitOptions
| RecalculateCompareOptions;
export interface RecalculatePrOptions {
type: "pr";
owner: string;
repo: string;
pr: number;
}
export interface RecalculateCommitOptions {
type: "commit";
owner: string;
repo: string;
ref: string;
}
export interface RecalculateCompareOptions {
type: "compare";
owner: string;
repo: string;
commitRefs: [string, string];
}
export interface RecalculateResult {
all: DiffSummary;
include: DiffSummary;
exclude: DiffSummary;
}
export interface DiffSummary {
additions: number;
deletions: number;
changes: number;
}