@argos-ci/core 1.4.2-alpha.6 → 1.5.1

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 +44 -16
  2. package/package.json +2 -2
package/dist/index.mjs CHANGED
@@ -118,18 +118,37 @@ const debugTimeEnd = (arg)=>{
118
118
  };
119
119
 
120
120
  /**
121
- * When triggered by a deployment we try to get the pull request number from the
122
- * deployment sha.
121
+ * Get a pull request from a head sha.
122
+ * Fetch the last 30 pull requests sorted by updated date
123
+ * then try to find the one that matches the head sha.
124
+ * If no pull request is found, return null.
123
125
  */ async function getPullRequestFromHeadSha({ env }, sha) {
124
126
  debug("Fetching pull request number from head sha", sha);
125
- if (!env.GITHUB_REPOSITORY || !env.GITHUB_TOKEN) {
126
- debug("Aborting because GITHUB_REPOSITORY or GITHUB_TOKEN is missing");
127
+ if (!env.GITHUB_REPOSITORY) {
128
+ throw new Error("GITHUB_REPOSITORY is missing");
129
+ }
130
+ if (!env.GITHUB_TOKEN) {
131
+ // For security reasons, people doesn't want to expose their GITHUB_TOKEN
132
+ // That's why we allow to disable this warning.
133
+ if (!env.DISABLE_GITHUB_TOKEN_WARNING) {
134
+ console.log(`
135
+ Running argos from a "deployment_status" event requires a GITHUB_TOKEN.
136
+ Please add \`GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}\` as environment variable.
137
+
138
+ Read more at https://argos-ci.com/docs/run-on-preview-deployment
139
+
140
+ To disable this warning, add \`DISABLE_GITHUB_TOKEN_WARNING: true\` as environment variable.
141
+ `.trim());
142
+ }
127
143
  return null;
128
144
  }
129
145
  try {
130
146
  const result = await axios.get(`https://api.github.com/repos/${env.GITHUB_REPOSITORY}/pulls`, {
131
147
  params: {
132
- head: sha
148
+ state: "open",
149
+ sort: "updated",
150
+ per_page: 30,
151
+ page: 1
133
152
  },
134
153
  headers: {
135
154
  Accept: "application/vnd.github+json",
@@ -141,11 +160,15 @@ const debugTimeEnd = (arg)=>{
141
160
  debug("Aborting because no pull request found");
142
161
  return null;
143
162
  }
144
- const firstPr = result.data[0];
145
- debug("PR found", firstPr);
146
- return firstPr;
163
+ const matchingPr = result.data.find((pr)=>pr.head.sha === sha);
164
+ if (matchingPr) {
165
+ debug("Pull request found", matchingPr);
166
+ return matchingPr;
167
+ }
168
+ debug("Aborting because no pull request found");
169
+ return null;
147
170
  } catch (error) {
148
- debug("Error while fetching pull request number from head sha", error);
171
+ debug("Error while fetching pull request from head sha", error);
149
172
  return null;
150
173
  }
151
174
  }
@@ -154,11 +177,11 @@ const getBranch = ({ env })=>{
154
177
  return env.GITHUB_HEAD_REF;
155
178
  }
156
179
  const branchRegex = /refs\/heads\/(.*)/;
157
- const matches = branchRegex.exec(env.GITHUB_REF || "");
158
- if (matches) {
159
- return matches[1];
180
+ if (!env.GITHUB_REF) {
181
+ return null;
160
182
  }
161
- return null;
183
+ const matches = branchRegex.exec(env.GITHUB_REF);
184
+ return (matches === null || matches === void 0 ? void 0 : matches[1]) ?? null;
162
185
  };
163
186
  const getRepository$1 = ({ env })=>{
164
187
  if (!env.GITHUB_REPOSITORY) return null;
@@ -177,7 +200,12 @@ const service$4 = {
177
200
  const payload = readEventPayload({
178
201
  env
179
202
  });
203
+ const sha = process.env.GITHUB_SHA || null;
204
+ if (!sha) {
205
+ throw new Error(`GITHUB_SHA is missing`);
206
+ }
180
207
  const commonConfig = {
208
+ commit: sha,
181
209
  owner: env.GITHUB_REPOSITORY_OWNER || null,
182
210
  repository: getRepository$1({
183
211
  env
@@ -189,13 +217,14 @@ const service$4 = {
189
217
  // If the job is triggered by from a "deployment" or a "deployment_status"
190
218
  if (payload === null || payload === void 0 ? void 0 : payload.deployment) {
191
219
  debug("Deployment event detected");
192
- const { sha } = payload.deployment;
220
+ // Try to find a relevant pull request for the sha
193
221
  const pullRequest = await getPullRequestFromHeadSha({
194
222
  env
195
223
  }, sha);
196
224
  return {
197
225
  ...commonConfig,
198
- commit: payload.deployment.sha,
226
+ // If no pull request is found, we fallback to the deployment environment as branch name
227
+ // Branch name is required to create a build but has no real impact on the build.
199
228
  branch: (pullRequest === null || pullRequest === void 0 ? void 0 : pullRequest.head.ref) || payload.deployment.environment || null,
200
229
  prNumber: (pullRequest === null || pullRequest === void 0 ? void 0 : pullRequest.number) || null,
201
230
  prHeadCommit: (pullRequest === null || pullRequest === void 0 ? void 0 : pullRequest.head.sha) || null
@@ -203,7 +232,6 @@ const service$4 = {
203
232
  }
204
233
  return {
205
234
  ...commonConfig,
206
- commit: process.env.GITHUB_SHA || null,
207
235
  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({
208
236
  env
209
237
  }) || null,
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.2-alpha.6+505aabe",
4
+ "version": "1.5.1",
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": "505aabe2ed2b8455172e9a8a473bccf683591a7c"
62
+ "gitHead": "174a9847d8a547d4fa4acb7602ea3687dc9bde8e"
63
63
  }