@vizzly-testing/cli 0.23.0 → 0.23.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.
@@ -4,6 +4,47 @@
4
4
  * Generic functions to extract git and PR information from any CI provider
5
5
  */
6
6
 
7
+ import { readFileSync } from 'node:fs';
8
+
9
+ // Cache for GitHub Actions event payload to avoid re-reading the file
10
+ let _githubEventCache = null;
11
+
12
+ /**
13
+ * Read and parse the GitHub Actions event payload from GITHUB_EVENT_PATH.
14
+ *
15
+ * GitHub Actions sets GITHUB_EVENT_PATH to a file containing the full webhook
16
+ * payload that triggered the workflow. This is essential for pull_request and
17
+ * pull_request_target events because GITHUB_SHA points to a merge commit,
18
+ * not the actual head commit.
19
+ *
20
+ * @returns {Object} Parsed event payload or empty object on failure
21
+ */
22
+ export function getGitHubEvent() {
23
+ if (_githubEventCache !== null) {
24
+ return _githubEventCache;
25
+ }
26
+ let eventPath = process.env.GITHUB_EVENT_PATH;
27
+ if (!eventPath) {
28
+ _githubEventCache = {};
29
+ return _githubEventCache;
30
+ }
31
+ try {
32
+ let content = readFileSync(eventPath, 'utf8');
33
+ _githubEventCache = JSON.parse(content);
34
+ } catch {
35
+ // File doesn't exist or invalid JSON - fail silently with empty object
36
+ _githubEventCache = {};
37
+ }
38
+ return _githubEventCache;
39
+ }
40
+
41
+ /**
42
+ * Reset the GitHub event cache. Useful for testing.
43
+ */
44
+ export function resetGitHubEventCache() {
45
+ _githubEventCache = null;
46
+ }
47
+
7
48
  /**
8
49
  * Get the branch name from CI environment variables
9
50
  * @returns {string|null} Branch name or null if not available
@@ -45,15 +86,41 @@ export function getBranch() {
45
86
  }
46
87
 
47
88
  /**
48
- * Get the commit SHA from CI environment variables
89
+ * Get the commit SHA from CI environment variables.
90
+ *
91
+ * IMPORTANT: For GitHub Actions pull_request events, GITHUB_SHA points to a
92
+ * temporary merge commit, NOT the actual head commit of the PR. This function
93
+ * reads the event payload from GITHUB_EVENT_PATH to extract the correct SHA.
94
+ *
95
+ * See: https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request
96
+ * "GITHUB_SHA for this event is the last merge commit of the pull request merge branch.
97
+ * If you want to get the commit ID for the last commit to the head branch of the
98
+ * pull request, use github.event.pull_request.head.sha instead."
99
+ *
49
100
  * @returns {string|null} Commit SHA or null if not available
50
101
  */
51
102
  export function getCommit() {
52
- return process.env.VIZZLY_COMMIT_SHA ||
53
- // Vizzly override
54
- process.env.GITHUB_SHA ||
55
- // GitHub Actions
56
- process.env.CI_COMMIT_SHA ||
103
+ // Vizzly override always takes priority
104
+ if (process.env.VIZZLY_COMMIT_SHA) {
105
+ return process.env.VIZZLY_COMMIT_SHA;
106
+ }
107
+
108
+ // GitHub Actions: extract the correct SHA based on event type
109
+ if (process.env.GITHUB_ACTIONS) {
110
+ let event = getGitHubEvent();
111
+
112
+ // For pull_request events, use the actual head commit SHA (not the merge commit)
113
+ // The event payload contains pull_request.head.sha which is what we want
114
+ if (event.pull_request?.head?.sha) {
115
+ return event.pull_request.head.sha;
116
+ }
117
+
118
+ // For push events or if event parsing failed, GITHUB_SHA is correct
119
+ return process.env.GITHUB_SHA || null;
120
+ }
121
+
122
+ // Other CI providers
123
+ return process.env.CI_COMMIT_SHA ||
57
124
  // GitLab CI
58
125
  process.env.CIRCLE_SHA1 ||
59
126
  // CircleCI
@@ -170,15 +237,30 @@ export function getPullRequestNumber() {
170
237
  }
171
238
 
172
239
  /**
173
- * Get the PR head SHA from CI environment variables
240
+ * Get the PR head SHA from CI environment variables.
241
+ *
242
+ * For GitHub Actions, this reads from the event payload to get the actual
243
+ * head commit SHA, not the merge commit that GITHUB_SHA points to.
244
+ *
174
245
  * @returns {string|null} PR head SHA or null if not available
175
246
  */
176
247
  export function getPullRequestHeadSha() {
177
- return process.env.VIZZLY_PR_HEAD_SHA ||
178
- // Vizzly override
179
- process.env.GITHUB_SHA ||
180
- // GitHub Actions
181
- process.env.CI_COMMIT_SHA ||
248
+ // Vizzly override always takes priority
249
+ if (process.env.VIZZLY_PR_HEAD_SHA) {
250
+ return process.env.VIZZLY_PR_HEAD_SHA;
251
+ }
252
+
253
+ // GitHub Actions: extract from event payload for PRs
254
+ if (process.env.GITHUB_ACTIONS) {
255
+ let event = getGitHubEvent();
256
+ if (event.pull_request?.head?.sha) {
257
+ return event.pull_request.head.sha;
258
+ }
259
+ return process.env.GITHUB_SHA || null;
260
+ }
261
+
262
+ // Other CI providers
263
+ return process.env.CI_COMMIT_SHA ||
182
264
  // GitLab CI
183
265
  process.env.CIRCLE_SHA1 ||
184
266
  // CircleCI
@@ -200,13 +282,29 @@ export function getPullRequestHeadSha() {
200
282
  }
201
283
 
202
284
  /**
203
- * Get the PR base SHA from CI environment variables
285
+ * Get the PR base SHA from CI environment variables.
286
+ *
287
+ * For GitHub Actions, this reads from the event payload to get the base
288
+ * branch SHA that the PR is targeting.
289
+ *
204
290
  * @returns {string|null} PR base SHA or null if not available
205
291
  */
206
292
  export function getPullRequestBaseSha() {
207
- return process.env.VIZZLY_PR_BASE_SHA ||
208
- // Vizzly override
209
- process.env.CI_MERGE_REQUEST_TARGET_BRANCH_SHA ||
293
+ // Vizzly override always takes priority
294
+ if (process.env.VIZZLY_PR_BASE_SHA) {
295
+ return process.env.VIZZLY_PR_BASE_SHA;
296
+ }
297
+
298
+ // GitHub Actions: extract from event payload
299
+ if (process.env.GITHUB_ACTIONS) {
300
+ let event = getGitHubEvent();
301
+ if (event.pull_request?.base?.sha) {
302
+ return event.pull_request.base.sha;
303
+ }
304
+ }
305
+
306
+ // Other CI providers
307
+ return process.env.CI_MERGE_REQUEST_TARGET_BRANCH_SHA ||
210
308
  // GitLab CI
211
309
  null // Most CIs don't provide this
212
310
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vizzly-testing/cli",
3
- "version": "0.23.0",
3
+ "version": "0.23.1",
4
4
  "description": "Visual review platform for UI developers and designers",
5
5
  "keywords": [
6
6
  "visual-testing",