@argos-ci/core 0.7.1 → 0.7.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 (2) hide show
  1. package/dist/index.mjs +99 -104
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -1,13 +1,13 @@
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 { existsSync, readFileSync, createReadStream } from 'node:fs';
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';
8
9
  import sharp from 'sharp';
9
10
  import tmp from 'tmp';
10
- import { createReadStream } from 'node:fs';
11
11
  import { createHash } from 'node:crypto';
12
12
  import axios from 'axios';
13
13
  import { readFile } from 'node:fs/promises';
@@ -115,20 +115,41 @@ const createConfig = ()=>{
115
115
  });
116
116
  };
117
117
 
118
+ /**
119
+ * Returns the head commit.
120
+ */ const head = ()=>{
121
+ try {
122
+ return execSync("git rev-parse HEAD").toString().trim();
123
+ } catch {
124
+ return null;
125
+ }
126
+ };
127
+ /**
128
+ * Returns the current branch.
129
+ */ const branch = ()=>{
130
+ try {
131
+ const headRef = execSync("git rev-parse --abbrev-ref HEAD").toString().trim();
132
+ if (headRef === "HEAD") {
133
+ return null;
134
+ }
135
+ return headRef;
136
+ } catch {
137
+ return null;
138
+ }
139
+ };
140
+
118
141
  const service$4 = {
119
142
  name: "Buildkite",
120
143
  detect: ({ env })=>Boolean(env.BUILDKITE),
121
144
  config: ({ env })=>{
122
- const ciProps = envCiDetection({
123
- env
124
- });
125
145
  return {
126
- commit: ciProps?.commit || null,
127
- branch: env.BUILDKITE_BRANCH || null,
146
+ // Buildkite doesn't work well so we fallback to git to ensure we have commit and branch
147
+ commit: env.BUILDKITE_COMMIT || head() || null,
148
+ branch: env.BUILDKITE_BRANCH || branch() || null,
128
149
  owner: env.BUILDKITE_ORGANIZATION_SLUG || null,
129
150
  repository: env.BUILDKITE_PROJECT_SLUG || null,
130
- jobId: env.BUILDKITE_JOB_ID || null,
131
- runId: ciProps?.runId || null,
151
+ jobId: null,
152
+ runId: null,
132
153
  prNumber: env.BUILDKITE_PULL_REQUEST ? Number(env.BUILDKITE_PULL_REQUEST) : null
133
154
  };
134
155
  }
@@ -142,95 +163,60 @@ const service$3 = {
142
163
  branch: env.HEROKU_TEST_RUN_BRANCH || null,
143
164
  owner: null,
144
165
  repository: null,
145
- jobId: env.HEROKU_TEST_RUN_ID || null,
166
+ jobId: null,
146
167
  runId: null,
147
168
  prNumber: null
148
169
  })
149
170
  };
150
171
 
