Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ const createIssue = (octokit, issueInfo, organization, repository) => {
return new Promise((resolve, reject) => {
octokit
.request(
"POST /repos/" + organization + "/" + repository + "/import/issues",
"POST /repos/" + organization + "/" + repository + "/issues",
issueInfo
)
.then(
(res) => {
// console.log("res", res);
if (res.status === 202) {
console.log(`Imported issue: ${issueInfo.issue.title}`);
if (res.status === 201) {
console.log(`Imported issue: ${issueInfo.title}`);
resolve(res);
} else {
// error creating the issue
Expand All @@ -23,4 +23,29 @@ const createIssue = (octokit, issueInfo, organization, repository) => {
});
};

module.exports = { createIssue };
const updateIssue = (octokit, issueInfo, organization, repository, number) => {
return new Promise((resolve, reject) => {
octokit
.request(
"PATCH /repos/" + organization + "/" + repository + "/issues/" + number,
issueInfo
)
.then(
(res) => {
// console.log("res", res);
if (res.status === 200) {
console.log(`Updated issue: ${issueInfo.title}`);
resolve(res);
} else {
// error creating the issue
reject(res);
}
},
(err) => {
reject(err);
}
);
});
};

module.exports = { createIssue, updateIssue };
47 changes: 31 additions & 16 deletions import.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const csv = require("csv");
const fs = require("fs");

const { createIssue } = require("./helpers.js");
const { createIssue, updateIssue } = require("./helpers.js");

const importFile = (octokit, file, values) => {
fs.readFile(file, "utf8", (err, data) => {
Expand All @@ -26,50 +26,65 @@ const importFile = (octokit, file, values) => {
const bodyIndex = cols.indexOf("body");
const labelsIndex = cols.indexOf("labels");
const milestoneIndex = cols.indexOf("milestone");
const assigneeIndex = cols.indexOf("assignee");
const assigneesIndex = cols.indexOf("assignees");
const 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) => {
const createPromises = csvRows.reduce((pr, row) => {
const sendObj = {
issue: {},
owner: values.userOrOrganization,
repo: values.repo,
title: row[titleIndex],
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
};

sendObj.issue.title = row[titleIndex];

// if we have a body column, pass that.
if (bodyIndex > -1 && row[bodyIndex] !== "") {
sendObj.issue.body = row[bodyIndex];
sendObj.body = row[bodyIndex];
}

// if we have a labels column, pass that.
if (labelsIndex > -1 && row[labelsIndex] !== "") {
sendObj.issue.labels = row[labelsIndex].split(",");
sendObj.labels = row[labelsIndex].split(",");
}

// if we have a milestone column, pass that.
if (milestoneIndex > -1 && row[milestoneIndex] !== "") {
sendObj.issue.milestone = Number(row[milestoneIndex]);
sendObj.milestone = Number(row[milestoneIndex]);
}

// if we have an assignee column, pass that.
if (assigneeIndex > -1 && row[assigneeIndex] !== "") {
sendObj.issue.assignee = row[assigneeIndex];
if (assigneesIndex > -1 && row[assigneesIndex] !== "") {
sendObj.assignees = row[assigneesIndex].split(",");
}

if (stateIndex > -1 && row[stateIndex].toLowerCase() === "closed") {
sendObj.issue.closed = true;
}
return createIssue(
const promise = createIssue(
octokit,
sendObj,
values.userOrOrganization,
values.repo
);
});
if (stateIndex > -1 && row[stateIndex].toLowerCase() === "closed") {
sendObj.state = 'closed';

pr.push(promise.then((res) => {
return updateIssue(
octokit,
sendObj,
values.userOrOrganization,
values.repo,
res.data.number);
}));
} else {
pr.push(promise);
}
return pr;
}, []);

Promise.all(createPromises).then(
(res) => {
Expand Down
4 changes: 2 additions & 2 deletions test/4.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
title,body,labels,assignee
title,body,labels,assignees
Test 1, This is the test1 desc, bug,github-user
Test 2, This is the test2 desc, "question,bug",github-user
Test 2, This is the test2 desc, "question,bug","github-user,another-github-user"
2 changes: 1 addition & 1 deletion test/5.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
title,body,labels,assignee
title,body,labels,assignees
Test 1,This is a placeholder description.,bug,
Test 2,This is a placeholder description.,"question,bug",
Test 3,This is a placeholder description.,,
Expand Down