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.
This commit is contained in:
Robin van Boven 2020-02-04 12:09:54 +01:00 committed by GitHub
parent 115740e7fe
commit 492f2ff6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 5 deletions

View File

@ -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<Repository>}
* 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<Repository> {
// 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<Repository> {
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.

View File

@ -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);
}