forked from gavinr/github-csv-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.js
102 lines (88 loc) · 3.04 KB
/
import.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
const csv = require("csv");
const fs = require("fs");
const { createIssue } = require("./helpers.js");
const importFile = (octokit, file, values) => {
fs.readFile(file, "utf8", (err, data) => {
if (err) {
console.error("Error reading file.");
process.exit(1);
}
csv.parse(
data,
{
trim: true,
},
(err, csvRows) => {
if (err) throw err;
var cols = csvRows[0];
csvRows.shift();
// get indexes of the fields we need
var titleIndex = cols.indexOf("title");
var bodyIndex = cols.indexOf("description");
var labelsIndex = cols.indexOf("labels");
var milestoneIndex = cols.indexOf("milestone");
var assigneeIndex = cols.indexOf("assignee");
var stateIndex = cols.indexOf("state");
if (titleIndex === -1) {
console.error("Title required by GitHub, but not found in CSV.");
process.exit(1);
}
const createPromises = csvRows.map((row) => {
var sendObj = {
owner: values.userOrOrganization,
repo: values.repo,
title: row[titleIndex],
};
// if we have a body column, pass that.
if (bodyIndex > -1) {
sendObj.body = row[bodyIndex];
}
// if we have a labels column, pass that.
if (labelsIndex > -1 && row[labelsIndex] !== "") {
sendObj.labels = row[labelsIndex].split(",");
}
// if we have a milestone column, pass that.
if (milestoneIndex > -1 && row[milestoneIndex] !== "") {
sendObj.milestone = row[milestoneIndex];
}
// if we have an assignee column, pass that.
if (assigneeIndex > -1 && row[assigneeIndex] !== "") {
sendObj.assignees = row[assigneeIndex].replace(/ /g, "").split(",");
}
// console.log("sendObj", sendObj);
let state = false;
if (stateIndex > -1 && row[stateIndex] === "closed") {
state = row[stateIndex];
}
return createIssue(octokit, sendObj, state);
});
Promise.all(createPromises).then(
(res) => {
const successes = res.filter((cr) => {
return cr.status === 200 || cr.status === 201;
});
const fails = res.filter((cr) => {
return cr.status !== 200 && cr.status !== 201;
});
console.log(
`Created ${successes.length} issues, and had ${fails.length} failures.`
);
console.log(
"❤ ❗ If this project has provided you value, please ⭐ star the repo to show your support: ➡ https://github.com/gavinr/github-csv-tools"
);
if (fails.length > 0) {
console.log(fails);
}
process.exit(0);
},
(err) => {
console.error("Error");
console.error(err);
process.exit(0);
}
);
}
);
});
};
module.exports = { importFile };