feat(scaffolder): add support for autocompleting GitHub branches to handleAutocompleteRequest

Signed-off-by: Benjamin Janssens <benji.janssens@gmail.com>
This commit is contained in:
Benjamin Janssens
2025-02-03 16:09:44 +01:00
parent 478c67228a
commit 3323fc8bc7
2 changed files with 45 additions and 2 deletions
@@ -29,6 +29,7 @@ const mockOctokit = {
rest: {
repos: {
listForAuthenticatedUser: jest.fn(),
listBranches: jest.fn(),
},
},
};
@@ -67,6 +68,33 @@ describe('handleAutocompleteRequest', () => {
});
});
it('should return branches', async () => {
const handleAutocompleteRequest = createHandleAutocompleteRequest({
integrations: mockIntegrations,
});
mockOctokit.rest.repos.listBranches.mockResolvedValue({
data: [
{
name: 'main',
},
],
});
const result = await handleAutocompleteRequest({
resource: 'branches',
token: 'token',
context: {
owner: 'backstage',
repository: 'backstage',
},
});
expect(result).toEqual({
results: [{ id: 'main' }],
});
});
it('should throw an error for invalid resource', async () => {
const handleAutocompleteRequest = createHandleAutocompleteRequest({
integrations: mockIntegrations,
@@ -81,15 +109,15 @@ describe('handleAutocompleteRequest', () => {
).rejects.toThrow(InputError);
});
it('should throw an error if context id is missing for repositories', async () => {
it('should throw an error when there are missing parameters', async () => {
const handleAutocompleteRequest = createHandleAutocompleteRequest({
integrations: mockIntegrations,
});
await expect(
handleAutocompleteRequest({
resource: 'repositories',
token: 'token',
resource: 'branches',
context: {},
}),
).rejects.toThrow(InputError);
@@ -49,6 +49,21 @@ export function createHandleAutocompleteRequest(options: {
return { results };
}
case 'branches': {
if (!context.owner || !context.repository)
throw new InputError(
'Missing owner and/or repository context parameter',
);
const branches = await client.paginate(client.rest.repos.listBranches, {
owner: context.owner,
repo: context.repository,
});
const results = branches.map(r => ({ id: r.name }));
return { results };
}
default:
throw new InputError(`Invalid resource: ${resource}`);
}