From 492f2ff6b4d42608540fb2858cef754232b10b2e Mon Sep 17 00:00:00 2001 From: Robin van Boven <497556+Beanow@users.noreply.github.com> Date: Tue, 4 Feb 2020 12:09:54 +0100 Subject: [PATCH] Github: add fetchGithubRepoFromCache (#1569) Updating the mirror and using the mirror data should be separated. As unified reference detection requires the mirror of all plugins are updated. Upcoming refactors to the load system will solidify this. While this refactor is ongoing, we will ignore the fetchGithubRepo return value, using it as a mirror update only. And use this new function to extract the data as needed in other loading steps. --- src/plugins/github/fetchGithubRepo.js | 55 +++++++++++++++++++++++++-- src/plugins/github/loadGraph.js | 12 +++++- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/src/plugins/github/fetchGithubRepo.js b/src/plugins/github/fetchGithubRepo.js index 1f09f91..2d1271f 100644 --- a/src/plugins/github/fetchGithubRepo.js +++ b/src/plugins/github/fetchGithubRepo.js @@ -20,6 +20,57 @@ import {validateToken} from "./token"; import {cacheIdForRepoId} from "./cacheId"; import {type CacheProvider} from "../../backend/cache"; +type FetchRepoOptions = {| + +token: string, + +cache: CacheProvider, +|}; + +/** + * Retrieve previously scraped data for a GitHub repo from cache. + * + * Note: the GithubToken requirement is planned to be removed. + * See https://github.com/sourcecred/sourcecred/issues/1580 + * + * @param {RepoId} repoId + * the GitHub repository to retrieve from cache + * @param {GithubToken} token + * authentication token to be used for the GitHub API; generate a + * token at: https://github.com/settings/tokens + * @return {Promise} + * a promise that resolves to a JSON object containing the data + * scraped from the repository, with data format to be specified + * later + */ +export async function fetchGithubRepoFromCache( + repoId: RepoId, + {token, cache}: FetchRepoOptions +): Promise { + // Right now, only warn on likely to be bad tokens (see #1461). + // This lets us proceed to the GitHub API validating the token, + // while giving users instructions to remedy if it was their mistake. + try { + validateToken(token); + } catch (e) { + console.warn(`Warning: ${e}`); + } + + // TODO: remove the need for a GithubToken to resolve the ID. + // See https://github.com/sourcecred/sourcecred/issues/1580 + const postQueryWithToken = (payload) => postQuery(payload, token); + const resolvedId: Schema.ObjectId = await resolveRepositoryGraphqlId( + postQueryWithToken, + repoId + ); + + const db = await cache.database(cacheIdForRepoId(repoId)); + const mirror = new Mirror(db, schema(), { + blacklistedIds: BLACKLISTED_IDS, + guessTypename: _guessTypename, + }); + + return ((mirror.extract(resolvedId): any): Repository); +} + /** * Scrape data from a GitHub repo using the GitHub API. * @@ -35,10 +86,8 @@ import {type CacheProvider} from "../../backend/cache"; */ export default async function fetchGithubRepo( repoId: RepoId, - options: {|+token: string, +cache: CacheProvider|} + {token, cache}: FetchRepoOptions ): Promise { - const {token, cache} = options; - // Right now, only warn on likely to be bad tokens (see #1461). // This lets us proceed to the GitHub API validating the token, // while giving users instructions to remedy if it was their mistake. diff --git a/src/plugins/github/loadGraph.js b/src/plugins/github/loadGraph.js index 79d012f..c4d246d 100644 --- a/src/plugins/github/loadGraph.js +++ b/src/plugins/github/loadGraph.js @@ -10,7 +10,10 @@ import {TaskReporter} from "../../util/taskReporter"; import {createGraph} from "./createGraph"; -import fetchGithubRepo from "./fetchGithubRepo"; +import { + default as fetchGithubRepo, + fetchGithubRepoFromCache, +} from "./fetchGithubRepo"; import {RelationalView} from "./relationalView"; import {type RepoId, repoIdToString} from "./repoId"; import {Graph} from "../../core/graph"; @@ -44,7 +47,12 @@ export async function loadGraph( await fetchGithubRepo(repoId, { token: options.token, cache: options.cache, - }) + }).then((_) => + fetchGithubRepoFromCache(repoId, { + token: options.token, + cache: options.cache, + }) + ) ); taskReporter.finish(taskId); }