From c1238cc022fdfe9467ef23833770629cd0c65846 Mon Sep 17 00:00:00 2001 From: Simon Knittel Date: Sat, 1 May 2021 11:07:17 +0200 Subject: [PATCH] Use different query for GitHub Discovery depending on the repository owner Signed-off-by: Simon Knittel --- .../src/ingestion/processors/github/github.ts | 97 +++++++++++++------ 1 file changed, 66 insertions(+), 31 deletions(-) diff --git a/plugins/catalog-backend/src/ingestion/processors/github/github.ts b/plugins/catalog-backend/src/ingestion/processors/github/github.ts index 0b7f3d831d..f5230ce044 100644 --- a/plugins/catalog-backend/src/ingestion/processors/github/github.ts +++ b/plugins/catalog-backend/src/ingestion/processors/github/github.ts @@ -66,6 +66,14 @@ export type Connection = { nodes: T[]; }; +export type RepositoryOwnerType = { + __typename: 'Organization' | 'User'; +}; + +export type RepositoryOwnerResponse = { + repositoryOwner: RepositoryOwnerType | null; +}; + /** * Gets all the users out of a GitHub organization. * @@ -229,38 +237,49 @@ export async function getOrganizationRepositories( client: typeof graphql, org: string, ): Promise<{ repositories: Repository[] }> { - const query = ` - query repositories($org: String!, $cursor: String) { - organization(login: $org) { - name - repositories(first: 100, after: $cursor) { - nodes { - name - url - isArchived - } - pageInfo { - hasNextPage - endCursor - } - } - } - user(login: $org) { - name - repositories(first: 100, after: $cursor) { - nodes { - name - url - isArchived - } - pageInfo { - hasNextPage - endCursor - } - } - } + let query = ``; + + switch (await getLoginType(client, org)) { + case 'Organization': + query = ` + query repositories($org: String!, $cursor: String) { + organization(login: $org) { + name + repositories(first: 100, after: $cursor) { + nodes { + name + url + isArchived + } + pageInfo { + hasNextPage + endCursor + } + } + } + }`; + break; + + default: + query = ` + query repositories($org: String!, $cursor: String) { + user(login: $org) { + name + repositories(first: 100, after: $cursor) { + nodes { + name + url + isArchived + } + pageInfo { + hasNextPage + endCursor + } + } + } + }`; + break; } - `; const repositories = await queryWithPaging( client, @@ -372,3 +391,19 @@ export async function queryWithPaging< return result; } + +export async function getLoginType(client: typeof graphql, login: string) { + const query = ` + query repositoryOwner($login: String!) { + repositoryOwner(login: $login) { + __typename + } + }`; + + const response: RepositoryOwnerResponse = await client(query, { login }); + if (response.repositoryOwner === null) { + throw new Error(`Unknown repository owner for "${login}"`); + } + + return response.repositoryOwner.__typename; +}