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
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core.DI;
using Tango.Integration.Operation;
using Tango.Logging;
using Tango.PPC.Common.Application;
using Tango.PPC.Common.Connection;
using Tango.TFS;
using Tango.Core.ExtensionMethods;
using Tango.Core.Helpers;
using Tango.Settings;
using Tango.PPC.Common;
using Tango.PPC.Common.Build;
namespace Tango.PPC.BugReporting.TFS
{
public class TeamFoundationServicePPCClient : TeamFoundationServiceClient
{
private IPPCApplicationManager _applicationManager;
private IMachineProvider _machineProvider;
private IBuildProvider _buildProvider;
public Project Project { get; private set; }
public event EventHandler Initialized;
public bool IsInitialized { get; private set; }
public TeamFoundationServicePPCClient(string collectionURL, string userName, string personalToken) : base(collectionURL, userName, personalToken)
{
_applicationManager = TangoIOC.Default.GetInstance<IPPCApplicationManager>();
_machineProvider = TangoIOC.Default.GetInstance<IMachineProvider>();
_buildProvider = TangoIOC.Default.GetInstance<IBuildProvider>();
_applicationManager.ApplicationReady += ApplicationManager_ApplicationReady;
}
private void ApplicationManager_ApplicationReady(object sender, EventArgs e)
{
Init();
}
private async void Init()
{
try
{
Project = await GetProject("Tango");
IsInitialized = true;
Initialized?.Invoke(this, new EventArgs());
}
catch (Exception ex)
{
LogManager.Log(ex, "Error initializing TFS.");
}
}
private string[] GetLogFiles(FileLogger logger)
{
string[] fileEntries = new string[1];
fileEntries[0] = logger.LogFile;
string fileName = Path.GetFileNameWithoutExtension(logger.LogFile);
int indexPos = fileName.IndexOf(FileLogger.FILE_SET_EXTENSION);
if (indexPos > 0)
{
string extension = Path.GetExtension(logger.LogFile);
fileName = fileName.Substring(0, indexPos);
fileEntries = Directory.GetFiles(logger.Folder, $"{fileName}*{extension}").Where(x => Path.GetFileName(x).StartsWith(logger.Tag)).OrderBy(x => x.Length).ThenBy(x => x).ToArray();
}
return fileEntries;
}
public async Task SubmitBug(String title, String steps, TeamMember createdBy, TeamMember assignedTo, Severity severity)
{
LogManager.Log("Submitting bug report...");
WorkItem item = new WorkItem();
var tempFolder = TemporaryManager.CreateFolder();
await Task.Factory.StartNew(() =>
{
item.Title = title;
item.Area = Project.GetAreaByName(_buildProvider.MachineType.ToTFSAreaName());
item.Iteration = Project.Iterations.FirstOrDefault();
item.CreatedBy = createdBy;
item.ChangedBy = createdBy;
item.AuthorizedAs = createdBy;
item.AssignedTo = assignedTo;
item.StepsToReproduce = steps;
item.FoundInBuild = _applicationManager.Version.ToString();
item.Priority = Priority.Priority3;
item.Severity = severity;
item.State = State.New;
item.Type = WorkItemType.Bug;
item.Environment = SettingsManager.Default.GetOrCreate<PPCSettings>().DeploymentSlot.ToDescription();
item.MachineType = (MachineType)_buildProvider.MachineType;
FileLogger appFileLogger = LogManager.Default.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger;
FileLogger embeddedFileLogger = MachineOperator.EmbeddedLogManager.RegisteredLoggers.FirstOrDefault(x => x.GetType() == typeof(FileLogger)) as FileLogger;
if (appFileLogger != null)
{
LogManager.Log($"Attaching application log file ${appFileLogger.LogFile}");
string[] logFiles = GetLogFiles(appFileLogger);
foreach (string file in logFiles)
{
var appLogFile = tempFolder.CreateImaginaryFile();
File.Copy(file, appLogFile.Path);
item.Attachments.Add(new Attachment()
{
Description = "Application Log File",
FilePath = appLogFile.Path,
Name = Path.GetFileName(file),
});
}
}
if (embeddedFileLogger != null && File.Exists(embeddedFileLogger.LogFile))
{
LogManager.Log($"Attaching embedded log file ${embeddedFileLogger.LogFile}");
string[] logFiles = GetLogFiles(embeddedFileLogger);
foreach (string file in logFiles)
{
var embeddedLogFile = tempFolder.CreateImaginaryFile();
File.Copy(file, embeddedLogFile.Path);
item.Attachments.Add(new Attachment()
{
Description = "Embedded Log File",
FilePath = embeddedLogFile.Path,
Name = Path.GetFileName(file),
});
}
}
//Add session log file..
if (MachineOperator.EnableSessionLogFile)
{
var file = MachineOperator.SessionLogger.LogFile;
if (file != null && File.Exists(file))
{
var sessionLogFile = tempFolder.CreateImaginaryFile();
File.Copy(file, sessionLogFile.Path);
item.Attachments.Add(new Attachment()
{
Description = "Session Log File",
FilePath = sessionLogFile.Path,
Name = Path.GetFileName(file),
});
}
}
SystemInformationModel sysModel = new SystemInformationModel();
sysModel.ApplicationVersion = _applicationManager.Version.ToString();
sysModel.EmbeddedVersion = "N/A";
sysModel.HostName = Environment.MachineName;
sysModel.UserName = createdBy.AssignName;
IMachineOperator op = _machineProvider.MachineOperator;
LogManager.Log("Checking machine status...");
if (op.Status != MachineStatuses.Disconnected)
{
LogManager.Log("Machine is connected. Getting device information and configurations...");
Machine machine = _machineProvider.Machine;
sysModel.Machine = machine;
sysModel.EmbeddedVersion = op.DeviceInformation.Version;
sysModel.ConfigurationString = machine.Configuration.Clone().ToJsonString();
if (op.CurrentProcessParameters != null)
{
sysModel.LoadedProcessParametersString = op.CurrentProcessParameters.ToJsonString(nameof(ProcessParametersTable.ProcessParametersTablesGroup));
}
if (op.CurrentHardwareConfiguration != null)
{
sysModel.LoadedHardwareConfigurationString = op.CurrentHardwareConfiguration.ToJsonString();
}
}
else
{
LogManager.Log("Machine is disconnected.");
}
if (_machineProvider.MachineOperator.DeviceInformation != null)
{
item.EmbeddedVersion = _machineProvider.MachineOperator.DeviceInformation.Version;
}
if (_machineProvider.Machine != null)
{
item.MachineSerialNumber = _machineProvider.Machine.SerialNumber;
}
String html = String.Empty;
LogManager.Log("Generating HTML system information from template...");
using (var stream = EmbeddedResourceHelper.GetEmbeddedResourceStream("Tango.PPC.BugReporting.TFS.SystemInformationTemplate.cshtml"))
{
StreamReader reader = new StreamReader(stream);
html = reader.ReadToEnd();
}
item.SystemInformation = CodeGeneration.Helper.Parse(html, sysModel);
item.StepsToReproduce = String.Format("<div style=\"white-space:pre\">{0}</div>", item.StepsToReproduce);
});
LogManager.Log("Uploading work item...");
await UploadWorkItem(Project, item);
LogManager.Log("Deleting temporary folder...");
await tempFolder.DeleteAsync();
LogManager.Log("Upload completed.");
}
}
}
|