diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index 04b4c146d7d37cde8e06a05e1b4d2169727ea04b..18d3737f9a3a27e2d59e6254375211cc4fde6065 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -196,12 +196,15 @@ export async function initRepo({ [config.repositoryOwner, config.repositoryName] = repository.split('/'); let repo: GhRepo; try { - repo = await githubApi.queryRepo<GhRepo>(repoInfoQuery, { + const res = await githubApi.requestGraphql<{ + repository: GhRepo; + }>(repoInfoQuery, { variables: { owner: config.repositoryOwner, name: config.repositoryName, }, }); + repo = res?.data?.repository; // istanbul ignore if if (!repo) { throw new Error(REPOSITORY_NOT_FOUND); diff --git a/lib/util/http/github.spec.ts b/lib/util/http/github.spec.ts index 9a4af9071aeeeafd326d050119e8eadc8965619b..c4043d3122bff538ede01653797b6e7e63f1f74f 100644 --- a/lib/util/http/github.spec.ts +++ b/lib/util/http/github.spec.ts @@ -353,7 +353,7 @@ describe('util/http/github', () => { .scope('https://ghe.mycompany.com') .post('/api/graphql') .reply(200, { data: { repository } }); - await githubApi.queryRepo(graphqlQuery); + await githubApi.requestGraphql(graphqlQuery); const [req] = httpMock.getTrace(); expect(req).toBeDefined(); expect(req.url).toEqual('https://ghe.mycompany.com/api/graphql'); @@ -432,9 +432,9 @@ describe('util/http/github', () => { .post('/graphql') .reply(200, { data: { repository } }); - const result = await githubApi.queryRepo(graphqlQuery); + const { data } = await githubApi.requestGraphql(graphqlQuery); expect(httpMock.getTrace()).toHaveLength(1); - expect(result).toStrictEqual(repository); + expect(data).toStrictEqual({ repository }); }); it('queryRepoField', async () => { httpMock diff --git a/lib/util/http/github.ts b/lib/util/http/github.ts index e555fed631089165b046f0f247c7a1f5670f1c2d..bc578f223f6bc86edfd813247a4df43395941c14 100644 --- a/lib/util/http/github.ts +++ b/lib/util/http/github.ts @@ -30,10 +30,12 @@ export interface GithubHttpOptions extends InternalHttpOptions { token?: string; } +interface GithubGraphqlRepoData<T = unknown> { + repository?: T; +} + interface GithubGraphqlResponse<T = unknown> { - data?: { - repository?: T; - }; + data?: T; errors?: { message: string; locations: unknown }[]; } @@ -242,10 +244,10 @@ export class GithubHttp extends Http<GithubHttpOptions, GithubHttpOptions> { return result; } - public async queryRepo<T = unknown>( + public async requestGraphql<T = unknown>( query: string, options: GraphqlOptions = {} - ): Promise<T> { + ): Promise<GithubGraphqlResponse<T>> { let result = null; const path = 'graphql'; @@ -274,7 +276,7 @@ export class GithubHttp extends Http<GithubHttpOptions, GithubHttpOptions> { 'graphql', opts ); - result = res?.body?.data?.repository; + result = res?.body; } catch (err) { if (err instanceof ExternalHostError) { const gotError = err.err as GotLegacyError; @@ -309,14 +311,15 @@ export class GithubHttp extends Http<GithubHttpOptions, GithubHttpOptions> { let isIterating = true; while (isIterating) { - const gqlRes = await this.queryRepo<T>(query, { + const res = await this.requestGraphql<GithubGraphqlRepoData<T>>(query, { ...options, count: Math.min(count, limit), cursor, paginate, }); - if (gqlRes?.[fieldName]) { - const { nodes = [], edges = [], pageInfo } = gqlRes[fieldName]; + const fieldData = res?.data?.repository?.[fieldName]; + if (fieldData) { + const { nodes = [], edges = [], pageInfo } = fieldData; result.push(...nodes); result.push(...edges); @@ -335,7 +338,7 @@ export class GithubHttp extends Http<GithubHttpOptions, GithubHttpOptions> { } else { count = Math.floor(count / 2); if (count === 0) { - logger.error({ gqlRes }, 'Error fetching GraphQL nodes'); + logger.error({ res }, 'Error fetching GraphQL nodes'); isIterating = false; } }