@argos-ci/core 6.2.0 → 6.2.2

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.
Files changed (2) hide show
  1. package/dist/index.mjs +82 -27
  2. package/package.json +6 -5
package/dist/index.mjs CHANGED
@@ -1,6 +1,8 @@
1
1
  import { createRequire } from "node:module";
2
2
  import convict from "convict";
3
- import { execSync } from "node:child_process";
3
+ import { execFile, execFileSync, execSync } from "node:child_process";
4
+ import { promisify } from "node:util";
5
+ import pRetry from "p-retry";
4
6
  import createDebug from "debug";
5
7
  import { createReadStream, existsSync, readFileSync } from "node:fs";
6
8
  import { readFile, stat } from "node:fs/promises";
@@ -9,7 +11,6 @@ import glob from "fast-glob";
9
11
  import mime from "mime-types";
10
12
  import { createClient, throwAPIError } from "@argos-ci/api-client";
11
13
  import { createHash } from "node:crypto";
12
- import { promisify } from "node:util";
13
14
  import sharp from "sharp";
14
15
  import tmp from "tmp";
15
16
  import { getPlaywrightTracePath, readMetadata, readVersionFromPackage } from "@argos-ci/util";
@@ -25,6 +26,7 @@ const debugTimeEnd = (arg) => {
25
26
  };
26
27
  //#endregion
27
28
  //#region src/ci-environment/git.ts
29
+ const execFileAsync = promisify(execFile);
28
30
  /**
29
31
  * Check if the current directory is a git repository.
30
32
  */
