diff --git a/action.yml b/action.yml index 00a94b3e..e1a9cbb4 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/src/action.spec.ts b/src/action.spec.ts index 341eafbd..f069df48 100644 --- a/src/action.spec.ts +++ b/src/action.spec.ts @@ -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 => { @@ -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"); } @@ -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", () => { @@ -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(); + }); }); }); diff --git a/src/action.ts b/src/action.ts index 168b6031..9d65656d 100644 --- a/src/action.ts +++ b/src/action.ts @@ -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; @@ -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")), }; } @@ -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 @@ -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; +} diff --git a/src/main.ts b/src/main.ts index f5219285..119f0357 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,7 +24,7 @@ async function run(): Promise { } // Dispatch the action - await api.dispatchWorkflow(DISTINCT_ID); + await api.dispatchWorkflow(config.distinctId ?? DISTINCT_ID); const timeoutMs = config.workflowTimeoutSeconds * 1000; let attemptNo = 0;