@argos-ci/core 0.7.1 → 0.7.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 +87 -56
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -1,7 +1,8 @@
1
1
  import convict from 'convict';
2
- import envCi from 'env-ci';
3
- import { execSync } from 'child_process';
2
+ import { execSync } from 'node:child_process';
3
+ import { execSync as execSync$1 } from 'child_process';
4
4
  import createDebug from 'debug';
5
+ import envCi from 'env-ci';
5
6
  import { resolve } from 'node:path';
6
7
  import glob from 'fast-glob';
7
8
  import { promisify } from 'node:util';
@@ -115,20 +116,41 @@ const createConfig = ()=>{
115
116
  });
116
117
  };
117
118
 
119
+ /**
120
+ * Returns the head commit.
121
+ */ const head = ()=>{
122
+ try {
123
+ return execSync("git rev-parse HEAD").toString().trim();
124
+ } catch {
125
+ return null;
126
+ }
127
+ };
128
+ /**
129
+ * Returns the current branch.
130
+ */ const branch = ()=>{
131
+ try {
132
+ const headRef = execSync("git rev-parse --abbrev-ref HEAD").toString().trim();
133
+ if (headRef === "HEAD") {
134
+ return null;
135
+ }
136
+ return headRef;
137
+ } catch {
138
+ return null;
139
+ }
140
+ };
141
+
118
142
  const service$4 = {
119
143
  name: "Buildkite",
120
144
  detect: ({ env })=>Boolean(env.BUILDKITE),
121
145
  config: ({ env })=>{
122
- const ciProps = envCiDetection({
123
- env
124
- });
125
146
  return {
126
- commit: ciProps?.commit || null,
127
- branch: env.BUILDKITE_BRANCH || null,
147
+ // Buildkite doesn't work well so we fallback to git to ensure we have commit and branch
148
+ commit: env.BUILDKITE_COMMIT || head() || null,
149
+ branch: env.BUILDKITE_BRANCH || branch() || null,
128
150
  owner: env.BUILDKITE_ORGANIZATION_SLUG || null,
129
151
  repository: env.BUILDKITE_PROJECT_SLUG || null,
130
- jobId: env.BUILDKITE_JOB_ID || null,
131
- runId: ciProps?.runId || null,
152
+ jobId: null,
153
+ runId: null,
132
154
  prNumber: env.BUILDKITE_PULL_REQUEST ? Number(env.BUILDKITE_PULL_REQUEST) : null
133
155
  };
134
156
  }
@@ -142,7 +164,7 @@ const service$3 = {
142
164
  branch: env.HEROKU_TEST_RUN_BRANCH || null,
143
165
  owner: null,
144
166
  repository: null,
145
- jobId: env.HEROKU_TEST_RUN_ID || null,
167
+ jobId: null,
146
168
  runId: null,
147
169
  prNumber: null
148
170
  })
@@ -152,7 +174,7 @@ const getSha = ({ env })=>{
152
174
  const isPr = env.GITHUB_EVENT_NAME === "pull_request" || env.GITHUB_EVENT_NAME === "pull_request_target";
153
175
  if (isPr) {
154
176
  const mergeCommitRegex = /^[a-z0-9]{40} [a-z0-9]{40}$/;
155
- const mergeCommitMessage = execSync("git show --no-patch --format=%P").toString().trim();
177
+ const mergeCommitMessage = execSync$1("git show --no-patch --format=%P").toString().trim();
156
178
  // console.log(
157
179
  // `Handling PR with parent hash(es) '${mergeCommitMessage}' of current commit.`
158
180
  // );
@@ -181,22 +203,22 @@ Please run "actions/checkout" with "fetch-depth: 2". Example:
181
203
  }
182
204
  return process.env.GITHUB_SHA ?? null;
183
205
  };
