@argos-ci/core 5.0.1 → 5.0.3-alpha.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.
Files changed (2) hide show
  1. package/dist/index.js +190 -79
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -238,11 +238,19 @@ var heroku_default = service3;
238
238
 
239
239
  // src/ci-environment/services/github-actions.ts
240
240
  import { existsSync, readFileSync } from "fs";
241
- async function getPullRequestFromHeadSha({ env }, sha) {
242
- debug("Fetching pull request number from head sha", sha);
243
- if (!env.GITHUB_REPOSITORY) {
241
+
242
+ // src/ci-environment/github.ts
243
+ function getGitHubRepository(ctx) {
244
+ return ctx.env.GITHUB_REPOSITORY || null;
245
+ }
246
+ function assertGitHubRepository(ctx) {
247
+ const repo = getGitHubRepository(ctx);
248
+ if (!repo) {
244
249
  throw new Error("GITHUB_REPOSITORY is missing");
245
250
  }
251
+ return repo;
252
+ }
253
+ function getGitHubToken({ env }) {
246
254
  if (!env.GITHUB_TOKEN) {
247
255
  if (!env.DISABLE_GITHUB_TOKEN_WARNING) {
248
256
  console.log(
@@ -262,43 +270,113 @@ DISABLE_GITHUB_TOKEN_WARNING: true
262
270
  }
263
271
  return null;
264
272
  }
265
- try {
266
- const url = new URL(
267
- `https://api.github.com/repos/${env.GITHUB_REPOSITORY}/pulls`
273
+ return env.GITHUB_TOKEN;
274
+ }
275
+ async function fetchGitHubAPI(ctx, url) {
276
+ const githubToken = getGitHubToken(ctx);
277
+ if (!githubToken) {
278
+ return null;
279
+ }
280
+ const response = await fetch(url, {
281
+ headers: {
282
+ Accept: "application/vnd.github+json",
283
+ Authorization: `Bearer ${githubToken}`,
284
+ "X-GitHub-Api-Version": "2022-11-28"
285
+ },
286
+ signal: AbortSignal.timeout(1e4)
287
+ });
288
+ return response;
289
+ }
290
+ var GITHUB_API_BASE_URL = "https://api.github.com";
291
+ async function getPullRequestFromHeadSha(ctx, sha) {
292
+ debug(`Fetching pull request details from head sha: ${sha}`);
293
+ const githubRepository = assertGitHubRepository(ctx);
294
+ const url = new URL(`/repos/${githubRepository}/pulls`, GITHUB_API_BASE_URL);
295
+ url.search = new URLSearchParams({
296
+ state: "open",
297
+ sort: "updated",
298
+ per_page: "30",
299
+ page: "1"
300
+ }).toString();
301
+ const response = await fetchGitHubAPI(ctx, url);
302
+ if (!response) {
303
+ return null;
304
+ }
305
+ if (!response.ok) {
306
+ throw new Error(
307
+ `Non-OK response (status: ${response.status}) while fetching pull request details from head sha (${sha})`
268
308
  );
269
- url.search = new URLSearchParams({
270
- state: "open",
271
- sort: "updated",
272
- per_page: "30",
273
- page: "1"
274
- }).toString();
275
- const response = await fetch(url, {
276
- headers: {
277
- Accept: "application/vnd.github+json",
278
- Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
279
- "X-GitHub-Api-Version": "2022-11-28"
280
- },
281
- signal: AbortSignal.timeout(1e4)
282
- });
283
- if (!response.ok) {
284
- throw new Error(`Failed to fetch pull requests: ${response.statusText}`);
285
- }
286
- const result = await response.json();
287
- if (result.length === 0) {
288
- debug("Aborting because no pull request found");
289
- return null;
290
- }
291
- const matchingPr = result.find((pr) => pr.head.sha === sha);
292
- if (matchingPr) {
293
- debug("Pull request found", matchingPr);
294
- return matchingPr;
295
- }
296
- debug("Aborting because no pull request found");
309
+ }
310
+ const result = await response.json();
311
+ if (result.length === 0) {
312
+ debug("No results, no pull request found");
297
313
  return null;
298
- } catch (error) {
299
- debug("Error while fetching pull request from head sha", error);
314
+ }
315
+ const matchingPr = result.find((pr) => pr.head.sha === sha);
316
+ if (matchingPr) {
317
+ debug("Pull request found", matchingPr);
318
+ return matchingPr;
319
+ }
320
+ debug("No matching pull request found");
321
+ return null;
322
+ }
323
+ async function getPullRequestFromPrNumber(ctx, prNumber) {
324
+ debug(`Fetching pull request #${prNumber}`);
325
+ const githubRepository = assertGitHubRepository(ctx);
326
+ const response = await fetchGitHubAPI(
327
+ ctx,
328
+ new URL(
329
+ `/repos/${githubRepository}/pulls/${prNumber}`,
330
+ GITHUB_API_BASE_URL
331
+ )
332
+ );
333
+ if (!response) {
334
+ return null;
335
+ }
336
+ if (response.status === 404) {
337
+ debug(
338
+ "No pull request found, pr detection from branch was probably a mistake"
339
+ );
300
340
  return null;
301
341
  }
342
+ if (!response.ok) {
343
+ throw new Error(
344
+ `Non-OK response (status: ${response.status}) while fetching pull request #${prNumber}`
345
+ );
346
+ }
347
+ const result = await response.json();
348
+ return result;
349
+ }
350
+ function getPRNumberFromMergeGroupBranch(branch2) {
351
+ const prMatch = /queue\/[^/]*\/pr-(\d+)-/.exec(branch2);
352
+ if (prMatch) {
353
+ const prNumber = Number(prMatch[1]);
354
+ return prNumber;
355
+ }
356
+ return null;
357
+ }
358
+
359
+ // src/ci-environment/services/github-actions.ts
360
+ function readEventPayload({ env }) {
361
+ if (!env.GITHUB_EVENT_PATH) {
362
+ return null;
363
+ }
364
+ if (!existsSync(env.GITHUB_EVENT_PATH)) {
365
+ return null;
366
+ }
367
+ return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, "utf-8"));
368
+ }
369
+ function getVercelDeploymentPayload(payload) {
370
+ if (process.env.GITHUB_EVENT_NAME === "repository_dispatch" && payload && "action" in payload && payload.action === "vercel.deployment.success") {
371
+ return payload;
372
+ }
373
+ return null;
374
+ }
375
+ function getMergeGroupPayload(payload) {
376
+ if (payload && process.env.GITHUB_EVENT_NAME === "merge_group" && "action" in payload && payload.action === "checks_requested") {
377
+ return payload;
378
+ }
379
+ return null;
302
380
  }
303
381
  function getBranchFromContext(context) {
304
382
  const { env } = context;
@@ -321,6 +399,29 @@ function getBranchFromPayload(payload) {
321
399
  }
322
400
  return null;
323
401
  }
402
+ function getBranch(args) {
403
+ const { payload, mergeGroupPayload, vercelPayload, pullRequest, context } = args;
404
+ if (mergeGroupPayload && pullRequest?.head.ref) {
405
+ return pullRequest.head.ref;
406
+ }
407
+ if (vercelPayload) {
408
+ return vercelPayload.client_payload.git.ref;
409
+ }
410
+ if (payload) {
411
+ const fromPayload = getBranchFromPayload(payload);
412
+ if (fromPayload) {
413
+ return fromPayload;
414
+ }
415
+ }
416
+ const fromContext = getBranchFromContext(context);
417
+ if (fromContext) {
418
+ return fromContext;
419
+ }
420
+ if (pullRequest) {
421
+ return pullRequest.head.ref;
422
+ }
423
+ return null;
424
+ }
324
425
  function getRepository3(context, payload) {
325
426
  if (payload && "pull_request" in payload && payload.pull_request) {
326
427
  const pr = payload.pull_request;
@@ -328,20 +429,16 @@ function getRepository3(context, payload) {
328
429
  return pr.head.repo.full_name;
329
430
  }
330
431
  }
331
- return getOriginalRepository(context);
332
- }
333
- function getOriginalRepository(context) {
334
- const { env } = context;
335
- return env.GITHUB_REPOSITORY || null;
432
+ return getGitHubRepository(context);
336
433
  }
337
- function readEventPayload({ env }) {
338
- if (!env.GITHUB_EVENT_PATH) {
339
- return null;
434
+ function getSha(context, vercelPayload) {
435
+ if (vercelPayload) {
436
+ return vercelPayload.client_payload.git.sha;
340
437
  }
341
- if (!existsSync(env.GITHUB_EVENT_PATH)) {
342
- return null;
438
+ if (!context.env.GITHUB_SHA) {
439
+ throw new Error(`GITHUB_SHA is missing`);
343
440
  }
344
- return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, "utf-8"));
441
+ return context.env.GITHUB_SHA;
345
442
  }
346
443
  function getPullRequestFromPayload(payload) {
347
444
  if ("pull_request" in payload && payload.pull_request && payload.pull_request) {
@@ -355,26 +452,27 @@ function getPullRequestFromPayload(payload) {
355
452
  }
356
453
  return null;
357
454
  }
358
- function getVercelDeploymentPayload(payload) {
359
- if (process.env.GITHUB_EVENT_NAME === "repository_dispatch" && payload && "action" in payload && payload.action === "vercel.deployment.success") {
360
- return payload;
361
- }
362
- return null;
363
- }
364
- function getMergeGroupPayload(payload) {
365
- if (payload && process.env.GITHUB_EVENT_NAME === "merge_group" && "action" in payload && payload.action === "checks_requested") {
366
- return payload;
455
+ async function getPullRequest(args) {
456
+ const { payload, vercelPayload, mergeGroupPayload, context, sha } = args;
457
+ if (vercelPayload || !payload) {
458
+ return getPullRequestFromHeadSha(context, sha);
459
+ }
460
+ if (mergeGroupPayload) {
461
+ const prNumber = getPRNumberFromMergeGroupBranch(
462
+ mergeGroupPayload.merge_group.head_ref
463
+ );
464
+ if (!prNumber) {
465
+ debug(
466
+ `No PR found from merge group head ref: ${mergeGroupPayload.merge_group.head_ref}`
467
+ );
468
+ return null;
469
+ }
470
+ debug(
471
+ `PR #${prNumber} found from merge group head ref (${mergeGroupPayload.merge_group.head_ref})`
472
+ );
473
+ return getPullRequestFromPrNumber(context, prNumber);
367
474
  }
368
- return null;
369
- }
370
- function getSha(context, vercelPayload) {
371
- if (vercelPayload) {
372
- return vercelPayload.client_payload.git.sha;
373
- }
374
- if (!context.env.GITHUB_SHA) {
375
- throw new Error(`GITHUB_SHA is missing`);
376
- }
377
- return context.env.GITHUB_SHA;
475
+ return getPullRequestFromPayload(payload);
378
476
  }
379
477
  var service4 = {
380
478
  name: "GitHub Actions",
@@ -386,20 +484,33 @@ var service4 = {
386
484
  const vercelPayload = getVercelDeploymentPayload(payload);
387
485
  const mergeGroupPayload = getMergeGroupPayload(payload);
388
486
  const sha = getSha(context, vercelPayload);
389
- const pullRequest = payload && !vercelPayload ? getPullRequestFromPayload(payload) : await getPullRequestFromHeadSha(context, sha);
487
+ const pullRequest = await getPullRequest({
488
+ payload,
489
+ vercelPayload,
490
+ mergeGroupPayload,
491
+ sha,
492
+ context
493
+ });
494
+ const branch2 = getBranch({
495
+ payload,
496
+ vercelPayload,
497
+ mergeGroupPayload,
498
+ context,
499
+ pullRequest
500
+ });
390
501
  return {
391
502
  commit: sha,
392
503
  repository: getRepository3(context, payload),
393
- originalRepository: getOriginalRepository(context),
504
+ originalRepository: getGitHubRepository(context),
394
505
  jobId: env.GITHUB_JOB || null,
395
506
  runId: env.GITHUB_RUN_ID || null,
396
507
  runAttempt: env.GITHUB_RUN_ATTEMPT ? Number(env.GITHUB_RUN_ATTEMPT) : null,
397
508
  nonce: `${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}`,
398
- branch: vercelPayload?.client_payload?.git?.ref || getBranchFromContext(context) || pullRequest?.head.ref || (payload ? getBranchFromPayload(payload) : null) || null,
509
+ branch: branch2,
399
510
  prNumber: pullRequest?.number || null,
400
511
  prHeadCommit: pullRequest?.head.sha ?? null,
401
512
  prBaseBranch: pullRequest?.base.ref ?? null,
402
- mergeQueue: mergeGroupPayload?.action === "checks_requested"
513
+ mergeQueue: Boolean(mergeGroupPayload)
403
514
  };
404
515
  },
405
516
  getMergeBaseCommitSha,
@@ -421,9 +532,9 @@ function getRepository4(context) {
421
532
  if (env.CIRCLE_PR_REPONAME && env.CIRCLE_PR_USERNAME) {
422
533
  return `${env.CIRCLE_PR_USERNAME}/${env.CIRCLE_PR_REPONAME}`;
423
534
  }
424
- return getOriginalRepository2(context);
535
+ return getOriginalRepository(context);
425
536
  }
426
- function getOriginalRepository2(context) {
537
+ function getOriginalRepository(context) {
427
538
  const { env } = context;
428
539
  if (env.CIRCLE_PROJECT_USERNAME && env.CIRCLE_PROJECT_REPONAME) {
429
540
  return `${env.CIRCLE_PROJECT_USERNAME}/${env.CIRCLE_PROJECT_REPONAME}`;
@@ -440,7 +551,7 @@ var service5 = {
440
551
  commit: env.CIRCLE_SHA1 || null,
441
552
  branch: env.CIRCLE_BRANCH || null,
442
553
  repository: getRepository4(context),
443
- originalRepository: getOriginalRepository2(context),
554
+ originalRepository: getOriginalRepository(context),
444
555
  jobId: null,
445
556
  runId: null,
446
557
  runAttempt: null,
@@ -462,9 +573,9 @@ function getRepository5(context) {
462
573
  if (env.TRAVIS_PULL_REQUEST_SLUG) {
463
574
  return env.TRAVIS_PULL_REQUEST_SLUG;
464
575
  }
465
- return getOriginalRepository3(context);
576
+ return getOriginalRepository2(context);
466
577
  }
467
- function getOriginalRepository3(context) {
578
+ function getOriginalRepository2(context) {
468
579
  const { env } = context;
469
580
  return env.TRAVIS_REPO_SLUG || null;
470
581
  }
@@ -485,7 +596,7 @@ var service6 = {
485
596
  commit: env.TRAVIS_COMMIT || null,
486
597
  branch: env.TRAVIS_BRANCH || null,
487
598
  repository: getRepository5(ctx),
488
- originalRepository: getOriginalRepository3(ctx),
599
+ originalRepository: getOriginalRepository2(ctx),
489
600
  jobId: null,
490
601
  runId: null,
491
602
  runAttempt: null,
@@ -507,9 +618,9 @@ function getRepository6(context) {
507
618
  if (env.CI_MERGE_REQUEST_PROJECT_PATH) {
508
619
  return env.CI_MERGE_REQUEST_PROJECT_PATH;
509
620
  }
510
- return getOriginalRepository4(context);
621
+ return getOriginalRepository3(context);
511
622
  }
512
- function getOriginalRepository4(context) {
623
+ function getOriginalRepository3(context) {
513
624
  const { env } = context;
514
625
  return env.CI_PROJECT_PATH || null;
515
626
  }
@@ -523,7 +634,7 @@ var service7 = {
523
634
  commit: env.CI_COMMIT_SHA || null,
524
635
  branch: env.CI_COMMIT_REF_NAME || null,
525
636
  repository: getRepository6(context),
526
- originalRepository: getOriginalRepository4(context),
637
+ originalRepository: getOriginalRepository3(context),
527
638
  jobId: null,
528
639
  runId: null,
529
640
  runAttempt: null,
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": "5.0.1",
4
+ "version": "5.0.3-alpha.1+4a513df",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "exports": {
@@ -40,8 +40,8 @@
40
40
  "access": "public"
41
41
  },
42
42
  "dependencies": {
43
- "@argos-ci/api-client": "0.15.0",
44
- "@argos-ci/util": "3.2.0",
43
+ "@argos-ci/api-client": "0.15.1-alpha.5+4a513df",
44
+ "@argos-ci/util": "3.2.1-alpha.19+4a513df",
45
45
  "convict": "^6.2.4",
46
46
  "debug": "^4.4.3",
47
47
  "fast-glob": "^3.3.3",
@@ -67,5 +67,5 @@
67
67
  "lint": "eslint .",
68
68
  "test": "vitest"
69
69
  },
70
- "gitHead": "389e8ae7fe5cd4bf74d2627853b8c62e4b09e86a"
70
+ "gitHead": "4a513dfc1b959dc5fb000ffb77d122c3096bf481"
71
71
  }