Skip to content

Commit d6375f9

Browse files
authored
Merge pull request #255 from Codex-/fix_unescaped_regex
Fix unescaped regex
2 parents 08325d2 + 182a06d commit d6375f9

File tree

9 files changed

+178
-98
lines changed

9 files changed

+178
-98
lines changed

dist/index.mjs

Lines changed: 19 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__snapshots__/return-dispatch.spec.ts.snap

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,16 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 1`] = `
4-
[
5-
"Attempting to identify run ID for workflow.yml (123)",
6-
]
7-
`;
8-
9-
exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 2`] = `
10-
[
11-
"Attempting to get step names for Run IDs: [0]",
12-
]
13-
`;
14-
15-
exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 3`] = `
16-
[
17-
"Attempting to identify run ID from steps...",
18-
]
19-
`;
20-
21-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 1`] = `
22-
[
23-
"Attempting to identify run ID for workflow.yml (123)",
24-
]
25-
`;
26-
27-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 2`] = `
28-
[
29-
"Attempting to get step names for Run IDs: [0]",
30-
]
31-
`;
32-
33-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 3`] = `
34-
[
35-
"Attempting to identify run ID from steps...",
36-
]
37-
`;
3+
exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 1`] = `"Attempting to identify run ID for workflow.yml (123)"`;
4+
5+
exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 2`] = `"Attempting to get step names for Run IDs: [0]"`;
6+
7+
exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 3`] = `"Attempting to identify run ID from steps..."`;
8+
9+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 1`] = `"Attempting to identify run ID for workflow.yml (123)"`;
10+
11+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 2`] = `"Attempting to get step names for Run IDs: [0]"`;
12+
13+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 3`] = `"Attempting to identify run ID from steps..."`;
3814

3915
exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 1`] = `"Attempting to identify run ID from steps..."`;
4016

@@ -46,35 +22,15 @@ exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an
4622

4723
exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 5`] = `"Attempting to get step names for Run IDs: [0]"`;
4824

49-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 1`] = `
50-
[
51-
"Attempting to identify run ID for workflow.yml (123)",
52-
]
53-
`;
54-
55-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 2`] = `
56-
[
57-
"Attempting to get step names for Run IDs: [0]",
58-
]
59-
`;
60-
61-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 3`] = `
62-
[
63-
"Attempting to identify run ID from steps...",
64-
]
65-
`;
66-
67-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable failing to get the run IDs 1`] = `
68-
[
69-
"Attempting to identify run ID for workflow.yml (123)",
70-
]
71-
`;
72-
73-
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable failing to get the run IDs 2`] = `
74-
[
75-
"Attempting to identify run ID from steps...",
76-
]
77-
`;
25+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 1`] = `"Attempting to identify run ID for workflow.yml (123)"`;
26+
27+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 2`] = `"Attempting to get step names for Run IDs: [0]"`;
28+
29+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 3`] = `"Attempting to identify run ID from steps..."`;
30+
31+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable failing to get the run IDs 1`] = `"Attempting to identify run ID for workflow.yml (123)"`;
32+
33+
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable failing to get the run IDs 2`] = `"Attempting to identify run ID from steps..."`;
7834

