forked from wustep/github-project-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
224 lines (199 loc) · 6.7 KB
/
main.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
const { default: axios } = require("axios");
const prompt = require("prompt");
const Papa = require("papaparse");
const fs = require("fs-extra");
require("dotenv").config();
const { GitHubQuery } = require("./github");
(async () => {
try {
console.log("GitHub Project Exporter");
console.log("-----------------------");
prompt.start();
const { owner, repo, token } = await prompt.get([
{
name: "owner",
default: process.env.OWNER ?? undefined,
description: "Owner (username or organization)",
required: true,
},
{
name: "repo",
default: process.env.REPO ?? "",
description: "Repo name (leave blank for user/org project)", // Prompt displayed to the user. If not supplied name will be used.
type: "string",
},
{
name: "token",
default: process.env.TOKEN ?? undefined,
description: "Personal access token",
required: true,
type: "string",
},
]);
const isOwnerUser =
repo.length === 0
? (
await prompt.get({
name: "ownerIsUser",
before: (v) => {
return v[0] === "u";
},
default: "user",
description: "Is the owner a user or org?",
message: "Must be 'user' or 'org'",
pattern: /(u|user|org|organization)$/,
type: "string",
required: true,
})
).ownerIsUser
: true;
const GitHub = new GitHubQuery({ owner, repo, token, isOwnerUser });
process.stdout.write("Getting projects...");
const projects = await GitHub.getProjects();
const numProjects = projects?.length ?? 0;
process.stdout.clearLine();
process.stdout.cursorTo(0);
if (numProjects === 0) {
console.log("\r\nNo projects found!");
return;
}
console.group("\r\nAvailable Projects:");
projects.forEach((project, index) =>
console.log(`[${index}] ${project.name}: ${project.html_url}`)
);
console.log();
console.groupEnd();
const { projectIndex } = await prompt.get({
name: "projectIndex",
default: 0,
description: "Which project to export?",
minimum: 0,
maximum: numProjects - 1,
required: true,
type: "integer",
});
process.stdout.write("Getting cards...");
const projectId = projects[projectIndex].id;
const columns = await GitHub.getProjectColumns(projectId);
const exportData = [];
/**
* We use PapaParse for parsing to CSV, which uses the fields of the first object for the CSV headers,
* unless we manually specify an array of headers (`columnHeaders`).
* Since we might not need to have issue columns, if there's no issue-based cards, we only add them if needed.
*/
let csvHeaders = [];
let addedCardHeaders = false;
let addedIssueHeaders = false;
for (column of columns) {
const cards = await GitHub.getColumnCards(column.id);
column.numCards = cards.length;
for (card of cards) {
// For a regular note card, `card.note` is the card contents.
// For an Issue card, `card.note` is null.
let title = card.note;
// If card is an issue, populate issue fields.
let issueFields = {};
if (card.content_url?.includes("issues")) {
atleastOneIssue = true;
const issue = await GitHub.get(card.content_url, {
prependAPIURL: false,
});
title = issue.title;
issueFields = {
issue_state: issue.state,
issue_labels: issue.labels.length
? issue.labels.reduce((labelString, label) => {
return labelString === null
? label.name
: labelString + ", " + label.name;
}, null)
: undefined,
issue_link: issue.html_url, // This is either the PR request (if exists) or issue
issue_body: issue.body,
issue_assignees: issue.assignees.length
? issue.assignees.reduce((assigneeString, assignee) => {
return assigneeString === null
? assignee.login
: assigneeString + ", " + assignee.login;
}, null)
: undefined,
issue_created_at: issue.created_at,
issue_updated_at: issue.updated_at,
issue_closed_at: issue.closed_at,
};
}
const cardFields = {
title: title,
column: column.name,
creator: card.creator.login,
created_at: card.created_at,
updated_at: card.updated_at,
};
if (!addedCardHeaders) {
addedCardHeaders = true;
csvHeaders = csvHeaders.concat(Object.keys(cardFields));
}
if (!addedIssueHeaders && Object.keys(issueFields).length) {
addedIssueHeaders = true;
csvHeaders = csvHeaders.concat(Object.keys(issueFields));
}
exportData.push({
...cardFields,
...issueFields,
});
}
}
process.stdout.clearLine();
process.stdout.cursorTo(0);
const { outputFilename } = await prompt.get({
name: "outputFilename",
default: "output/export.csv",
description: "What file name to export to?",
type: "string",
required: true,
});
process.stdout.write("Writing to csv...");
const outputCSV = Papa.unparse(exportData, {
// These are all PapaParse defaults for now! Change as needed.
// https://www.papaparse.com/docs#json-to-csv
quotes: false,
quoteChar: '"',
escapeChar: '"',
delimiter: ",",
header: true,
newline: "\r\n",
skipEmptyLines: false,
// Custom values
columns: csvHeaders,
});
process.stdout.clearLine();
process.stdout.cursorTo(0);
console.log();
fs.outputFile(outputFilename, outputCSV, {encoding: "utf8"}, (err) => {
if (err) {
throw(err);
}
console.group(`Printed output to ${outputFilename}`);
columns.forEach((column) =>
console.log(`${column.name}: ${column.numCards}`)
);
console.groupEnd();
console.log();
});
} catch (err) {
let errorMessage;
switch (err.response?.status) {
case 404:
errorMessage = "[error] The resource you requested was not found!";
break;
case 401:
errorMessage =
"[error] You are unauthorized to access that resource!\r\nYou might have an invalid access token or just not have permission to access that project.";
break;
default:
errorMessage = err;
break;
}
console.error(`\r\n\r\n${errorMessage}`);
}
})();