Skip to content

Commit

Permalink
Merge pull request #255 from Codex-/fix_unescaped_regex
Browse files Browse the repository at this point in the history
Fix unescaped regex
  • Loading branch information
Codex- authored Oct 5, 2024
2 parents 08325d2 + 182a06d commit d6375f9
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 98 deletions.
24 changes: 19 additions & 5 deletions dist/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 20 additions & 64 deletions src/__snapshots__/return-dispatch.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,40 +1,16 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 1`] = `
[
"Attempting to identify run ID for workflow.yml (123)",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 2`] = `
[
"Attempting to get step names for Run IDs: [0]",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 3`] = `
[
"Attempting to identify run ID from steps...",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 1`] = `
[
"Attempting to identify run ID for workflow.yml (123)",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 2`] = `
[
"Attempting to get step names for Run IDs: [0]",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 3`] = `
[
"Attempting to identify run ID from steps...",
]
`;
exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 1`] = `"Attempting to identify run ID for workflow.yml (123)"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 2`] = `"Attempting to get step names for Run IDs: [0]"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > called fetchWorkflowRunIds with the provided workflowId and branch 3`] = `"Attempting to identify run ID from steps..."`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 1`] = `"Attempting to identify run ID for workflow.yml (123)"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 2`] = `"Attempting to get step names for Run IDs: [0]"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 3`] = `"Attempting to identify run ID from steps..."`;

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

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

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

exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 1`] = `
[
"Attempting to identify run ID for workflow.yml (123)",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 2`] = `
[
"Attempting to get step names for Run IDs: [0]",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 3`] = `
[
"Attempting to identify run ID from steps...",
]
`;

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)",
]
`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable failing to get the run IDs 2`] = `
[
"Attempting to identify run ID from steps...",
]
`;
exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 1`] = `"Attempting to identify run ID for workflow.yml (123)"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 2`] = `"Attempting to get step names for Run IDs: [0]"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should return the ID when found 3`] = `"Attempting to identify run ID from steps..."`;

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)"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable failing to get the run IDs 2`] = `"Attempting to identify run ID from steps..."`;

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

