Skip to content

[jsweep] Clean workflow_metadata_helpers.cjs#37490

Merged
pelikhan merged 2 commits into
mainfrom
signed/jsweep/workflow-metadata-helpers-714b7a50d8eae882
Jun 7, 2026
Merged

[jsweep] Clean workflow_metadata_helpers.cjs#37490
pelikhan merged 2 commits into
mainfrom
signed/jsweep/workflow-metadata-helpers-714b7a50d8eae882

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions Bot commented Jun 7, 2026

Summary

Cleans up workflow_metadata_helpers.cjs as part of the jsweep initiative: tightens a JSDoc type annotation and strengthens the associated test suite with additional coverage and reliable env-var cleanup.


Changes

actions/setup/js/workflow_metadata_helpers.cjs

  • JSDoc tightened: The ctx parameter type for buildWorkflowRunUrl is narrowed from the untyped {any} to the precise {{ serverUrl?: string, runId: number | string }}, making the function's contract explicit and improving IDE/type-checker support.

actions/setup/js/workflow_metadata_helpers.test.cjs

  • Four new test cases covering:
    • context.serverUrl taking priority over the GITHUB_SERVER_URL environment variable.
    • https://github.com as the fallback when no server URL source is present.
    • runId of 0 (falsy edge case).
  • Refactored an existing test to use try/finally for reliable env-var cleanup, fixing a gap where a test failure could leave environment state polluted for subsequent tests.

Impact

Area Impact Breaking
workflow_metadata_helpers.cjs Low — annotation only, no runtime change No
workflow_metadata_helpers.test.cjs Low — test-only additions and cleanup No

Type

  • Refactor / code quality (jsweep clean-up)
  • Test improvement

Test plan

Existing and new unit tests in workflow_metadata_helpers.test.cjs cover the changed code paths. No production logic was altered; the JSDoc change is annotation-only.

Generated by PR Description Updater for issue #37490 · 105.6 AIC · ⌖ 12.4 AIC · ⊞ 19.5K ·

- Improve JSDoc type for buildWorkflowRunUrl ctx parameter:
  {any} → {{ serverUrl?: string, runId: number | string }}
- Add 4 new test cases (7 → 11 total):
  - getWorkflowMetadata: prefers context.serverUrl over GITHUB_SERVER_URL
  - getWorkflowMetadata: falls back to https://github.com with no sources
  - buildWorkflowRunUrl: falls back to https://github.com with no sources
  - buildWorkflowRunUrl: handles runId of 0 correctly

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan pelikhan marked this pull request as ready for review June 7, 2026 05:36
Copilot AI review requested due to automatic review settings June 7, 2026 05:36
@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions Bot commented Jun 7, 2026

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions Bot commented Jun 7, 2026

🧪 Test Quality Sentinel completed test quality analysis.

@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions Bot commented Jun 7, 2026

PR Code Quality Reviewer completed the code quality review.

@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions Bot commented Jun 7, 2026

Design Decision Gate 🏗️ completed the design decision gate check.

No ADR enforcement needed: PR #37490 does not have the "implementation" label and has 0 new lines of code in business logic directories (≤100 threshold). No custom .design-gate.yml config present.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR is part of the jsweep “JavaScript unbloat” initiative and focuses on tightening the contract and test coverage for workflow run URL construction used in github-script contexts.

Changes:

  • Refines the JSDoc type for buildWorkflowRunUrl(ctx, ...) to a precise structural type for stronger API clarity.
  • Adds test coverage for server URL precedence, default URL fallback, and runId === 0 handling across getWorkflowMetadata and buildWorkflowRunUrl.
Show a summary per file
File Description
actions/setup/js/workflow_metadata_helpers.cjs Narrows buildWorkflowRunUrl’s JSDoc ctx type to a more explicit structural contract.
actions/setup/js/workflow_metadata_helpers.test.cjs Adds 4 tests covering server URL precedence and fallback behavior, plus runId edge cases.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 1

Comment on lines +150 to +163
it("should fall back to https://github.com when no server URL sources are available", () => {
const originalEnv = process.env.GITHUB_SERVER_URL;
delete process.env.GITHUB_SERVER_URL;

const url = buildWorkflowRunUrl({ runId: 1 }, { owner: "owner", repo: "repo" });

expect(url).toBe("https://github.com/owner/repo/actions/runs/1");

if (originalEnv === undefined) {
delete process.env.GITHUB_SERVER_URL;
} else {
process.env.GITHUB_SERVER_URL = originalEnv;
}
});
@github-actions github-actions Bot mentioned this pull request Jun 7, 2026
@github-actions
Copy link
Copy Markdown
Contributor Author

github-actions Bot commented Jun 7, 2026

🧪 Test Quality Sentinel Report

Test Quality Score: 90/100 — Excellent

Analyzed 4 test(s) across 1 JavaScript file (*.test.cjs): 4 design tests, 0 implementation tests, 0 coding-guideline violations. Minor test-isolation concerns noted.

