Skip to content

Commit

Permalink
feat: use more efficient branch handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 2, 2024
1 parent 95105b0 commit 20d9a07
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
42 changes: 31 additions & 11 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { type ActionConfig, getConfig } from "./action.ts";
import { getBranchName } from "./utils.ts";
import type { BranchNameResult } from "./utils.ts";

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

Expand Down Expand Up @@ -71,7 +71,7 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
},
);
let workflowId: number | undefined;

let workflowIdUrl: string | undefined;
for await (const response of workflowIterator) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (response.status !== 200) {
Expand All @@ -80,11 +80,13 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
);
}

workflowId = response.data.find((workflow) =>
const workflowData = response.data.find((workflow) =>
new RegExp(sanitisedFilename).test(workflow.path),
)?.id;
);
workflowId = workflowData?.id;

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

core.info(
`Fetched Workflow ID:\n` +
` Repository: ${config.owner}/${config.repo}\n` +
` Workflow ID: '${workflowId}'\n` +
` Input Filename: '${workflowFilename}'\n` +
` Sanitised Filename: '${sanitisedFilename}'\n` +
` URL: ${workflowIdUrl}`,
);

return workflowId;
} catch (error) {
if (error instanceof Error) {
Expand Down Expand Up @@ -140,22 +151,28 @@ export async function getWorkflowRunUrl(runId: number): Promise<string> {
}
}

export async function getWorkflowRunIds(workflowId: number): Promise<number[]> {
export async function getWorkflowRunIds(
workflowId: number,
branch: BranchNameResult,
): Promise<number[]> {
try {
const branchName = getBranchName(config.ref);
const useBranchFilter =
!branch.isTag &&
branch.branchName !== undefined &&
branch.branchName !== "";

// https://docs.github.com/en/rest/reference/actions#list-workflow-runs
const response = await octokit.rest.actions.listWorkflowRuns({
owner: config.owner,
repo: config.repo,
workflow_id: workflowId,
...(branchName
...(useBranchFilter
? {
branch: branchName,
per_page: 5,
branch: branch.branchName,
per_page: 10,
}
: {
per_page: 10,
per_page: 20,
}),
});

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

const branchMsg = useBranchFilter
? `true (${branch.branchName})`
: `false (${branch.ref})`;
core.debug(
"Fetched Workflow Runs:\n" +
` Repository: ${config.owner}/${config.repo}\n` +
` Branch: ${branchName}\n` +
` Branch Filter: ${branchMsg}\n` +
` Workflow ID: ${workflowId}\n` +
` Runs Fetched: [${runIds.join(", ")}]`,
);
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async function run(): Promise<void> {

// Get all runs for a given workflow ID
const fetchWorkflowRunIds = await api.retryOrTimeout(
() => api.getWorkflowRunIds(workflowId),
() => api.getWorkflowRunIds(workflowId, branch),
Math.max(WORKFLOW_FETCH_TIMEOUT_MS, timeoutMs),
);
if (fetchWorkflowRunIds.timeout) {
Expand Down

0 comments on commit 20d9a07

Please sign in to comment.