@argos-ci/core 5.0.2 → 5.0.3

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 (3) hide show
  1. package/LICENSE +7 -0
  2. package/dist/index.js +170 -125
  3. package/package.json +2 -2
package/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2022 Smooth Code
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/index.js CHANGED
@@ -238,11 +238,17 @@ var heroku_default = service3;
238
238
 
239
239
  // src/ci-environment/services/github-actions.ts
240
240
  import { existsSync, readFileSync } from "fs";
241
- function getGitHubRepository({ env }) {
242
- 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) {
243
249
  throw new Error("GITHUB_REPOSITORY is missing");
244
250
  }
245
- return env.GITHUB_REPOSITORY;
251
+ return repo;
246
252
  }
247
253
  function getGitHubToken({ env }) {
248
254
  if (!env.GITHUB_TOKEN) {
@@ -266,83 +272,111 @@ DISABLE_GITHUB_TOKEN_WARNING: true
266
272
  }
267
273
  return env.GITHUB_TOKEN;
268
274
  }
269
- function getGhAPIHeaders(ctx) {
275
+ async function fetchGitHubAPI(ctx, url) {
270
276
  const githubToken = getGitHubToken(ctx);
271
277
  if (!githubToken) {
272
278
  return null;
273
279
  }
274
- return {
275
- Accept: "application/vnd.github+json",
276
- Authorization: `Bearer ${githubToken}`,
277
- "X-GitHub-Api-Version": "2022-11-28"
278
- };
279
- }
280
- async function fetchGitHubAPI(ctx, url) {
281
- const headers = getGhAPIHeaders(ctx);
282
- if (!headers) {
283
- return null;
284
- }
285
280
  const response = await fetch(url, {
286
- headers,
281
+ headers: {
282
+ Accept: "application/vnd.github+json",
283
+ Authorization: `Bearer ${githubToken}`,
284
+ "X-GitHub-Api-Version": "2022-11-28"
285
+ },
287
286
  signal: AbortSignal.timeout(1e4)
288
287
  });
289
- if (!response.ok) {
290
- throw new Error(`Failed to fetch GitHub API: ${response.statusText}`);
291
- }
292
- return await response.json();
288
+ return response;
293
289
  }
290
+ var GITHUB_API_BASE_URL = "https://api.github.com";
294
291
  async function getPullRequestFromHeadSha(ctx, sha) {
295
- debug("Fetching pull request details from head sha", sha);
296
- const githubRepository = getGitHubRepository(ctx);
297
- try {
298
- const url = new URL(
299
- `https://api.github.com/repos/${githubRepository}/pulls`
300
- );
301
- url.search = new URLSearchParams({
302
- state: "open",
303
- sort: "updated",
304
- per_page: "30",
305
- page: "1"
306
- }).toString();
307
- const result = await fetchGitHubAPI(ctx, url);
308
- if (!result) {
309
- return null;
310
- }
311
- if (result.length === 0) {
312
- debug("Aborting because no pull request found");
313
- return null;
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("Aborting because no pull request found");
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) {
321
303
  return null;
322
- } catch (error) {
323
- debug("Error while fetching pull request details from head sha", error);
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})`
308
+ );
309
+ }
310
+ const result = await response.json();
311
+ if (result.length === 0) {
312
+ debug("No results, no pull request found");
324
313
  return null;
325
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;
326
322
  }
327
323
  async function getPullRequestFromPrNumber(ctx, prNumber) {
328
- debug("Fetching pull request details from pull request number", prNumber);
329
- const githubRepository = getGitHubRepository(ctx);
330
- const headers = getGhAPIHeaders(ctx);
331
- if (!headers) {
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) {
332
334
  return null;
333
335
  }
334
- try {
335
- return await fetchGitHubAPI(
336
- ctx,
337
- `https://api.github.com/repos/${githubRepository}/pulls/${prNumber}`
338
- );
339
- } catch (error) {
336
+ if (response.status === 404) {
340
337
  debug(
341
- "Error while fetching pull request details from pull request number",
342
- error
338
+ "No pull request found, pr detection from branch was probably a mistake"
343
339
  );
344
340
  return null;
345
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;
346
380
  }
347
381
  function getBranchFromContext(context) {
348
382
  const { env } = context;
@@ -356,14 +390,6 @@ function getBranchFromContext(context) {
356
390
  const matches = branchRegex.exec(env.GITHUB_REF);
357
391
  return matches?.[1] ?? null;
358
392
  }
359
- function getPRNumberFromMergeGroupBranch(branch2) {
360
- const prMatch = /queue\/[^/]*\/pr-(\d+)-/.exec(branch2);
361
- if (prMatch) {
362
- const prNumber = Number(prMatch[1]);
363
- return prNumber;
364
- }
365
- return null;
366
- }
367
393
  function getBranchFromPayload(payload) {
368
394
  if ("workflow_run" in payload && payload.workflow_run) {
369
395
  return payload.workflow_run.head_branch;
@@ -373,6 +399,29 @@ function getBranchFromPayload(payload) {
373
399
  }
374
400
  return null;
375
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
+ }
376
425
  function getRepository3(context, payload) {
377
426
  if (payload && "pull_request" in payload && payload.pull_request) {
378
427
  const pr = payload.pull_request;
@@ -380,20 +429,16 @@ function getRepository3(context, payload) {
380
429
  return pr.head.repo.full_name;
381
430
  }
382
431
  }
383
- return getOriginalRepository(context);
432
+ return getGitHubRepository(context);
384
433
  }
385
- function getOriginalRepository(context) {
386
- const { env } = context;
387
- return env.GITHUB_REPOSITORY || null;
388
- }
389
- function readEventPayload({ env }) {
390
- if (!env.GITHUB_EVENT_PATH) {
391
- return null;
434
+ function getSha(context, vercelPayload) {
435
+ if (vercelPayload) {
436
+ return vercelPayload.client_payload.git.sha;
392
437
  }
393
- if (!existsSync(env.GITHUB_EVENT_PATH)) {
394
- return null;
438
+ if (!context.env.GITHUB_SHA) {
439
+ throw new Error(`GITHUB_SHA is missing`);
395
440
  }
396
- return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, "utf-8"));
441
+ return context.env.GITHUB_SHA;
397
442
  }
398
443
  function getPullRequestFromPayload(payload) {
399
444
  if ("pull_request" in payload && payload.pull_request && payload.pull_request) {
@@ -407,26 +452,27 @@ function getPullRequestFromPayload(payload) {
407
452
  }
408
453
  return null;
409
454
  }
410
- function getVercelDeploymentPayload(payload) {
411
- if (process.env.GITHUB_EVENT_NAME === "repository_dispatch" && payload && "action" in payload && payload.action === "vercel.deployment.success") {
412
- return payload;
413
- }
414
- return null;
415
- }
416
- function getMergeGroupPayload(payload) {
417
- if (payload && process.env.GITHUB_EVENT_NAME === "merge_group" && "action" in payload && payload.action === "checks_requested") {
418
- return payload;
419
- }
420
- return null;
421
- }
422
- function getSha(context, vercelPayload) {
423
- if (vercelPayload) {
424
- return vercelPayload.client_payload.git.sha;
425
- }
426
- if (!context.env.GITHUB_SHA) {
427
- throw new Error(`GITHUB_SHA is missing`);
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);
428
474
  }
429
- return context.env.GITHUB_SHA;
475
+ return getPullRequestFromPayload(payload);
430
476
  }
431
477
  var service4 = {
432
478
  name: "GitHub Actions",
@@ -438,34 +484,33 @@ var service4 = {
438
484
  const vercelPayload = getVercelDeploymentPayload(payload);
439
485
  const mergeGroupPayload = getMergeGroupPayload(payload);
440
486
  const sha = getSha(context, vercelPayload);
441
- const pullRequest = await (() => {
442
- if (vercelPayload || !payload) {
443
- return getPullRequestFromHeadSha(context, sha);
444
- }
445
- if (mergeGroupPayload) {
446
- const prNumber = getPRNumberFromMergeGroupBranch(
447
- mergeGroupPayload.merge_group.head_ref
448
- );
449
- if (!prNumber) {
450
- return null;
451
- }
452
- return getPullRequestFromPrNumber(context, prNumber);
453
- }
454
- return getPullRequestFromPayload(payload);
455
- })();
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
+ });
456
501
  return {
457
502
  commit: sha,
458
503
  repository: getRepository3(context, payload),
459
- originalRepository: getOriginalRepository(context),
504
+ originalRepository: getGitHubRepository(context),
460
505
  jobId: env.GITHUB_JOB || null,
461
506
  runId: env.GITHUB_RUN_ID || null,
462
507
  runAttempt: env.GITHUB_RUN_ATTEMPT ? Number(env.GITHUB_RUN_ATTEMPT) : null,
463
508
  nonce: `${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}`,
464
- branch: (mergeGroupPayload ? pullRequest?.head.ref : null) || vercelPayload?.client_payload?.git?.ref || getBranchFromContext(context) || pullRequest?.head.ref || (payload ? getBranchFromPayload(payload) : null) || null,
509
+ branch: branch2,
465
510
  prNumber: pullRequest?.number || null,
466
511
  prHeadCommit: pullRequest?.head.sha ?? null,
467
512
  prBaseBranch: pullRequest?.base.ref ?? null,
468
- mergeQueue: mergeGroupPayload?.action === "checks_requested"
513
+ mergeQueue: Boolean(mergeGroupPayload)
469
514
  };
470
515
  },
471
516
  getMergeBaseCommitSha,
@@ -487,9 +532,9 @@ function getRepository4(context) {
487
532
  if (env.CIRCLE_PR_REPONAME && env.CIRCLE_PR_USERNAME) {
488
533
  return `${env.CIRCLE_PR_USERNAME}/${env.CIRCLE_PR_REPONAME}`;
489
534
  }
490
- return getOriginalRepository2(context);
535
+ return getOriginalRepository(context);
491
536
  }
492
- function getOriginalRepository2(context) {
537
+ function getOriginalRepository(context) {
493
538
  const { env } = context;
494
539
  if (env.CIRCLE_PROJECT_USERNAME && env.CIRCLE_PROJECT_REPONAME) {
495
540
  return `${env.CIRCLE_PROJECT_USERNAME}/${env.CIRCLE_PROJECT_REPONAME}`;
@@ -506,7 +551,7 @@ var service5 = {
506
551
  commit: env.CIRCLE_SHA1 || null,
507
552
  branch: env.CIRCLE_BRANCH || null,
508
553
  repository: getRepository4(context),
509
- originalRepository: getOriginalRepository2(context),
554
+ originalRepository: getOriginalRepository(context),
510
555
  jobId: null,
511
556
  runId: null,
512
557
  runAttempt: null,
@@ -528,9 +573,9 @@ function getRepository5(context) {
528
573
  if (env.TRAVIS_PULL_REQUEST_SLUG) {
529
574
  return env.TRAVIS_PULL_REQUEST_SLUG;
530
575
  }
531
- return getOriginalRepository3(context);
576
+ return getOriginalRepository2(context);
532
577
  }
533
- function getOriginalRepository3(context) {
578
+ function getOriginalRepository2(context) {
534
579
  const { env } = context;
535
580
  return env.TRAVIS_REPO_SLUG || null;
536
581
  }
@@ -551,7 +596,7 @@ var service6 = {
551
596
  commit: env.TRAVIS_COMMIT || null,
552
597
  branch: env.TRAVIS_BRANCH || null,
553
598
  repository: getRepository5(ctx),
554
- originalRepository: getOriginalRepository3(ctx),
599
+ originalRepository: getOriginalRepository2(ctx),
555
600
  jobId: null,
556
601
  runId: null,
557
602
  runAttempt: null,
@@ -573,9 +618,9 @@ function getRepository6(context) {
573
618
  if (env.CI_MERGE_REQUEST_PROJECT_PATH) {
574
619
  return env.CI_MERGE_REQUEST_PROJECT_PATH;
575
620
  }
576
- return getOriginalRepository4(context);
621
+ return getOriginalRepository3(context);
577
622
  }
578
- function getOriginalRepository4(context) {
623
+ function getOriginalRepository3(context) {
579
624
  const { env } = context;
580
625
  return env.CI_PROJECT_PATH || null;
581
626
  }
@@ -589,7 +634,7 @@ var service7 = {
589
634
  commit: env.CI_COMMIT_SHA || null,
590
635
  branch: env.CI_COMMIT_REF_NAME || null,
591
636
  repository: getRepository6(context),
592
- originalRepository: getOriginalRepository4(context),
637
+ originalRepository: getOriginalRepository3(context),
593
638
  jobId: null,
594
639
  runId: null,
595
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.2",
4
+ "version": "5.0.3",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "exports": {
@@ -67,5 +67,5 @@
67
67
  "lint": "eslint .",
68
68
  "test": "vitest"
69
69
  },
70
- "gitHead": "c0561c96e1f820633037a87448791f4ed57e086a"
70
+ "gitHead": "1f0eb0144b92ccb1368d0a15b31c67ea9479f727"
71
71
  }