//private void ConnectToTfs(ExceptionEntity exception, IApplicationInfo applicationInfo) //{ // //new NetworkCredential(@"os-lab\oslabadmin", "Y67uJi)9"); // var credentials = CredentialCache.DefaultNetworkCredentials;//new NetworkCredential(@"partner\xNataliaA", "xNataliaA62");// // tfs = new TfsTeamProjectCollection(new Uri(applicationInfo.TfsServer + "/" + applicationInfo.TeamProject), credentials); // tfs.EnsureAuthenticated(); // if (!tfs.HasAuthenticated) // { // throw new ExceptionReporterException("Failed to authenticate to TFS."); // } //} //private WorkItemType GetWorkItemType(ExceptionEntity exception, IApplicationInfo applicationInfo) //{ // var store = tfs.GetService<WorkItemStore>(); // var teamProject = store.Projects[applicationInfo.TeamProject]; // return teamProject.WorkItemTypes[ExceptionWorkItemType]; //} //private void AddLinkedWorkItem(WorkItem linkedWi, WorkItem wi) //{ // try // { // var rl = new RelatedLink(linkedWi.Id); // wi.Links.Add(rl); // } // catch (System.Exception ex) // { // ServiceLog.Warning( // $"Failed to link work item {linkedWi.Id} while create new exception with stackTrace CRC {wi.Fields[StackChecksumFieldName].Value}" + ex.Message); // } //} //protected virtual void Dispose(bool disposing) //{ // if (disposing) // { // // dispose managed resources // tfs?.Dispose(); // } // // free native resources //} //public void Dispose() //{ // Dispose(true); // GC.SuppressFinalize(this); //} public ExceptionEntity GetWorkItem(ExceptionEntity exceptionEntity) { var workItems = new ExceptionWorkItemCollection(exceptionEntity, this.ApplicationInfo); var wi = workItems.OpenWorkItems.FirstOrDefault(); if (wi == null) { throw new System.Exception("Failed to find Exception Work item same stack trace"); } // wi.Fields[AssignedToFieldName].Value = applicationInfo.AssignedTo; return(new ExceptionEntity() { ApplicationName = wi.Field(Application), Reporter = wi.Field(ExceptionReporterFieldName), Version = wi.Field(BuildVersionFieldName), ExceptionMessage = wi.Field(ExceptionMessageFieldName), ExceptionType = wi.Field(ExceptionTypeFieldName), ExceptionClass = wi.Field(ClassFieldName), ExceptionMethod = wi.Field(MethodFieldName), ExceptionSource = wi.Field(SourceFieldName), StackTrace = wi.Field(StackTraceFieldName), Comment = wi.Field(CommentFieldName), ExceptionTitle = wi.Field("System.Title") }); }
public void RegisterException(ExceptionEntity exceptionEntity) { WorkItem wi = null; WorkItem linkedWi = null; var workItems = new ExceptionWorkItemCollection(exceptionEntity, this.ApplicationInfo); if (!workItems.HasOpenWorkItems) { if (workItems.HasWorkItemsWithHigherChangeset) { wi = workItems.GetWorkItemWithHigherChangeset(); // If not Open and new exception has higher version, we should create new wi with link to the old one if (HasHigherVersion(exceptionEntity.Version, wi)) { linkedWi = wi; wi = null; } } else { wi = workItems.GetLatestNotOpenWorkItem(); } } else { wi = workItems.OpenWorkItems.FirstOrDefault(); if (wi != null && HasLowerVersion(exceptionEntity.Version, wi)) { ServiceLog.Information( $"Exception with newer version already exists with ID={wi.Id}. Coming exception will be dropped."); return; } } if (wi == null) { var json = CreateNewException(exceptionEntity, this.ApplicationInfo); SendException(json.Json, ExceptionWorkItemType); return; } else { wi = UpdateExisitingWorkItem(wi, exceptionEntity); } if (wi == null) { return; } var errors = wi.Validate(); if (!errors.Any()) { ServiceLog.Error($"Could not create a workitem but no errors found in it {wi.Id}"); } else { var errorString = new StringBuilder(); foreach (var f in errors) { errorString.AppendLine($"Error: {f}"); } ServiceLog.Warning("Workitem failed validation: " + errorString); //LARS: should throw exception when exception is not valid: throw new ArgumentException("TFSException contains invalid fields: " + errorString); } }