📊 Metrics & Test Classification (4 tests analyzed)
Metric Value
New/modified tests analyzed 4
✅ Design tests (behavioral contracts) 4 (100%)
⚠️ Implementation tests (low value) 0 (0%)
Tests with error/edge cases 4 (100%)
Duplicate test clusters 0
Test inflation detected ⚠️ Yes — 41:1 ratio (false positive; see note)
🚨 Coding-guideline violations 0

Test Classification Details

Test File Classification Issues Detected
should prefer context.serverUrl over GITHUB_SERVER_URL env var workflow_metadata_helpers.test.cjs (getWorkflowMetadata) ✅ Design global.context not restored after test; process.env leak
should fall back to https://github.com when no server URL sources are available workflow_metadata_helpers.test.cjs (getWorkflowMetadata) ✅ Design global.context not restored after test
should fall back to https://github.com when no server URL sources are available workflow_metadata_helpers.test.cjs (buildWorkflowRunUrl) ✅ Design Teardown not in try/finally (env leak if assertion throws)
should handle runId of 0 workflow_metadata_helpers.test.cjs (buildWorkflowRunUrl) ✅ Design None

Language Support

Tests analyzed:

  • 🟨 JavaScript (*.test.cjs): 4 tests (vitest)
  • 🐹 Go (*_test.go): 0 tests changed
⚠️ Test-Isolation Concerns (not coding-guideline violations, but worth fixing)

⚠️ global.context not restored after tests 1 & 2

Tests: should prefer context.serverUrl... and should fall back... (getWorkflowMetadata describe block)
Issue: Both tests write directly to global.context and never restore it. This creates hidden test-ordering dependencies — later tests that rely on global.context being absent may fail when run after these tests.
Suggested fix: Use vi.stubGlobal("context", { ... }) (auto-restored by Vitest) or add afterEach(() => { delete global.context }) cleanup.


⚠️ process.env.GITHUB_SERVER_URL leaks between tests 1 and 2

Issue: Test 1 sets process.env.GITHUB_SERVER_URL but never cleans it up. Test 2 accidentally clears it, masking the leak. If tests run in a different order (e.g., with --shuffle), test 1's side effect persists into downstream tests.
Suggested fix: Use vi.stubEnv("GITHUB_SERVER_URL", value) — Vitest automatically restores env vars after each test.


⚠️ Fragile inline teardown in buildWorkflowRunUrl fallback test

Issue: The env-var restore block in test 3 runs after the expect call. If the assertion throws, teardown is skipped and process.env.GITHUB_SERVER_URL remains deleted for the rest of the suite.
Suggested fix: Replace the manual save/restore pattern with vi.stubEnv("GITHUB_SERVER_URL", ...) or move cleanup into afterEach.


i️ Duplicate test names across describe blocks

Tests 2 and 3 share the name "should fall back to https://github.com when no server URL sources are available". While scoped under different describe blocks, identical names create ambiguity in CI failure output and --testNamePattern filtering. Consider distinct names such as "should fall back to https://github.com (no env var)" vs "should fall back to https://github.com (no context serverUrl or env var)".

📏 Test Inflation Note

The raw ratio is 41 test lines added : 1 production line added, which technically triggers the >2:1 inflation penalty (−10 pts). However, this is a false positive: the sole production change is a JSDoc comment improvement — no new logic was added. The new tests are backfilling coverage for pre-existing behavior, which is exactly what a "jsweep" cleanup PR should do. The −10 inflation penalty is applied mechanically per the scoring rubric but does not reflect a genuine quality concern.

Verdict

Check passed. 0% of new tests are implementation tests (threshold: 30%). All 4 new tests verify behavioral contracts with edge-case coverage. Minor test-isolation improvements are suggested above but do not block this PR.

📖 Understanding Test Classifications

Design Tests (High Value) verify what the system does:

  • Assert on observable outputs, return values, or state changes
  • Cover error paths and boundary conditions
  • Would catch a behavioral regression if deleted
  • Remain valid even after internal refactoring

Implementation Tests (Low Value) verify how the system does it:

  • Assert on internal function calls (mocking internals)
  • Only test the happy path with typical inputs
  • Break during legitimate refactoring even when behavior is correct
  • Give false assurance: they pass even when the system is wrong

Goal: Shift toward tests that describe the system's behavioral contract — the promises it makes to its users and collaborators.

References:

🧪 Test quality analysis by Test Quality Sentinel · sonnet46 813.2K · 244 AIC · ⌖ 19.7 AIC ·

Copy link
Copy Markdown
Contributor Author

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Test Quality Sentinel: 90/100. Test quality is excellent — 0% of new tests are implementation tests (threshold: 30%). All 4 new tests verify behavioral contracts with edge-case coverage.

Copy link
Copy Markdown
Contributor Author

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Skills-Based Review 🧠

Applied /zoom-out — one minor robustness observation in the new tests; no blocking issues.

📋 Key Themes & Highlights

Key Themes

  • Test env isolation: the new buildWorkflowRunUrl fallback test uses a manual save/restore pattern without try-finally. The fix is to add beforeEach/afterEach hooks to the buildWorkflowRunUrl describe block, matching the pattern already used in getWorkflowMetadata (inline comment on line 150).

