Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: look beyond first page of repo workflows #199

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 23 additions & 24 deletions src/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ interface MockResponse {
status: number;
}

async function* mockPageIterator<T, P>(apiMethod: (params: P) => T, params: P) {
yield apiMethod(params);
}

const mockOctokit = {
rest: {
actions: {
Expand All @@ -44,6 +48,9 @@ const mockOctokit = {
},
},
},
paginate: {
iterator: mockPageIterator,
},
};

describe("API", () => {
Expand Down Expand Up @@ -130,33 +137,28 @@ describe("API", () => {

describe("getWorkflowId", () => {
it("should return the workflow ID for a given workflow filename", async () => {
const mockData = {
total_count: 3,
workflows: [
{
id: 0,
path: ".github/workflows/cake.yml",
},
{
id: 1,
path: ".github/workflows/pie.yml",
},
{
id: 2,
path: ".github/workflows/slice.yml",
},
],
};
const mockData = [
{
id: 0,
path: ".github/workflows/cake.yml",
},
{
id: 1,
path: ".github/workflows/pie.yml",
},
{
id: 2,
path: ".github/workflows/slice.yml",
},
];
vi.spyOn(mockOctokit.rest.actions, "listRepoWorkflows").mockReturnValue(
Promise.resolve({
data: mockData,
status: 200,
}),
);

expect(await getWorkflowId("slice.yml")).toStrictEqual(
mockData.workflows[2].id,
);
expect(await getWorkflowId("slice.yml")).toStrictEqual(mockData[2].id);
});

it("should throw if a non-200 status is returned", async () => {
Expand All @@ -177,10 +179,7 @@ describe("API", () => {
const workflowName = "slice";
vi.spyOn(mockOctokit.rest.actions, "listRepoWorkflows").mockReturnValue(
Promise.resolve({
data: {
total_count: 0,
workflows: [],
},
data: [],
status: 200,
}),
);
Expand Down
43 changes: 28 additions & 15 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,38 @@ export async function dispatchWorkflow(distinctId: string): Promise<void> {

export async function getWorkflowId(workflowFilename: string): Promise<number> {
try {
// https://docs.github.com/en/rest/reference/actions#list-repository-workflows
const response = await octokit.rest.actions.listRepoWorkflows({
owner: config.owner,
repo: config.repo,
});

if (response.status !== 200) {
throw new Error(
`Failed to get workflows, expected 200 but received ${response.status}`,
);
}

const sanitisedFilename = workflowFilename.replace(
/[.*+?^${}()|[\]\\]/g,
"\\$&",
);
const workflowId = response.data.workflows.find((workflow) =>
new RegExp(sanitisedFilename).test(workflow.path),
)?.id;

// https://docs.github.com/en/rest/reference/actions#list-repository-workflows
const workflowIterator = octokit.paginate.iterator(
octokit.rest.actions.listRepoWorkflows,
{
owner: config.owner,
repo: config.repo,
},
);
let workflowId: number | undefined;

for await (const response of workflowIterator) {
if (response.status !== 200) {
throw new Error(
`Failed to get workflows, expected 200 but received ${response.status}`,
);
}
// wrong type definition
const workflows: typeof response.data.workflows = response.data;

workflowId = workflows.find((workflow) =>
new RegExp(sanitisedFilename).test(workflow.path),
)?.id;

if (workflowId !== undefined) {
break;
}
}

if (workflowId === undefined) {
throw new Error(`Unable to find ID for Workflow: ${workflowFilename}`);
Expand Down
Loading