-
Notifications
You must be signed in to change notification settings - Fork 416
[jsweep] Clean workflow_metadata_helpers.cjs #37490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -94,6 +94,27 @@ describe("getWorkflowMetadata", () => { | |
| expect(metadata.runId).toBe(0); | ||
| expect(metadata.runUrl).toBe("https://github.com/test-owner/test-repo/actions/runs/0"); | ||
| }); | ||
|
|
||
| it("should prefer context.serverUrl over GITHUB_SERVER_URL env var", () => { | ||
| global.context = { | ||
| runId: 42, | ||
| serverUrl: "https://context-server.example.com", | ||
| }; | ||
| process.env.GITHUB_SERVER_URL = "https://env-server.example.com"; | ||
|
|
||
| const metadata = getWorkflowMetadata("owner", "repo"); | ||
|
|
||
| expect(metadata.runUrl).toBe("https://context-server.example.com/owner/repo/actions/runs/42"); | ||
| }); | ||
|
|
||
| it("should fall back to https://github.com when no server URL sources are available", () => { | ||
| global.context = { runId: 7 }; | ||
| delete process.env.GITHUB_SERVER_URL; | ||
|
|
||
| const metadata = getWorkflowMetadata("my-owner", "my-repo"); | ||
|
|
||
| expect(metadata.runUrl).toBe("https://github.com/my-owner/my-repo/actions/runs/7"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("buildWorkflowRunUrl", () => { | ||
|
|
@@ -106,13 +127,16 @@ describe("buildWorkflowRunUrl", () => { | |
| it("should fall back to GITHUB_SERVER_URL when context.serverUrl is absent", () => { | ||
| const originalEnv = process.env.GITHUB_SERVER_URL; | ||
| process.env.GITHUB_SERVER_URL = "https://ghes.example.com"; | ||
| const ctx = { runId: 99 }; | ||
| const url = buildWorkflowRunUrl(ctx, { owner: "ent-owner", repo: "ent-repo" }); | ||
| expect(url).toBe("https://ghes.example.com/ent-owner/ent-repo/actions/runs/99"); | ||
| if (originalEnv === undefined) { | ||
| delete process.env.GITHUB_SERVER_URL; | ||
| } else { | ||
| process.env.GITHUB_SERVER_URL = originalEnv; | ||
| try { | ||
| const ctx = { runId: 99 }; | ||
| const url = buildWorkflowRunUrl(ctx, { owner: "ent-owner", repo: "ent-repo" }); | ||
| expect(url).toBe("https://ghes.example.com/ent-owner/ent-repo/actions/runs/99"); | ||
| } finally { | ||
| if (originalEnv === undefined) { | ||
| delete process.env.GITHUB_SERVER_URL; | ||
| } else { | ||
| process.env.GITHUB_SERVER_URL = originalEnv; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
|
|
@@ -125,4 +149,24 @@ describe("buildWorkflowRunUrl", () => { | |
| expect(url).not.toContain("cross-owner"); | ||
| expect(url).not.toContain("cross-repo"); | ||
| }); | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 �� Suggested fix — wrap in try/finallyit("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 |
||
| }); | ||
|
Comment on lines
+153
to
+166
|
||
|
|
||
| it("should handle runId of 0", () => { | ||
| const url = buildWorkflowRunUrl({ serverUrl: "https://github.com", runId: 0 }, { owner: "owner", repo: "repo" }); | ||
| expect(url).toBe("https://github.com/owner/repo/actions/runs/0"); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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 bytry-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
buildWorkflowRunUrldescribe blockThe
getWorkflowMetadatadescribe block (lines 5–118) already establishes the right pattern withbeforeEach/afterEach. Applying the same tobuildWorkflowRunUrlwould let individual tests drop their manual save/restore entirely: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 justelse { process.env.GITHUB_SERVER_URL = originalEnv; }.