diff --git a/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts b/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts index a107117129..9b9aaff9c2 100644 --- a/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts +++ b/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.test.ts @@ -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); diff --git a/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.ts b/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.ts index 08041640d9..35d6b4f930 100644 --- a/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.ts +++ b/plugins/scaffolder-backend-module-github/src/autocomplete/autocomplete.ts @@ -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}`); }