-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathissuesJson.js
189 lines (175 loc) · 5.12 KB
/
issuesJson.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
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 includes = require('lodash/includes');
const { graphqlClient, fmtJsonKey, fmtArgs } = require('./utils');
const { fetchDiscussionsJsonData, fetchDiscussionData } = require('./discussionsJson');
const { owner, repo, jsonfmt, jsontype } = fmtArgs();
const fmt = jsonfmt === 'true' ? 2 : 0;
const dataType = jsontype === 'md' ? 'body' : 'bodyHTML';
const ISSUES_STATE = ['CLOSED', 'OPEN'];
const state = (argv['issues-state'] || '')?.toLocaleUpperCase();
let filterList = '';
if (state && includes(ISSUES_STATE, state)) {
filterList = `filterBy: { states: [${state}]}, `;
}
module.exports = async function genIssuesJson(issuesTotal, discussionsTotal) {
const limit = 100;
let list = [];
let last = null;
const outdir = argv.outdir || '.';
fs.mkdirSync(path.resolve(outdir, 'issues'), { recursive: true });
if (owner && repo) {
// rgd.yml
for (let i = 0; i < Math.ceil(discussionsTotal / limit); i++) {
const temp = await fetchDiscussionsJsonData(last);
for (let j = 0; j < temp.length; j++) {
let _node = temp[j].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`);
});
break;
}
}
}
}
// issues list
for (let i = 0; i < Math.ceil(issuesTotal / limit); i++) {
const temp = await fetchIssuesJsonData(last);
last = Array.from(temp).pop().cursor;
list = [...list, ...temp];
}
fs.writeFile(path.resolve(outdir, 'discussions.json'), JSON.stringify(list, null, fmt), function(err) {
if (err) console.error(err);
});
// issues item
list.forEach(async ({ node }) => {
if (!node) return;
const issuesData = await fetchIssueData(node.number);
fs.writeFile(path.resolve(outdir, `issues/${node.number}.json`), JSON.stringify(issuesData, null, fmt), function(err) {
if (err) return console.error(err);
console.log(chalk.green`[#${node.number}]`, chalk.yellow`${issuesData.title}`);
});
});
}
async function fetchIssuesJsonData(lastCursor) {
const { repository } = await graphqlClient(`
query ($owner: String!, $repo: String!, $cursor: String) {
repository(owner: $owner, name: $repo) {
issues(${filterList}first: 100, after: $cursor) {
edges {
cursor
node {
title
number
state
createdAt
updatedAt
author {
login
avatarUrl
url
}
labels(first: 100) {
edges {
node {
id
name
color
description
}
}
}
}
}
}
}
}
`, {
owner: argv['issues-owner'],
repo: argv['issues-repo'],
cursor: lastCursor,
});
return repository.issues.edges;
}
async function fetchIssueData(number, type) {
const { repository } = await graphqlClient(`
query ($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
issue(number: $number) {
id
title
number
state
createdAt
updatedAt
${type || dataType}
author {
login
avatarUrl
url
}
labels(first: 100) {
edges {
node {
id
name
color
description
}
}
}
reactions(first: 100) {
totalCount
edges {
node {
id
content
}
}
}
comments(first: 100) {
edges {
node {
id
${dataType}
updatedAt
author {
login
avatarUrl
url
}
reactions(first: 100) {
totalCount
edges {
node {
id
content
}
}
}
}
}
}
}
}
}
`, {
owner: argv['issues-owner'],
repo: argv['issues-repo'],
number,
});
return repository.issue;
}