-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiscussionsJson.js
205 lines (190 loc) · 5.27 KB
/
discussionsJson.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const fs = require('fs');
const path = require('path');
const argv = require('minimist')(process.argv.slice(2));
const chalk = require('chalk');
const yaml = require('js-yaml');
const { graphqlClient, fmtJsonKey, fmtArgs } = require('./utils');
const { owner, repo, jsonfmt, jsontype } = fmtArgs();
const fmt = jsonfmt === 'true' ? 2 : 0;
const dataType = jsontype === 'md' ? 'body' : 'bodyHTML';
module.exports = {
genDiscussionsJson,
fetchDiscussionData,
fetchDiscussionsJsonData,
};
async function genDiscussionsJson(totalCount) {
const limit = 100;
let list = [];
let last = null;
for (let i = 0; i < Math.ceil(totalCount / limit); i++) {
const temp = await fetchDiscussionsJsonData(last);
temp.map(async (item) => {
let _node = item.node || {};
if (_node.title === 'rgd.yml') {
const rgdYml = await fetchDiscussionData(_node.number, 'body');
const _config = rgdYml.body.replace(/(```ya?ml)|(```)/g, '');
const _json = yaml.load(_config, 'utf8');
const _fmtJson = fmtJsonKey(_json);
fs.writeFile(path.resolve(outdir, `rgd.json`), JSON.stringify(_fmtJson, null, fmt), function(err) {
if (err) return console.error(err);
console.log(chalk.green`rgd.json`);
});
fs.writeFile(path.resolve(outdir, `rgd.yml`), _config, function(err) {
if (err) return console.error(err);
console.log(chalk.green`rgd.yml`);
});
}
return item;
});
last = Array.from(temp).pop().cursor;
list = [...list, ...temp];
}
const outdir = argv.outdir || '.';
fs.mkdirSync(path.resolve(outdir, 'issues'), { recursive: true });
fs.writeFile(path.resolve(outdir, 'discussions.json'), JSON.stringify(list, null, fmt), (err) => {
if (err) console.error(err);
console.log(chalk.green`discussions.json`);
});
list.forEach(async ({ node }) => {
if (!node) return;
const issuesData = await fetchDiscussionData(node.number);
fs.writeFile(path.resolve(outdir, `issues/${node.number}.json`), JSON.stringify(issuesData, null, fmt), (err) => {
if (err) return console.error(err);
console.log(chalk.green`[#${node.number}]`, chalk.yellow`${issuesData.title}`);
});
});
}
async function fetchDiscussionsJsonData(lastCursor, _owner = owner, _repo = repo) {
const { repository } = await graphqlClient(`
query ($owner: String!, $repo: String!, $cursor: String) {
repository(owner: $owner, name: $repo) {
discussions(first: 100, after: $cursor) {
edges {
cursor
node {
title
number
createdAt
updatedAt
author {
login
avatarUrl
url
}
category {
name
emoji
description
isAnswerable
}
labels(first: 100) {
edges {
node {
id
name
color
description
}
}
}
}
}
}
}
}
`, {
owner: _owner,
repo: _repo,
cursor: lastCursor,
});
return repository.discussions.edges;
}
async function fetchDiscussionData(number, type, _owner = owner, _repo = repo) {
const { repository } = await graphqlClient(`
query ($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
discussion(number: $number) {
id
title
number
upvoteCount
createdAt
updatedAt
${type || dataType}
author {
login
avatarUrl
url
}
category {
name
emoji
description
isAnswerable
}
labels(first: 100) {
edges {
node {
id
name
color
description
}
}
}
reactions(first: 100) {
totalCount
edges {
node {
id
content
}
}
}
comments(first: 100) {
edges {
node {
id
${dataType}
upvoteCount
updatedAt
author {
login
avatarUrl
url
}
reactions(first: 100) {
totalCount
edges {
node {
id
content
}
}
}
replies(first: 100) {
edges {
node {
id
author {
login
avatarUrl
url
}
${dataType}
}
}
}
}
}
}
}
}
}
`, {
owner: _owner,
repo: _repo,
number,
});
return repository.discussion;
}