Skip to content

Commit 4631cd5

Browse files
committed
refactor: API get to fetch
1 parent 9f87969 commit 4631cd5

File tree

6 files changed

+45
-44
lines changed

6 files changed

+45
-44
lines changed

src/api.spec.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import type { ActionConfig } from "./action.ts";
1414
import {
1515
dispatchWorkflow,
1616
fetchWorkflowId,
17-
getWorkflowRunIds,
18-
getWorkflowRunJobSteps,
17+
fetchWorkflowRunIds,
18+
fetchWorkflowRunJobSteps,
1919
fetchWorkflowRunUrl,
2020
init,
2121
retryOrTimeout,
@@ -249,14 +249,14 @@ describe("API", () => {
249249

250250
// Behaviour
251251
await expect(fetchWorkflowId("implode")).rejects.toThrow(
252-
`Failed to get workflows, expected 200 but received ${errorStatus}`,
252+
`Failed to fetch workflows, expected 200 but received ${errorStatus}`,
253253
);
254254

255255
// Logging
256256
assertOnlyCalled(coreErrorLogMock, coreDebugLogMock);
257257
expect(coreErrorLogMock).toHaveBeenCalledOnce();
258258
expect(coreErrorLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
259-
`"fetchWorkflowId: An unexpected error has occurred: Failed to get workflows, expected 200 but received 401"`,
259+
`"fetchWorkflowId: An unexpected error has occurred: Failed to fetch workflows, expected 200 but received 401"`,
260260
);
261261
});
262262

@@ -283,7 +283,7 @@ describe("API", () => {
283283
});
284284
});
285285

