Skip to content

Commit

Permalink
refactor: lift extra logging to main
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 6, 2024
1 parent d2aecb4 commit 3a03473
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 87 deletions.
44 changes: 11 additions & 33 deletions src/__snapshots__/return-dispatch.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,49 +1,27 @@
// 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 1`] = `"Attempting to get step names for Run IDs: [0]"`;

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 > should call retryOrTimeout with the larger workflowTimeoutMs timeout value 1`] = `"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 retry until an ID is found 1`] = `"No Run IDs found for workflow, attempt 1..."`;

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 retry until an ID is found 2`] = `"No Run IDs found for workflow, attempt 2..."`;

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 retry until an ID is found 3`] = `"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 return the ID when found 1`] = `"Attempting to get step names for Run IDs: [0]"`;

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

exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 2`] = `"No Run IDs found for workflow, attempt 1..."`;
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 1`] = `"Exhausted searching IDs in known runs, attempt 1..."`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 3`] = `"Attempting to identify run ID for workflow.yml (123)"`;
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 2`] = `"Attempting to get step names for Run IDs: [0]"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should retry until an ID is found 4`] = `"No Run IDs found for workflow, attempt 2..."`;

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 timeout when unable to find over time 1`] = `"Attempting to identify run ID from steps..."`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 2`] = `"Exhausted searching IDs in known runs, attempt 1..."`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 3`] = `"Attempting to identify run ID for workflow.yml (123)"`;
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 3`] = `"Exhausted searching IDs in known runs, attempt 2..."`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 4`] = `"Attempting to get step names for Run IDs: [0]"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 5`] = `"Exhausted searching IDs in known runs, attempt 2..."`;
exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 5`] = `"Exhausted searching IDs in known runs, attempt 3..."`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 6`] = `"Attempting to get step names for Run IDs: [0]"`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 7`] = `"Exhausted searching IDs in known runs, attempt 3..."`;

