-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (146 loc) · 5.3 KB
/
index.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
/**
* Script to import a CSV file and create Github issues. title, description
* and comments are mapped to the respective git issue entities. Status, Priority
* and Issue Type are modeled as labels
*/
const fs = require('fs')
const path = require('path')
const neatCsv = require('neat-csv')
const { Octokit } = require("@octokit/rest")
const GIT_REPO_OWNER = 'cheaseed'
const GIT_REPO = 'cheaseed-prototype'
//set the token value below before running the script
const PERSONAL_ACCESS_TOKEN = 'xxx'
const CSV_FILE_NAME = 'issues-Completed Issues Remaining.csv'
// Just using strings without context gets confusing when an issue is tagged with a number of labels
// The prefixes give some context to the label
const PRIORITY_PREFIX = "Priority:"
const STATUS_PREFIX = "Status:"
const ISSUE_TYPE_PREFIX = "Issue Type:"
const octokit = new Octokit({
auth: PERSONAL_ACCESS_TOKEN,
userAgent: 'Cheaseed Prototype 1.0',
timeZone: 'America/Los_Angeles',
log: require("console-log-level")({ level: "info" }),
request: {
agent: undefined,
fetch: undefined,
timeout: 0
}
})
// Map names to github users. TODO - look up based on full name and org
// for example octakit.search.users({q='fullname:Anand Sathe+org:Cheaseed'})
const userMap = new Map([
['Adam Galper', 'agalper'],
['Anand Sathe', 'anandsathe67'],
['Nicole Johnson', 'johnsonnicole'],
['Giselle Galper', 'gsgalper']
])
const filePath = path.join(__dirname, CSV_FILE_NAME)
fs.readFile(filePath, async (error, data) => {
if (error) {
return console.log('error reading file')
}
const parsedData = await neatCsv(data)
console.log("CSV File Data:" + JSON.stringify(parsedData))
/* const milestones = Array.from(new Set(parsedData.map(csvIssue => {
return csvIssue.Status
}).filter(function (val) { return val != null && val != "" })))
console.log("Milestones = " + milestones)
milestoneMap = new Map()
for (const milestone of milestones) {
const response = await createMilestone(milestone)
milestoneMap.set(response.data.title, response.data.number)
}
console.log("Milestone Map: ")
for (let [key, value] of milestoneMap) {
console.log(key + ' = ' + value)
}
*/
for (const csvIssue of parsedData) {
const githubUsers = csvIssue['Assigned to'].split(',').map(name => {
return userMap.get(name)
}).filter(Boolean)
console.log("Github Users = " + JSON.stringify(githubUsers))
const issue = await createIssue(csvIssue,
//milestoneMap.get(csvIssue.Status),
githubUsers)
console.log("Issue created:" + JSON.stringify(issue))
//console.log("Issue Number: " + issue.data.number)
if(csvIssue['Progress Comments']) {
const commentCreate = await octokit.issues.createComment({
owner: GIT_REPO_OWNER,
repo: GIT_REPO,
issue_number: issue.data.number,
body: csvIssue['Progress Comments']
})
console.log("Comment Created:" + JSON.stringify(commentCreate))
}
if(csvIssue.Attachments) {
const commentCreate = await octokit.issues.createComment({
owner: GIT_REPO_OWNER,
repo: GIT_REPO,
issue_number: issue.data.number,
body: ("Attachments: \n" + csvIssue.Attachments.replace(/,/g, ', '))
})
console.log("Comment Created:" + JSON.stringify(commentCreate))
}
if(csvIssue['Closed'] == "1") {
const commentCreate = await octokit.issues.createComment({
owner: GIT_REPO_OWNER,
repo: GIT_REPO,
issue_number: issue.data.number,
body: ("Date Completed:" + csvIssue['Date Completed'])
})
console.log("Comment Created:" + JSON.stringify(commentCreate))
}
}
})
async function createMilestone(title) {
const milestoneData = await octokit.issues.createMilestone({
owner: GIT_REPO_OWNER,
repo: GIT_REPO,
title: title
})
//console.log("MilestoneData Number = " + milestoneData.data.number)
return milestoneData
}
async function createIssue(csvIssue, /*milestoneId,*/ assignees) {
let labels = []
if (csvIssue.Priority) {
labels.push(PRIORITY_PREFIX + csvIssue.Priority)
}
if (csvIssue['Issue Type']) {
labels.push(ISSUE_TYPE_PREFIX + csvIssue['Issue Type'])
}
if (csvIssue.Status) {
labels.push(STATUS_PREFIX + csvIssue.Status)
}
let issue = await octokit.issues.create({
owner: GIT_REPO_OWNER,
repo: GIT_REPO,
title: csvIssue.Name,
body: csvIssue.Description,
assignees: assignees,
//milestone: milestoneId,
labels: labels
})
if(csvIssue['Closed'] == "1") {
issue = await octokit.issues.update({
owner: GIT_REPO_OWNER,
repo: GIT_REPO,
issue_number: issue.data.number,
state: "closed"
})
}
return issue
}
async function createComment(issueNumber, comment) {
const commentResponse = await octokit.issues.createComment({
owner: GIT_REPO_OWNER,
repo: GIT_REPO,
issue_number: issueNumber,
body: comment
})
return commentResponse
}