286-
describe("getWorkflowRunIds", () => {
286+
describe("fetchWorkflowRunIds", () => {
287287
const workflowIdCfg: ActionConfig = {
288288
token: "secret",
289289
ref: "/refs/heads/feature_branch",
@@ -315,7 +315,7 @@ describe("API", () => {
315315
);
316316

317317
// Behaviour
318-
await expect(getWorkflowRunIds(0, branch)).resolves.toStrictEqual(
318+
await expect(fetchWorkflowRunIds(0, branch)).resolves.toStrictEqual(
319319
mockData.workflow_runs.map((run) => run.id),
320320
);
321321

@@ -346,15 +346,15 @@ describe("API", () => {
346346
);
347347

348348
// Behaviour
349-
await expect(getWorkflowRunIds(0, branch)).rejects.toThrow(
350-
`Failed to get Workflow runs, expected 200 but received ${errorStatus}`,
349+
await expect(fetchWorkflowRunIds(0, branch)).rejects.toThrow(
350+
`Failed to fetch Workflow runs, expected 200 but received ${errorStatus}`,
351351
);
352352

353353
// Logging
354354
assertOnlyCalled(coreErrorLogMock, coreDebugLogMock);
355355
expect(coreErrorLogMock).toHaveBeenCalled();
356356
expect(coreErrorLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
357-
`"getWorkflowRunIds: An unexpected error has occurred: Failed to get Workflow runs, expected 200 but received 401"`,
357+
`"fetchWorkflowRunIds: An unexpected error has occurred: Failed to fetch Workflow runs, expected 200 but received 401"`,
358358
);
359359
});
360360

@@ -374,7 +374,7 @@ describe("API", () => {
374374
);
375375

376376
// Behaviour
377-
await expect(getWorkflowRunIds(0, branch)).resolves.toStrictEqual([]);
377+
await expect(fetchWorkflowRunIds(0, branch)).resolves.toStrictEqual([]);
378378

379379
// Logging
380380
assertOnlyCalled(coreDebugLogMock);
@@ -410,7 +410,7 @@ describe("API", () => {
410410
);
411411

412412
// Behaviour
413-
await expect(getWorkflowRunIds(0, branch)).resolves.not.toThrow();
413+
await expect(fetchWorkflowRunIds(0, branch)).resolves.not.toThrow();
414414
expect(parsedRef).toStrictEqual("master");
415415

416416
// Logging
@@ -447,7 +447,7 @@ describe("API", () => {
447447
);
448448

449449
// Behaviour
450-
await expect(getWorkflowRunIds(0, branch)).resolves.not.toThrow();
450+
await expect(fetchWorkflowRunIds(0, branch)).resolves.not.toThrow();
451451
expect(parsedRef).toBeUndefined();
452452

453453
// Logging
@@ -484,7 +484,7 @@ describe("API", () => {
484484
);
485485

486486
// Behaviour
487-
await expect(getWorkflowRunIds(0, branch)).resolves.not.toThrow();
487+
await expect(fetchWorkflowRunIds(0, branch)).resolves.not.toThrow();
488488
expect(parsedRef).toBeUndefined();
489489

490490
// Logging
@@ -502,7 +502,7 @@ describe("API", () => {
502502
});
503503
});
504504

505-
describe("getWorkflowRunJobSteps", () => {
505+
describe("fetchWorkflowRunJobSteps", () => {
506506
it("should get the step names for a given Workflow Run ID", async () => {
507507
const mockData = {
508508
total_count: 1,
@@ -533,7 +533,7 @@ describe("API", () => {
533533
);
534534

535535
// Behaviour
536-
await expect(getWorkflowRunJobSteps(0)).resolves.toStrictEqual([
536+
await expect(fetchWorkflowRunJobSteps(0)).resolves.toStrictEqual([
537537
"Test Step 1",
538538
"Test Step 2",
539539
]);
@@ -565,15 +565,15 @@ describe("API", () => {
565565
);
566566

567567
// Behaviour
568-
await expect(getWorkflowRunJobSteps(0)).rejects.toThrow(
569-
`Failed to get Workflow Run Jobs, expected 200 but received ${errorStatus}`,
568+
await expect(fetchWorkflowRunJobSteps(0)).rejects.toThrow(
569+
`Failed to fetch Workflow Run Jobs, expected 200 but received ${errorStatus}`,
570570
);
571571

572572
// Logging
573573
assertOnlyCalled(coreErrorLogMock, coreDebugLogMock);
574574
expect(coreErrorLogMock).toHaveBeenCalledOnce();
575575
expect(coreErrorLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
576-
`"getWorkflowRunJobSteps: An unexpected error has occurred: Failed to get Workflow Run Jobs, expected 200 but received 401"`,
576+
`"fetchWorkflowRunJobSteps: An unexpected error has occurred: Failed to fetch Workflow Run Jobs, expected 200 but received 401"`,
577577
);
578578
});
579579

@@ -598,7 +598,7 @@ describe("API", () => {
598598
);
599599

600600
// Behaviour
601-
await expect(getWorkflowRunJobSteps(0)).resolves.toStrictEqual([]);
601+
await expect(fetchWorkflowRunJobSteps(0)).resolves.toStrictEqual([]);
602602

603603
// Logging
604604
assertOnlyCalled(coreDebugLogMock);
@@ -642,14 +642,14 @@ describe("API", () => {
642642

643643
// Behaviour
644644
await expect(fetchWorkflowRunUrl(0)).rejects.toThrow(
645-
`Failed to get Workflow Run state, expected 200 but received ${errorStatus}`,
645+
`Failed to fetch Workflow Run state, expected 200 but received ${errorStatus}`,
646646
);
647647

648648
// Logging
649649
assertOnlyCalled(coreErrorLogMock, coreDebugLogMock);
650650
expect(coreErrorLogMock).toHaveBeenCalledOnce();
651651
expect(coreErrorLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
652-
`"fetchWorkflowRunUrl: An unexpected error has occurred: Failed to get Workflow Run state, expected 200 but received 401"`,
652+
`"fetchWorkflowRunUrl: An unexpected error has occurred: Failed to fetch Workflow Run state, expected 200 but received 401"`,
653653
);
654654
});
655655
});

src/api.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export async function fetchWorkflowId(
8080
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
8181
if (response.status !== 200) {
8282
throw new Error(
83-
`Failed to get workflows, expected 200 but received ${response.status}`,
83+
`Failed to fetch workflows, expected 200 but received ${response.status}`,
8484
);
8585
}
8686

@@ -132,7 +132,7 @@ export async function fetchWorkflowRunUrl(runId: number): Promise<string> {
132132
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
133133
if (response.status !== 200) {
134134
throw new Error(
135-
`Failed to get Workflow Run state, expected 200 but received ${response.status}`,
135+
`Failed to fetch Workflow Run state, expected 200 but received ${response.status}`,
136136
);
137137
}
138138

@@ -155,7 +155,7 @@ export async function fetchWorkflowRunUrl(runId: number): Promise<string> {
155155
}
156156
}
157157

158-
export async function getWorkflowRunIds(
158+
export async function fetchWorkflowRunIds(
159159
workflowId: number,
160160
branch: BranchNameResult,
161161
): Promise<number[]> {
@@ -183,7 +183,7 @@ export async function getWorkflowRunIds(
183183
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
184184
if (response.status !== 200) {
185185
throw new Error(
186-
`Failed to get Workflow runs, expected 200 but received ${response.status}`,
186+
`Failed to fetch Workflow runs, expected 200 but received ${response.status}`,
187187
);
188188
}
189189

@@ -206,15 +206,17 @@ export async function getWorkflowRunIds(
206206
} catch (error) {
207207
if (error instanceof Error) {
208208
core.error(
209-
`getWorkflowRunIds: An unexpected error has occurred: ${error.message}`,
209+
`fetchWorkflowRunIds: An unexpected error has occurred: ${error.message}`,
210210
);
211211
core.debug(error.stack ?? "");
212212
}
213213
throw error;
214214
}
215215
}
216216

217-
export async function getWorkflowRunJobSteps(runId: number): Promise<string[]> {
217+
export async function fetchWorkflowRunJobSteps(
218+
runId: number,
219+
): Promise<string[]> {
218220
try {
219221
// https://docs.github.com/en/rest/actions/workflow-jobs#list-jobs-for-a-workflow-run
220222
const response = await octokit.rest.actions.listJobsForWorkflowRun({
@@ -227,7 +229,7 @@ export async function getWorkflowRunJobSteps(runId: number): Promise<string[]> {
227229
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
228230
if (response.status !== 200) {
229231
throw new Error(
230-
`Failed to get Workflow Run Jobs, expected 200 but received ${response.status}`,
232+
`Failed to fetch Workflow Run Jobs, expected 200 but received ${response.status}`,
231233
);
232234
}
233235

@@ -249,7 +251,7 @@ export async function getWorkflowRunJobSteps(runId: number): Promise<string[]> {
249251
} catch (error) {
250252
if (error instanceof Error) {
251253
core.error(
252-
`getWorkflowRunJobSteps: An unexpected error has occurred: ${error.message}`,
254+
`fetchWorkflowRunJobSteps: An unexpected error has occurred: ${error.message}`,
253255
);
254256
core.debug(error.stack ?? "");
255257
}

src/main.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe("main", () => {
156156
distinctId: testCfg.distinctId,
157157
workflow: testCfg.workflow,
158158
workflowId: 0,
159-
workflowTimeoutSeconds: testCfg.workflowTimeoutSeconds,
159+
workflowTimeoutMs: testCfg.workflowTimeoutSeconds * 1000,
160160
});
161161

162162
// Result

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export async function main(): Promise<void> {
3333
distinctId: config.distinctId,
3434
workflow: config.workflow,
3535
workflowId,
36-
workflowTimeoutSeconds: config.workflowTimeoutSeconds,
36+
workflowTimeoutMs: config.workflowTimeoutSeconds * 1000,
3737
});
3838
if (result.success) {
3939
handleActionSuccess(result.value.id, result.value.url);

src/return-dispatch.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ describe("return-dispatch", () => {
165165
const testId = uuid();
166166

167167
let getWorkflowRunJobStepMock: MockInstance<
168-
typeof api.getWorkflowRunJobSteps
168+
typeof api.fetchWorkflowRunJobSteps
169169
>;
170170
let fetchWorkflowRunUrlMock: MockInstance<typeof api.fetchWorkflowRunUrl>;
171171

172172
beforeEach(() => {
173-
getWorkflowRunJobStepMock = vi.spyOn(api, "getWorkflowRunJobSteps");
173+
getWorkflowRunJobStepMock = vi.spyOn(api, "fetchWorkflowRunJobSteps");
174174
fetchWorkflowRunUrlMock = vi.spyOn(api, "fetchWorkflowRunUrl");
175175
});
176176

src/return-dispatch.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ export async function attemptToFindRunId(
4343
workflowRunIds: number[],
4444
): Promise<Result<{ id: number; url: string }>> {
4545
let currentWorkflowRunIndex = 0;
46-
let currentGetWorkflowRunJobStepsAttempt = 0;
46+
let currentFetchWorkflowRunJobStepsAttempt = 0;
4747
while (currentWorkflowRunIndex < workflowRunIds.length) {
4848
const id = workflowRunIds[currentWorkflowRunIndex];
4949
if (id === undefined) {
5050
break;
5151
}
5252

5353
try {
54-
const steps = await api.getWorkflowRunJobSteps(id);
54+
const steps = await api.fetchWorkflowRunJobSteps(id);
5555

5656
for (const step of steps) {
5757
if (idRegex.test(step)) {
@@ -66,17 +66,17 @@ export async function attemptToFindRunId(
6666

6767
const shouldRetry = shouldRetryOrThrow(
6868
error,
69-
currentGetWorkflowRunJobStepsAttempt,
69+
currentFetchWorkflowRunJobStepsAttempt,
7070
);
7171
if (shouldRetry) {
72-
currentGetWorkflowRunJobStepsAttempt++;
72+
currentFetchWorkflowRunJobStepsAttempt++;
7373
await sleep(constants.WORKFLOW_JOB_STEPS_SERVER_ERROR_RETRY_MS);
7474
// Continue without increasing the current index to retry the same ID.
7575
continue;
7676
}
7777
}
7878

79-
currentGetWorkflowRunJobStepsAttempt = 0;
79+
currentFetchWorkflowRunJobStepsAttempt = 0;
8080
currentWorkflowRunIndex++;
8181
}
8282

@@ -122,32 +122,31 @@ interface GetRunIdOpts {
122122
distinctId: string;
123123
workflow: string | number;
124124
workflowId: number;
125-
workflowTimeoutSeconds: number;
125+
workflowTimeoutMs: number;
126126
}
127127
export async function getRunId({
128128
startTime,
129129
branch,
130130
distinctId,
131131
workflow,
132132
workflowId,
133-
workflowTimeoutSeconds,
133+
workflowTimeoutMs,
134134
}: GetRunIdOpts): Promise<Result<{ id: number; url: string }>> {
135-
const timeoutMs = workflowTimeoutSeconds * 1000;
136135
const distinctIdRegex = new RegExp(distinctId);
137136

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

141140
let attemptNo = 0;
142141
let elapsedTime = Date.now() - startTime;
143-
while (elapsedTime < timeoutMs) {
142+
while (elapsedTime < workflowTimeoutMs) {
144143
attemptNo++;
145144
elapsedTime = Date.now() - startTime;
146145

147146
// Get all runs for a given workflow ID
148147
const fetchWorkflowRunIds = await api.retryOrTimeout(
149-
() => api.getWorkflowRunIds(workflowId, branch),
150-
Math.max(constants.WORKFLOW_FETCH_TIMEOUT_MS, timeoutMs),
148+
() => api.fetchWorkflowRunIds(workflowId, branch),
149+
Math.max(constants.WORKFLOW_FETCH_TIMEOUT_MS, workflowTimeoutMs),
151150
);
152151
if (!fetchWorkflowRunIds.success) {
153152
core.debug(

0 commit comments

Comments
 (0)