Skip to content

Commit

Permalink
feat: add support for specifying the distinct_id to use
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Aug 14, 2024
1 parent 0c77d72 commit 6970d41
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ inputs:
workflow_timeout_seconds:
description: Time until giving up waiting for the start of the workflow run.
default: 300
distinct_id:
description: Specify a static string to use instead of a random distinct ID.

runs:
using: node20
Expand Down
11 changes: 11 additions & 0 deletions src/action.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("Action", () => {
workflow: "workflow_name",
workflow_inputs: JSON.stringify(workflowInputs),
workflow_timeout_seconds: "60",
distinct_id: "distinct_id",
};

vi.spyOn(core, "getInput").mockImplementation((input: string): string => {
Expand All @@ -42,6 +43,8 @@ describe("Action", () => {
return mockEnvConfig.workflow_inputs;
case "workflow_timeout_seconds":
return mockEnvConfig.workflow_timeout_seconds;
case "distinct_id":
return mockEnvConfig.distinct_id;
default:
throw new Error("invalid input requested");
}
Expand All @@ -64,6 +67,7 @@ describe("Action", () => {
expect(config.workflow).toStrictEqual("workflow_name");
expect(config.workflowInputs).toStrictEqual(workflowInputs);
expect(config.workflowTimeoutSeconds).toStrictEqual(60);
expect(config.distinctId).toStrictEqual("distinct_id");
});

it("should have a number for a workflow when given a workflow ID", () => {
Expand Down Expand Up @@ -110,5 +114,12 @@ describe("Action", () => {
mockEnvConfig.workflow_inputs = '{"fruit":[]}';
expect(() => getConfig()).toThrowError('"fruit" value is Array');
});

it("should handle no distinct_id being provided", () => {
mockEnvConfig.distinct_id = "";
const config: ActionConfig = getConfig();

expect(config.distinctId).toBeUndefined();
});
});
});
19 changes: 17 additions & 2 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export interface ActionConfig {
* Time until giving up on identifying the Run ID.
*/
workflowTimeoutSeconds: number;

/**
* Specify a static ID to use instead of a distinct ID.
*/
distinctId?: string;
}

type ActionWorkflowInputs = Record<string, string | number | boolean>;
Expand All @@ -55,11 +60,14 @@ export function getConfig(): ActionConfig {
ref: core.getInput("ref", { required: true }),
repo: core.getInput("repo", { required: true }),
owner: core.getInput("owner", { required: true }),
workflow: getWorkflowValue(core.getInput("workflow", { required: true })),
workflow: getWorkflowValueAsNumber(
core.getInput("workflow", { required: true }),
),
workflowInputs: getWorkflowInputs(core.getInput("workflow_inputs")),
workflowTimeoutSeconds:
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ??
WORKFLOW_TIMEOUT_SECONDS,
distinctId: getOptionalWorkflowValue(core.getInput("distinct_id")),
};
}

Expand Down Expand Up @@ -111,7 +119,7 @@ function getWorkflowInputs(
}
}

function getWorkflowValue(workflowInput: string): string | number {
function getWorkflowValueAsNumber(workflowInput: string): string | number {
try {
// We can assume that the string is defined and not empty at this point.
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -121,3 +129,10 @@ function getWorkflowValue(workflowInput: string): string | number {
return workflowInput;
}
}

/**
* We want empty strings to simply be undefined.
*/
function getOptionalWorkflowValue(workflowInput: string): string | undefined {
return workflowInput || undefined;
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function run(): Promise<void> {
}

// Dispatch the action
await api.dispatchWorkflow(DISTINCT_ID);
await api.dispatchWorkflow(config.distinctId ?? DISTINCT_ID);

const timeoutMs = config.workflowTimeoutSeconds * 1000;
let attemptNo = 0;
Expand Down

0 comments on commit 6970d41

Please sign in to comment.