Expand Down
12 changes: 11 additions & 1 deletion src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ describe("main", () => {
let utilsLogInfoForBranchNameResult: MockInstance<
typeof utils.logInfoForBranchNameResult
>;
let utilsCreateDistinctIdRegexMock: MockInstance<
typeof utils.createDistinctIdRegex
>;

// Return Dispatch
let returnDispatchGetRunIdAndUrlMock: MockInstance<
Expand Down Expand Up @@ -93,6 +96,7 @@ describe("main", () => {
utils,
"logInfoForBranchNameResult",
);
utilsCreateDistinctIdRegexMock = vi.spyOn(utils, "createDistinctIdRegex");

returnDispatchGetRunIdAndUrlMock = vi.spyOn(
returnDispatch,
Expand All @@ -114,6 +118,7 @@ describe("main", () => {
});

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

utilsGetBranchNameMock.mockReturnValue(testBranch);
utilsCreateDistinctIdRegexMock.mockReturnValue(distinctIdRegex);
returnDispatchGetWorkflowIdMock.mockResolvedValue(0);
returnDispatchGetRunIdAndUrlMock.mockResolvedValue(
returnDispatchSuccessResult,
Expand Down Expand Up @@ -154,13 +160,17 @@ describe("main", () => {
testBranch,
testCfg.ref,
);
expect(utilsCreateDistinctIdRegexMock).toHaveBeenCalledOnce();
expect(utilsCreateDistinctIdRegexMock).toHaveBeenCalledWith(
testCfg.distinctId,
);

// Get run ID
expect(returnDispatchGetRunIdAndUrlMock).toHaveBeenCalledOnce();
expect(returnDispatchGetRunIdAndUrlMock).toHaveBeenCalledWith({
startTime: Date.now(),
branch: testBranch,
distinctId: testCfg.distinctId,
distinctIdRegex: distinctIdRegex,
workflow: testCfg.workflow,
workflowId: 0,
workflowTimeoutMs: testCfg.workflowTimeoutSeconds * 1000,
Expand Down
10 changes: 8 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import {
handleActionSuccess,
getRunIdAndUrl,
} from "./return-dispatch.ts";
import { getBranchName, logInfoForBranchNameResult } from "./utils.ts";
import {
createDistinctIdRegex,
getBranchName,
logInfoForBranchNameResult,
} from "./utils.ts";

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

const distinctIdRegex = createDistinctIdRegex(config.distinctId);

const result = await getRunIdAndUrl({
startTime,
branch,
distinctId: config.distinctId,
distinctIdRegex,
workflow: config.workflow,
workflowId,
workflowTimeoutMs: config.workflowTimeoutSeconds * 1000,
Expand Down
33 changes: 13 additions & 20 deletions src/return-dispatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ import * as utils from "./utils.ts";

vi.mock("@actions/core");
vi.mock("./api.ts");
// vi.mock(import('./utils.ts'), async (importOriginal) => {
// const mod = await importOriginal() // type is inferred
// return {
// ...mod,
// // replace some exports
// total: vi.fn(mod.sleep),
// }
// })

describe("return-dispatch", () => {
const {
Expand Down Expand Up @@ -473,6 +465,7 @@ describe("return-dispatch", () => {

describe("getRunIdAndUrl", () => {
const distinctId = crypto.randomUUID();
const distinctIdRegex = new RegExp(distinctId);
const workflow = "workflow.yml";
const workflowId = 123;
const branch: utils.BranchNameResult = Object.freeze({
Expand All @@ -483,7 +476,7 @@ describe("return-dispatch", () => {
const defaultOpts: GetRunIdAndUrlOpts = {
startTime: Date.now(),
branch: branch,
distinctId: distinctId,
distinctIdRegex: distinctIdRegex,
workflow: workflow,
workflowId: workflowId,
workflowTimeoutMs: 100,
Expand Down Expand Up @@ -556,11 +549,11 @@ describe("return-dispatch", () => {
// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
expect(coreDebugLogMock.mock.calls[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
});

it("should call retryOrTimeout with the larger WORKFLOW_FETCH_TIMEOUT_MS timeout value", async () => {
Expand Down Expand Up @@ -634,11 +627,11 @@ describe("return-dispatch", () => {
// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
expect(coreDebugLogMock.mock.calls[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
});

it("called fetchWorkflowRunIds with the provided workflowId and branch", async () => {
Expand Down Expand Up @@ -669,11 +662,11 @@ describe("return-dispatch", () => {
// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
expect(coreDebugLogMock.mock.calls[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
});

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

expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]).toMatchSnapshot();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
});

it("should timeout when unable to find over time", async () => {
Expand Down
5 changes: 2 additions & 3 deletions src/return-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,19 @@ export function handleActionFail(): void {
export interface GetRunIdAndUrlOpts {
startTime: number;
branch: BranchNameResult;
distinctId: string;
distinctIdRegex: RegExp;
workflow: string | number;
workflowId: number;
workflowTimeoutMs: number;
}
export async function getRunIdAndUrl({
startTime,
branch,
distinctId,
distinctIdRegex,
workflow,
workflowId,
workflowTimeoutMs,
}: GetRunIdAndUrlOpts): Promise<Result<{ id: number; url: string }>> {
const distinctIdRegex = new RegExp(distinctId);
const retryTimeout = Math.max(
constants.WORKFLOW_FETCH_TIMEOUT_MS,
workflowTimeoutMs,
Expand Down
7 changes: 7 additions & 0 deletions src/test-utils/logging.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { type MockInstance, vi, expect } from "vitest";
interface MockedLoggingFunctions {
coreDebugLogMock: MockInstance<(message: string) => void>;
coreInfoLogMock: MockInstance<(message: string) => void>;
coreWarningLogMock: MockInstance<(message: string) => void>;
coreErrorLogMock: MockInstance<(message: string) => void>;
assertOnlyCalled: (
...coreLogMocks: MockInstance<(message: string) => void>[]
Expand All @@ -22,13 +23,18 @@ export function mockLoggingFunctions(): MockedLoggingFunctions {
const coreInfoLogMock: MockInstance<typeof core.info> = vi
.spyOn(core, "info")
.mockImplementation(() => undefined);
const coreWarningLogMock: MockInstance<typeof core.error> = vi.spyOn(
core,
"warning",
);
const coreErrorLogMock: MockInstance<typeof core.error> = vi
.spyOn(core, "error")
.mockImplementation(() => undefined);

const coreLogMockSet = new Set<MockInstance<(message: string) => void>>([
coreDebugLogMock,
coreInfoLogMock,
coreWarningLogMock,
coreErrorLogMock,
]);
const assertOnlyCalled = (
Expand All @@ -44,6 +50,7 @@ export function mockLoggingFunctions(): MockedLoggingFunctions {
return {
coreDebugLogMock,
coreInfoLogMock,
coreWarningLogMock,
coreErrorLogMock,
assertOnlyCalled,
assertNoneCalled,
Expand Down
Loading

0 comments on commit d6375f9

Please sign in to comment.