Skip to content

Commit ef76ed5

Browse files
authored
[WIP] Revive JavaScript formatting and ensure stability when running multiple times (#6851)
1 parent 0328ab2 commit ef76ed5

62 files changed

Lines changed: 540 additions & 3243 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pkg/workflow/js/add_comment.test.cjs

Lines changed: 20 additions & 185 deletions
Large diffs are not rendered by default.

pkg/workflow/js/add_labels.test.cjs

Lines changed: 14 additions & 76 deletions
Large diffs are not rendered by default.

pkg/workflow/js/add_reaction_and_edit_comment.test.cjs

Lines changed: 2 additions & 75 deletions
Large diffs are not rendered by default.

pkg/workflow/js/assign_issue.test.cjs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,44 @@
11
import { describe, it, expect, beforeEach, vi } from "vitest";
22
import fs from "fs";
33
import path from "path";
4-
// Mock the global objects that GitHub Actions provides
54
const mockCore = {
6-
// Core logging functions
75
debug: vi.fn(),
86
info: vi.fn(),
97
notice: vi.fn(),
108
warning: vi.fn(),
119
error: vi.fn(),
12-
// Core workflow functions
1310
setFailed: vi.fn(),
1411
setOutput: vi.fn(),
1512
exportVariable: vi.fn(),
1613
setSecret: vi.fn(),
17-
// Input/state functions
1814
getInput: vi.fn(),
1915
getBooleanInput: vi.fn(),
2016
getMultilineInput: vi.fn(),
2117
getState: vi.fn(),
2218
saveState: vi.fn(),
23-
// Group functions
2419
startGroup: vi.fn(),
2520
endGroup: vi.fn(),
2621
group: vi.fn(),
27-
// Other utility functions
2822
addPath: vi.fn(),
2923
setCommandEcho: vi.fn(),
3024
isDebug: vi.fn().mockReturnValue(!1),
3125
getIDToken: vi.fn(),
3226
toPlatformPath: vi.fn(),
3327
toPosixPath: vi.fn(),
3428
toWin32Path: vi.fn(),
35-
// Summary object with chainable methods
3629
summary: { addRaw: vi.fn().mockReturnThis(), write: vi.fn().mockResolvedValue() },
3730
},
3831
mockExec = { exec: vi.fn() },
3932
mockGithub = { graphql: vi.fn() },
4033
mockContext = { repo: { owner: "testowner", repo: "testrepo" } };
41-
// Set up global variables
4234
((global.core = mockCore),
4335
(global.exec = mockExec),
4436
(global.github = mockGithub),
4537
(global.context = mockContext),
4638
describe("assign_issue.cjs", () => {
4739
let assignIssueScript;
4840
(beforeEach(() => {
49-
// Reset all mocks
50-
(vi.clearAllMocks(),
51-
// Reset environment variables
52-
delete process.env.GH_TOKEN,
53-
delete process.env.ASSIGNEE,
54-
delete process.env.ISSUE_NUMBER);
55-
// Read the script content
41+
(vi.clearAllMocks(), delete process.env.GH_TOKEN, delete process.env.ASSIGNEE, delete process.env.ISSUE_NUMBER);
5642
const scriptPath = path.join(process.cwd(), "assign_issue.cjs");
5743
assignIssueScript = fs.readFileSync(scriptPath, "utf8");
5844
}),
@@ -61,7 +47,6 @@ const mockCore = {
6147
((process.env.ASSIGNEE = "test-user"),
6248
(process.env.ISSUE_NUMBER = "123"),
6349
delete process.env.GH_TOKEN,
64-
// Execute the script
6550
await eval(`(async () => { ${assignIssueScript} })()`),
6651
expect(mockCore.setFailed).toHaveBeenCalledWith(expect.stringContaining("GH_TOKEN environment variable is required but not set")),
6752
expect(mockCore.setFailed).toHaveBeenCalledWith(expect.stringContaining("https://githubnext.github.io/gh-aw/reference/safe-outputs/#assigning-issues-to-copilot")),
@@ -71,7 +56,6 @@ const mockCore = {
7156
((process.env.GH_TOKEN = " "),
7257
(process.env.ASSIGNEE = "test-user"),
7358
(process.env.ISSUE_NUMBER = "123"),
74-
// Execute the script
7559
await eval(`(async () => { ${assignIssueScript} })()`),
7660
expect(mockCore.setFailed).toHaveBeenCalledWith(expect.stringContaining("GH_TOKEN environment variable is required but not set")),
7761
expect(mockExec.exec).not.toHaveBeenCalled());
@@ -80,7 +64,6 @@ const mockCore = {
8064
((process.env.GH_TOKEN = "ghp_test123"),
8165
(process.env.ISSUE_NUMBER = "123"),
8266
delete process.env.ASSIGNEE,
83-
// Execute the script
8467
await eval(`(async () => { ${assignIssueScript} })()`),
8568
expect(mockCore.setFailed).toHaveBeenCalledWith("ASSIGNEE environment variable is required but not set"),
8669
expect(mockExec.exec).not.toHaveBeenCalled());
@@ -89,7 +72,6 @@ const mockCore = {
8972
((process.env.GH_TOKEN = "ghp_test123"),
9073
(process.env.ASSIGNEE = " "),
9174
(process.env.ISSUE_NUMBER = "123"),
92-
// Execute the script
9375
await eval(`(async () => { ${assignIssueScript} })()`),
9476
expect(mockCore.setFailed).toHaveBeenCalledWith("ASSIGNEE environment variable is required but not set"),
9577
expect(mockExec.exec).not.toHaveBeenCalled());
@@ -98,7 +80,6 @@ const mockCore = {
9880
((process.env.GH_TOKEN = "ghp_test123"),
9981
(process.env.ASSIGNEE = "test-user"),
10082
delete process.env.ISSUE_NUMBER,
101-
// Execute the script
10283
await eval(`(async () => { ${assignIssueScript} })()`),
10384
expect(mockCore.setFailed).toHaveBeenCalledWith("ISSUE_NUMBER environment variable is required but not set"),
10485
expect(mockExec.exec).not.toHaveBeenCalled());
@@ -107,7 +88,6 @@ const mockCore = {
10788
((process.env.GH_TOKEN = "ghp_test123"),
10889
(process.env.ASSIGNEE = "test-user"),
10990
(process.env.ISSUE_NUMBER = " "),
110-
// Execute the script
11191
await eval(`(async () => { ${assignIssueScript} })()`),
11292
expect(mockCore.setFailed).toHaveBeenCalledWith("ISSUE_NUMBER environment variable is required but not set"),
11393
expect(mockExec.exec).not.toHaveBeenCalled());
@@ -119,7 +99,6 @@ const mockCore = {
11999
(process.env.ASSIGNEE = "test-user"),
120100
(process.env.ISSUE_NUMBER = "456"),
121101
mockExec.exec.mockResolvedValue(0),
122-
// Execute the script
123102
await eval(`(async () => { ${assignIssueScript} })()`),
124103
expect(mockCore.info).toHaveBeenCalledWith("Assigning issue #456 to test-user"),
125104
expect(mockExec.exec).toHaveBeenCalledWith("gh", ["issue", "edit", "456", "--add-assignee", "test-user"], expect.objectContaining({ env: expect.objectContaining({ GH_TOKEN: "ghp_test123" }) })),
@@ -133,7 +112,6 @@ const mockCore = {
133112
(process.env.ASSIGNEE = " test-user "),
134113
(process.env.ISSUE_NUMBER = " 123 "),
135114
mockExec.exec.mockResolvedValue(0),
136-
// Execute the script
137115
await eval(`(async () => { ${assignIssueScript} })()`),
138116
expect(mockCore.info).toHaveBeenCalledWith("Assigning issue #123 to test-user"),
139117
expect(mockExec.exec).toHaveBeenCalledWith("gh", ["issue", "edit", "123", "--add-assignee", "test-user"], expect.any(Object)),
@@ -144,7 +122,6 @@ const mockCore = {
144122
(process.env.ASSIGNEE = "test-user"),
145123
(process.env.ISSUE_NUMBER = "123"),
146124
mockExec.exec.mockResolvedValue(0),
147-
// Execute the script
148125
await eval(`(async () => { ${assignIssueScript} })()`),
149126
expect(mockCore.summary.addRaw).toHaveBeenCalledWith(expect.stringContaining("## Issue Assignment")),
150127
expect(mockCore.summary.addRaw).toHaveBeenCalledWith(expect.stringContaining("Successfully assigned issue #123 to `test-user`")),
@@ -156,7 +133,6 @@ const mockCore = {
156133
((process.env.GH_TOKEN = "ghp_test123"), (process.env.ASSIGNEE = "test-user"), (process.env.ISSUE_NUMBER = "999"));
157134
const testError = new Error("User not found");
158135
(mockExec.exec.mockRejectedValue(testError),
159-
// Execute the script
160136
await eval(`(async () => { ${assignIssueScript} })()`),
161137
expect(mockCore.error).toHaveBeenCalledWith("Failed to assign issue: User not found"),
162138
expect(mockCore.setFailed).toHaveBeenCalledWith("Failed to assign issue #999 to test-user: User not found"));
@@ -165,20 +141,14 @@ const mockCore = {
165141
((process.env.GH_TOKEN = "ghp_test123"), (process.env.ASSIGNEE = "test-user"), (process.env.ISSUE_NUMBER = "999"));
166142
const stringError = "Command failed";
167143
(mockExec.exec.mockRejectedValue(stringError),
168-
// Execute the script
169144
await eval(`(async () => { ${assignIssueScript} })()`),
170145
expect(mockCore.error).toHaveBeenCalledWith("Failed to assign issue: Command failed"),
171146
expect(mockCore.setFailed).toHaveBeenCalledWith("Failed to assign issue #999 to test-user: Command failed"));
172147
}),
173148
it("should handle top-level errors with catch handler", async () => {
174149
((process.env.GH_TOKEN = "ghp_test123"), (process.env.ASSIGNEE = "test-user"), (process.env.ISSUE_NUMBER = "123"));
175-
// Mock exec to throw an error that isn't caught in main
176150
const uncaughtError = new Error("Uncaught error");
177-
(mockExec.exec.mockRejectedValue(uncaughtError),
178-
// Execute the script
179-
await eval(`(async () => { ${assignIssueScript} })()`),
180-
// The error should be caught in the main catch handler
181-
expect(mockCore.setFailed).toHaveBeenCalled());
151+
(mockExec.exec.mockRejectedValue(uncaughtError), await eval(`(async () => { ${assignIssueScript} })()`), expect(mockCore.setFailed).toHaveBeenCalled());
182152
}));
183153
}),
184154
describe("Edge cases for regular users", () => {
@@ -187,7 +157,6 @@ const mockCore = {
187157
(process.env.ASSIGNEE = "test-user"),
188158
(process.env.ISSUE_NUMBER = "123"),
189159
mockExec.exec.mockResolvedValue(0),
190-
// Execute the script
191160
await eval(`(async () => { ${assignIssueScript} })()`),
192161
expect(mockExec.exec).toHaveBeenCalledWith("gh", ["issue", "edit", "123", "--add-assignee", "test-user"], expect.any(Object)));
193162
}),
@@ -197,7 +166,6 @@ const mockCore = {
197166
(process.env.ISSUE_NUMBER = "123"),
198167
(process.env.OTHER_VAR = "other_value"),
199168
mockExec.exec.mockResolvedValue(0),
200-
// Execute the script
201169
await eval(`(async () => { ${assignIssueScript} })()`),
202170
expect(mockExec.exec).toHaveBeenCalledWith("gh", ["issue", "edit", "123", "--add-assignee", "test-user"], { env: expect.objectContaining({ GH_TOKEN: "ghp_test123", OTHER_VAR: "other_value" }) }));
203171
}),
@@ -206,17 +174,12 @@ const mockCore = {
206174
(process.env.ASSIGNEE = "user-with-dash"),
207175
(process.env.ISSUE_NUMBER = "123"),
208176
mockExec.exec.mockResolvedValue(0),
209-
// Execute the script
210177
await eval(`(async () => { ${assignIssueScript} })()`),
211178
expect(mockExec.exec).toHaveBeenCalledWith("gh", ["issue", "edit", "123", "--add-assignee", "user-with-dash"], expect.any(Object)),
212179
expect(mockCore.setFailed).not.toHaveBeenCalled());
213180
}),
214181
it("should include documentation link in error message", async () => {
215-
(delete process.env.GH_TOKEN,
216-
(process.env.ASSIGNEE = "test-user"),
217-
(process.env.ISSUE_NUMBER = "123"),
218-
// Execute the script
219-
await eval(`(async () => { ${assignIssueScript} })()`));
182+
(delete process.env.GH_TOKEN, (process.env.ASSIGNEE = "test-user"), (process.env.ISSUE_NUMBER = "123"), await eval(`(async () => { ${assignIssueScript} })()`));
220183
const failedCall = mockCore.setFailed.mock.calls[0][0];
221184
expect(failedCall).toContain("https://githubnext.github.io/gh-aw/reference/safe-outputs/#assigning-issues-to-copilot");
222185
}));

pkg/workflow/js/assign_milestone.test.cjs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
22
import fs from "fs";
33
import path from "path";
4-
// Mock the global objects that GitHub Actions provides
54
const mockCore = { debug: vi.fn(), info: vi.fn(), warning: vi.fn(), error: vi.fn(), setFailed: vi.fn(), setOutput: vi.fn(), summary: { addRaw: vi.fn().mockReturnThis(), write: vi.fn().mockResolvedValue() } },
65
mockContext = { repo: { owner: "test-owner", repo: "test-repo" }, eventName: "issues", payload: { issue: { number: 123 } } },
76
mockGithub = { rest: { issues: { update: vi.fn(), listMilestones: vi.fn() } } };
8-
// Set up global mocks before importing the module
97
((global.core = mockCore),
108
(global.context = mockContext),
119
(global.github = mockGithub),
1210
describe("assign_milestone", () => {
1311
let assignMilestoneScript, tempFilePath;
14-
// Helper function to set agent output via file
1512
const setAgentOutput = data => {
1613
tempFilePath = path.join("/tmp", `test_agent_output_${Date.now()}_${Math.random().toString(36).slice(2)}.json`);
1714
const content = "string" == typeof data ? data : JSON.stringify(data);
1815
(fs.writeFileSync(tempFilePath, content), (process.env.GH_AW_AGENT_OUTPUT = tempFilePath));
1916
};
2017
(beforeEach(() => {
21-
// Reset mocks before each test
2218
(vi.clearAllMocks(), delete process.env.GH_AW_AGENT_OUTPUT, delete process.env.GH_AW_SAFE_OUTPUTS_STAGED, delete process.env.GH_AW_MILESTONE_ALLOWED, delete process.env.GH_AW_MILESTONE_MAX_COUNT);
23-
// Read the script content
2419
const scriptPath = path.join(process.cwd(), "assign_milestone.cjs");
2520
assignMilestoneScript = fs.readFileSync(scriptPath, "utf8");
2621
}),
2722
afterEach(() => {
28-
// Clean up temp file
2923
tempFilePath && fs.existsSync(tempFilePath) && fs.unlinkSync(tempFilePath);
3024
}),
3125
it("should handle empty agent output", async () => {
@@ -47,9 +41,7 @@ const mockCore = { debug: vi.fn(), info: vi.fn(), warning: vi.fn(), error: vi.fn
4741
((process.env.GH_AW_SAFE_OUTPUTS_STAGED = "true"),
4842
setAgentOutput({ items: [{ type: "assign_milestone", issue_number: 42, milestone_number: 5 }], errors: [] }),
4943
await eval(`(async () => { ${assignMilestoneScript} })()`),
50-
// In staged mode, should not call the API
5144
expect(mockGithub.rest.issues.update).not.toHaveBeenCalled(),
52-
// Should generate preview
5345
expect(mockCore.summary.addRaw).toHaveBeenCalled());
5446
const summaryCall = mockCore.summary.addRaw.mock.calls[0][0];
5547
(expect(summaryCall).toContain("🎭 Staged Mode"), expect(summaryCall).toContain("Issue:** #42"), expect(summaryCall).toContain("Milestone Number:** 5"));
@@ -66,7 +58,6 @@ const mockCore = { debug: vi.fn(), info: vi.fn(), warning: vi.fn(), error: vi.fn
6658
}),
6759
mockGithub.rest.issues.update.mockResolvedValue({}),
6860
await eval(`(async () => { ${assignMilestoneScript} })()`),
69-
// Should only process first 2 items
7061
expect(mockGithub.rest.issues.update).toHaveBeenCalledTimes(2),
7162
expect(mockCore.warning).toHaveBeenCalledWith(expect.stringContaining("Found 3 milestone assignments, but max is 2")));
7263
}),
@@ -81,9 +72,7 @@ const mockCore = { debug: vi.fn(), info: vi.fn(), warning: vi.fn(), error: vi.fn
8172
}),
8273
mockGithub.rest.issues.update.mockResolvedValue({}),
8374
await eval(`(async () => { ${assignMilestoneScript} })()`),
84-
// Should fetch milestones for validation
8575
expect(mockGithub.rest.issues.listMilestones).toHaveBeenCalledWith({ owner: "test-owner", repo: "test-repo", state: "all", per_page: 100 }),
86-
// Should allow v1.0 (milestone #5)
8776
expect(mockGithub.rest.issues.update).toHaveBeenCalledWith({ owner: "test-owner", repo: "test-repo", issue_number: 42, milestone: 5 }));
8877
}),
8978
it("should reject milestone not in allowed list", async () => {
@@ -96,7 +85,6 @@ const mockCore = { debug: vi.fn(), info: vi.fn(), warning: vi.fn(), error: vi.fn
9685
],
9786
}),
9887
await eval(`(async () => { ${assignMilestoneScript} })()`),
99-
// Should NOT assign milestone (v3.0 is not in allowed list)
10088
expect(mockGithub.rest.issues.update).not.toHaveBeenCalled(),
10189
expect(mockCore.warning).toHaveBeenCalledWith(expect.stringContaining('Milestone "v3.0" (#6) is not in the allowed list')));
10290
}),
@@ -121,7 +109,7 @@ const mockCore = { debug: vi.fn(), info: vi.fn(), warning: vi.fn(), error: vi.fn
121109
expect(mockCore.error).toHaveBeenCalledWith(expect.stringContaining("Invalid milestone_number")));
122110
}),
123111
it("should handle multiple milestone assignments", async () => {
124-
((process.env.GH_AW_MILESTONE_MAX_COUNT = "2"), // Set max to allow 2 assignments
112+
((process.env.GH_AW_MILESTONE_MAX_COUNT = "2"),
125113
setAgentOutput({
126114
items: [
127115
{ type: "assign_milestone", issue_number: 1, milestone_number: 5 },

pkg/workflow/js/check_command_position.test.cjs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,44 @@
11
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
22
import fs from "fs";
33
import path from "path";
4-
// Mock the global objects that GitHub Actions provides
54
const mockCore = {
6-
// Core logging functions
75
debug: vi.fn(),
86
info: vi.fn(),
97
notice: vi.fn(),
108
warning: vi.fn(),
119
error: vi.fn(),
12-
// Core workflow functions
1310
setFailed: vi.fn(),
1411
setOutput: vi.fn(),
1512
exportVariable: vi.fn(),
1613
setSecret: vi.fn(),
17-
// Input/state functions
1814
getInput: vi.fn(),
1915
getBooleanInput: vi.fn(),
2016
getMultilineInput: vi.fn(),
2117
getState: vi.fn(),
2218
saveState: vi.fn(),
23-
// Group functions
2419
startGroup: vi.fn(),
2520
endGroup: vi.fn(),
2621
group: vi.fn(),
27-
// Other utility functions
2822
addPath: vi.fn(),
2923
setCommandEcho: vi.fn(),
3024
isDebug: vi.fn().mockReturnValue(!1),
3125
getIDToken: vi.fn(),
3226
toPlatformPath: vi.fn(),
3327
toPosixPath: vi.fn(),
3428
toWin32Path: vi.fn(),
35-
// Summary object with chainable methods
3629
summary: { addRaw: vi.fn().mockReturnThis(), write: vi.fn().mockResolvedValue() },
3730
},
3831
mockContext = { eventName: "issues", payload: {}, runId: 12345, repo: { owner: "testowner", repo: "testrepo" } };
39-
// Set up global variables
4032
((global.core = mockCore),
4133
(global.context = mockContext),
4234
describe("check_command_position.cjs", () => {
4335
let checkCommandPositionScript, originalEnv;
4436
(beforeEach(() => {
45-
// Reset all mocks
46-
(vi.clearAllMocks(),
47-
// Store original environment
48-
(originalEnv = { GH_AW_COMMAND: process.env.GH_AW_COMMAND }));
49-
// Load the script
37+
(vi.clearAllMocks(), (originalEnv = { GH_AW_COMMAND: process.env.GH_AW_COMMAND }));
5038
const scriptPath = path.join(__dirname, "check_command_position.cjs");
51-
((checkCommandPositionScript = fs.readFileSync(scriptPath, "utf8")),
52-
// Reset context
53-
(mockContext.eventName = "issues"),
54-
(mockContext.payload = {}));
39+
((checkCommandPositionScript = fs.readFileSync(scriptPath, "utf8")), (mockContext.eventName = "issues"), (mockContext.payload = {}));
5540
}),
5641
afterEach(() => {
57-
// Restore original environment
5842
void 0 !== originalEnv.GH_AW_COMMAND ? (process.env.GH_AW_COMMAND = originalEnv.GH_AW_COMMAND) : delete process.env.GH_AW_COMMAND;
5943
}),
6044
it("should fail when GH_AW_COMMAND is not set", async () => {

0 commit comments

Comments
 (0)