7935
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 1`] = `"Attempting to identify run ID from steps..."`;
8036

src/main.spec.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ describe("main", () => {
5757
let utilsLogInfoForBranchNameResult: MockInstance<
5858
typeof utils.logInfoForBranchNameResult
5959
>;
60+
let utilsCreateDistinctIdRegexMock: MockInstance<
61+
typeof utils.createDistinctIdRegex
62+
>;
6063

6164
// Return Dispatch
6265
let returnDispatchGetRunIdAndUrlMock: MockInstance<
@@ -93,6 +96,7 @@ describe("main", () => {
9396
utils,
9497
"logInfoForBranchNameResult",
9598
);
99+
utilsCreateDistinctIdRegexMock = vi.spyOn(utils, "createDistinctIdRegex");
96100

97101
returnDispatchGetRunIdAndUrlMock = vi.spyOn(
98102
returnDispatch,
@@ -114,6 +118,7 @@ describe("main", () => {
114118
});
115119

116120
it("should successfully complete", async () => {
121+
const distinctIdRegex = new RegExp(testCfg.distinctId);
117122
const returnDispatchSuccessResult = {
118123
success: true,
119124
value: {
@@ -123,6 +128,7 @@ describe("main", () => {
123128
} as const;
124129

125130
utilsGetBranchNameMock.mockReturnValue(testBranch);
131+
utilsCreateDistinctIdRegexMock.mockReturnValue(distinctIdRegex);
126132
returnDispatchGetWorkflowIdMock.mockResolvedValue(0);
127133
returnDispatchGetRunIdAndUrlMock.mockResolvedValue(
128134
returnDispatchSuccessResult,
@@ -154,13 +160,17 @@ describe("main", () => {
154160
testBranch,
155161
testCfg.ref,
156162
);
163+
expect(utilsCreateDistinctIdRegexMock).toHaveBeenCalledOnce();
164+
expect(utilsCreateDistinctIdRegexMock).toHaveBeenCalledWith(
165+
testCfg.distinctId,
166+
);
157167

158168
// Get run ID
159169
expect(returnDispatchGetRunIdAndUrlMock).toHaveBeenCalledOnce();
160170
expect(returnDispatchGetRunIdAndUrlMock).toHaveBeenCalledWith({
161171
startTime: Date.now(),
162172
branch: testBranch,
163-
distinctId: testCfg.distinctId,
173+
distinctIdRegex: distinctIdRegex,
164174
workflow: testCfg.workflow,
165175
workflowId: 0,
166176
workflowTimeoutMs: testCfg.workflowTimeoutSeconds * 1000,

src/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
handleActionSuccess,
99
getRunIdAndUrl,
1010
} from "./return-dispatch.ts";
11-
import { getBranchName, logInfoForBranchNameResult } from "./utils.ts";
11+
import {
12+
createDistinctIdRegex,
13+
getBranchName,
14+
logInfoForBranchNameResult,
15+
} from "./utils.ts";
1216

1317
export async function main(): Promise<void> {
1418
try {
@@ -27,10 +31,12 @@ export async function main(): Promise<void> {
2731
const branch = getBranchName(config.ref);
2832
logInfoForBranchNameResult(branch, config.ref);
2933

34+
const distinctIdRegex = createDistinctIdRegex(config.distinctId);
35+
3036
const result = await getRunIdAndUrl({
3137
startTime,
3238
branch,
33-
distinctId: config.distinctId,
39+
distinctIdRegex,
3440
workflow: config.workflow,
3541
workflowId,
3642
workflowTimeoutMs: config.workflowTimeoutSeconds * 1000,

src/return-dispatch.spec.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@ import * as utils from "./utils.ts";
2929

3030
vi.mock("@actions/core");
3131
vi.mock("./api.ts");
32-
// vi.mock(import('./utils.ts'), async (importOriginal) => {
33-
// const mod = await importOriginal() // type is inferred
34-
// return {
35-
// ...mod,
36-
// // replace some exports
37-
// total: vi.fn(mod.sleep),
38-
// }
39-
// })
4032

4133
describe("return-dispatch", () => {
4234
const {
@@ -473,6 +465,7 @@ describe("return-dispatch", () => {
473465

474466
describe("getRunIdAndUrl", () => {
475467
const distinctId = crypto.randomUUID();
468+
const distinctIdRegex = new RegExp(distinctId);
476469
const workflow = "workflow.yml";
477470
const workflowId = 123;
478471
const branch: utils.BranchNameResult = Object.freeze({
@@ -483,7 +476,7 @@ describe("return-dispatch", () => {
483476
const defaultOpts: GetRunIdAndUrlOpts = {
484477
startTime: Date.now(),
485478
branch: branch,
486-
distinctId: distinctId,
479+
distinctIdRegex: distinctIdRegex,
487480
workflow: workflow,
488481
workflowId: workflowId,
489482
workflowTimeoutMs: 100,
@@ -556,11 +549,11 @@ describe("return-dispatch", () => {
556549
// Logging
557550
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
558551
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
559-
expect(coreDebugLogMock.mock.calls[0]).toMatchSnapshot();
560-
expect(coreDebugLogMock.mock.calls[1]).toMatchSnapshot();
552+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
553+
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();
561554

562555
expect(coreInfoLogMock).toHaveBeenCalledOnce();
563-
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
556+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
564557
});
565558

566559
it("should call retryOrTimeout with the larger WORKFLOW_FETCH_TIMEOUT_MS timeout value", async () => {
@@ -634,11 +627,11 @@ describe("return-dispatch", () => {
634627
// Logging
635628
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
636629
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
637-
expect(coreDebugLogMock.mock.calls[0]).toMatchSnapshot();
638-
expect(coreDebugLogMock.mock.calls[1]).toMatchSnapshot();
630+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
631+
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();
639632

640633
expect(coreInfoLogMock).toHaveBeenCalledOnce();
641-
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
634+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
642635
});
643636

644637
it("called fetchWorkflowRunIds with the provided workflowId and branch", async () => {
@@ -669,11 +662,11 @@ describe("return-dispatch", () => {
669662
// Logging
670663
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
671664
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
672-
expect(coreDebugLogMock.mock.calls[0]).toMatchSnapshot();
673-
expect(coreDebugLogMock.mock.calls[1]).toMatchSnapshot();
665+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
666+
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();
674667

675668
expect(coreInfoLogMock).toHaveBeenCalledOnce();
676-
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
669+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
677670
});
678671

679672
it("should retry until an ID is found", async () => {
@@ -789,13 +782,13 @@ describe("return-dispatch", () => {
789782
// Logging
790783
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
791784
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
792-
expect(coreDebugLogMock.mock.calls[0]).toMatchSnapshot();
785+
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
793786
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatch(
794787
/Timed out while attempting to fetch Workflow Run IDs, waited [0-9]+ms/,
795788
);
796789

797790
expect(coreInfoLogMock).toHaveBeenCalledOnce();
798-
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
791+
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
799792
});
800793

801794
it("should timeout when unable to find over time", async () => {

src/return-dispatch.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,19 @@ export function handleActionFail(): void {
126126
export interface GetRunIdAndUrlOpts {
127127
startTime: number;
128128
branch: BranchNameResult;
129-
distinctId: string;
129+
distinctIdRegex: RegExp;
130130
workflow: string | number;
131131
workflowId: number;
132132
workflowTimeoutMs: number;
133133
}
134134
export async function getRunIdAndUrl({
135135
startTime,
136136
branch,
137-
distinctId,
137+
distinctIdRegex,
138138
workflow,
139139
workflowId,
140140
workflowTimeoutMs,
141141
}: GetRunIdAndUrlOpts): Promise<Result<{ id: number; url: string }>> {
142-
const distinctIdRegex = new RegExp(distinctId);
143142
const retryTimeout = Math.max(
144143
constants.WORKFLOW_FETCH_TIMEOUT_MS,
145144
workflowTimeoutMs,

src/test-utils/logging.mock.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { type MockInstance, vi, expect } from "vitest";
88
interface MockedLoggingFunctions {
99
coreDebugLogMock: MockInstance<(message: string) => void>;
1010
coreInfoLogMock: MockInstance<(message: string) => void>;
11+
coreWarningLogMock: MockInstance<(message: string) => void>;
1112
coreErrorLogMock: MockInstance<(message: string) => void>;
1213
assertOnlyCalled: (
1314
...coreLogMocks: MockInstance<(message: string) => void>[]
@@ -22,13 +23,18 @@ export function mockLoggingFunctions(): MockedLoggingFunctions {
2223
const coreInfoLogMock: MockInstance<typeof core.info> = vi
2324
.spyOn(core, "info")
2425
.mockImplementation(() => undefined);
26+
const coreWarningLogMock: MockInstance<typeof core.error> = vi.spyOn(
27+
core,
28+
"warning",
29+
);
2530
const coreErrorLogMock: MockInstance<typeof core.error> = vi
2631
.spyOn(core, "error")
2732
.mockImplementation(() => undefined);
2833

2934
const coreLogMockSet = new Set<MockInstance<(message: string) => void>>([
3035
coreDebugLogMock,
3136
coreInfoLogMock,
37+
coreWarningLogMock,
3238
coreErrorLogMock,
3339
]);
3440
const assertOnlyCalled = (
@@ -44,6 +50,7 @@ export function mockLoggingFunctions(): MockedLoggingFunctions {
4450
return {
4551
coreDebugLogMock,
4652
coreInfoLogMock,
53+
coreWarningLogMock,
4754
coreErrorLogMock,
4855
assertOnlyCalled,
4956
assertNoneCalled,

0 commit comments

Comments
 (0)