184
- function getBranch({ env }) {
206
+ const getBranch = ({ env })=>{
185
207
  if (env.GITHUB_HEAD_REF) {
186
208
  return env.GITHUB_HEAD_REF;
187
209
  }
188
210
  const branchRegex = /refs\/heads\/(.*)/;
189
- const branchMatches = branchRegex.exec(env.GITHUB_REF || "");
190
- if (branchMatches) {
191
- return branchMatches[1];
211
+ const matches = branchRegex.exec(env.GITHUB_REF || "");
212
+ if (matches) {
213
+ return matches[1];
192
214
  }
193
215
  return null;
194
- }
195
- function getRepository({ env }) {
216
+ };
217
+ const getRepository$1 = ({ env })=>{
196
218
  if (!env.GITHUB_REPOSITORY) return null;
197
219
  return env.GITHUB_REPOSITORY.split("/")[1];
198
- }
199
- const getPrNumber$1 = ({ env })=>{
220
+ };
221
+ const getPrNumber$2 = ({ env })=>{
200
222
  const branchRegex = /refs\/pull\/(\d+)/;
201
223
  const branchMatches = branchRegex.exec(env.GITHUB_REF || "");
202
224
  if (branchMatches) {
@@ -215,22 +237,22 @@ const service$2 = {
215
237
  env
216
238
  }),
217
239
  owner: env.GITHUB_REPOSITORY_OWNER || null,
218
- repository: getRepository({
240
+ repository: getRepository$1({
219
241
  env
220
242
  }),
221
243
  jobId: env.GITHUB_JOB || null,
222
244
  runId: env.GITHUB_RUN_ID || null,
223
- prNumber: getPrNumber$1({
245
+ prNumber: getPrNumber$2({
224
246
  env
225
247
  })
226
248
  })
227
249
  };
228
250
 
229
- const getPrNumber = ({ env })=>{
251
+ const getPrNumber$1 = ({ env })=>{
230
252
  const branchRegex = /pull\/(\d+)/;
231
- const branchMatches = branchRegex.exec(env.CIRCLE_PULL_REQUEST || "");
232
- if (branchMatches) {
233
- return Number(branchMatches[1]);
253
+ const matches = branchRegex.exec(env.CIRCLE_PULL_REQUEST || "");
254
+ if (matches) {
255
+ return Number(matches[1]);
234
256
  }
235
257
  return null;
236
258
  };
@@ -238,52 +260,52 @@ const service$1 = {
238
260
  name: "CircleCI",
239
261
  detect: ({ env })=>Boolean(env.CIRCLECI),
240
262
  config: ({ env })=>{
241
- const ciProps = envCiDetection({
242
- env
243
- });
244
263
  return {
245
- commit: ciProps?.commit || null,
246
- branch: ciProps?.branch || null,
247
- owner: ciProps?.owner || null,
248
- repository: ciProps?.repository || null,
249
- jobId: ciProps?.jobId || null,
250
- runId: ciProps?.runId || null,
251
- prNumber: getPrNumber({
264
+ commit: env.CIRCLE_SHA1 || null,
265
+ branch: env.CIRCLE_BRANCH || null,
266
+ owner: env.CIRCLE_PROJECT_USERNAME || null,
267
+ repository: env.CIRCLE_PROJECT_REPONAME || null,
268
+ jobId: null,
269
+ runId: null,
270
+ prNumber: getPrNumber$1({
252
271
  env
253
272
  })
254
273
  };
255
274
  }
256
275
  };
257
276
 
277
+ const getOwner = ({ env })=>{
278
+ if (!env.TRAVIS_REPO_SLUG) return null;
279
+ return env.TRAVIS_REPO_SLUG.split("/")[0] || null;
280
+ };
281
+ const getRepository = ({ env })=>{
282
+ if (!env.TRAVIS_REPO_SLUG) return null;
283
+ return env.TRAVIS_REPO_SLUG.split("/")[1] || null;
284
+ };
285
+ const getPrNumber = ({ env })=>{
286
+ if (env.TRAVIS_PULL_REQUEST) return Number(env.TRAVIS_PULL_REQUEST);
287
+ return null;
288
+ };
258
289
  const service = {
259
290
  name: "Travis CI",
260
291
  detect: ({ env })=>Boolean(env.TRAVIS),
261
- config: ({ env })=>{
262
- const ciProps = envCiDetection({
263
- env
264
- });
292
+ config: (ctx)=>{
293
+ const { env } = ctx;
265
294
  return {
266
- commit: ciProps?.commit || null,
267
- branch: ciProps?.branch || null,
268
- owner: ciProps?.owner || null,
269
- repository: ciProps?.repository || null,
270
- jobId: ciProps?.jobId || null,
271
- runId: ciProps?.runId || null,
272
- prNumber: env.TRAVIS_PULL_REQUEST ? Number(env.TRAVIS_PULL_REQUEST) : null
295
+ commit: env.TRAVIS_COMMIT || null,
296
+ branch: env.TRAVIS_BRANCH || null,
297
+ owner: getOwner(ctx),
298
+ repository: getRepository(ctx),
299
+ jobId: null,
300
+ runId: null,
301
+ prNumber: getPrNumber(ctx)
273
302
  };
274
303
  }
275
304
  };
276
305
 
277
306
  const debug = createDebug("@argos-ci/core");
278
307
 
279
- const services = [
280
- service$3,
281
- service$2,
282
- service$1,
283
- service,
284
- service$4
285
- ];
286
- const envCiDetection = (ctx)=>{
308
+ const getCiEnvironmentFromEnvCi = (ctx)=>{
287
309
  const ciContext = envCi(ctx);
288
310
  const name = ciContext.isCi ? ciContext.name ?? null : ciContext.commit ? "Git" : null;
289
311
  const commit = ciContext.commit ?? null;
@@ -305,6 +327,14 @@ const envCiDetection = (ctx)=>{
305
327
  prNumber
306
328
  } : null;
307
329
  };
330
+
331
+ const services = [
332
+ service$3,
333
+ service$2,
334
+ service$1,
335
+ service,
336
+ service$4
337
+ ];
308
338
  const getCiEnvironment = ({ env =process.env } = {})=>{
309
339
  const ctx = {
310
340
  env
@@ -313,7 +343,7 @@ const getCiEnvironment = ({ env =process.env } = {})=>{
313
343
  env
314
344
  });
315
345
  const service = services.find((service)=>service.detect(ctx));
316
- // Internal service matched
346
+ // Service matched
317
347
  if (service) {
318
348
  debug("Internal service matched", service.name);
319
349
  const variables = service.config(ctx);
@@ -324,8 +354,9 @@ const getCiEnvironment = ({ env =process.env } = {})=>{
324
354
  debug("CI environment", ciEnvironment);
325
355
  return ciEnvironment;
326
356
  }
357
+ // We fallback on "env-ci" library, not very good but it's better than nothing
327
358
  debug("Falling back on env-ci");
328
- const ciEnvironment1 = envCiDetection(ctx);
359
+ const ciEnvironment1 = getCiEnvironmentFromEnvCi(ctx);
329
360
  debug("CI environment", ciEnvironment1);
330
361
  return ciEnvironment1;
331
362
  };
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": "0.7.1",
4
+ "version": "0.7.2",
5
5
  "scripts": {
6
6
  "prebuild": "rm -rf dist",
7
7
  "build": "rollup -c",
@@ -60,5 +60,5 @@
60
60
  "rollup-plugin-dts": "^4.2.3",
61
61
  "rollup-plugin-swc3": "^0.6.0"
62
62
  },
63
- "gitHead": "0ae6b18c7728deb14beb7ffee85e9faf104c4365"
63
+ "gitHead": "7adf2cb62ceb13887c7ddf675f4d19e2bd2572b9"
64
64
  }