-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchCommits.js
More file actions
35 lines (25 loc) · 1.13 KB
/
fetchCommits.js
File metadata and controls
35 lines (25 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
async function* fetchCommits(repo) {
let url = `https://api.github.com/repos/${repo}/commits`;
while (url) {
const response = await fetch(url, { // (1)
headers: {'User-Agent': 'Our script'}, // GitHub требует заголовок user-agent
});
const body = await response.json(); // (2) ответ в формате JSON (массив коммитов)
// (3) Ссылка на следующую страницу находится в заголовках, извлекаем её
let nextPage = response.headers.get('Link').match(/<(.*?)>; rel="next"/);
nextPage = nextPage && nextPage[1];
url = nextPage;
for(let commit of body) { // (4) вернуть коммиты один за другим, до окончания страницы
yield commit;
}
}
}
(async () => {
let count = 0;
for await (const commit of fetchCommits('javascript-tutorial/en.javascript.info')) {
console.log(commit.author.login);
if (++count == 100) { // остановимся на 100 коммитах
break;
}
}
})();