public TFSProductBacklogItem CreateProductBacklogItem(SNDevelopmentItem developmentItem, string teamProject) { TFSProductBacklogItem tfsPBI = null; // Construct the object containing field values required for the new work item JsonPatchDocument patchDocument = new JsonPatchDocument(); patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.Title", Value = developmentItem.Short_Description == null? "No description in ServiceNow" : developmentItem.Short_Description }); patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/FET.SNEnhancement", Value = developmentItem.Number } ); patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/FET.SNInternalId", Value = developmentItem.SystemId } ); string serverUrl = (string)configReader.GetValue("TFSServerUrl", typeof(string)); //Initialise the connection to the TFS server VssConnection connection = new VssConnection(new Uri(serverUrl), new VssCredentials(new WindowsCredential(true))); using (WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>()) { // Get the project to create the work item in TeamProjectReference projectReference = FindProject(connection, teamProject); if (projectReference != null) { // Create the new work item WorkItem newWorkItem = workItemTrackingClient.CreateWorkItemAsync(patchDocument, projectReference.Id, "Product Backlog Item").Result; if (newWorkItem != null) { tfsPBI = GetPBIFromSNDevItem(developmentItem); } } } return(tfsPBI); }
private bool UpdateEnhancement(TFSProductBacklogItem backlogItem) { bool retValue = false; SNDevelopmentItem devItem = new SNDevelopmentItem() { Number = backlogItem.ServiceNowEnhancement, SystemId = backlogItem.ServiceNowInternalId, External_Reference_Id = string.Format("PBI{0}", backlogItem.ID), Short_Description = backlogItem.Title }; try { var devItem2 = snClient.GetEnhancement(devItem.SystemId); string snState = Settings.Instance.TfsWIStateMapping.GetValueOrDefault(backlogItem.State); string snAssignmentGroup = devItem2.Assignment_Group; string assignmentGroup = Settings.Instance.TfsWIStateAssignmentGroup.GetValueOrDefault(backlogItem.State); if (!string.IsNullOrEmpty(snState)) { // Do not modify SN state if TFS state order is lower than ServiceNow state order int tfsStateOrder = Settings.Instance.TfsStateOrder.GetValueOrDefault(backlogItem.State, 0); int snStateOrder = Settings.Instance.SnEnhancementStateOrder.GetValueOrDefault(devItem2.State, 0); if (snStateOrder <= tfsStateOrder) { devItem.State = snState; } // Only modify the assignment group if the state is "Work in Progress" if (!string.IsNullOrEmpty(assignmentGroup) && (snState == "Work in Progress")) { if ((snAssignmentGroup == "Navision Development") || (snAssignmentGroup == "Navision Support")) { devItem.Assignment_Group = assignmentGroup; } } if ((devItem.State != devItem2.State) || (devItem.Assignment_Group != devItem2.Assignment_Group) || (devItem.External_Reference_Id != devItem2.External_Reference_Id)) { retValue = snClient.UpdateEnhancement(devItem); } } } catch (Exception ex) { logger.Error(ex); } return(retValue); }
private TFSProductBacklogItem InsertOrUpdateSinglePBI(SNDevelopmentItem developmentItem) { TFSProductBacklogItem curtfsPBI = tfsClient.GetPBIFromSNDevItem(developmentItem); TFSProductBacklogItem tfsPBI = null; try { if (curtfsPBI != null) { //Update PBI in TFS try { tfsPBI = curtfsPBI; string tfsState = Settings.Instance.SnEnhancementStateMapping.GetValueOrDefault(developmentItem.State); if (!string.IsNullOrEmpty(tfsState)) { int tfsStateOrder = Settings.Instance.TfsStateOrder.GetValueOrDefault(tfsState, 0); int snStateOrder = Settings.Instance.SnEnhancementStateOrder.GetValueOrDefault(developmentItem.State, 0); if (snStateOrder > tfsStateOrder) { tfsPBI.State = tfsState; } } if (curtfsPBI.Title != developmentItem.Short_Description) { tfsPBI.Title = developmentItem.Short_Description; } if ((tfsPBI.Title != curtfsPBI.Title) || (tfsPBI.State != curtfsPBI.State)) { tfsClient.UpdateProductBacklogItem(developmentItem, tfsPBI); nbPBIUpdated++; tfsPBI = null; } } catch (Exception ex) { logger.Error(ex); } } else { //Create PBI in TFS try { // The project come from the mapping between Assignment Group and team project in Settings file string assginmentGroup = developmentItem.Assignment_Group == null?string.Empty: developmentItem.Assignment_Group; string teamProject = Settings.Instance.SnAssignGroupMapping.GetValueOrDefault(assginmentGroup); string tfsState = Settings.Instance.SnEnhancementStateMapping.GetValueOrDefault(developmentItem.State); if ((tfsState == "New") && (!string.IsNullOrEmpty(teamProject))) { tfsPBI = tfsClient.CreateProductBacklogItem(developmentItem, teamProject); nbPBICreated++; } } catch (Exception ex) { logger.Error(ex); } } } catch (Exception ex) { logger.Error(ex); } return(tfsPBI); }
public TFSProductBacklogItem GetPBIFromSNDevItem(SNDevelopmentItem developmentItem) { TFSProductBacklogItem tfsPBI = null; try { //create a wiql object and build our query Wiql wiql = new Wiql() { Query = "Select [ID],[Title],[ServiceNow InternalId] " + "From WorkItems " + "Where [Work Item Type] = 'Product Backlog Item' " + "And [ServiceNow InternalId] = '" + developmentItem.SystemId + "' " }; string serverUrl = (string)configReader.GetValue("TFSServerUrl", typeof(string)); //Initialise the connection to the TFS server VssConnection connection = new VssConnection(new Uri(serverUrl), new VssCredentials(new WindowsCredential(true))); //create instance of work item tracking http client using (WorkItemTrackingHttpClient workItemTrackingHttpClient = connection.GetClient <WorkItemTrackingHttpClient>()) { //execute the query to get the list of work items in teh results WorkItemQueryResult workItemQueryResult = workItemTrackingHttpClient.QueryByWiqlAsync(wiql).Result; //Some error handling if (workItemQueryResult.WorkItems.Count() != 0) { //need to get the work item Id for the GET request int wiID = workItemQueryResult.WorkItems.First().Id; //build a list of the fields we want to see string[] fields = new string[5]; fields[0] = "System.Id"; fields[1] = "System.Title"; fields[2] = "System.State"; fields[3] = "FET.SNEnhancement"; fields[4] = "FET.SNInternalId"; var workItem = workItemTrackingHttpClient.GetWorkItemAsync(wiID, fields, workItemQueryResult.AsOf).Result; if (workItem != null) { tfsPBI = new TFSProductBacklogItem() { ID = workItem.Id.Value, Title = (string)workItem.Fields[fields[1]], State = (string)workItem.Fields[fields[2]], ServiceNowEnhancement = (string)workItem.Fields[fields[3]], ServiceNowInternalId = (string)workItem.Fields[fields[4]] }; } } } } catch (Exception ex) { logger.Error(ex); } return(tfsPBI); }
public TFSProductBacklogItem UpdateProductBacklogItem(SNDevelopmentItem developmentItem, TFSProductBacklogItem tfsPBI) { // Construct the object containing field values required for the new work item JsonPatchDocument patchDocument = new JsonPatchDocument(); patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.Title", Value = developmentItem.Short_Description == null ? "No description in ServiceNow" : developmentItem.Short_Description }); patchDocument.Add( new JsonPatchOperation() { Operation = Operation.Add, Path = "/fields/System.State", Value = tfsPBI.State } ); string serverUrl = (string)configReader.GetValue("TFSServerUrl", typeof(string)); //Initialise the connection to the TFS server VssConnection connection = new VssConnection(new Uri(serverUrl), new VssCredentials(new WindowsCredential(true))); using (WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>()) { // Create the new work item WorkItem UpdatedWorkItem = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, tfsPBI.ID).Result; if (UpdatedWorkItem != null) { tfsPBI = GetPBIFromSNDevItem(developmentItem); } } return(tfsPBI); }