Skip to content

Commit

Permalink
fix: when workflow name is the substring of another workflow name
Browse files Browse the repository at this point in the history
  • Loading branch information
Codex- committed Oct 16, 2024
1 parent 74e3f6f commit 83052a1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
40 changes: 40 additions & 0 deletions src/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,46 @@ describe("API", () => {
`"fetchWorkflowId: An unexpected error has occurred: Unable to find ID for Workflow: slice"`,
);
});

it("should return the workflow ID when the name is a substring of another workflow name", async () => {
const mockData = [
{
id: 0,
path: ".github/workflows/small-cake.yml",
},
{
id: 1,
path: ".github/workflows/big-cake.yml",
},
{
id: 2,
path: ".github/workflows/cake.yml",
},
];
vi.spyOn(mockOctokit.rest.actions, "listRepoWorkflows").mockReturnValue(
Promise.resolve({
data: mockData,
status: 200,
}),
);

// Behaviour
expect(await fetchWorkflowId("cake.yml")).toStrictEqual(mockData[2]!.id);

// Logging
assertOnlyCalled(coreInfoLogMock);
expect(coreInfoLogMock).toHaveBeenCalledOnce();
expect(coreInfoLogMock.mock.calls[0]?.[0]).toMatchInlineSnapshot(
`
"Fetched Workflow ID:
Repository: owner/repo
Workflow ID: '2'
Input Filename: 'cake.yml'
Sanitised Filename: 'cake\\.yml'
URL: undefined"
`,
);
});
});

describe("fetchWorkflowRunIds", () => {
Expand Down
10 changes: 5 additions & 5 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ export async function fetchWorkflowId(
workflowFilename: string,
): Promise<number> {
try {
const sanitisedFilename = workflowFilename.replace(
/[.*+?^${}()|[\]\\]/g,
"\\$&",
);
const sanitisedFilename = workflowFilename
.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
.trim();
const filenameRegex = new RegExp(`/${sanitisedFilename}`);

// https://docs.github.com/en/rest/actions/workflows#list-repository-workflows
const workflowIterator = octokit.paginate.iterator(
Expand All @@ -85,7 +85,7 @@ export async function fetchWorkflowId(
}

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

Expand Down

0 comments on commit 83052a1

Please sign in to comment.