Add github pagination

This commit is contained in:
Rodrigo Pombo 2019-02-18 23:56:07 -03:00
parent 33c7f44157
commit 532ec1b428

View File

@ -52,35 +52,44 @@ function showLanding() {
return !repo; return !repo;
} }
async function getCommits(last = 10) { const cache = {};
const [repo, sha, path] = getUrlParams();
const commitsResponse = await fetch(
`https://api.github.com/repos/${repo}/commits?sha=${sha}&path=${path}`,
{ headers: getHeaders() }
);
if (!commitsResponse.ok) {
throw commitsResponse;
}
const commitsJson = await commitsResponse.json();
const commits = commitsJson.slice(0, last).map(commit => ({ async function getCommits(path, last) {
sha: commit.sha, const [repo, sha] = getUrlParams();
date: new Date(commit.commit.author.date),
author: { if (!cache[path]) {
login: commit.author ? commit.author.login : commit.commit.author.name, const commitsResponse = await fetch(
avatar: commit.author `https://api.github.com/repos/${repo}/commits?sha=${sha}&path=${path}`,
? commit.author.avatar_url { headers: getHeaders() }
: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png" );
}, if (!commitsResponse.ok) {
commitUrl: commit.html_url, throw commitsResponse;
message: commit.commit.message }
})); const commitsJson = await commitsResponse.json();
cache[path] = commitsJson.map(commit => ({
sha: commit.sha,
date: new Date(commit.commit.author.date),
author: {
login: commit.author ? commit.author.login : commit.commit.author.name,
avatar: commit.author
? commit.author.avatar_url
: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
},
commitUrl: commit.html_url,
message: commit.commit.message
}));
}
const commits = cache[path].slice(0, last);
await Promise.all( await Promise.all(
commits.map(async commit => { commits.map(async commit => {
const info = await getContent(repo, commit.sha, path); if (!commit.content) {
commit.content = info.content; const info = await getContent(repo, commit.sha, path);
commit.fileUrl = info.url; commit.content = info.content;
commit.fileUrl = info.url;
}
}) })
); );