|
1 | | -import dotenv from 'dotenv'; |
2 | | -import { outputJson } from 'fs-extra'; |
3 | | -import fetch from 'node-fetch'; |
4 | | -import { resolve } from 'path'; |
5 | | -import url from 'url'; |
6 | | - |
7 | | -dotenv.config(); |
8 | | - |
9 | | -const OUTPUT_PATH = resolve( |
10 | | - __dirname, |
11 | | - '../data/github-commits.json' |
12 | | -); |
13 | | - |
14 | | -export default { |
15 | | - title: 'Getting a list of past commits', |
16 | | - task: async (_: any, task: any) => { |
17 | | - const History = await getAllGHCommits(task); |
18 | | - if (Object.keys(History).length > 0) { |
19 | | - outputJson(OUTPUT_PATH, History, { spaces: 2 }); |
20 | | - } |
21 | | - } |
22 | | -}; |
23 | | - |
24 | | -const getAllGHCommits = async (task: any, page = 1) => { |
25 | | - try { |
26 | | - task.output = `Getting commits: ${page * 100 - 99} - ${page * 100}`; |
27 | | - const request = await fetch( |
28 | | - url.format({ |
29 | | - protocol: 'https', |
30 | | - hostname: 'api.github.com', |
31 | | - pathname: 'repos/ionic-team/ionic-docs/commits', |
32 | | - query: { |
33 | | - per_page: 100, |
34 | | - page |
35 | | - } |
36 | | - }), { |
37 | | - headers: { |
38 | | - 'Authorization': process.env.GITHUB_TOKEN !== undefined ? `token ${process.env.GITHUB_TOKEN}` : '' |
39 | | - } |
40 | | - } |
41 | | - ); |
42 | | - |
43 | | - let commits = await request.json().then(list => list.reduce((obj: any, commit: any) => { |
44 | | - obj[commit.sha] = { |
45 | | - id: commit.author.login, |
46 | | - avatar: commit.author.avatar_url, |
47 | | - time: commit.commit.committer.date |
48 | | - }; |
49 | | - return obj; |
50 | | - }, {})); |
51 | | - |
52 | | - // recursively get more commits if there are more to get, limit 2k |
53 | | - if (Object.keys(commits).length === 100 && page < 20) { |
54 | | - commits = { ...commits, ...await getAllGHCommits(task, page + 1) }; |
55 | | - } |
56 | | - |
57 | | - return commits; |
58 | | - } catch { |
59 | | - return {}; |
60 | | - } |
61 | | -}; |
| 1 | +import dotenv from 'dotenv'; |
| 2 | +import { outputJson } from 'fs-extra'; |
| 3 | +import { resolve } from 'path'; |
| 4 | +import url from 'url'; |
| 5 | + |
| 6 | +dotenv.config(); |
| 7 | + |
| 8 | +const OUTPUT_PATH = resolve( |
| 9 | + __dirname, |
| 10 | + '../data/github-commits.json' |
| 11 | +); |
| 12 | + |
| 13 | +export default { |
| 14 | + title: 'Getting a list of past commits', |
| 15 | + task: async (_: any, task: any) => { |
| 16 | + const History = await getAllGHCommits(task); |
| 17 | + if (Object.keys(History).length > 0) { |
| 18 | + outputJson(OUTPUT_PATH, History, { spaces: 2 }); |
| 19 | + } |
| 20 | + } |
| 21 | +}; |
| 22 | + |
| 23 | +const getAllGHCommits = async (task: any, page = 1) => { |
| 24 | + try { |
| 25 | + task.output = `Getting commits: ${page * 100 - 99} - ${page * 100}`; |
| 26 | + const request = await fetch( |
| 27 | + url.format({ |
| 28 | + protocol: 'https', |
| 29 | + hostname: 'api.github.com', |
| 30 | + pathname: 'repos/ionic-team/ionic-docs/commits', |
| 31 | + query: { |
| 32 | + per_page: 100, |
| 33 | + page |
| 34 | + } |
| 35 | + }), { |
| 36 | + headers: { |
| 37 | + 'Authorization': process.env.GITHUB_TOKEN !== undefined ? `token ${process.env.GITHUB_TOKEN}` : '' |
| 38 | + } |
| 39 | + } |
| 40 | + ); |
| 41 | + |
| 42 | + let commits = await request.json().then(list => list.reduce((obj: any, commit: any) => { |
| 43 | + obj[commit.sha] = { |
| 44 | + id: commit.author.login, |
| 45 | + avatar: commit.author.avatar_url, |
| 46 | + time: commit.commit.committer.date |
| 47 | + }; |
| 48 | + return obj; |
| 49 | + }, {})); |
| 50 | + |
| 51 | + // recursively get more commits if there are more to get, limit 2k |
| 52 | + if (Object.keys(commits).length === 100 && page < 20) { |
| 53 | + commits = { ...commits, ...await getAllGHCommits(task, page + 1) }; |
| 54 | + } |
| 55 | + |
| 56 | + return commits; |
| 57 | + } catch { |
| 58 | + return {}; |
| 59 | + } |
| 60 | +}; |
0 commit comments