151
- const getSha = ({ env })=>{
152
- const isPr = env.GITHUB_EVENT_NAME === "pull_request" || env.GITHUB_EVENT_NAME === "pull_request_target";
153
- if (isPr) {
154
- const mergeCommitRegex = /^[a-z0-9]{40} [a-z0-9]{40}$/;
155
- const mergeCommitMessage = execSync("git show --no-patch --format=%P").toString().trim();
156
- // console.log(
157
- // `Handling PR with parent hash(es) '${mergeCommitMessage}' of current commit.`
158
- // );
159
- if (mergeCommitRegex.exec(mergeCommitMessage)) {
160
- const mergeCommit = mergeCommitMessage.split(" ")[1];
161
- // console.log(
162
- // `Fixing merge commit SHA ${process.env.GITHUB_SHA} -> ${mergeCommit}`
163
- // );
164
- return mergeCommit;
165
- } else if (mergeCommitMessage === "") {
166
- console.error(`Error: automatic detection of commit SHA failed.
167
-
168
- Please run "actions/checkout" with "fetch-depth: 2". Example:
169
-
170
- steps:
171
- - uses: actions/checkout@v3
172
- with:
173
- fetch-depth: 2
174
-
175
- `);
176
- process.exit(1);
177
- } else {
178
- console.error(`Commit with SHA ${process.env.GITHUB_SHA} is not a valid commit`);
179
- process.exit(1);
180
- }
181
- }
182
- return process.env.GITHUB_SHA ?? null;
183
- };
184
- function getBranch({ env }) {
172
+ const getBranch = ({ env })=>{
185
173
  if (env.GITHUB_HEAD_REF) {
186
174
  return env.GITHUB_HEAD_REF;
187
175
  }
188
176
  const branchRegex = /refs\/heads\/(.*)/;
189
- const branchMatches = branchRegex.exec(env.GITHUB_REF || "");
190
- if (branchMatches) {
191
- return branchMatches[1];
177
+ const matches = branchRegex.exec(env.GITHUB_REF || "");
178
+ if (matches) {
179
+ return matches[1];
192
180
  }
193
181
  return null;
194
- }
195
- function getRepository({ env }) {
182
+ };
183
+ const getRepository$1 = ({ env })=>{
196
184
  if (!env.GITHUB_REPOSITORY) return null;
197
185
  return env.GITHUB_REPOSITORY.split("/")[1];
198
- }
199
- const getPrNumber$1 = ({ env })=>{
200
- const branchRegex = /refs\/pull\/(\d+)/;
201
- const branchMatches = branchRegex.exec(env.GITHUB_REF || "");
202
- if (branchMatches) {
203
- return Number(branchMatches[1]);
204
- }
205
- return null;
186
+ };
187
+ const readEventPayload = ({ env })=>{
188
+ if (!env.GITHUB_EVENT_PATH) return null;
189
+ if (!existsSync(env.GITHUB_EVENT_PATH)) return null;
190
+ return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, "utf-8"));
206
191
  };
