Skip to content

Commit 5210b85

Browse files
authored
Merge pull request #294 from carlobeltrame/filter-workflow-runs
Filter workflow runs
2 parents d55ca73 + 96e779d commit 5210b85

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

dist/index.mjs

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.spec.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ describe("API", () => {
326326
});
327327

328328
describe("fetchWorkflowRunIds", () => {
329+
const startTimeISO = "2025-06-17T22:24:23.238Z";
329330
const workflowIdCfg: ActionConfig = {
330331
token: "secret",
331332
ref: "/refs/heads/feature_branch",
@@ -358,9 +359,9 @@ describe("API", () => {
358359
);
359360

360361
// Behaviour
361-
await expect(fetchWorkflowRunIds(0, branch)).resolves.toStrictEqual(
362-
mockData.workflow_runs.map((run) => run.id),
363-
);
362+
await expect(
363+
fetchWorkflowRunIds(0, branch, startTimeISO),
364+
).resolves.toStrictEqual(mockData.workflow_runs.map((run) => run.id));
364365

365366
// Logging
366367
assertOnlyCalled(coreDebugLogMock);
@@ -371,6 +372,7 @@ describe("API", () => {
371372
Repository: owner/repository
372373
Branch Filter: true (feature_branch)
373374
Workflow ID: 0
375+
Created: >=2025-06-17T22:24:23.238Z
374376
Runs Fetched: [0, 1, 2]"
375377
`,
376378
);
@@ -389,7 +391,9 @@ describe("API", () => {
389391
);
390392

391393
// Behaviour
392-
await expect(fetchWorkflowRunIds(0, branch)).rejects.toThrow(
394+
await expect(
395+
fetchWorkflowRunIds(0, branch, startTimeISO),
396+
).rejects.toThrow(
393397
`Failed to fetch Workflow runs, expected 200 but received ${errorStatus}`,
394398
);
395399

@@ -417,7 +421,9 @@ describe("API", () => {
417421
);
418422

419423
// Behaviour
420-
await expect(fetchWorkflowRunIds(0, branch)).resolves.toStrictEqual([]);
424+
await expect(
425+
fetchWorkflowRunIds(0, branch, startTimeISO),
426+
).resolves.toStrictEqual([]);
421427

422428
// Logging
423429
assertOnlyCalled(coreDebugLogMock);
@@ -428,6 +434,7 @@ describe("API", () => {
428434
Repository: owner/repository
429435
Branch Filter: true (feature_branch)
430436
Workflow ID: 0
437+
Created: >=2025-06-17T22:24:23.238Z
431438
Runs Fetched: []"
432439
`,
433440
);
@@ -453,7 +460,9 @@ describe("API", () => {
453460
);
454461

455462
// Behaviour
456-
await expect(fetchWorkflowRunIds(0, branch)).resolves.not.toThrow();
463+
await expect(
464+
fetchWorkflowRunIds(0, branch, startTimeISO),
465+
).resolves.not.toThrow();
457466
expect(parsedRef).toStrictEqual("master");
458467

459468
// Logging
@@ -465,6 +474,7 @@ describe("API", () => {
465474
Repository: owner/repository
466475
Branch Filter: true (master)
467476
Workflow ID: 0
477+
Created: >=2025-06-17T22:24:23.238Z
468478
Runs Fetched: []"
469479
`,
470480
);
@@ -490,7 +500,9 @@ describe("API", () => {
490500
);
491501

492502
// Behaviour
493-
await expect(fetchWorkflowRunIds(0, branch)).resolves.not.toThrow();
503+
await expect(
504+
fetchWorkflowRunIds(0, branch, startTimeISO),
505+
).resolves.not.toThrow();
494506
expect(parsedRef).toBeUndefined();
495507

496508
// Logging
@@ -502,6 +514,7 @@ describe("API", () => {
502514
Repository: owner/repository
503515
Branch Filter: false (/refs/tags/1.5.0)
504516
Workflow ID: 0
517+
Created: >=2025-06-17T22:24:23.238Z
505518
Runs Fetched: []"
506519
`,
507520
);
@@ -527,7 +540,9 @@ describe("API", () => {
527540
);
528541

529542
// Behaviour
530-
await expect(fetchWorkflowRunIds(0, branch)).resolves.not.toThrow();
543+
await expect(
544+
fetchWorkflowRunIds(0, branch, startTimeISO),
545+
).resolves.not.toThrow();
531546
expect(parsedRef).toBeUndefined();
532547

533548
// Logging
@@ -539,6 +554,7 @@ describe("API", () => {
539554
Repository: owner/repository
540555
Branch Filter: false (/refs/cake)
541556
Workflow ID: 0
557+
Created: >=2025-06-17T22:24:23.238Z
542558
Runs Fetched: []"
543559
`,
544560
);

src/api.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,23 @@ export async function fetchWorkflowRunUrl(runId: number): Promise<string> {
158158
export async function fetchWorkflowRunIds(
159159
workflowId: number,
160160
branch: BranchNameResult,
161+
startTimeISO: string,
161162
): Promise<number[]> {
162163
try {
163164
const useBranchFilter =
164165
!branch.isTag &&
165166
branch.branchName !== undefined &&
166167
branch.branchName !== "";
167168

169+
const createdFrom = `>=${startTimeISO}`;
170+
168171
// https://docs.github.com/en/rest/actions/workflow-runs#list-workflow-runs-for-a-repository
169172
const response = await octokit.rest.actions.listWorkflowRuns({
170173
owner: config.owner,
171174
repo: config.repo,
172175
workflow_id: workflowId,
176+
created: createdFrom,
177+
event: "workflow_dispatch",
173178
...(useBranchFilter
174179
? {
175180
branch: branch.branchName,
@@ -199,6 +204,7 @@ export async function fetchWorkflowRunIds(
199204
` Repository: ${config.owner}/${config.repo}\n` +
200205
` Branch Filter: ${branchMsg}\n` +
201206
` Workflow ID: ${workflowId}\n` +
207+
` Created: ${createdFrom}\n` +
202208
` Runs Fetched: [${runIds.join(", ")}]`,
203209
);
204210

src/return-dispatch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export async function getRunIdAndUrl({
139139
workflowTimeoutMs,
140140
workflowJobStepsRetryMs,
141141
}: GetRunIdAndUrlOpts): Promise<Result<{ id: number; url: string }>> {
142+
const startTimeISO = new Date(startTime).toISOString();
142143
const retryTimeout = Math.max(
143144
constants.WORKFLOW_FETCH_TIMEOUT_MS,
144145
workflowTimeoutMs,
@@ -151,7 +152,7 @@ export async function getRunIdAndUrl({
151152

152153
// Get all runs for a given workflow ID
153154
const fetchWorkflowRunIds = await api.retryOrTimeout(
154-
() => api.fetchWorkflowRunIds(workflowId, branch),
155+
() => api.fetchWorkflowRunIds(workflowId, branch, startTimeISO),
155156
retryTimeout,
156157
);
157158
if (!fetchWorkflowRunIds.success) {

0 commit comments

Comments
 (0)