Positive Highlights

  • JSDoc type upgrade — replacing {any} with {{ serverUrl?: string, runId: number | string }} is a clear API-contract improvement that benefits all ~20 callers with zero runtime risk.
  • Four well-targeted new tests — serverUrl priority ordering, https://github.com fallback, and runId: 0 edge case were genuine coverage gaps; all now documented as specifications.
  • getWorkflowMetadata tests use beforeEach/afterEach correctly — clean, robust, easy to extend.
  • ✅ All 11 tests pass; format, lint, and typecheck ✓.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · sonnet46 569K · 170.7 AIC · ⌖ 13.3 AIC

expect(url).not.toContain("cross-repo");
});

it("should fall back to https://github.com when no server URL sources are available", () => {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[/zoom-out] This test manually saves/restores GITHUB_SERVER_URL, but the cleanup at lines 158–162 is not guarded by try-finally. If the assertion on line 155 throws, cleanup is skipped and the deleted env var leaks into subsequent tests in this block.

�� Suggested: add lifecycle hooks to the buildWorkflowRunUrl describe block

The getWorkflowMetadata describe block (lines 5–118) already establishes the right pattern with beforeEach/afterEach. Applying the same to buildWorkflowRunUrl would let individual tests drop their manual save/restore entirely:

describe("buildWorkflowRunUrl", () => {
  let savedServerUrl;

  beforeEach(() => {
    savedServerUrl = process.env.GITHUB_SERVER_URL;
  });

  afterEach(() => {
    if (savedServerUrl === undefined) {
      delete process.env.GITHUB_SERVER_URL;
    } else {
      process.env.GITHUB_SERVER_URL = savedServerUrl;
    }
  });

  // tests can now freely mutate process.env.GITHUB_SERVER_URL
});

As a smaller improvement, the existing if (originalEnv === undefined) { delete process.env.GITHUB_SERVER_URL; } branch (line 158) is a no-op — the var was already deleted at line 152 — so the branch can be simplified to just else { process.env.GITHUB_SERVER_URL = originalEnv; }.

Copy link
Copy Markdown
Contributor Author

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One non-blocking finding in the new test code. The JSDoc improvement to buildWorkflowRunUrl is correct.

🔍 Review summary

What was checked

  • workflow_metadata_helpers.cjs: only the @param {any}@param {{ serverUrl?: string, runId: number | string }} JSDoc change — clean improvement, no issues.
  • workflow_metadata_helpers.test.cjs: 4 new test cases added.

Finding: env cleanup not failure-safe (medium, non-blocking)

The new buildWorkflowRunUrl test "should fall back to https://github.com when no server URL sources are available" restores GITHUB_SERVER_URL after the expect() call. A failing assertion skips the restore, leaving GITHUB_SERVER_URL deleted for subsequent tests. The existing test at line 127 has the same pattern and would benefit from the same fix. Inline comment posted.

Dropped (out of diff)

Several pre-existing concerns were surfaced (implicit context global, URL component encoding, runId undefined guard) but none of those lines were touched in this PR and so are out of scope for this review.

🔎 Code quality review by PR Code Quality Reviewer · sonnet46 349.9K · ⌖ 13.4 AIC

delete process.env.GITHUB_SERVER_URL;
} else {
process.env.GITHUB_SERVER_URL = originalEnv;
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Env-var cleanup runs after the assertion — state leaks on test failure.

If expect(url).toBe(...) throws, the if/else restore block below it is never reached and GITHUB_SERVER_URL stays deleted for all tests that run afterward in the same process.

�� Suggested fix — wrap in try/finally
it("should fall back to https://github.com when no server URL sources are available", () => {
  const originalEnv = process.env.GITHUB_SERVER_URL;
  delete process.env.GITHUB_SERVER_URL;
  try {
    const url = buildWorkflowRunUrl({ runId: 1 }, { owner: "owner", repo: "repo" });
    expect(url).toBe("https://github.com/owner/repo/actions/runs/1");
  } finally {
    if (originalEnv === undefined) {
      delete process.env.GITHUB_SERVER_URL;
    } else {
      process.env.GITHUB_SERVER_URL = originalEnv;
    }
  }
});

Alternatively, add a beforeEach/afterEach to the buildWorkflowRunUrl describe block that snapshots and restores process.env (matching the pattern already used in the getWorkflowMetadata suite above). The existing test at line 127 has the same fragility and would benefit from the same fix.

@pelikhan
Copy link
Copy Markdown
Collaborator

pelikhan commented Jun 7, 2026

@copilot review all comments

Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI commented Jun 7, 2026

@copilot review all comments

Addressed. I updated the workflow metadata helper tests to restore GITHUB_SERVER_URL in try/finally blocks so cleanup always runs, and pushed commit 4979a27.

Copilot AI requested a review from pelikhan June 7, 2026 06:14
@pelikhan pelikhan merged commit 069cda2 into main Jun 7, 2026
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants