@argos-ci/core 0.4.1 → 0.4.2-alpha.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 +77 -21
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -29,7 +29,7 @@ const mustBeCommit = (value)=>{
29
29
  }
30
30
  };
31
31
  const mustBeArgosToken = (value)=>{
32
- if (value.length !== 40) {
32
+ if (value && value.length !== 40) {
33
33
  throw new Error("Must be a valid Argos repository token");
34
34
  }
35
35
  };
@@ -52,7 +52,7 @@ const schema = {
52
52
  },
53
53
  token: {
54
54
  env: "ARGOS_TOKEN",
55
- default: "",
55
+ default: null,
56
56
  format: mustBeArgosToken
57
57
  },
58
58
  buildName: {
@@ -82,6 +82,21 @@ const schema = {
82
82
  format: String,
83
83
  default: null,
84
84
  nullable: true
85
+ },
86
+ jobId: {
87
+ format: String,
88
+ default: null,
89
+ nullable: true
90
+ },
91
+ owner: {
92
+ format: String,
93
+ default: null,
94
+ nullable: true
95
+ },
96
+ repository: {
97
+ format: String,
98
+ default: null,
99
+ nullable: true
85
100
  }
86
101
  };
87
102
  const createConfig = ()=>{
@@ -107,7 +122,10 @@ const service$1 = {
107
122
  config: ({ env })=>({
108
123
  name: "Heroku",
109
124
  commit: env.HEROKU_TEST_RUN_COMMIT_VERSION || null,
110
- branch: env.HEROKU_TEST_RUN_BRANCH || null
125
+ branch: env.HEROKU_TEST_RUN_BRANCH || null,
126
+ owner: null,
127
+ repository: null,
128
+ jobId: env.HEROKU_TEST_RUN_ID || null
111
129
  })
112
130
  };
113
131
 
@@ -116,20 +134,26 @@ const getSha = ({ env })=>{
116
134
  if (isPr) {
117
135
  const mergeCommitRegex = /^[a-z0-9]{40} [a-z0-9]{40}$/;
118
136
  const mergeCommitMessage = execSync("git show --no-patch --format=%P").toString().trim();
119
- console.log(`Handling PR with parent hash(es) '${mergeCommitMessage}' of current commit.`);
137
+ // console.log(
138
+ // `Handling PR with parent hash(es) '${mergeCommitMessage}' of current commit.`
139
+ // );
120
140
  if (mergeCommitRegex.exec(mergeCommitMessage)) {
121
141
  const mergeCommit = mergeCommitMessage.split(" ")[1];
122
- console.log(`Fixing merge commit SHA ${process.env.GITHUB_SHA} -> ${mergeCommit}`);
142
+ // console.log(
143
+ // `Fixing merge commit SHA ${process.env.GITHUB_SHA} -> ${mergeCommit}`
144
+ // );
123
145
  return mergeCommit;
124
146
  } else if (mergeCommitMessage === "") {
125
- console.error(`
126
- Issue detecting commit SHA. Please run actions/checkout with "fetch-depth: 2":
147
+ console.error(`Error: automatic detection of commit SHA failed.
148
+
149
+ Please run "actions/checkout" with "fetch-depth: 2". Example:
127
150
 
128
- steps:
129
- - uses: actions/checkout@v3
130
- with:
131
- fetch-depth: 2
132
- `.trim());
151
+ steps:
152
+ - uses: actions/checkout@v3
153
+ with:
154
+ fetch-depth: 2
155
+
156
+ `);
133
157
  process.exit(1);
134
158
  } else {
135
159
  console.error(`Commit with SHA ${process.env.GITHUB_SHA} is not a valid commit`);
@@ -152,13 +176,16 @@ function getBranch({ env }) {
152
176
  const service = {
153
177
  detect: ({ env })=>Boolean(env.GITHUB_ACTIONS),
154
178
  config: ({ env })=>({
155
- name: "GiHub Actions",
179
+ name: "GitHub Actions",
156
180
  commit: getSha({
157
181
  env
158
182
  }),
159
183
  branch: getBranch({
160
184
  env
161
- })
185
+ }),
186
+ owner: env.GITHUB_REPOSITORY_OWNER || null,
187
+ repository: env.GITHUB_REPOSITORY || null,
188
+ jobId: env.GITHUB_JOB || null
162
189
  })
163
190
  };
164
191
 
@@ -180,10 +207,17 @@ const getCiEnvironment = ({ env =process.env } = {})=>{
180
207
  const name = ciContext.isCi ? ciContext.name ?? null : ciContext.commit ? "Git" : null;
181
208
  const commit = ciContext.commit ?? null;
182
209
  const branch = (ciContext.branch || ciContext.prBranch) ?? null;
210
+ const slug = ciContext.slug ? ciContext.slug.split("/") : null;
211
+ const owner = slug ? slug[0] : null;
212
+ const repository = slug ? slug[1] : null;
213
+ const jobId = ciContext.job ?? null;
183
214
  return commit ? {
184
215
  name,
185
216
  commit,
186
- branch
217
+ branch,
218
+ owner,
219
+ repository,
220
+ jobId
187
221
  } : null;
188
222
  };
189
223
 
@@ -242,11 +276,30 @@ const hashFile = async (filepath)=>{
242
276
  return hash.read().toString("hex");
243
277
  };
244
278
 
279
+ const base64Encode = (obj)=>Buffer.from(JSON.stringify(obj), "utf8").toString("base64");
280
+ const getBearerToken = ({ token , ciService , owner , repository , jobId })=>{
281
+ if (token) return `Bearer ${token}`;
282
+ switch(ciService){
283
+ case "GitHub Actions":
284
+ {
285
+ if (!owner || !repository || !jobId) {
286
+ throw new Error(`Automatic ${ciService} variables detection failed. Please add the 'ARGOS_TOKEN'`);
287
+ }
288
+ return `Bearer tokenless-github-${base64Encode({
289
+ owner,
290
+ repository,
291
+ jobId
292
+ })}`;
293
+ }
294
+ default:
295
+ throw new Error("Missing Argos repository token 'ARGOS_TOKEN'");
296
+ }
297
+ };
245
298
  const createArgosApiClient = (options)=>{
246
299
  const axiosInstance = axios.create({
247
300
  baseURL: options.baseUrl,
248
301
  headers: {
249
- Authorization: `Bearer ${options.token}`,
302
+ Authorization: options.bearerToken,
250
303
  "Content-Type": "application/json",
251
304
  Accept: "application/json"
252
305
  }
@@ -324,7 +377,10 @@ const getConfigFromOptions = (options)=>{
324
377
  config.load(omitUndefined({
325
378
  commit: ciEnv.commit,
326
379
  branch: ciEnv.branch,
327
- ciService: ciEnv.name
380
+ ciService: ciEnv.name,
381
+ owner: ciEnv.owner,
382
+ repository: ciEnv.repository,
383
+ jobId: ciEnv.jobId
328
384
  }));
329
385
  }
330
386
  }
@@ -339,6 +395,10 @@ const getConfigFromOptions = (options)=>{
339
395
  const files = params.files ?? [
340
396
  "**/*.{png,jpg,jpeg}"
341
397
  ];
398
+ const apiClient = createArgosApiClient({
399
+ baseUrl: config.apiBaseUrl,
400
+ bearerToken: getBearerToken(config)
401
+ });
342
402
  // Collect screenshots
343
403
  const foundScreenshots = await discoverScreenshots(files, {
344
404
  root: params.root,
@@ -356,10 +416,6 @@ const getConfigFromOptions = (options)=>{
356
416
  hash
357
417
  };
358
418
  }));
359
- const apiClient = createArgosApiClient({
360
- baseUrl: config.apiBaseUrl,
361
- token: config.token
362
- });
363
419
  // Create build
364
420
  debug("Creating build");
365
421
  const result = await apiClient.createBuild({
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.4.1",
4
+ "version": "0.4.2-alpha.3+1c181d8",
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.2",
61
61
  "rollup-plugin-swc3": "^0.3.0"
62
62
  },
63
- "gitHead": "ec7abd2a79de3d185681ff34b5e699c56dd964d1"
63
+ "gitHead": "1c181d8e4e5258b2749840a3790cabda30b9a24e"
64
64
  }