exports[`return-dispatch > handleAction > getRunIdAndUrl > should timeout when unable to find over time 8`] = `"Attempting to get step names for Run IDs: [0]"`;
11 changes: 8 additions & 3 deletions src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ describe("main", () => {
startTime: Date.now(),
branch: testBranch,
distinctIdRegex: distinctIdRegex,
workflow: testCfg.workflow,
workflowId: 0,
workflowTimeoutMs: testCfg.workflowTimeoutSeconds * 1000,
});
Expand All @@ -187,12 +186,18 @@ describe("main", () => {

// Logging
assertOnlyCalled(coreInfoLogMock, coreDebugLogMock);
expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock).toHaveBeenCalledTimes(2);
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
`"Attempt to extract branch name from ref..."`,
);
expect(coreDebugLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[1]?.[0]).toMatchInlineSnapshot(
`"Attempting to identify run ID from steps..."`,
);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
`"Attempting to identify run ID for test-workflow (0)"`,
);
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchInlineSnapshot(
`"Completed (0ms)"`,
);
});
Expand Down
6 changes: 5 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ export async function main(): Promise<void> {

const distinctIdRegex = createDistinctIdRegex(config.distinctId);

core.info("Attempting to identify run ID from steps...");
core.debug(
`Attempting to identify run ID for ${config.workflow} (${workflowId})`,
);

const result = await getRunIdAndUrl({
startTime,
branch,
distinctIdRegex,
workflow: config.workflow,
workflowId,
workflowTimeoutMs: config.workflowTimeoutSeconds * 1000,
});
Expand Down
59 changes: 14 additions & 45 deletions src/return-dispatch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@ 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({
isTag: false,
Expand All @@ -477,7 +476,6 @@ describe("return-dispatch", () => {
startTime: Date.now(),
branch: branch,
distinctIdRegex: distinctIdRegex,
workflow: workflow,
workflowId: workflowId,
workflowTimeoutMs: 100,
};
Expand Down Expand Up @@ -547,13 +545,10 @@ describe("return-dispatch", () => {
expect(utilSleepMock).not.toHaveBeenCalled();

// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
assertOnlyCalled(coreDebugLogMock);
expect(coreDebugLogMock).toHaveBeenCalledOnce();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

expect(coreInfoLogMock).toHaveBeenCalledOnce();
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 @@ -583,19 +578,11 @@ describe("return-dispatch", () => {
expect(utilSleepMock).not.toHaveBeenCalled();

// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
assertOnlyCalled(coreDebugLogMock);
expect(coreDebugLogMock).toHaveBeenCalledOnce();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
`"Attempting to identify run ID for workflow.yml (123)"`,
);
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchInlineSnapshot(
`"Attempting to get step names for Run IDs: [0]"`,
);

expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
`"Attempting to identify run ID from steps..."`,
);
});

it("should call retryOrTimeout with the larger workflowTimeoutMs timeout value", async () => {
Expand Down Expand Up @@ -625,13 +612,9 @@ describe("return-dispatch", () => {
expect(utilSleepMock).not.toHaveBeenCalled();

// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
assertOnlyCalled(coreDebugLogMock);
expect(coreDebugLogMock).toHaveBeenCalledOnce();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

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

it("called fetchWorkflowRunIds with the provided workflowId and branch", async () => {
Expand Down Expand Up @@ -660,13 +643,9 @@ describe("return-dispatch", () => {
);

// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
assertOnlyCalled(coreDebugLogMock);
expect(coreDebugLogMock).toHaveBeenCalledOnce();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

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

it("should retry until an ID is found", async () => {
Expand Down Expand Up @@ -698,12 +677,8 @@ describe("return-dispatch", () => {

assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);

expect(coreInfoLogMock).toHaveBeenCalledTimes(2);
expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreInfoLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

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

expect(utilSleepMock).toHaveBeenCalledOnce();
expect(utilSleepMock).toHaveBeenCalledWith(5000);
Expand Down Expand Up @@ -780,15 +755,11 @@ describe("return-dispatch", () => {
expect(utilSleepMock).not.toHaveBeenCalled();

// Logging
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);
expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatch(
assertOnlyCalled(coreDebugLogMock);
expect(coreDebugLogMock).toHaveBeenCalledOnce();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatch(
/Timed out while attempting to fetch Workflow Run IDs, waited [0-9]+ms/,
);

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

it("should timeout when unable to find over time", async () => {
Expand Down Expand Up @@ -817,13 +788,11 @@ describe("return-dispatch", () => {
expect(apiFetchWorkflowRunJobStepsMock).toHaveBeenCalledOnce();
assertOnlyCalled(coreDebugLogMock, coreInfoLogMock);

expect(coreInfoLogMock).toHaveBeenCalledTimes(2);
expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreInfoLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

expect(coreDebugLogMock).toHaveBeenCalledTimes(2);
expect(coreDebugLogMock).toHaveBeenCalledOnce();
expect(coreDebugLogMock.mock.calls[0]?.[0]).toMatchSnapshot();
expect(coreDebugLogMock.mock.calls[1]?.[0]).toMatchSnapshot();

expect(utilSleepMock).toHaveBeenCalledOnce();
expect(utilSleepMock).toHaveBeenCalledWith(5000);
Expand Down
5 changes: 0 additions & 5 deletions src/return-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,13 @@ export interface GetRunIdAndUrlOpts {
startTime: number;
branch: BranchNameResult;
distinctIdRegex: RegExp;
workflow: string | number;
workflowId: number;
workflowTimeoutMs: number;
}
export async function getRunIdAndUrl({
startTime,
branch,
distinctIdRegex,
workflow,
workflowId,
workflowTimeoutMs,
}: GetRunIdAndUrlOpts): Promise<Result<{ id: number; url: string }>> {
Expand All @@ -144,9 +142,6 @@ export async function getRunIdAndUrl({
workflowTimeoutMs,
);

core.info("Attempting to identify run ID from steps...");
core.debug(`Attempting to identify run ID for ${workflow} (${workflowId})`);

let attemptNo = 0;
let elapsedTime = Date.now() - startTime;
while (elapsedTime < workflowTimeoutMs) {
Expand Down

0 comments on commit 3a03473

Please sign in to comment.