@@ -72,17 +74,63 @@ function getRepositoryURL() {
72
74
  */
73
75
  function gitMergeBase(input) {
74
76
  try {
75
- return execSync(`git merge-base ${input.head} ${input.base}`).toString().trim();
77
+ return execFileSync("git", [
78
+ "merge-base",
79
+ input.head,
80
+ input.base
81
+ ]).toString().trim();
76
82
  } catch (error) {
77
83
  if (checkIsExecError(error) && error.status === 1 && error.stderr.toString() === "") return null;
78
84
  throw error;
79
85
  }
80
86
  }
81
87
  /**
88
+ * Combine an error's message and stderr into a single searchable string.
89
+ */
90
+ function getGitErrorOutput(error) {
91
+ if (!(error instanceof Error)) return "";
92
+ const stderr = "stderr" in error ? String(error.stderr ?? "") : "";
93
+ return `${error.message}\n${stderr}`;
94
+ }
95
+ /**
96
+ * Check whether a git error is a `.git/*.lock` contention error.
97
+ * This happens when another git process is running concurrently (e.g. parallel
98
+ * CI shards fetching the same repository) or when a stale lock was left behind
99
+ * by a crashed git process.
100
+ */
101
+ function checkIsGitLockError(error) {
102
+ const output = getGitErrorOutput(error);
103
+ return output.includes(".lock': File exists") || output.includes("Another git process seems to be running");
104
+ }
105
+ /**
106
+ * Run `git fetch` with the given arguments.
107
+ *
108
+ * Retries on lock contention (`.git/shallow.lock` "File exists") with an
109
+ * exponential backoff, since this is usually a transient conflict with another
110
+ * git process and resolves once that process releases the lock.
111
+ */
112
+ function runGitFetch(args) {
113
+ return pRetry(() => execFileAsync("git", ["fetch", ...args]), {
114
+ retries: 3,
115
+ minTimeout: 500,
116
+ shouldRetry: ({ error }) => checkIsGitLockError(error),
117
+ onFailedAttempt: ({ error, retriesLeft, retryDelay }) => {
118
+ if (checkIsGitLockError(error) && retriesLeft > 0) debug(`git fetch failed on lock contention, retrying in ${retryDelay}ms (${retriesLeft} left)`);
119
+ }
120
+ });
121
+ }
122
+ /**
82
123
  * Run git fetch with a specific ref and depth.
83
124
  */
84
- function gitFetch(input) {
85
- execSync(`git fetch --force --update-head-ok --depth ${input.depth} origin ${input.ref}:${input.target}`);
125
+ async function gitFetch(input) {
126
+ await runGitFetch([
127
+ "--force",
128
+ "--update-head-ok",
129
+ "--depth",
130
+ String(input.depth),
131
+ "origin",
132
+ `${input.ref}:${input.target}`
133
+ ]);
86
134
  }
87
135
  /**
88
136
  * Check if an error is an exec error that includes stderr.
@@ -95,17 +143,17 @@ function checkIsExecError(error) {
95
143
  * Fetch both base and head with depth and then run merge base.
96
144
  * Try to find a merge base with a depth of 1000 max.
97
145
  */
98
- function getMergeBaseCommitSha$1(input) {
146
+ async function getMergeBaseCommitSha$1(input) {
99
147
  let depth = 200;
100
148
  const argosBaseRef = `argos/${input.base}`;
101
149
  const argosHeadRef = `argos/${input.head}`;
102
150
  while (depth < 1e3) {
103
- gitFetch({
151
+ await gitFetch({
104
152
  ref: input.head,
105
153
  depth,
106
154
  target: argosHeadRef
107
155
  });
108
- gitFetch({
156
+ await gitFetch({
109
157
  ref: input.base,
110
158
  depth,
111
159
  target: argosBaseRef
@@ -127,14 +175,21 @@ function getMergeBaseCommitSha$1(input) {
127
175
  return null;
128
176
  }
129
177
  function listShas(path, maxCount) {
130
- return execSync(`git log --format="%H" ${maxCount ? `--max-count=${maxCount}` : ""} ${path}`.trim()).toString().trim().split("\n");
178
+ const args = ["log", "--format=%H"];
179
+ if (maxCount) args.push(`--max-count=${maxCount}`);
180
+ args.push(path);
181
+ return execFileSync("git", args).toString().trim().split("\n");
131
182
  }
132
- function listParentCommits$1(input) {
183
+ async function listParentCommits$1(input) {
133
184
  const limit = 200;
134
185
  try {
135
- execSync(`git fetch --depth=${limit} origin ${input.sha}`);
186
+ await runGitFetch([
187
+ `--depth=${limit}`,
188
+ "origin",
189
+ input.sha
190
+ ]);
136
191
  } catch (error) {
137
- if (error instanceof Error && error.message.includes("not our ref")) return [];
192
+ if (getGitErrorOutput(error).includes("not our ref")) return [];
138
193
  }
139
194
  return listShas(input.sha, limit);
140
195
  }
@@ -684,7 +739,7 @@ function getCiService(context) {
684
739
  function getMergeBaseCommitSha(input) {
685
740
  const context = createContext();
686
741
  const service = getCiService(context);
687
- if (!service) return null;
742
+ if (!service) return Promise.resolve(null);
688
743
  return service.getMergeBaseCommitSha(input, context);
689
744
  }
690
745
  /**
@@ -693,7 +748,7 @@ function getMergeBaseCommitSha(input) {
693
748
  function listParentCommits(input) {
694
749
  const context = createContext();
695
750
  const service = getCiService(context);
696
- if (!service) return null;
751
+ if (!service) return Promise.resolve(null);
697
752
  return service.listParentCommits(input, context);
698
753
  }
699
754
  /**
@@ -1123,7 +1178,7 @@ async function exchangeGitHubActionsOidcToken(args) {
1123
1178
  branch: config.branch,
1124
1179
  pullRequestNumber: config.prNumber ?? void 0
1125
1180
  } });
1126
- if (result.error) throwAPIError(result.error);
1181
+ if (result.error) throwAPIError(result.error, result.response);
1127
1182
  return result.data.token;
1128
1183
  }
1129
1184
  //#endregion
@@ -1163,7 +1218,7 @@ async function exchangeGitHubActionsTokenlessToken(args) {
1163
1218
  commit,
1164
1219
  branch: config.branch
1165
1220
  } });
1166
- if (result.error) throwAPIError(result.error);
1221
+ if (result.error) throwAPIError(result.error, result.response);
1167
1222
  return result.data.token;
1168
1223
  }
1169
1224
  //#endregion
@@ -1250,7 +1305,7 @@ async function deploy(params) {
1250
1305
  contentType
1251
1306
  }))
1252
1307
  } });
1253
- if (createResponse.error) throwAPIError(createResponse.error);
1308
+ if (createResponse.error) throwAPIError(createResponse.error, createResponse.response);
1254
1309
  const { deploymentId, uploadFiles: filesToUpload } = createResponse.data;
1255
1310
  debug(`Deployment created: ${deploymentId}, files to upload: ${filesToUpload.length}`);
1256
1311
  const uploadChunks = chunk(filesToUpload.map(({ path, uploadUrl }) => {
@@ -1274,7 +1329,7 @@ async function deploy(params) {
1274
1329
  }
1275
1330
  debug("Finalizing deployment");
1276
1331
  const finalizeResponse = await apiClient.POST("/deployments/{deploymentId}/finalize", { params: { path: { deploymentId } } });
1277
- if (finalizeResponse.error) throwAPIError(finalizeResponse.error);
1332
+ if (finalizeResponse.error) throwAPIError(finalizeResponse.error, finalizeResponse.response);
1278
1333
  return finalizeResponse.data;
1279
1334
  }
1280
1335
  //#endregion
@@ -1291,7 +1346,7 @@ async function finalize(params) {
1291
1346
  });
1292
1347
  if (!config.parallelNonce) throw new Error("parallel.nonce is required to finalize the build");
1293
1348
  const finalizeBuildsResult = await apiClient.POST("/builds/finalize", { body: { parallelNonce: config.parallelNonce } });
1294
- if (finalizeBuildsResult.error) throwAPIError(finalizeBuildsResult.error);
1349
+ if (finalizeBuildsResult.error) throwAPIError(finalizeBuildsResult.error, finalizeBuildsResult.response);
1295
1350
  return finalizeBuildsResult.data;
1296
1351
  }
1297
1352
  //#endregion
@@ -1423,7 +1478,7 @@ async function skip(params) {
1423
1478
  pwTraceKeys: [],
1424
1479
  parentCommits: []
1425
1480
  } });
1426
- if (createBuildResponse.error) throwAPIError(createBuildResponse.error);
1481
+ if (createBuildResponse.error) throwAPIError(createBuildResponse.error, createBuildResponse.response);
1427
1482
  return { build: createBuildResponse.data.build };
1428
1483
  }
1429
1484
  //#endregion
@@ -1492,16 +1547,16 @@ async function upload(params) {
1492
1547
  });
1493
1548
  debug("Fetch project");
1494
1549
  const projectResponse = await apiClient.GET("/project");
1495
- if (projectResponse.error) throwAPIError(projectResponse.error);
1550
+ if (projectResponse.error) throwAPIError(projectResponse.error, projectResponse.response);
1496
1551
  debug("Project fetched", projectResponse.data);
1497
1552
  const { defaultBaseBranch, hasRemoteContentAccess } = projectResponse.data;
1498
- const referenceCommit = (() => {
1553
+ const referenceCommit = await (async () => {
1499
1554
  if (config.referenceCommit) {
1500
1555
  debug("Found reference commit in config", config.referenceCommit);
1501
1556
  return config.referenceCommit;
1502
1557
  }
1503
1558
  if (hasRemoteContentAccess) return null;
1504
- const sha = getMergeBaseCommitSha({
1559
+ const sha = await getMergeBaseCommitSha({
1505
1560
  base: config.referenceBranch || config.prBaseBranch || defaultBaseBranch,
1506
1561
  head: config.branch
1507
1562
  });
@@ -1509,10 +1564,10 @@ async function upload(params) {
1509
1564
  else debug("No merge base found");
1510
1565
  return sha;
1511
1566
  })();
1512
- const parentCommits = (() => {
1567
+ const parentCommits = await (async () => {
1513
1568
  if (hasRemoteContentAccess) return null;
1514
1569
  if (referenceCommit) {
1515
- const commits = listParentCommits({ sha: referenceCommit });
1570
+ const commits = await listParentCommits({ sha: referenceCommit });
1516
1571
  if (commits) debug("Found parent commits", commits);
1517
1572
  else debug("No parent commits found");
1518
1573
  return commits;
@@ -1551,7 +1606,7 @@ async function upload(params) {
1551
1606
  mergeQueuePrNumbers: config.mergeQueuePrNumbers,
1552
1607
  subset: config.subset
1553
1608
  } });
1554
- if (createBuildResponse.error) throwAPIError(createBuildResponse.error);
1609
+ if (createBuildResponse.error) throwAPIError(createBuildResponse.error, createBuildResponse.response);
1555
1610
  const result = createBuildResponse.data;
1556
1611
  debug("Got uploads url", result);
1557
1612
  await uploadFilesToS3([...result.screenshots.map((upload) => {
@@ -1589,7 +1644,7 @@ async function upload(params) {
1589
1644
  metadata: params.metadata
1590
1645
  }
1591
1646
  });
1592
- if (uploadBuildResponse.error) throwAPIError(uploadBuildResponse.error);
1647
+ if (uploadBuildResponse.error) throwAPIError(uploadBuildResponse.error, uploadBuildResponse.response);
1593
1648
  return {
1594
1649
  build: uploadBuildResponse.data.build,
1595
1650
  screenshots: snapshots
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@argos-ci/core",
3
3
  "description": "Node.js SDK for visual testing with Argos.",
4
- "version": "6.2.0",
4
+ "version": "6.2.2",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": {
@@ -39,14 +39,15 @@
39
39
  "access": "public"
40
40
  },
41
41
  "dependencies": {
42
- "@argos-ci/api-client": "0.22.0",
42
+ "@argos-ci/api-client": "0.22.1",
43
43
  "@argos-ci/util": "4.0.0",
44
44
  "convict": "^6.2.5",
45
45
  "debug": "^4.4.3",
46
46
  "fast-glob": "^3.3.3",
47
47
  "mime-types": "^3.0.2",
48
- "sharp": "^0.34.5",
49
- "tmp": "^0.2.5"
48
+ "p-retry": "^8.0.0",
49
+ "sharp": "^0.35.2",
50
+ "tmp": "^0.2.7"
50
51
  },
51
52
  "devDependencies": {
52
53
  "@octokit/webhooks": "^14.2.0",
@@ -67,5 +68,5 @@
67
68
  "lint": "eslint .",
68
69
  "test": "vitest"
69
70
  },
70
- "gitHead": "1787fe78cba46f408493e02114150f2d73f47fa4"
71
+ "gitHead": "5bcfacce28cbe061590665bf5dec220439c8aebc"
71
72
  }