Example #1
0
        public TFSBug CreateBug(SNDevelopmentItem developmentItem, string teamProject)
        {
            TFSBug tfsBug = 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.SNDefect",
                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, "Bug").Result;

                    if (newWorkItem != null)
                    {
                        tfsBug = GetBugFromSNDevItem(developmentItem);
                    }
                }
            }
            return(tfsBug);
        }
        private bool UpdateDefect(TFSBug backlogItem)
        {
            bool retValue             = false;
            SNDevelopmentItem devItem = new SNDevelopmentItem()
            {
                Number   = backlogItem.ServiceNowDefect,
                SystemId = backlogItem.ServiceNowInternalId,
                External_Reference_Id = string.Format("BUG{0}", backlogItem.ID)
            };

            try
            {
                var    devItem2        = snClient.GetDefect(devItem.SystemId);
                string snState         = Settings.Instance.TfsWIStateMapping.GetValueOrDefault(backlogItem.State);
                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.SnDefectStateOrder.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"))
                    {
                        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.UpdateDefect(devItem);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return(retValue);
        }
Example #3
0
        public TFSBug UpdateBug(SNDevelopmentItem developmentItem, TFSBug tFSBug)
        {
            // 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     = tFSBug.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, tFSBug.ID).Result;
                if (UpdatedWorkItem != null)
                {
                    tFSBug = GetBugFromSNDevItem(developmentItem);
                }
            }
            return(tFSBug);
        }
        public TFSBug InsertOrUpdateSingleBug(SNDevelopmentItem developmentItem)
        {
            TFSBug tfsBug    = null;
            TFSBug curtfsBug = tfsClient.GetBugFromSNDevItem(developmentItem);

            try
            {
                if (curtfsBug != null)
                {
                    //Update Bug in TFS
                    try
                    {
                        tfsBug = curtfsBug;
                        string tfsState = Settings.Instance.SnDefectStateMapping.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)
                            {
                                tfsBug.State = tfsState;
                            }
                        }
                        if (curtfsBug.Title != developmentItem.Short_Description)
                        {
                            tfsBug.Title = developmentItem.Short_Description;
                        }
                        if ((tfsBug.Title != curtfsBug.Title) || (tfsBug.State != curtfsBug.State))
                        {
                            tfsClient.UpdateBug(developmentItem, tfsBug);
                            nbBugUpdated++;
                            tfsBug = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                    }
                }
                else
                {
                    //Create Bug 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.SnDefectStateMapping.GetValueOrDefault(developmentItem.State);
                        if ((tfsState == "New") && (!string.IsNullOrEmpty(teamProject)))
                        {
                            tfsBug = tfsClient.CreateBug(developmentItem, teamProject);
                            nbBugCreated++;
                        }
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return(tfsBug);
        }
Example #5
0
        public TFSBug GetBugFromSNDevItem(SNDevelopmentItem developmentItem)
        {
            TFSBug tfsBug = 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] = 'Bug' " +
                            "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.SNDefect";
                        fields[4] = "FET.SNInternalId";

                        var workItem = workItemTrackingHttpClient.GetWorkItemAsync(wiID, fields, workItemQueryResult.AsOf).Result;

                        if (workItem != null)
                        {
                            tfsBug = new TFSBug()
                            {
                                ID                   = workItem.Id.Value,
                                Title                = (string)workItem.Fields[fields[1]],
                                State                = (string)workItem.Fields[fields[2]],
                                ServiceNowDefect     = (string)workItem.Fields[fields[3]],
                                ServiceNowInternalId = (string)workItem.Fields[fields[4]]
                            };
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return(tfsBug);
        }