@flakiness/sdk 0.148.0 → 0.149.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +95 -2
- package/lib/{httpUtils.js → _internalUtils.js} +70 -13
- package/lib/_stable-hash.d.js +1 -0
- package/lib/browser.js +154 -0
- package/lib/ciUtils.js +42 -0
- package/lib/createEnvironment.js +92 -1
- package/lib/createTestStepSnippets.js +17 -122
- package/lib/flakinessProjectConfig.js +368 -25
- package/lib/gitWorktree.js +312 -0
- package/lib/index.js +827 -514
- package/lib/normalizeReport.js +114 -0
- package/lib/reportUtils.js +382 -111
- package/lib/reportUtilsBrowser.js +138 -0
- package/lib/showReport.js +401 -47
- package/lib/stripAnsi.js +9 -0
- package/lib/systemUtilizationSampler.js +21 -0
- package/lib/{reportUploader.js → uploadReport.js} +67 -63
- package/lib/visitTests.js +19 -0
- package/lib/writeReport.js +31 -0
- package/package.json +5 -7
- package/types/tsconfig.tsbuildinfo +1 -1
- package/lib/browser/index.js +0 -127
- package/lib/git.js +0 -55
- package/lib/localGit.js +0 -48
- package/lib/pathutils.js +0 -20
- package/lib/utils.js +0 -44
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// src/gitWorktree.ts
|
|
2
|
+
import assert from "assert";
|
|
3
|
+
import { exec } from "child_process";
|
|
4
|
+
import debug from "debug";
|
|
5
|
+
import { posix as posixPath, win32 as win32Path } from "path";
|
|
6
|
+
import { promisify } from "util";
|
|
7
|
+
|
|
8
|
+
// src/_internalUtils.ts
|
|
9
|
+
import { spawnSync } from "child_process";
|
|
10
|
+
import http from "http";
|
|
11
|
+
import https from "https";
|
|
12
|
+
import util from "util";
|
|
13
|
+
import zlib from "zlib";
|
|
14
|
+
var asyncBrotliCompress = util.promisify(zlib.brotliCompress);
|
|
15
|
+
var FLAKINESS_DBG = !!process.env.FLAKINESS_DBG;
|
|
16
|
+
function errorText(error) {
|
|
17
|
+
return FLAKINESS_DBG ? error.stack : error.message;
|
|
18
|
+
}
|
|
19
|
+
async function retryWithBackoff(job, backoff = []) {
|
|
20
|
+
for (const timeout of backoff) {
|
|
21
|
+
try {
|
|
22
|
+
return await job();
|
|
23
|
+
} catch (e) {
|
|
24
|
+
if (e instanceof AggregateError)
|
|
25
|
+
console.error(`[flakiness.io err]`, errorText(e.errors[0]));
|
|
26
|
+
else if (e instanceof Error)
|
|
27
|
+
console.error(`[flakiness.io err]`, errorText(e));
|
|
28
|
+
else
|
|
29
|
+
console.error(`[flakiness.io err]`, e);
|
|
30
|
+
await new Promise((x) => setTimeout(x, timeout));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return await job();
|
|
34
|
+
}
|
|
35
|
+
var httpUtils;
|
|
36
|
+
((httpUtils2) => {
|
|
37
|
+
function createRequest({ url, method = "get", headers = {} }) {
|
|
38
|
+
let resolve;
|
|
39
|
+
let reject;
|
|
40
|
+
const responseDataPromise = new Promise((a, b) => {
|
|
41
|
+
resolve = a;
|
|
42
|
+
reject = b;
|
|
43
|
+
});
|
|
44
|
+
const protocol = url.startsWith("https") ? https : http;
|
|
45
|
+
headers = Object.fromEntries(Object.entries(headers).filter(([key, value]) => value !== void 0));
|
|
46
|
+
const request = protocol.request(url, { method, headers }, (res) => {
|
|
47
|
+
const chunks = [];
|
|
48
|
+
res.on("data", (chunk) => chunks.push(chunk));
|
|
49
|
+
res.on("end", () => {
|
|
50
|
+
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300)
|
|
51
|
+
resolve(Buffer.concat(chunks));
|
|
52
|
+
else
|
|
53
|
+
reject(new Error(`Request to ${url} failed with ${res.statusCode}`));
|
|
54
|
+
});
|
|
55
|
+
res.on("error", (error) => reject(error));
|
|
56
|
+
});
|
|
57
|
+
request.on("error", reject);
|
|
58
|
+
return { request, responseDataPromise };
|
|
59
|
+
}
|
|
60
|
+
httpUtils2.createRequest = createRequest;
|
|
61
|
+
async function getBuffer(url, backoff) {
|
|
62
|
+
return await retryWithBackoff(async () => {
|
|
63
|
+
const { request, responseDataPromise } = createRequest({ url });
|
|
64
|
+
request.end();
|
|
65
|
+
return await responseDataPromise;
|
|
66
|
+
}, backoff);
|
|
67
|
+
}
|
|
68
|
+
httpUtils2.getBuffer = getBuffer;
|
|
69
|
+
async function getText(url, backoff) {
|
|
70
|
+
const buffer = await getBuffer(url, backoff);
|
|
71
|
+
return buffer.toString("utf-8");
|
|
72
|
+
}
|
|
73
|
+
httpUtils2.getText = getText;
|
|
74
|
+
async function getJSON(url) {
|
|
75
|
+
return JSON.parse(await getText(url));
|
|
76
|
+
}
|
|
77
|
+
httpUtils2.getJSON = getJSON;
|
|
78
|
+
async function postText(url, text, backoff) {
|
|
79
|
+
const headers = {
|
|
80
|
+
"Content-Type": "application/json",
|
|
81
|
+
"Content-Length": Buffer.byteLength(text) + ""
|
|
82
|
+
};
|
|
83
|
+
return await retryWithBackoff(async () => {
|
|
84
|
+
const { request, responseDataPromise } = createRequest({ url, headers, method: "post" });
|
|
85
|
+
request.write(text);
|
|
86
|
+
request.end();
|
|
87
|
+
return await responseDataPromise;
|
|
88
|
+
}, backoff);
|
|
89
|
+
}
|
|
90
|
+
httpUtils2.postText = postText;
|
|
91
|
+
async function postJSON(url, json, backoff) {
|
|
92
|
+
const buffer = await postText(url, JSON.stringify(json), backoff);
|
|
93
|
+
return JSON.parse(buffer.toString("utf-8"));
|
|
94
|
+
}
|
|
95
|
+
httpUtils2.postJSON = postJSON;
|
|
96
|
+
})(httpUtils || (httpUtils = {}));
|
|
97
|
+
function shell(command, args, options) {
|
|
98
|
+
try {
|
|
99
|
+
const result = spawnSync(command, args, { encoding: "utf-8", ...options });
|
|
100
|
+
if (result.status !== 0) {
|
|
101
|
+
return void 0;
|
|
102
|
+
}
|
|
103
|
+
return result.stdout.trim();
|
|
104
|
+
} catch (e) {
|
|
105
|
+
console.error(e);
|
|
106
|
+
return void 0;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/gitWorktree.ts
|
|
111
|
+
var log = debug("fk:git");
|
|
112
|
+
var execAsync = promisify(exec);
|
|
113
|
+
var IS_WIN32_PATH = new RegExp("^[a-zA-Z]:\\\\", "i");
|
|
114
|
+
var IS_ALMOST_POSIX_PATH = new RegExp("^[a-zA-Z]:/", "i");
|
|
115
|
+
function toPosixAbsolutePath(absolutePath) {
|
|
116
|
+
if (IS_WIN32_PATH.test(absolutePath)) {
|
|
117
|
+
absolutePath = absolutePath.split(win32Path.sep).join(posixPath.sep);
|
|
118
|
+
}
|
|
119
|
+
if (IS_ALMOST_POSIX_PATH.test(absolutePath))
|
|
120
|
+
return "/" + absolutePath[0] + absolutePath.substring(2);
|
|
121
|
+
return absolutePath;
|
|
122
|
+
}
|
|
123
|
+
function toNativeAbsolutePath(posix) {
|
|
124
|
+
if (process.platform !== "win32")
|
|
125
|
+
return posix;
|
|
126
|
+
assert(posix.startsWith("/"), "The path must be absolute");
|
|
127
|
+
const m = posix.match(/^\/([a-zA-Z])(\/.*)?$/);
|
|
128
|
+
assert(m, `Invalid POSIX path: ${posix}`);
|
|
129
|
+
const drive = m[1];
|
|
130
|
+
const rest = (m[2] ?? "").split(posixPath.sep).join(win32Path.sep);
|
|
131
|
+
return drive.toUpperCase() + ":" + rest;
|
|
132
|
+
}
|
|
133
|
+
var GitWorktree = class _GitWorktree {
|
|
134
|
+
constructor(_gitRoot) {
|
|
135
|
+
this._gitRoot = _gitRoot;
|
|
136
|
+
this._posixGitRoot = toPosixAbsolutePath(this._gitRoot);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Creates a GitWorktree instance from any path inside a git repository.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} somePathInsideGitRepo - Any path (file or directory) within a git repository.
|
|
142
|
+
* Can be absolute or relative. The function will locate the git root directory.
|
|
143
|
+
*
|
|
144
|
+
* @returns {GitWorktree} A new GitWorktree instance bound to the discovered git root.
|
|
145
|
+
*
|
|
146
|
+
* @throws {Error} Throws if the path is not inside a git repository or if git commands fail.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* const worktree = GitWorktree.create('./src/my-test.ts');
|
|
151
|
+
* const gitRoot = worktree.rootPath();
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
static create(somePathInsideGitRepo) {
|
|
155
|
+
const root = shell(`git`, ["rev-parse", "--show-toplevel"], {
|
|
156
|
+
cwd: somePathInsideGitRepo,
|
|
157
|
+
encoding: "utf-8"
|
|
158
|
+
});
|
|
159
|
+
assert(root, `FAILED: git rev-parse --show-toplevel HEAD @ ${somePathInsideGitRepo}`);
|
|
160
|
+
return new _GitWorktree(root);
|
|
161
|
+
}
|
|
162
|
+
_posixGitRoot;
|
|
163
|
+
/**
|
|
164
|
+
* Returns the native absolute path of the git repository root directory.
|
|
165
|
+
*
|
|
166
|
+
* @returns {string} Native absolute path to the git root. Format matches the current platform
|
|
167
|
+
* (Windows or POSIX).
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* const root = worktree.rootPath();
|
|
172
|
+
* // On Windows: 'D:\project'
|
|
173
|
+
* // On Unix: '/project'
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
rootPath() {
|
|
177
|
+
return this._gitRoot;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Returns the commit ID (SHA-1 hash) of the current HEAD commit.
|
|
181
|
+
*
|
|
182
|
+
* @returns {FlakinessReport.CommitId} Full 40-character commit hash of the HEAD commit.
|
|
183
|
+
*
|
|
184
|
+
* @throws {Error} Throws if git command fails or repository is in an invalid state.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const commitId = worktree.headCommitId();
|
|
189
|
+
* // Returns: 'a1b2c3d4e5f6...' (40-character SHA-1)
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
headCommitId() {
|
|
193
|
+
const sha = shell(`git`, ["rev-parse", "HEAD"], {
|
|
194
|
+
cwd: this._gitRoot,
|
|
195
|
+
encoding: "utf-8"
|
|
196
|
+
});
|
|
197
|
+
assert(sha, `FAILED: git rev-parse HEAD @ ${this._gitRoot}`);
|
|
198
|
+
return sha.trim();
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Converts a native absolute path to a git-relative POSIX path.
|
|
202
|
+
*
|
|
203
|
+
* Takes any absolute path (Windows or POSIX format) and converts it to a POSIX path
|
|
204
|
+
* relative to the git repository root. This is essential for Flakiness reports where
|
|
205
|
+
* all file paths must be git-relative and use POSIX separators.
|
|
206
|
+
*
|
|
207
|
+
* @param {string} absolutePath - Native absolute path to convert. Can be in Windows format
|
|
208
|
+
* (e.g., `D:\project\src\test.ts`) or POSIX format (e.g., `/project/src/test.ts`).
|
|
209
|
+
*
|
|
210
|
+
* @returns {FlakinessReport.GitFilePath} POSIX path relative to git root (e.g., `src/test.ts`).
|
|
211
|
+
* Returns an empty string if the path is the git root itself.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* const gitPath = worktree.gitPath('/Users/project/src/test.ts');
|
|
216
|
+
* // Returns: 'src/test.ts'
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
219
|
+
gitPath(absolutePath) {
|
|
220
|
+
return posixPath.relative(this._posixGitRoot, toPosixAbsolutePath(absolutePath));
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Converts a git-relative POSIX path to a native absolute path.
|
|
224
|
+
*
|
|
225
|
+
* Takes a POSIX path relative to the git root and converts it to the native absolute path
|
|
226
|
+
* format for the current platform (Windows or POSIX). This is the inverse of `gitPath()`.
|
|
227
|
+
*
|
|
228
|
+
* @param {FlakinessReport.GitFilePath} relativePath - POSIX path relative to git root
|
|
229
|
+
* (e.g., `src/test.ts`).
|
|
230
|
+
*
|
|
231
|
+
* @returns {string} Native absolute path. On Windows, returns Windows format (e.g., `D:\project\src\test.ts`).
|
|
232
|
+
* On POSIX systems, returns POSIX format (e.g., `/project/src/test.ts`).
|
|
233
|
+
*
|
|
234
|
+
* @example
|
|
235
|
+
* ```typescript
|
|
236
|
+
* const absolutePath = worktree.absolutePath('src/test.ts');
|
|
237
|
+
* // On Windows: 'D:\project\src\test.ts'
|
|
238
|
+
* // On Unix: '/project/src/test.ts'
|
|
239
|
+
* ```
|
|
240
|
+
*/
|
|
241
|
+
absolutePath(relativePath) {
|
|
242
|
+
return toNativeAbsolutePath(posixPath.join(this._posixGitRoot, relativePath));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Lists recent commits from the repository.
|
|
246
|
+
*
|
|
247
|
+
* Retrieves commit information including commit ID, timestamp, author, message, and parent commits.
|
|
248
|
+
* Note: CI environments often have shallow checkouts with limited history, which may affect
|
|
249
|
+
* the number of commits returned.
|
|
250
|
+
*
|
|
251
|
+
* @param {number} count - Maximum number of commits to retrieve, starting from HEAD.
|
|
252
|
+
*
|
|
253
|
+
* @returns {Promise<GitCommit[]>} Promise that resolves to an array of commit objects, ordered
|
|
254
|
+
* from most recent to oldest. Each commit includes:
|
|
255
|
+
* - `commitId` - Full commit hash
|
|
256
|
+
* - `timestamp` - Commit timestamp in milliseconds since Unix epoch
|
|
257
|
+
* - `message` - Commit message (subject line)
|
|
258
|
+
* - `author` - Author name
|
|
259
|
+
* - `parents` - Array of parent commit IDs
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```typescript
|
|
263
|
+
* const commits = await worktree.listCommits(10);
|
|
264
|
+
* console.log(`Latest commit: ${commits[0].message}`);
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
async listCommits(count) {
|
|
268
|
+
return await listCommits(this._gitRoot, "HEAD", count);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
async function listCommits(gitRoot, head, count) {
|
|
272
|
+
const FIELD_SEPARATOR = "|~|";
|
|
273
|
+
const RECORD_SEPARATOR = "\0";
|
|
274
|
+
const prettyFormat = [
|
|
275
|
+
"%H",
|
|
276
|
+
// Full commit hash
|
|
277
|
+
"%ct",
|
|
278
|
+
// Commit timestamp (Unix seconds)
|
|
279
|
+
"%an",
|
|
280
|
+
// Author name
|
|
281
|
+
"%s",
|
|
282
|
+
// Subject line
|
|
283
|
+
"%P"
|
|
284
|
+
// Parent hashes (space-separated)
|
|
285
|
+
].join(FIELD_SEPARATOR);
|
|
286
|
+
const command = `git log ${head} -n ${count} --pretty=format:"${prettyFormat}" -z`;
|
|
287
|
+
try {
|
|
288
|
+
const { stdout } = await execAsync(command, { cwd: gitRoot });
|
|
289
|
+
if (!stdout) {
|
|
290
|
+
return [];
|
|
291
|
+
}
|
|
292
|
+
return stdout.trim().split(RECORD_SEPARATOR).filter((record) => record).map((record) => {
|
|
293
|
+
const [commitId, timestampStr, author, message, parentsStr] = record.split(FIELD_SEPARATOR);
|
|
294
|
+
const parents = parentsStr ? parentsStr.split(" ").filter((p) => p) : [];
|
|
295
|
+
return {
|
|
296
|
+
commitId,
|
|
297
|
+
timestamp: parseInt(timestampStr, 10) * 1e3,
|
|
298
|
+
author,
|
|
299
|
+
message,
|
|
300
|
+
parents,
|
|
301
|
+
walkIndex: 0
|
|
302
|
+
};
|
|
303
|
+
});
|
|
304
|
+
} catch (error) {
|
|
305
|
+
log(`Failed to list commits for repository at ${gitRoot}:`, error);
|
|
306
|
+
return [];
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
export {
|
|
310
|
+
GitWorktree
|
|
311
|
+
};
|
|
312
|
+
//# sourceMappingURL=gitWorktree.js.map
|