@argos-ci/core 1.4.1 → 1.5.0

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/dist/index.d.ts CHANGED
@@ -32,7 +32,7 @@ interface UploadParameters {
32
32
  /**
33
33
  * Upload screenshots to argos-ci.com.
34
34
  */
35
- declare const upload: (params: UploadParameters) => Promise<{
35
+ declare function upload(params: UploadParameters): Promise<{
36
36
  build: {
37
37
  id: string;
38
38
  url: string;
@@ -67,5 +67,5 @@ interface Config {
67
67
  prNumber: number | null;
68
68
  prHeadCommit: string | null;
69
69
  }
70
- declare const readConfig: (options?: Partial<Config>) => Config;
70
+ declare function readConfig(options?: Partial<Config>): Promise<Config>;
71
71
  export { UploadParameters, upload, Config, readConfig };
package/dist/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import convict from 'convict';
2
2
  import { execSync } from 'node:child_process';
3
3
  import { existsSync, readFileSync, createReadStream } from 'node:fs';
4
+ import axios from 'axios';
4
5
  import createDebug from 'debug';
5
6
  import { resolve } from 'node:path';
6
7
  import glob from 'fast-glob';
@@ -8,7 +9,6 @@ import { promisify } from 'node:util';
8
9
  import sharp from 'sharp';
9
10
  import tmp from 'tmp';
10
11
  import { createHash } from 'node:crypto';
11
- import axios from 'axios';
12
12
  import { readFile } from 'node:fs/promises';
13
13
  import { readMetadata, getPlaywrightTracePath } from '@argos-ci/util';
14
14
 
@@ -102,6 +102,65 @@ const service$5 = {
102
102
  })
103
103
  };
104
104
 
105
+ const KEY = "@argos-ci/core";
106
+ const debug = createDebug(KEY);
107
+ const debugTime = (arg)=>{
108
+ const enabled = createDebug.enabled(KEY);
109
+ if (enabled) {
110
+ console.time(arg);
111
+ }
112
+ };
113
+ const debugTimeEnd = (arg)=>{
114
+ const enabled = createDebug.enabled(KEY);
115
+ if (enabled) {
116
+ console.timeEnd(arg);
117
+ }
118
+ };
119
+
120
+ /**
121
+ * When triggered by a deployment we try to get the pull request number from the
122
+ * deployment sha.
123
+ */ async function getPullRequestFromHeadSha({ env }, sha) {
124
+ debug("Fetching pull request number from head sha", sha);
125
+ if (!env.GITHUB_REPOSITORY) {
126
+ throw new Error("GITHUB_REPOSITORY is missing");
127
+ }
128
+ if (!env.GITHUB_TOKEN) {
129
+ if (!env.DISABLE_GITHUB_TOKEN_WARNING) {
130
+ console.log(`
131
+ Running argos from a "deployment_status" event requires a GITHUB_TOKEN.
132
+ Please add \`GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}\` as environment variable.
133
+
134
+ Read more at https://argos-ci.com/docs/run-on-preview-deployment
135
+
136
+ To disable this warning, add \`DISABLE_GITHUB_TOKEN_WARNING: true\` as environment variable.
137
+ `.trim());
138
+ }
139
+ return null;
140
+ }
141
+ try {
142
+ const result = await axios.get(`https://api.github.com/repos/${env.GITHUB_REPOSITORY}/pulls`, {
143
+ params: {
144
+ head: sha
145
+ },
146
+ headers: {
147
+ Accept: "application/vnd.github+json",
148
+ Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
149
+ "X-GitHub-Api-Version": "2022-11-28"
150
+ }
151
+ });
152
+ if (result.data.length === 0) {
153
+ debug("Aborting because no pull request found");
154
+ return null;
155
+ }
156
+ const firstPr = result.data[0];
157
+ debug("PR found", firstPr);
158
+ return firstPr;
159
+ } catch (error) {
160
+ debug("Error while fetching pull request from head sha", error);
161
+ return null;
162
+ }
163
+ }
105
164
  const getBranch = ({ env })=>{
106
165
  if (env.GITHUB_HEAD_REF) {
107
166
  return env.GITHUB_HEAD_REF;
@@ -125,26 +184,46 @@ const readEventPayload = ({ env })=>{
125
184
  const service$4 = {
126
185
  name: "GitHub Actions",
127
186
  detect: ({ env })=>Boolean(env.GITHUB_ACTIONS),
128
- config: ({ env })=>{
187
+ config: async ({ env })=>{
129
188
  var _payload_pull_request, _payload_pull_request1, _payload_pull_request2;
130
189
  const payload = readEventPayload({
131
190
  env
132
191
  });
133
- return {
134
- commit: process.env.GITHUB_SHA || null,
135
- branch: (payload === null || payload === void 0 ? void 0 : (_payload_pull_request = payload.pull_request) === null || _payload_pull_request === void 0 ? void 0 : _payload_pull_request.head.ref) || getBranch({
136
- env
137
- }) || null,
192
+ const sha = process.env.GITHUB_SHA || null;
193
+ if (!sha) {
194
+ throw new Error(`GITHUB_SHA is missing`);
195
+ }
196
+ const commonConfig = {
197
+ commit: sha,
138
198
  owner: env.GITHUB_REPOSITORY_OWNER || null,
139
199
  repository: getRepository$1({
140
200
  env
141
201
  }),
142
202
  jobId: env.GITHUB_JOB || null,
143
203
  runId: env.GITHUB_RUN_ID || null,
144
- prNumber: (payload === null || payload === void 0 ? void 0 : (_payload_pull_request1 = payload.pull_request) === null || _payload_pull_request1 === void 0 ? void 0 : _payload_pull_request1.number) || null,
145
- prHeadCommit: (payload === null || payload === void 0 ? void 0 : (_payload_pull_request2 = payload.pull_request) === null || _payload_pull_request2 === void 0 ? void 0 : _payload_pull_request2.head.sha) ?? null,
146
204
  nonce: `${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` || null
147
205
  };
206
+ // If the job is triggered by from a "deployment" or a "deployment_status"
207
+ if (payload === null || payload === void 0 ? void 0 : payload.deployment) {
208
+ debug("Deployment event detected");
209
+ const pullRequest = await getPullRequestFromHeadSha({
210
+ env
211
+ }, sha);
212
+ return {
213
+ ...commonConfig,
214
+ branch: (pullRequest === null || pullRequest === void 0 ? void 0 : pullRequest.head.ref) || payload.deployment.environment || null,
215
+ prNumber: (pullRequest === null || pullRequest === void 0 ? void 0 : pullRequest.number) || null,
216
+ prHeadCommit: (pullRequest === null || pullRequest === void 0 ? void 0 : pullRequest.head.sha) || null
217
+ };
218
+ }
219
+ return {
220
+ ...commonConfig,
221
+ branch: (payload === null || payload === void 0 ? void 0 : (_payload_pull_request = payload.pull_request) === null || _payload_pull_request === void 0 ? void 0 : _payload_pull_request.head.ref) || getBranch({
222
+ env
223
+ }) || null,
224
+ prNumber: (payload === null || payload === void 0 ? void 0 : (_payload_pull_request1 = payload.pull_request) === null || _payload_pull_request1 === void 0 ? void 0 : _payload_pull_request1.number) || null,
225
+ prHeadCommit: (payload === null || payload === void 0 ? void 0 : (_payload_pull_request2 = payload.pull_request) === null || _payload_pull_request2 === void 0 ? void 0 : _payload_pull_request2.head.sha) ?? null
226
+ };
148
227
  }
149
228
  };
150
229
 
@@ -243,21 +322,6 @@ const service = {
243
322
  }
244
323
  };
245
324
 
246
- const KEY = "@argos-ci/core";
247
- const debug = createDebug(KEY);
248
- const debugTime = (arg)=>{
249
- const enabled = createDebug.enabled(KEY);
250
- if (enabled) {
251
- console.time(arg);
252
- }
253
- };
254
- const debugTimeEnd = (arg)=>{
255
- const enabled = createDebug.enabled(KEY);
256
- if (enabled) {
257
- console.timeEnd(arg);
258
- }
259
- };
260
-
261
325
  // List of services ordered by usage
262
326
  // "git" must be the last one
263
327
  const services = [
@@ -270,7 +334,7 @@ const services = [
270
334
  service$7,
271
335
  service
272
336
  ];
273
- const getCiEnvironment = ({ env = process.env } = {})=>{
337
+ async function getCiEnvironment({ env = process.env } = {}) {
274
338
  const ctx = {
275
339
  env
276
340
  };
@@ -281,7 +345,7 @@ const getCiEnvironment = ({ env = process.env } = {})=>{
281
345
  // Service matched
282
346
  if (service) {
283
347
  debug("Internal service matched", service.name);
284
- const variables = service.config(ctx);
348
+ const variables = await service.config(ctx);
285
349
  const ciEnvironment = {
286
350
  name: service.name,
287
351
  ...variables
@@ -290,7 +354,7 @@ const getCiEnvironment = ({ env = process.env } = {})=>{
290
354
  return ciEnvironment;
291
355
  }
292
356
  return null;
293
- };
357
+ }
294
358
 
295
359
  const mustBeApiBaseUrl = (value)=>{
296
360
  const URL_REGEX = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/;
@@ -412,9 +476,9 @@ const createConfig = ()=>{
412
476
  args: []
413
477
  });
414
478
  };
415
- const readConfig = (options = {})=>{
479
+ async function readConfig(options = {}) {
416
480
  const config = createConfig();
417
- const ciEnv = getCiEnvironment();
481
+ const ciEnv = await getCiEnvironment();
418
482
  config.load({
419
483
  apiBaseUrl: options.apiBaseUrl ?? config.get("apiBaseUrl"),
420
484
  commit: options.commit ?? config.get("commit") ?? (ciEnv === null || ciEnv === void 0 ? void 0 : ciEnv.commit) ?? null,
@@ -436,7 +500,7 @@ const readConfig = (options = {})=>{
436
500
  });
437
501
  config.validate();
438
502
  return config.get();
439
- };
503
+ }
440
504
 
441
505
  const discoverScreenshots = async (patterns, { root = process.cwd(), ignore } = {})=>{
442
506
  const matches = await glob(patterns, {
@@ -581,15 +645,14 @@ const upload$1 = async (input)=>{
581
645
  /**
582
646
  * Size of the chunks used to upload screenshots to Argos.
583
647
  */ const CHUNK_SIZE = 10;
584
- const getConfigFromOptions = ({ parallel, ...options })=>{
585
- const config = readConfig({
648
+ async function getConfigFromOptions({ parallel, ...options }) {
649
+ return readConfig({
586
650
  ...options,
587
651
  parallel: Boolean(parallel),
588
652
  parallelNonce: parallel ? parallel.nonce : null,
589
653
  parallelTotal: parallel ? parallel.total : null
590
654
  });
591
- return config;
592
- };
655
+ }
593
656
  async function uploadFilesToS3(files) {
594
657
  debug(`Split files in chunks of ${CHUNK_SIZE}`);
595
658
  const chunks = chunk(files, CHUNK_SIZE);
@@ -611,11 +674,11 @@ async function uploadFilesToS3(files) {
611
674
  }
612
675
  /**
613
676
  * Upload screenshots to argos-ci.com.
614
- */ const upload = async (params)=>{
677
+ */ async function upload(params) {
615
678
  var _result_pwTraces;
616
679
  debug("Starting upload with params", params);
617
680
  // Read config
618
- const config = getConfigFromOptions(params);
681
+ const config = await getConfigFromOptions(params);
619
682
  const files = params.files ?? [
620
683
  "**/*.{png,jpg,jpeg}"
621
684
  ];
@@ -728,6 +791,6 @@ async function uploadFilesToS3(files) {
728
791
  build: result.build,
729
792
  screenshots
730
793
  };
731
- };
794
+ }
732
795
 
733
796
  export { readConfig, upload };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@argos-ci/core",
3
3
  "description": "Visual testing solution to avoid visual regression. The core component of Argos SDK that handles build creation.",
4
- "version": "1.4.1",
4
+ "version": "1.5.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
7
7
  "types": "./dist/index.d.ts",
@@ -59,5 +59,5 @@
59
59
  "build": "rollup -c",
60
60
  "e2e": "node ./e2e/upload.cjs && node ./e2e/upload.mjs"
61
61
  },
62
- "gitHead": "b601673bab3248f9a615ded0cca76af78830eeec"
62
+ "gitHead": "140f6598e42c90fcaa929fd213fadea15b0f5f3d"
63
63
  }