-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
332 lines (305 loc) · 8.55 KB
/
index.ts
File metadata and controls
332 lines (305 loc) · 8.55 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { markdownToAtlassianWikiMarkup } from '@kenchan0130/markdown-to-atlassian-wiki-markup';
import chalk from 'chalk';
import fs from 'fs';
import yaml from 'yaml';
import GithubDataLoader from './GithubDataLoader';
const LOG_LEVEL = 'debug';
const CONFIG_YAML = 'config.yaml';
const RAW_GITHUB_FILENAME = 'github.json';
const OUTPUT_FILENAME = 'output.json';
const config = yaml.parse(fs.readFileSync(CONFIG_YAML, 'utf8'));
const { auth, owner, repo, state } = config.github;
const { projectKey } = config.jira;
const {
priorityMap,
defaultPriority = 'Medium',
issueTypeMap,
defaultIssueType = 'Story',
} = config;
const userMap = config.userMap;
export interface GithubComment {
body: string;
user: GithubUser;
created_at: string;
issue_url: string;
}
export interface GithubIssue {
labels: GithubLabel[];
number: number;
state: 'open' | 'closed';
user: GithubUser;
assignee: GithubUser;
created_at: string;
updated_at: string;
title: string;
body: string;
milestone: GithubMilestone;
}
export interface GithubLabel {
name: string;
}
export interface GithubMilestone {
title: string;
}
export interface GithubUser {
login: string;
}
export interface JiraComment {
body: string;
author: string;
created: string;
}
export interface JiraCustomField {
fieldName: string;
fieldType: string;
value: string[];
}
export interface JiraIssue {
key: string;
status: string;
resolution: string | null;
reporter: string;
assignee?: string;
fixedVersions: string[];
created: string;
updated: string;
summary: string;
description: string;
issueType: string;
priority: string;
labels: string[];
customFieldValues: JiraCustomField[];
comments: JiraComment[];
}
export interface JiraProject {
name: string;
externalName: string;
key: string;
issues: JiraIssue[];
versions: any;
}
/**
* Writes a serializable POJO to disk as JSON
* @param data - The data to write to disk
* @param filename - The name of the file to write
*/
const writeJSON = (data: object, filename: string): object => {
if (LOG_LEVEL === 'debug') {
console.log(`Writing ${chalk.yellow(filename)}...\n`);
}
fs.writeFileSync(filename, JSON.stringify(data, null, 2), 'utf8');
return data;
};
/**
* Attempts to convert github-flavored markdown to Atlassian wiki format.
* If the conversion fails, it returns the original markdown.
* @param md - Source markdown
*/
const mdToWiki = (md: string): string => {
try {
return markdownToAtlassianWikiMarkup(md);
} catch (e) {
console.log(
`Failed to convert the following markdown to Wiki format, falling back to raw markdown:\n${JSON.stringify(
md
)}`
);
return md;
}
};
const mapGithubUserToJiraUser = (githubUser: GithubUser) =>
githubUser ? userMap[githubUser.login] || githubUser.login : null;
/**
* Transforms an array of Github comment objects to JIRA format.
* @param githubComments
*/
const mapGithubCommentsToJiraComments = (
githubComments: GithubComment[]
): JiraComment[] => {
console.log(
`\tMapping ${chalk.blueBright(
githubComments.length.toString()
)} comments...`
);
return githubComments.map(comment => ({
body: mdToWiki(comment.body),
author: mapGithubUserToJiraUser(comment.user),
created: mapDate(comment.created_at),
}));
};
const mapLabelToValue = (
githubIssue: GithubIssue,
map: object,
defaultValue: string
): string => {
if (!map || !Object.values(map).length) {
return defaultValue;
}
return Object.keys(map).reduce((currentValue, githubLabel) => {
const githubLabelNames = githubIssue.labels.map(l => l.name);
if (githubLabelNames.includes(githubLabel)) {
// TODO Not too happy about this side effect.
githubIssue.labels = githubIssue.labels.filter(
label => label.name !== githubLabel
);
return map[githubLabel];
}
return currentValue;
}, defaultValue);
};
/**
* Transforms Github labels to JIRA custom fields.
* @param githubLabels
*/
const mapLabelsToCustomFields = (
githubLabels: GithubLabel[]
): JiraCustomField[] => {
const { customFields } = config;
if (!customFields.length) {
return [];
}
return customFields.map(({ fieldName, fieldType, map, prefixes }) => ({
fieldName,
fieldType,
value: map
? githubLabels
.map(l => l.name)
.reduce(
(acc, githubLabel) =>
map[githubLabel] ? [...acc, map[githubLabel]] : acc,
<string[]>[]
)
: githubLabels
.map(l => l.name)
.filter(label => prefixes.some(prefix => label.startsWith(prefix)))
.reduce(
(acc, label) => [
...acc,
prefixes.reduce(
(result, prefix) => result.replace(prefix, ''),
label
),
],
<string[]>[]
)
.map(label => label.split(' ').join('_')),
}));
};
/**
* Converts UCT 'Z' shorthand date format returned by Github to explicit '+00:00' for JIRA
* @param githubDate
*/
const mapDate = (githubDate: string): string =>
`${githubDate.slice(0, -1)}+00:00`;
/**
* Transforms a Github issue and any associated comments to a JIRA issue with embedded comments.
* @param issue
* @param issueComments
*/
const mapGithubIssueToJiraIssue = (
issue: GithubIssue,
issueComments: GithubComment[]
): JiraIssue => {
console.log(
`\tTransforming Github issue #${chalk.blueBright(
issue.number.toString()
)} to JIRA format`
);
return {
key: `${projectKey}-${issue.number}`,
status: issue.state === 'closed' ? 'Done' : 'To Do',
resolution: issue.state === 'closed' ? 'Fixed' : null,
reporter: mapGithubUserToJiraUser(issue.user),
assignee: mapGithubUserToJiraUser(issue.assignee),
fixedVersions: issue.milestone ? [issue.milestone.title] : [],
created: mapDate(issue.created_at),
updated: mapDate(issue.updated_at),
summary: issue.title,
description: mdToWiki(issue.body),
issueType: mapLabelToValue(issue, issueTypeMap, defaultIssueType),
priority: mapLabelToValue(issue, priorityMap, defaultPriority),
labels: issue.labels.map(label => label.name),
customFieldValues: mapLabelsToCustomFields(issue.labels),
comments: issueComments
? mapGithubCommentsToJiraComments(issueComments)
: [],
};
};
/**
* Maps a collection of Github issues and comments to JIRA issues with embedded comments
*
* @param githubIssues - An array of Github issues
* @param commentsDictionary - A dictionary of Github comments keyed by issue number.
*/
const mapGithubIssuesToJiraIssues = (githubIssues, commentsDictionary) => {
console.log(
`Mapping ${chalk.yellow(githubIssues.length)} Github issues to JIRA format`
);
return githubIssues.map(issue =>
mapGithubIssueToJiraIssue(issue, commentsDictionary[issue.number])
);
};
/**
* Create a dictionary of Github comments keyed by issue number.
* @param githubComments - An array of Github comments
*/
const createCommentDictionary = (githubComments: GithubComment[]): object =>
githubComments.reduce((dictionary, comment) => {
const issueNumber = <string>comment.issue_url.split('/').pop();
dictionary[issueNumber] = dictionary[issueNumber]
? [...dictionary[issueNumber], comment]
: [comment];
return dictionary;
}, {});
const mapGithubDataToJiraData = githubData => {
const { githubIssues, githubComments } = githubData;
const commentDictionary = createCommentDictionary(githubComments);
const issues = mapGithubIssuesToJiraIssues(githubIssues, commentDictionary);
const project: JiraProject = {
name: repo,
externalName: repo,
key: config.jira.projectKey,
issues,
versions: [...new Set(issues.flatMap(issue => issue.fixedVersions))],
};
return {
projects: [project],
};
};
console.log(
`Fetching Github data from ${chalk.yellow(owner)}/${chalk.yellow(repo)}`
);
const issueLoader = new GithubDataLoader({
auth,
owner,
repo,
state,
});
const dataSets = [
issueLoader.fetchIssues(),
config.github.includeComments
? issueLoader.fetchComments()
: Promise.resolve(null),
];
Promise.all(dataSets)
.then(([githubIssues, githubComments]) => {
return {
githubIssues,
githubComments,
};
})
.then(objData =>
LOG_LEVEL === 'debug' ? writeJSON(objData, RAW_GITHUB_FILENAME) : objData
)
.then(mapGithubDataToJiraData)
.then(jiraData => writeJSON(jiraData, OUTPUT_FILENAME))
.catch(e => {
console.log(
`${chalk.redBright('Failed to convert issues:')} \n${JSON.stringify(
e,
null,
2
)}`
);
throw e;
});