207
192
  const service$2 = {
208
193
  name: "GitHub Actions",
209
194
  detect: ({ env })=>Boolean(env.GITHUB_ACTIONS),
210
- config: ({ env })=>({
211
- commit: getSha({
212
- env
213
- }),
214
- branch: getBranch({
195
+ config: ({ env })=>{
196
+ const payload = readEventPayload({
197
+ env
198
+ });
199
+ return {
200
+ commit: payload?.pull_request?.head.sha || process.env.GITHUB_SHA || null,
201
+ branch: payload?.pull_request?.head.ref || getBranch({
215
202
  env
216
- }),
203
+ }) || null,
217
204
  owner: env.GITHUB_REPOSITORY_OWNER || null,
218
- repository: getRepository({
205
+ repository: getRepository$1({
219
206
  env
220
207
  }),
221
208
  jobId: env.GITHUB_JOB || null,
222
209
  runId: env.GITHUB_RUN_ID || null,
223
- prNumber: getPrNumber$1({
224
- env
225
- })
226
- })
210
+ prNumber: payload?.pull_request?.number || null
211
+ };
212
+ }
227
213
  };
228
214
 
229
- const getPrNumber = ({ env })=>{
215
+ const getPrNumber$1 = ({ env })=>{
230
216
  const branchRegex = /pull\/(\d+)/;
231
- const branchMatches = branchRegex.exec(env.CIRCLE_PULL_REQUEST || "");
232
- if (branchMatches) {
233
- return Number(branchMatches[1]);
217
+ const matches = branchRegex.exec(env.CIRCLE_PULL_REQUEST || "");
218
+ if (matches) {
219
+ return Number(matches[1]);
234
220
  }
235
221
  return null;
236
222
  };
@@ -238,52 +224,52 @@ const service$1 = {
238
224
  name: "CircleCI",
239
225
  detect: ({ env })=>Boolean(env.CIRCLECI),
240
226
  config: ({ env })=>{
241
- const ciProps = envCiDetection({
242
- env
243
- });
244
227
  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({
228
+ commit: env.CIRCLE_SHA1 || null,
229
+ branch: env.CIRCLE_BRANCH || null,
230
+ owner: env.CIRCLE_PROJECT_USERNAME || null,
231
+ repository: env.CIRCLE_PROJECT_REPONAME || null,
232
+ jobId: null,
233
+ runId: null,
234
+ prNumber: getPrNumber$1({
252
235
  env
253
236
  })
254
237
  };
255
238
  }
256
239
  };
257
240
 
241
+ const getOwner = ({ env })=>{
242
+ if (!env.TRAVIS_REPO_SLUG) return null;
243
+ return env.TRAVIS_REPO_SLUG.split("/")[0] || null;
244
+ };
245
+ const getRepository = ({ env })=>{
246
+ if (!env.TRAVIS_REPO_SLUG) return null;
247
+ return env.TRAVIS_REPO_SLUG.split("/")[1] || null;
248
+ };
249
+ const getPrNumber = ({ env })=>{
250
+ if (env.TRAVIS_PULL_REQUEST) return Number(env.TRAVIS_PULL_REQUEST);
251
+ return null;
252
+ };
258
253
  const service = {
259
254
  name: "Travis CI",
260
255
  detect: ({ env })=>Boolean(env.TRAVIS),
261
- config: ({ env })=>{
262
- const ciProps = envCiDetection({
263
- env
264
- });
256
+ config: (ctx)=>{
257
+ const { env } = ctx;
265
258
  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
259
+ commit: env.TRAVIS_COMMIT || null,
260
+ branch: env.TRAVIS_BRANCH || null,
261
+ owner: getOwner(ctx),
262
+ repository: getRepository(ctx),
263
+ jobId: null,
264
+ runId: null,
265
+ prNumber: getPrNumber(ctx)
273
266
  };
274
267
  }
275
268
  };
276
269
 
277
270
  const debug = createDebug("@argos-ci/core");
278
271
 
279
- const services = [
280
- service$3,
281
- service$2,
282
- service$1,
283
- service,
284
- service$4
285
- ];
286
- const envCiDetection = (ctx)=>{
272
+ const getCiEnvironmentFromEnvCi = (ctx)=>{
287
273
  const ciContext = envCi(ctx);
288
274
  const name = ciContext.isCi ? ciContext.name ?? null : ciContext.commit ? "Git" : null;
289
275
  const commit = ciContext.commit ?? null;
@@ -305,6 +291,14 @@ const envCiDetection = (ctx)=>{
305
291
  prNumber
306
292
  } : null;
307
293
  };
294
+
295
+ const services = [
296
+ service$3,
297
+ service$2,
298
+ service$1,
299
+ service,
300
+ service$4
301
+ ];
308
302
  const getCiEnvironment = ({ env =process.env } = {})=>{
309
303
  const ctx = {
310
304
  env
@@ -313,7 +307,7 @@ const getCiEnvironment = ({ env =process.env } = {})=>{
313
307
  env
314
308
  });
315
309
  const service = services.find((service)=>service.detect(ctx));
316
- // Internal service matched
310
+ // Service matched
317
311
  if (service) {
318
312
  debug("Internal service matched", service.name);
319
313
  const variables = service.config(ctx);
@@ -324,8 +318,9 @@ const getCiEnvironment = ({ env =process.env } = {})=>{
324
318
  debug("CI environment", ciEnvironment);
325
319
  return ciEnvironment;
326
320
  }
321
+ // We fallback on "env-ci" library, not very good but it's better than nothing
327
322
  debug("Falling back on env-ci");
328
- const ciEnvironment1 = envCiDetection(ctx);
323
+ const ciEnvironment1 = getCiEnvironmentFromEnvCi(ctx);
329
324
  debug("CI environment", ciEnvironment1);
330
325
  return ciEnvironment1;
331
326
  };
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.3",
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": "56d7e2259161c49265d8983ce0f0ecfe71910d2b"
64
64
  }