- commit
- db4defd
- parent
- dabd6e5
- author
- Eric Bower
- date
- 2021-10-25 03:20:34 +0000 UTC
updated at for resources now references the last commit #15
1 files changed,
+39,
-4
+39,
-4
1@@ -79,13 +79,29 @@ async function fetchReadme({
2 };
3 }
4
5-async function fetchRepo({ username, repo }: Props): Promise<Resp<{ [key: string]: any }>> {
6- const url = `https://api.github.com/repos/${username}/${repo}`;
7+async function delay(ms: number): Promise<void> {
8+ return new Promise((resolve) => setTimeout(() => resolve(), ms));
9+}
10+
11+async function githubApi(endpoint: string): Promise<Resp<{ [key: string]: any }>> {
12+ const url = `https://api.github.com${endpoint}`;
13 console.log(`Fetching ${url}`);
14 const token = Buffer.from(`${accessUsername}:${accessToken}`).toString('base64');
15 const res = await fetch(url, {
16 headers: { Authorization: `Basic ${token}` },
17 });
18+
19+ const rateLimitRemaining = parseInt(res.headers.get('X-RateLimit-Remaining'));
20+ const rateLimitReset = parseInt(res.headers.get('X-RateLimit-Reset'));
21+ console.log(`rate limit remaining: ${rateLimitRemaining}`);
22+ if (rateLimitRemaining === 1) {
23+ const now = Date.now();
24+ const RESET_BUFFER = 500;
25+ const wait = rateLimitReset + RESET_BUFFER - now;
26+ console.log(`About to hit github rate limit, waiting ${wait * 1000} seconds`);
27+ await delay(wait);
28+ }
29+
30 const data = await res.json();
31 if (res.ok) {
32 return {
33@@ -103,6 +119,20 @@ async function fetchRepo({ username, repo }: Props): Promise<Resp<{ [key: string
34 };
35 }
36
37+async function fetchRepo({ username, repo }: Props): Promise<Resp<{ [key: string]: any }>> {
38+ const result = await githubApi(`/repos/${username}/${repo}`);
39+ return result;
40+}
41+
42+async function fetchBranch({
43+ username,
44+ repo,
45+ branch,
46+}: Props & { branch: string }): Promise<Resp<{ [key: string]: any }>> {
47+ const result = await githubApi(`/repos/${username}/${repo}/branches/${branch}`);
48+ return result;
49+}
50+
51 interface ApiSuccess<D = any> {
52 ok: true;
53 data: D;
54@@ -122,12 +152,16 @@ async function fetchGithubData(props: Props): Promise<Resp<any>> {
55 return repo;
56 }
57
58+ const branch = await fetchBranch({ ...props, branch: repo.data.default_branch });
59+ if (branch.ok === false) {
60+ console.log(`${branch.data.status}: ${branch.data.error.message}`);
61+ }
62+
63 const readme = await fetchReadme({
64 username: props.username,
65 repo: props.repo,
66 branch: repo.data.default_branch,
67 });
68-
69 if (readme.ok === false) {
70 console.log(`${readme.data.status}: ${readme.data.error.message}`);
71 }
72@@ -137,6 +171,7 @@ async function fetchGithubData(props: Props): Promise<Resp<any>> {
73 data: {
74 readme: readme.ok ? readme.data : '',
75 repo: repo.data,
76+ branch: branch.data,
77 },
78 };
79 }
80@@ -171,7 +206,7 @@ async function processResources(resources: Resource[]) {
81 network: resp.repo.network_count,
82 description: resp.repo.description,
83 createdAt: resp.repo.created_at,
84- updatedAt: resp.repo.updated_at,
85+ updatedAt: resp.branch.commit.commit.committer.date,
86 });
87 }
88 }