Skip to content

Commit 20d9a07

Browse files
committed
feat: use more efficient branch handling
1 parent 95105b0 commit 20d9a07

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/api.ts

+31-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from "@actions/core";
22
import * as github from "@actions/github";
33
import { type ActionConfig, getConfig } from "./action.ts";
4-
import { getBranchName } from "./utils.ts";
4+
import type { BranchNameResult } from "./utils.ts";
55

66
type Octokit = ReturnType<(typeof github)["getOctokit"]>;
77

@@ -71,7 +71,7 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
7171
},
7272
);
7373
let workflowId: number | undefined;
74-
74+
let workflowIdUrl: string | undefined;
7575
for await (const response of workflowIterator) {
7676
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
7777
if (response.status !== 200) {
@@ -80,11 +80,13 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
8080
);
8181
}
8282

83-
workflowId = response.data.find((workflow) =>
83+
const workflowData = response.data.find((workflow) =>
8484
new RegExp(sanitisedFilename).test(workflow.path),
85-
)?.id;
85+
);
86+
workflowId = workflowData?.id;
8687

8788
if (workflowId !== undefined) {
89+
workflowIdUrl = workflowData?.html_url;
8890
break;
8991
}
9092
}
@@ -93,6 +95,15 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
9395
throw new Error(`Unable to find ID for Workflow: ${workflowFilename}`);
9496
}
9597

98+
core.info(
99+
`Fetched Workflow ID:\n` +
100+
` Repository: ${config.owner}/${config.repo}\n` +
101+
` Workflow ID: '${workflowId}'\n` +
102+
` Input Filename: '${workflowFilename}'\n` +
103+
` Sanitised Filename: '${sanitisedFilename}'\n` +
104+
` URL: ${workflowIdUrl}`,
105+
);
106+
96107
return workflowId;
97108
} catch (error) {
98109
if (error instanceof Error) {
@@ -140,22 +151,28 @@ export async function getWorkflowRunUrl(runId: number): Promise<string> {
140151
}
141152
}
142153

143-
export async function getWorkflowRunIds(workflowId: number): Promise<number[]> {
154+
export async function getWorkflowRunIds(
155+
workflowId: number,
156+
branch: BranchNameResult,
157+
): Promise<number[]> {
144158
try {
145-
const branchName = getBranchName(config.ref);
159+
const useBranchFilter =
160+
!branch.isTag &&
161+
branch.branchName !== undefined &&
162+
branch.branchName !== "";
146163

147164
// https://docs.github.com/en/rest/reference/actions#list-workflow-runs
148165
const response = await octokit.rest.actions.listWorkflowRuns({
149166
owner: config.owner,
150167
repo: config.repo,
151168
workflow_id: workflowId,
152-
...(branchName
169+
...(useBranchFilter
153170
? {
154-
branch: branchName,
155-
per_page: 5,
171+
branch: branch.branchName,
172+
per_page: 10,
156173
}
157174
: {
158-
per_page: 10,
175+
per_page: 20,
159176
}),
160177
});
161178

@@ -170,10 +187,13 @@ export async function getWorkflowRunIds(workflowId: number): Promise<number[]> {
170187
(workflowRun) => workflowRun.id,
171188
);
172189

190+
const branchMsg = useBranchFilter
191+
? `true (${branch.branchName})`
192+
: `false (${branch.ref})`;
173193
core.debug(
174194
"Fetched Workflow Runs:\n" +
175195
` Repository: ${config.owner}/${config.repo}\n` +
176-
` Branch: ${branchName}\n` +
196+
` Branch Filter: ${branchMsg}\n` +
177197
` Workflow ID: ${workflowId}\n` +
178198
` Runs Fetched: [${runIds.join(", ")}]`,
179199
);

src/main.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async function run(): Promise<void> {
131131

132132
// Get all runs for a given workflow ID
133133
const fetchWorkflowRunIds = await api.retryOrTimeout(
134-
() => api.getWorkflowRunIds(workflowId),
134+
() => api.getWorkflowRunIds(workflowId, branch),
135135
Math.max(WORKFLOW_FETCH_TIMEOUT_MS, timeoutMs),
136136
);
137137
if (fetchWorkflowRunIds.timeout) {

0 commit comments

Comments
 (0)