Handle incorrect return type

Signed-off-by: Eric Voshall <6836155+ericvoshall@users.noreply.github.com>
This commit is contained in:
Eric Voshall
2022-07-22 11:01:15 -04:00
parent b7926841a8
commit b62a9bfc37
2 changed files with 79 additions and 1 deletions
@@ -20,6 +20,7 @@ const octokit = {
paginate: async (fn: any) => (await fn()).data,
apps: {
listInstallations: jest.fn(),
listReposAccessibleToInstallation: jest.fn(),
createInstallationAccessToken: jest.fn(),
},
};
@@ -325,4 +326,78 @@ describe('SingleInstanceGithubCredentialsProvider tests', () => {
expect(headers).toEqual({ Authorization: 'Bearer secret_token' });
expect(token).toEqual('secret_token');
});
it('should not throw when paginate response is an array of repositories', async () => {
const repoName = 'foobar';
octokit.apps.listInstallations.mockResolvedValue({
headers: {
etag: '123',
},
data: [
{
id: 1,
repository_selection: 'all',
account: {
login: 'backstage',
},
},
],
} as RestEndpointMethodTypes['apps']['listInstallations']['response']);
octokit.apps.createInstallationAccessToken.mockResolvedValueOnce({
data: {
expires_at: DateTime.local().plus({ hours: 1 }).toString(),
token: 'secret_token',
repository_selection: 'selected',
},
} as RestEndpointMethodTypes['apps']['createInstallationAccessToken']['response']);
octokit.apps.listReposAccessibleToInstallation.mockReturnValue({
data: [{ name: repoName }],
} as unknown as RestEndpointMethodTypes['apps']['listReposAccessibleToInstallation']['response']);
await expect(
github.getCredentials({
url: `https://github.com/backstage/${repoName}`,
}),
).resolves.not.toThrow();
});
it('should not throw when paginate response is an object with a property containing an array of repositories', async () => {
const repoName = 'foobar';
octokit.apps.listInstallations.mockResolvedValue({
headers: {
etag: '123',
},
data: [
{
id: 1,
repository_selection: 'all',
account: {
login: 'backstage',
},
},
],
} as RestEndpointMethodTypes['apps']['listInstallations']['response']);
octokit.apps.createInstallationAccessToken.mockResolvedValueOnce({
data: {
expires_at: DateTime.local().plus({ hours: 1 }).toString(),
token: 'secret_token',
repository_selection: 'selected',
},
} as RestEndpointMethodTypes['apps']['createInstallationAccessToken']['response']);
octokit.apps.listReposAccessibleToInstallation.mockReturnValue({
data: {
repositories: [{ name: repoName }],
},
} as RestEndpointMethodTypes['apps']['listReposAccessibleToInstallation']['response']);
await expect(
github.getCredentials({
url: `https://github.com/backstage/${repoName}`,
}),
).resolves.not.toThrow();
});
});
@@ -119,7 +119,10 @@ class GithubAppManager {
const repos = await installationClient.paginate(
installationClient.apps.listReposAccessibleToInstallation,
);
const hasRepo = repos.repositories.some(repository => {
// The return type of the paginate method is incorrect.
const repositories: RestEndpointMethodTypes['apps']['listReposAccessibleToInstallation']['response']['data']['repositories'] =
repos.repositories ?? repos;
const hasRepo = repositories.some(repository => {
return repository.name === repo;
});
if (!hasRepo) {