Skip to content

Commit 0328d6a

Browse files
authored
Merge pull request #240 from Codex-/feat/static_id
feat: add support for specifying the distinct_id to use
2 parents 0c77d72 + 6970d41 commit 0328d6a

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

action.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ inputs:
3030
workflow_timeout_seconds:
3131
description: Time until giving up waiting for the start of the workflow run.
3232
default: 300
33+
distinct_id:
34+
description: Specify a static string to use instead of a random distinct ID.
3335

3436
runs:
3537
using: node20

src/action.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe("Action", () => {
2323
workflow: "workflow_name",
2424
workflow_inputs: JSON.stringify(workflowInputs),
2525
workflow_timeout_seconds: "60",
26+
distinct_id: "distinct_id",
2627
};
2728

2829
vi.spyOn(core, "getInput").mockImplementation((input: string): string => {
@@ -42,6 +43,8 @@ describe("Action", () => {
4243
return mockEnvConfig.workflow_inputs;
4344
case "workflow_timeout_seconds":
4445
return mockEnvConfig.workflow_timeout_seconds;
46+
case "distinct_id":
47+
return mockEnvConfig.distinct_id;
4548
default:
4649
throw new Error("invalid input requested");
4750
}
@@ -64,6 +67,7 @@ describe("Action", () => {
6467
expect(config.workflow).toStrictEqual("workflow_name");
6568
expect(config.workflowInputs).toStrictEqual(workflowInputs);
6669
expect(config.workflowTimeoutSeconds).toStrictEqual(60);
70+
expect(config.distinctId).toStrictEqual("distinct_id");
6771
});
6872

6973
it("should have a number for a workflow when given a workflow ID", () => {
@@ -110,5 +114,12 @@ describe("Action", () => {
110114
mockEnvConfig.workflow_inputs = '{"fruit":[]}';
111115
expect(() => getConfig()).toThrowError('"fruit" value is Array');
112116
});
117+
118+
it("should handle no distinct_id being provided", () => {
119+
mockEnvConfig.distinct_id = "";
120+
const config: ActionConfig = getConfig();
121+
122+
expect(config.distinctId).toBeUndefined();
123+
});
113124
});
114125
});

src/action.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ export interface ActionConfig {
4040
* Time until giving up on identifying the Run ID.
4141
*/
4242
workflowTimeoutSeconds: number;
43+
44+
/**
45+
* Specify a static ID to use instead of a distinct ID.
46+
*/
47+
distinctId?: string;
4348
}
4449

4550
type ActionWorkflowInputs = Record<string, string | number | boolean>;
@@ -55,11 +60,14 @@ export function getConfig(): ActionConfig {
5560
ref: core.getInput("ref", { required: true }),
5661
repo: core.getInput("repo", { required: true }),
5762
owner: core.getInput("owner", { required: true }),
58-
workflow: getWorkflowValue(core.getInput("workflow", { required: true })),
63+
workflow: getWorkflowValueAsNumber(
64+
core.getInput("workflow", { required: true }),
65+
),
5966
workflowInputs: getWorkflowInputs(core.getInput("workflow_inputs")),
6067
workflowTimeoutSeconds:
6168
getNumberFromValue(core.getInput("workflow_timeout_seconds")) ??
6269
WORKFLOW_TIMEOUT_SECONDS,
70+
distinctId: getOptionalWorkflowValue(core.getInput("distinct_id")),
6371
};
6472
}
6573

@@ -111,7 +119,7 @@ function getWorkflowInputs(
111119
}
112120
}
113121

114-
function getWorkflowValue(workflowInput: string): string | number {
122+
function getWorkflowValueAsNumber(workflowInput: string): string | number {
115123
try {
116124
// We can assume that the string is defined and not empty at this point.
117125
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@@ -121,3 +129,10 @@ function getWorkflowValue(workflowInput: string): string | number {
121129
return workflowInput;
122130
}
123131
}
132+
133+
/**
134+
* We want empty strings to simply be undefined.
135+
*/
136+
function getOptionalWorkflowValue(workflowInput: string): string | undefined {
137+
return workflowInput || undefined;
138+
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ async function run(): Promise<void> {
2424
}
2525

2626
// Dispatch the action
27-
await api.dispatchWorkflow(DISTINCT_ID);
27+
await api.dispatchWorkflow(config.distinctId ?? DISTINCT_ID);
2828

2929
const timeoutMs = config.workflowTimeoutSeconds * 1000;
3030
let attemptNo = 0;

0 commit comments

Comments
 (0)