-
-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathJiraMapper.cs
503 lines (438 loc) · 19.2 KB
/
JiraMapper.cs
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using Common.Config;
using Migration.Common;
using Migration.Common.Config;
using Migration.Common.Log;
using Migration.WIContract;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Migration.Jira-Export.Tests")]
namespace JiraExport
{
internal class JiraMapper : BaseMapper<JiraRevision>
{
private readonly IJiraProvider _jiraProvider;
private readonly Dictionary<string, FieldMapping<JiraRevision>> _fieldMappingsPerType;
private readonly HashSet<string> _targetTypes;
private readonly ConfigJson _config;
private readonly ExportIssuesSummary exportIssuesSummary;
public JiraMapper(IJiraProvider jiraProvider, ConfigJson config, ExportIssuesSummary exportIssuesSummary)
: base(jiraProvider?.GetSettings()?.UserMappingFile)
{
_jiraProvider = jiraProvider;
_config = config;
_targetTypes = InitializeTypeMappings();
_fieldMappingsPerType = InitializeFieldMappings(exportIssuesSummary);
this.exportIssuesSummary = exportIssuesSummary;
}
#region Mapping definitions
internal WiItem Map(JiraItem issue)
{
if (issue == null)
throw new ArgumentNullException(nameof(issue));
var wiItem = new WiItem();
if (_config.TypeMap.Types != null)
{
var type = (from t in _config.TypeMap.Types where t.Source == issue.Type select t.Target).FirstOrDefault();
if (type != null)
{
var revisions = issue.Revisions.Select(r => MapRevision(r)).ToList();
wiItem.OriginId = issue.Key;
wiItem.Type = type;
wiItem.Revisions = revisions;
}
else
{
Logger.Log(LogLevel.Error, $"Type mapping missing for '{issue.Key}' with Jira type '{issue.Type}'. Item was not exported which may cause missing links in issues referencing this item.");
exportIssuesSummary.AddUnmappedIssueType(issue.Type);
return null;
}
}
return wiItem;
}
internal Dictionary<string, FieldMapping<JiraRevision>> InitializeFieldMappings(ExportIssuesSummary exportIssuesSummary)
{
Logger.Log(LogLevel.Info, "Initializing Jira field mapping...");
var commonFields = new FieldMapping<JiraRevision>();
var typeFields = new Dictionary<string, FieldMapping<JiraRevision>>();
foreach (var targetType in _targetTypes)
{
if (!typeFields.ContainsKey(targetType))
typeFields.Add(targetType, new FieldMapping<JiraRevision>());
}
foreach (var item in _config.FieldMap.Fields)
{
if (item.Source != null)
{
var isCustomField = item.SourceType == "name";
if (isCustomField && _jiraProvider.GetCustomId(item.Source) == null)
Logger.Log(LogLevel.Warning, $"Could not find the field id for '{item.Source}', please check the field mapping!");
Func<JiraRevision, (bool, object)> value;
if (item.Mapping?.Values != null)
{
value = r => FieldMapperUtils.MapValue(r, item.Source, item.Target, _config, exportIssuesSummary);
}
else if (!string.IsNullOrWhiteSpace(item.Mapper))
{
switch (item.Mapper)
{
case "MapTitle":
value = r => FieldMapperUtils.MapTitle(r);
break;
case "MapTitleWithoutKey":
value = r => FieldMapperUtils.MapTitleWithoutKey(r);
break;
case "MapUser":
value = IfChanged<string>(item.Source, isCustomField, MapUser);
break;
case "MapSprint":
value = IfChanged<string>(item.Source, isCustomField, FieldMapperUtils.MapSprint);
break;
case "MapTags":
value = IfChanged<string>(item.Source, isCustomField, FieldMapperUtils.MapTags);
break;
case "MapArray":
value = IfChanged<string>(item.Source, isCustomField, FieldMapperUtils.MapArray);
break;
case "MapRemainingWork":
value = IfChanged<string>(item.Source, isCustomField, FieldMapperUtils.MapRemainingWork);
break;
case "MapRendered":
value = r => FieldMapperUtils.MapRenderedValue(r, item.Source, isCustomField, _jiraProvider.GetCustomId(item.Source), _config);
break;
case "MapLexoRank":
value = IfChanged<string>(item.Source, isCustomField, FieldMapperUtils.MapLexoRank);
break;
default:
value = IfChanged<string>(item.Source, isCustomField);
break;
}
}
else
{
var dataType = item.Type.ToLower();
if (dataType == "double")
{
value = IfChanged<double>(item.Source, isCustomField);
}
else if (dataType == "int" || dataType == "integer")
{
value = IfChanged<int>(item.Source, isCustomField);
}
else if (dataType == "datetime" || dataType == "date")
{
value = IfChangedDateTime(item.Source, isCustomField);
}
else
{
value = IfChanged<string>(item.Source, isCustomField);
}
}
// Check if not-for has been set, if so get all work item types except that one, else for has been set and get those
var currentWorkItemTypes = !string.IsNullOrWhiteSpace(item.NotFor) ? GetWorkItemTypes(item.NotFor.Split(',')) : item.For.Split(',').ToList();
foreach (var wit in currentWorkItemTypes)
{
try
{
if (wit == "All" || wit == "Common")
{
commonFields.Add(item.Target, value);
}
else
{
// If we haven't mapped the Type then we probably want to ignore the field
if (typeFields.TryGetValue(wit, out FieldMapping<JiraRevision> fm))
{
fm.Add(item.Target, value);
}
else
{
Logger.Log(LogLevel.Warning, $"No target type '{wit}' is set, field {item.Source} cannot be mapped.");
}
}
}
catch (Exception)
{
Logger.Log(LogLevel.Warning, $"Ignoring target mapping with key: '{item.Target}', because it is already configured.");
}
}
}
}
// Now go through the list of built up type fields (which we will eventually get to
// and then add them to the complete dictionary per type.
var mappingPerWiType = new Dictionary<string, FieldMapping<JiraRevision>>();
foreach (KeyValuePair<string, FieldMapping<JiraRevision>> item in typeFields)
{
mappingPerWiType.Add(item.Key, MergeMapping(commonFields, item.Value));
}
return mappingPerWiType;
}
internal WiDevelopmentLink MapDevelopmentLink(JiraRevision jiraRevision)
{
if (jiraRevision == null)
throw new ArgumentNullException(nameof(jiraRevision));
if (jiraRevision.DevelopmentLink == null)
{
return null;
}
var jiraDevelopmentLink = jiraRevision.DevelopmentLink.Value;
var respositoryTarget = jiraDevelopmentLink.Repository;
var respositoryOverride = _config
.RepositoryMap
.Repositories?
.Find(r => r.Source == respositoryTarget)?
.Target;
if (!string.IsNullOrEmpty(respositoryOverride))
{
respositoryTarget = respositoryOverride;
}
var developmentLink = new WiDevelopmentLink()
{
Id = jiraDevelopmentLink.Id,
Repository = respositoryTarget,
Type = jiraDevelopmentLink.Type.ToString()
};
return developmentLink;
}
internal List<WiLink> MapLinks(JiraRevision r)
{
if (r == null)
throw new ArgumentNullException(nameof(r));
var links = new List<WiLink>();
if (r.LinkActions == null)
return links;
// map issue links
foreach (var jiraLinkAction in r.LinkActions)
{
var changeType = jiraLinkAction.ChangeType == RevisionChangeType.Added ? ReferenceChangeType.Added : ReferenceChangeType.Removed;
var link = new WiLink();
if (_config.LinkMap.Links != null)
{
var linkType = (from t in _config.LinkMap.Links where t.Source == jiraLinkAction.Value.LinkType select t.Target).FirstOrDefault();
if (linkType != null)
{
// If link is inward link, reverse the direction of the mapped link
if (jiraLinkAction.Value.IsInwardLink)
{
linkType = GetReverseLinkTypeReferenceName(linkType);
}
link.Change = changeType;
link.SourceOriginId = jiraLinkAction.Value.SourceItem;
link.TargetOriginId = jiraLinkAction.Value.TargetItem;
link.WiType = linkType;
links.Add(link);
}
}
}
// map epic link
LinkMapperUtils.AddRemoveSingleLink(r, links, _jiraProvider.GetSettings().EpicLinkField, "Epic", _config);
// map parent
LinkMapperUtils.AddRemoveSingleLink(r, links, "parent", "Parent", _config);
// map epic child
LinkMapperUtils.MapEpicChildLink(r, links, "epic child", "Child", _config);
return links;
}
internal List<WiAttachment> MapAttachments(JiraRevision rev)
{
if (rev == null)
throw new ArgumentNullException(nameof(rev));
var attachments = new List<WiAttachment>();
if (rev.AttachmentActions == null)
return attachments;
_jiraProvider.DownloadAttachments(rev).Wait();
foreach (var att in rev.AttachmentActions)
{
var change = att.ChangeType == RevisionChangeType.Added ? ReferenceChangeType.Added : ReferenceChangeType.Removed;
var wiAtt = new WiAttachment()
{
Change = change,
AttOriginId = att.Value.Id,
FilePath = att.Value.LocalPath,
Comment = "Imported from Jira"
};
attachments.Add(wiAtt);
}
return attachments;
}
internal List<WiField> MapFields(JiraRevision r)
{
if (r == null)
throw new ArgumentNullException(nameof(r));
var fields = new List<WiField>();
if (_config.TypeMap.Types != null)
{
var type = (from t in _config.TypeMap.Types where t.Source == r.Type select t.Target).FirstOrDefault();
if (type != null && _fieldMappingsPerType.TryGetValue(type, out var mapping))
{
foreach (var field in mapping)
{
try
{
var fieldreference = field.Key;
var (include, value) = field.Value(r);
if (include)
{
value = TruncateField(value, fieldreference);
if(value == null)
{
value = "";
}
Logger.Log(LogLevel.Debug, $"Mapped value '{value}' to field '{fieldreference}'.");
fields.Add(new WiField()
{
ReferenceName = fieldreference,
Value = value
});
}
}
catch (Exception ex)
{
Logger.Log(ex, $"Error mapping field '{field.Key}' on item '{r.OriginId}'.");
}
}
}
}
return fields;
}
internal WiRevision MapRevision(JiraRevision r)
{
Logger.Log(LogLevel.Debug, $"Mapping revision {r.Index}.");
List<WiAttachment> attachments = MapAttachments(r);
List<WiField> fields = MapFields(r);
List<WiLink> links = MapLinks(r);
var developmentLink = MapDevelopmentLink(r);
return new WiRevision()
{
ParentOriginId = r.ParentItem.Key,
Index = r.Index,
Time = r.Time,
Author = MapUser(r.Author),
Attachments = attachments,
Fields = fields,
Links = links,
AttachmentReferences = attachments.Any(),
DevelopmentLink = developmentLink
};
}
protected override string MapUser(string sourceUser)
{
if (string.IsNullOrWhiteSpace(sourceUser))
return null;
var email = _jiraProvider.GetUserEmail(sourceUser);
return base.MapUser(email);
}
private HashSet<string> InitializeTypeMappings()
{
HashSet<string> types = new HashSet<string>();
_config.TypeMap.Types.ForEach(t => types.Add(t.Target));
return types;
}
private Func<JiraRevision, (bool, object)> IfChanged<T>(string sourceField, bool isCustomField, Func<T, object> mapperFunc = null)
{
if (isCustomField)
{
sourceField = _jiraProvider.GetCustomId(sourceField) ?? sourceField;
}
return (r) =>
{
if (r.Fields.TryGetValue(sourceField.ToLower(), out object value))
{
if (mapperFunc != null)
{
return (true, mapperFunc((T)value));
}
else
{
return (true, (T)value);
}
}
else
{
return (false, null);
}
};
}
private Func<JiraRevision, (bool, object)> IfChangedDateTime(string sourceField, bool isCustomField, Func<DateTime, object> mapperFunc = null)
{
if (isCustomField)
{
sourceField = _jiraProvider.GetCustomId(sourceField) ?? sourceField;
}
return (r) =>
{
if (r.Fields.TryGetValue(sourceField.ToLower(), out object value))
{
if (mapperFunc != null)
{
return (true, mapperFunc((DateTime)value));
}
else
{
if (DateTime.TryParseExact(value.ToString(), "dd/MMM/yy", CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out DateTime result))
{
return (true, result.ToUniversalTime());
}
else
{
return (true, value);
}
}
}
else
{
return (false, null);
}
};
}
#endregion
private List<string> GetWorkItemTypes(params string[] notFor)
{
List<string> list;
if (notFor != null && notFor.Any())
{
list = _targetTypes.Where(t => !notFor.Contains(t)).ToList();
}
else
{
list = _targetTypes.ToList();
}
return list;
}
internal object TruncateField(object value, string field)
{
if (value == null) return value;
string valueStr = value.ToString();
var fieldLimits = new Dictionary<string, int>()
{
{ WiFieldReference.Title, 255 },
{ WiFieldReference.Description, 1048576 }
};
if (fieldLimits.ContainsKey(field))
{
int limit = fieldLimits[field];
if (valueStr.Length > limit)
{
string truncated = valueStr.Substring(0, limit - 3) + "...";
Logger.Log(LogLevel.Warning, $"Field {field} was truncated. Maximum length: {limit}, new value: {truncated}");
return truncated;
}
}
return valueStr;
}
private string GetReverseLinkTypeReferenceName(string referenceName)
{
string Forward = "Forward";
string Reverse = "Reverse";
if (referenceName.Contains(Forward))
{
return referenceName.Replace(Forward, Reverse);
}
else
{
return referenceName.Replace(Reverse, Forward);
}
}
}
}