chore: update BitbucketRepoBranchPicker and handleAutocompleteRequest
Signed-off-by: Benjamin Janssens <benji.janssens@gmail.com>
This commit is contained in:
+8
-4
@@ -95,12 +95,16 @@ describe('handleAutocompleteRequest', () => {
|
||||
});
|
||||
|
||||
it('should return branches', async () => {
|
||||
const result = await handleBitbucketCloudRequest('foo', 'branches', {
|
||||
workspace: 'workspace1',
|
||||
repository: 'repository1',
|
||||
const result = await handleAutocompleteRequest({
|
||||
token: 'foo',
|
||||
resource: 'branches',
|
||||
context: {
|
||||
workspace: 'workspace1',
|
||||
repository: 'repository1',
|
||||
},
|
||||
});
|
||||
|
||||
expect(result).toEqual(['branch1']);
|
||||
expect(result).toEqual({ results: [{ title: 'branch1' }] });
|
||||
});
|
||||
|
||||
it('should throw an error when passing an invalid resource', async () => {
|
||||
|
||||
@@ -78,21 +78,21 @@ export async function handleAutocompleteRequest({
|
||||
return { results: result.map(title => ({ title })) };
|
||||
}
|
||||
case 'branches': {
|
||||
if (!parameters.workspace || !parameters.repository)
|
||||
if (!context.workspace || !context.repository)
|
||||
throw new InputError(
|
||||
'Missing workspace and/or repository query parameter',
|
||||
'Missing workspace and/or repository context parameter',
|
||||
);
|
||||
|
||||
const result: string[] = [];
|
||||
|
||||
for await (const page of client
|
||||
.listBranchesByRepository(parameters.repository, parameters.workspace)
|
||||
.listBranchesByRepository(context.repository, context.workspace)
|
||||
.iteratePages()) {
|
||||
const names = [...page.values!].map(p => p.name!);
|
||||
result.push(...names);
|
||||
}
|
||||
|
||||
return result;
|
||||
return { results: result.map(title => ({ title })) };
|
||||
}
|
||||
default:
|
||||
throw new InputError(`Invalid resource: ${resource}`);
|
||||
|
||||
+3
-1
@@ -26,7 +26,9 @@ import userEvent from '@testing-library/user-event';
|
||||
|
||||
describe('BitbucketRepoBranchPicker', () => {
|
||||
const scaffolderApiMock: Partial<ScaffolderApi> = {
|
||||
autocomplete: jest.fn().mockResolvedValue(['branch1']),
|
||||
autocomplete: jest
|
||||
.fn()
|
||||
.mockResolvedValue({ results: [{ title: 'branch1' }] }),
|
||||
};
|
||||
|
||||
it('renders an input field', () => {
|
||||
|
||||
+9
-8
@@ -57,16 +57,17 @@ export const BitbucketRepoBranchPicker = ({
|
||||
host === 'bitbucket.org' &&
|
||||
accessToken &&
|
||||
workspace &&
|
||||
repository
|
||||
repository &&
|
||||
scaffolderApi.autocomplete
|
||||
) {
|
||||
const result = await scaffolderApi.autocomplete(
|
||||
accessToken,
|
||||
'bitbucketCloud',
|
||||
'branches',
|
||||
{ workspace, repository },
|
||||
);
|
||||
const { results } = await scaffolderApi.autocomplete({
|
||||
token: accessToken,
|
||||
resource: 'branches',
|
||||
context: { workspace, repository },
|
||||
provider: 'bitbucket-cloud',
|
||||
});
|
||||
|
||||
setAvailableBranches(result);
|
||||
setAvailableBranches(results.map(r => r.title));
|
||||
} else {
|
||||
setAvailableBranches([]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user