Skip to content

Commit

Permalink
Remove usage of labs.w3.org github cache
Browse files Browse the repository at this point in the history
generates length retry fetches
  • Loading branch information
dontcallmedom committed May 16, 2024
1 parent 865bce6 commit 5483fd3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
13 changes: 4 additions & 9 deletions lib/github-activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ async function recursiveGhFetch(url, acc = []) {

function fetchGithubRepo(owner, repo, size) {
return Promise.all([
recursiveGhFetch('https://labs.w3.org/github-cache/v3/repos/' + owner + '/' + repo + '/issues?state=all')
// if the github cache doesn't work, try hitting github directly
.catch(() =>
recursiveGhFetch('https://api.github.com/repos/' + owner + '/' + repo + '/issues?state=all&per_page=100&direction=asc'))
recursiveGhFetch('https://api.github.com/repos/' + owner + '/' + repo + '/issues?state=all&per_page=100&direction=asc')
.then(data => data.map(i => { return {html_url: i.html_url, created_at: i.created_at};}))
.catch(() => []),
recursiveGhFetch('https://api.github.com/repos/' + owner + '/' + repo + '/pulls?state=all&per_page=100&direction=asc')
Expand All @@ -30,11 +27,9 @@ function fetchGithubRepo(owner, repo, size) {
// if no pull request, we take a look at commits instead
// unless the repo is empty
if (size === 0) return [];
return recursiveGhFetch('https://labs.w3.org/github-cache/v3/repos/' + owner + '/' + repo + '/commits')
// if the github cache doesn't work, try hitting github directly
.catch(() =>
recursiveGhFetch('https://api.github.com/repos/' + owner + '/' + repo + '/commits?per_page=100&direction=asc'))
.then(data => data.map(i => { return {html_url: i.html_url, created_at: i.created_at, commit: i.commit}; }));
return recursiveGhFetch('https://api.github.com/repos/' + owner + '/' + repo + '/commits?per_page=100&direction=asc')
.then(data => data.map(i => { return {html_url: i.html_url, created_at: i.created_at, commit: i.commit}; }))
.catch(() => []);
}
return pulls;
}).catch(() => [])
Expand Down
13 changes: 7 additions & 6 deletions test/github-activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const origDispatcher = getGlobalDispatcher();
const agent = new MockAgent();

const toCacheUrl = (u, path) => "https://labs.w3.org/github-cache/v3/repos" + (new URL(u)).pathname + '/' + path + (path === 'issues' ? '?state=all' : '');
const toGhApiUrl = (u, path) => "https://api.github.com/repos" + (new URL(u)).pathname + '/' + path + '?state=all&per_page=100&direction=asc';
const toGhApiUrl = (u, path) => "https://api.github.com/repos" + (new URL(u)).pathname + '/' + path + '?' + (path !== 'commits' ? 'state=all&' : '') + 'per_page=100&direction=asc';


const ghObject = [
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('The Github Activity monitor', function () {

it('detects issues and PRs in a single repo', async function() {
const repo = "https://github.com/acme/test";
const issueUrl = toCacheUrl(repo, "issues");
const issueUrl = toGhApiUrl(repo, "issues");
const issueSecondPage = 'https://example.test/gh-issues-page-2';
const prUrl = toGhApiUrl(repo, "pulls");
mock(issueUrl, ghObject, issueSecondPage);
Expand All @@ -70,10 +70,11 @@ describe('The Github Activity monitor', function () {
});

it('detects issues and commits in a single repo', async function() {
this.timeout(5000);
const repo = "https://github.com/acme/test-commits";
const issueUrl = toCacheUrl(repo, "issues");
const issueUrl = toGhApiUrl(repo, "issues");
const prUrl = toGhApiUrl(repo, "pulls");
const commitsUrl = toCacheUrl(repo, "commits");
const commitsUrl = toGhApiUrl(repo, "commits");
mock(issueUrl, ghObject);
mock(prUrl, []);
mock(commitsUrl, ghObject.concat(ghObject));
Expand All @@ -85,9 +86,9 @@ describe('The Github Activity monitor', function () {
this.timeout(5000);
const repoUrls = ["https://github.com/acme/test-empty", "https://github.com/acme/test-org"];
mock("https://api.github.com/users/acme/repos?per_page=100&direction=asc", repos);
mock(toCacheUrl(repoUrls[0], "issues"), ghObject);
mock(toGhApiUrl(repoUrls[0], "issues"), ghObject);
mock(toGhApiUrl(repoUrls[0], "pulls"), []);
mock(toCacheUrl(repoUrls[1], "issues"), ghObject);
mock(toGhApiUrl(repoUrls[1], "issues"), ghObject);
mock(toGhApiUrl(repoUrls[1], "pulls"), ghObject);
const {items: data} = await fetchGithub("https://github.com/acme");
assert.equal(data.length, 3, 'Three items retrieved from github');
Expand Down

0 comments on commit 5483fd3

Please sign in to comment.