From c5bdc454ed85ad5071f5dd8527a622b4dbb1e5d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A5l=20Kristian=20T=C3=B8nder?= Date: Tue, 2 Apr 2024 10:03:06 +0200 Subject: [PATCH] Support for import of issue with multiple assignees --- helpers.js | 33 +++++++++++++++++++++++++++++---- import.js | 47 +++++++++++++++++++++++++++++++---------------- test/4.csv | 4 ++-- test/5.csv | 2 +- 4 files changed, 63 insertions(+), 23 deletions(-) diff --git a/helpers.js b/helpers.js index c3a209b..3eb23f3 100644 --- a/helpers.js +++ b/helpers.js @@ -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 @@ -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 }; diff --git a/import.js b/import.js index b0c12b2..563801b 100644 --- a/import.js +++ b/import.js @@ -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) => { @@ -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) => { diff --git a/test/4.csv b/test/4.csv index 3580bd4..b20fc79 100644 --- a/test/4.csv +++ b/test/4.csv @@ -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" diff --git a/test/5.csv b/test/5.csv index f3f7fe0..ee5a5e8 100644 --- a/test/5.csv +++ b/test/5.csv @@ -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.,,