@argos-ci/core 6.6.0 → 6.6.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 +75 -11
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -139,36 +139,100 @@ function checkIsExecError(error) {
139
139
  return error instanceof Error && "status" in error && typeof error.status === "number" && "stderr" in error && Buffer.isBuffer(error.stderr);
140
140
  }
141
141
  /**
142
+ * Check whether a git fetch error means the requested ref does not exist on
143
+ * the remote, e.g. a branch that has not been pushed to origin yet.
144
+ */
145
+ function checkIsMissingRemoteRefError(error) {
146
+ return getGitErrorOutput(error).toLowerCase().includes("couldn't find remote ref");
147
+ }
148
+ /**
149
+ * Check that a ref exists in the local repository and points to a commit.
150
+ */
151
+ function checkLocalRefExists(ref) {
152
+ try {
153
+ execFileSync("git", [
154
+ "rev-parse",
155
+ "--verify",
156
+ "--quiet",
157
+ `${ref}^{commit}`
158
+ ]);
159
+ return true;
160
+ } catch {
161
+ return false;
162
+ }
163
+ }
164
+ /**
165
+ * Fetch a ref from origin to make it available for the merge-base computation.
166
+ *
167
+ * When the fetch fails, fall back to the history available locally instead of
168
+ * failing the whole upload — the merge base is only used to find the baseline
169
+ * and a build without baseline is better than no build at all:
170
+ * - When the ref is missing on origin (e.g. a branch not pushed yet, common on
171
+ * a first local run), prefer the local ref, which is the exact state being
172
+ * built. Expected, so only logged in debug mode.
173
+ * - For other failures (network, authentication…), prefer the target of a
174
+ * previous fetch (the freshest known remote state) and surface a warning.
175
+ */
176
+ async function fetchMergeBaseRef(input) {
177
+ try {
178
+ await gitFetch(input);
179
+ return {
180
+ ref: input.target,
181
+ fetchable: true
182
+ };
183
+ } catch (error) {
184
+ const isMissingRemoteRef = checkIsMissingRemoteRefError(error);
185
+ if (isMissingRemoteRef) debug(`Ref "${input.ref}" not found on origin, falling back to local history`, getGitErrorOutput(error));
186
+ else {
187
+ console.warn(`Argos failed to fetch "${input.ref}" from origin, falling back to the local history to find the merge base.`);
188
+ debug(`git fetch failed for "${input.ref}"`, getGitErrorOutput(error));
189
+ }
190
+ return {
191
+ ref: (isMissingRemoteRef ? [input.ref, input.target] : [input.target, input.ref]).find((ref) => checkLocalRefExists(ref)) ?? null,
192
+ fetchable: false
193
+ };
194
+ }
195
+ }
196
+ /**
142
197
  * Get the merge base commit SHA.
143
198
  * Fetch both base and head with depth and then run merge base.
144
199
  * Try to find a merge base with a depth of 1000 max.
200
+ *
201
+ * Never fails when a ref can't be fetched from origin: falls back to the local
202
+ * history and returns `null` when no merge base can be found, which resolves
203
+ * to a build without baseline (see `resolveBaseline`).
145
204
  */
146
205
  async function getMergeBaseCommitSha$1(input) {
147
- let depth = 200;
148
206
  const argosBaseRef = `argos/${input.base}`;
149
207
  const argosHeadRef = `argos/${input.head}`;
150
- while (depth < 1e3) {
151
- await gitFetch({
208
+ let head = null;
209
+ let base = null;
210
+ for (let depth = 200; depth < 1e3; depth += 200) {
211
+ if (!head || head.fetchable) head = await fetchMergeBaseRef({
152
212
  ref: input.head,
153
213
  depth,
154
214
  target: argosHeadRef
155
215
  });
156
- await gitFetch({
216
+ if (!base || base.fetchable) base = await fetchMergeBaseRef({
157
217
  ref: input.base,
158
218
  depth,
159
219
  target: argosBaseRef
160
220
  });
221
+ if (!head.ref || !base.ref) {
222
+ debug(`No usable ref for "${!head.ref ? input.head : input.base}", no merge base`);
223
+ return null;
224
+ }
161
225
  const mergeBase = gitMergeBase({
162
- base: argosBaseRef,
163
- head: argosHeadRef
226
+ base: base.ref,
227
+ head: head.ref
164
228
  });
165
229
  if (mergeBase) return mergeBase;
166
- depth += 200;
230
+ if (!head.fetchable && !base.fetchable) break;
167
231
  }
168
- if (isDebugEnabled) {
169
- const headShas = listShas(argosHeadRef);
170
- const baseShas = listShas(argosBaseRef);
171
- debug(`No merge base found for ${input.head} and ${input.base} with depth ${depth}`);
232
+ if (isDebugEnabled && head?.ref && base?.ref) {
233
+ const headShas = listShas(head.ref);
234
+ const baseShas = listShas(base.ref);
235
+ debug(`No merge base found for ${input.head} and ${input.base}`);
172
236
  debug(`Found ${headShas.length} commits in ${input.head}: ${headShas.join(", ")}`);
173
237
  debug(`Found ${baseShas.length} commits in ${input.base}: ${baseShas.join(", ")}`);
174
238
  }
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.6.0",
4
+ "version": "6.6.2",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": {
@@ -39,7 +39,7 @@
39
39
  "access": "public"
40
40
  },
41
41
  "dependencies": {
42
- "@argos-ci/api-client": "0.25.0",
42
+ "@argos-ci/api-client": "0.26.0",
43
43
  "@argos-ci/util": "4.0.4",
44
44
  "convict": "^6.2.5",
45
45
  "debug": "^4.4.3",
@@ -68,5 +68,5 @@
68
68
  "lint": "eslint .",
69
69
  "test": "vitest"
70
70
  },
71
- "gitHead": "686b6fa8385ed77598ec86f66b073e9897cef176"
71
+ "gitHead": "a86f2dc1a243591e064a2d5c4e03d712c8f03b86"
72
72
  }