github = {
const url = (repo, path) => `https://api.github.com/repos/${repo.name}${path}`;
function responseJson(res) {
if (!res.ok) throw new Error(res.status);
return res.json();
}
function getJson(url) {
const reqInit = {};
if (hasApiKey) {
reqInit.headers = {'Authorization': `token ${Secret("GITHUB_ACCESS_TOKEN")}`};
}
return fetch(url, reqInit).then(responseJson);
}
const getTree = (repo, sha) => getJson(url(repo, `/git/trees/${sha}`));
const getRoot = repo => getJson(url(repo, `/branches/${repo.branch}`)).then(res => getTree(repo, res.commit.sha));
return { getRoot, getTree };
}