/
AttachmentsProcessor.cs
208 lines (184 loc) · 10.9 KB
/
AttachmentsProcessor.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Common.Config;
using Logging;
using Microsoft.Extensions.Logging;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
namespace Common.Migration
{
public class AttachmentsProcessor : IPhase2Processor
{
static ILogger Logger { get; } = MigratorLogging.CreateLogger<AttachmentsProcessor>();
public string Name => Constants.RelationPhaseAttachments;
public bool IsEnabled(ConfigJson config)
{
return config.MoveAttachments;
}
public async Task Preprocess(IMigrationContext migrationContext, IBatchMigrationContext batchContext, IList<WorkItem> sourceWorkItems, IList<WorkItem> targetWorkItems)
{
}
public async Task<IEnumerable<JsonPatchOperation>> Process(IMigrationContext migrationContext, IBatchMigrationContext batchContext, WorkItem sourceWorkItem, WorkItem targetWorkItem)
{
IList<JsonPatchOperation> jsonPatchOperations = new List<JsonPatchOperation>();
if (sourceWorkItem.Relations == null)
{
return jsonPatchOperations;
}
IEnumerable<WorkItemRelation> sourceAttachmentWorkItemRelations = GetAttachmentWorkItemRelations(sourceWorkItem.Relations);
if (sourceAttachmentWorkItemRelations.Any())
{
foreach (WorkItemRelation sourceAttachmentWorkItemRelation in sourceAttachmentWorkItemRelations)
{
WorkItemRelation targetAttachmentRelation = GetAttachmentIfExistsOnTarget(targetWorkItem, sourceAttachmentWorkItemRelation);
if (targetAttachmentRelation != null) // is on target
{
JsonPatchOperation attachmentAddOperation = MigrationHelpers.GetRelationAddOperation(targetAttachmentRelation);
jsonPatchOperations.Add(attachmentAddOperation);
}
else // is not on target
{
AttachmentLink attachmentLink = await UploadAttachmentFromSourceRelation(migrationContext, batchContext, sourceWorkItem, sourceAttachmentWorkItemRelation, migrationContext.Config.MaxAttachmentSize);
if (attachmentLink != null)
{
WorkItemRelation newAttachmentWorkItemRelation = new WorkItemRelation();
newAttachmentWorkItemRelation.Rel = sourceAttachmentWorkItemRelation.Rel;
newAttachmentWorkItemRelation.Url = attachmentLink.AttachmentReference.Url;
newAttachmentWorkItemRelation.Attributes = new Dictionary<string, object>();
newAttachmentWorkItemRelation.Attributes[Constants.RelationAttributeName] = attachmentLink.FileName;
newAttachmentWorkItemRelation.Attributes[Constants.RelationAttributeResourceSize] = attachmentLink.ResourceSize;
newAttachmentWorkItemRelation.Attributes[Constants.RelationAttributeComment] = attachmentLink.Comment;
JsonPatchOperation attachmentAddOperation = MigrationHelpers.GetRelationAddOperation(newAttachmentWorkItemRelation);
jsonPatchOperations.Add(attachmentAddOperation);
}
}
}
}
return jsonPatchOperations;
}
private WorkItemRelation GetAttachmentIfExistsOnTarget(WorkItem targetWorkItem, WorkItemRelation sourceRelation)
{
if (targetWorkItem.Relations == null)
{
return null;
}
foreach (WorkItemRelation targetRelation in targetWorkItem.Relations)
{
if (targetRelation.Rel.Equals(Constants.AttachedFile) && targetRelation.Url.Equals(sourceRelation.Url, StringComparison.OrdinalIgnoreCase))
{
return targetRelation;
}
}
return null;
}
private IEnumerable<WorkItemRelation> GetAttachmentWorkItemRelations(IList<WorkItemRelation> relations)
{
IList<WorkItemRelation> result = new List<WorkItemRelation>();
if (relations == null)
{
return result;
}
foreach (WorkItemRelation relation in relations)
{
if (IsRelationAttachedFile(relation))
{
result.Add(relation);
}
}
return result;
}
private bool IsRelationAttachedFile(WorkItemRelation relation)
{
return relation.Rel.Equals(Constants.AttachedFile, StringComparison.OrdinalIgnoreCase);
}
private async Task<AttachmentLink> UploadAttachmentFromSourceRelation(IMigrationContext migrationContext, IBatchMigrationContext batchContext, WorkItem sourceWorkItem, WorkItemRelation sourceRelation, int maxAttachmentSize)
{
//Attachments are of type Rel = "AttachedFile"
if (sourceRelation.Rel == Constants.AttachedFile)
{
string filename = null;
string comment = null;
long resourceSize = 0;
//get the file name and comment
if (sourceRelation.Attributes.ContainsKey(Constants.RelationAttributeName))
{
filename = sourceRelation.Attributes[Constants.RelationAttributeName].ToString();
}
if (sourceRelation.Attributes.ContainsKey(Constants.RelationAttributeComment))
{
comment = sourceRelation.Attributes[Constants.RelationAttributeComment].ToString();
}
//get the guid from the url
Guid attachmentId;
if (Guid.TryParse(sourceRelation.Url.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries).Last(), out attachmentId))
{
Stream stream = null;
try
{
Logger.LogTrace(LogDestination.File, $"Reading attachment {filename} for source work item {sourceWorkItem.Id} from the source account");
stream = await WorkItemTrackingHelpers.GetAttachmentAsync(migrationContext.SourceClient.WorkItemTrackingHttpClient, attachmentId);
Logger.LogTrace(LogDestination.File, $"Completed reading attachment {filename} for source work item {sourceWorkItem.Id} from the source account");
}
catch (Exception e)
{
Logger.LogError(LogDestination.File, e, $"Unable to download attachment {filename} for source work item {sourceWorkItem.Id} from the source account");
ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItem.Id.Value, FailureReason.AttachmentDownloadError, batchContext.WorkItemMigrationState);
return null;
}
AttachmentReference aRef = null;
using (MemoryStream memstream = new MemoryStream())
{
using (stream)
{
try
{
Logger.LogTrace(LogDestination.File, $"Downloading attachment {filename} for source work item {sourceWorkItem.Id} from the source account");
await ClientHelpers.CopyStreamAsync(stream, memstream);
Logger.LogTrace(LogDestination.File, $"Completed downloading attachment {filename} for source work item {sourceWorkItem.Id} from the source account");
}
catch (Exception e)
{
Logger.LogError(LogDestination.File, e, $"Unable to read downloaded attachment {filename} for source work item {sourceWorkItem.Id} from the source account");
ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItem.Id.Value, FailureReason.AttachmentDownloadError, batchContext.WorkItemMigrationState);
return null;
}
}
resourceSize = memstream.Length;
if (resourceSize > maxAttachmentSize)
{
Logger.LogWarning(LogDestination.File, $"Attachment of source work item with id {sourceWorkItem.Id} and url {sourceRelation.Url} exceeded the maximum attachment size of {maxAttachmentSize} bytes." +
$" Skipping creating the attachment in target account.");
return null;
}
memstream.Position = 0;
//upload the attachment to the target
try
{
Logger.LogTrace(LogDestination.File, $"Uploading attachment {filename} of {resourceSize} bytes for source work item {sourceWorkItem.Id} from the source account");
aRef = await WorkItemTrackingHelpers.CreateAttachmentChunkedAsync(migrationContext.TargetClient.WorkItemTrackingHttpClient, migrationContext.TargetClient.Connection, memstream, migrationContext.Config.AttachmentUploadChunkSize);
Logger.LogTrace(LogDestination.File, $"Completed uploading attachment {filename} for source work item {sourceWorkItem.Id} from the source account");
}
catch (Exception e)
{
Logger.LogError(LogDestination.File, e, $"Unable to upload attachment {filename} for source work item {sourceWorkItem.Id} to the target account");
ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItem.Id.Value, FailureReason.AttachmentUploadError, batchContext.WorkItemMigrationState);
}
}
if (aRef != null)
{
return new AttachmentLink(filename, aRef, resourceSize, comment);
}
}
else
{
Logger.LogError(LogDestination.File, $"Attachment link is incorrect for {sourceWorkItem.Id} {sourceRelation.Url}. Skipping creating the attachment in target account.");
ClientHelpers.AddFailureReasonToWorkItemMigrationState(sourceWorkItem.Id.Value, FailureReason.AttachmentUploadError, batchContext.WorkItemMigrationState);
}
}
return null;
}
}
}