Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parentId">this work item id</param>
        /// <param name="childIds">Return all the work item ids with child relation to this item</param>
        public void GetChildWorkItemByParentWorkItemId(int parentId, ref List <int> childIds)
        {
            WorkItemTrackingHttpClient witClient = this.Connection.GetClient <WorkItemTrackingHttpClient>();
            WorkItem parentWorkItem = null;

            try
            {
                parentWorkItem = witClient.GetWorkItemAsync(parentId, expand: WorkItemExpand.Relations).Result;
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("The work item id # {0} does not exist. Error - {1}", parentId, ex.InnerException.Message);
                WriteLog.WriteLogLine("The work item id # {0} does not exist. Error - {1}", parentId, ex.InnerException.Message);
            }

            if (parentWorkItem != null && parentWorkItem.Relations != null)
            {
                foreach (var relation in parentWorkItem.Relations)
                {
                    //get the child links
                    if (relation.Rel == "System.LinkTypes.Hierarchy-Forward")
                    {
                        var lastIndex = relation.Url.LastIndexOf("/");
                        var itemId    = relation.Url.Substring(lastIndex + 1);
                        childIds.Add(Convert.ToInt32(itemId));

                        GetChildWorkItemByParentWorkItemId(Convert.ToInt32(itemId), ref childIds);
                    }
                    ;
                }
            }
        }
Пример #2
0
        private int ReadCommonMetadataInformation()
        {
            if (!File.Exists(_metadataFile))
            {
                Console.WriteLine("Failed to read metadata file - {0}", _metadataFile);
                WriteLog.WriteLogLine("Failed to read metadata file - {0}", _metadataFile);
                return(0);
            }
            using (StreamReader reader = new StreamReader(_metadataFile))
                using (CsvReader csvReader = new CsvReader(reader))
                {
                    csvReader.Configuration.Delimiter       = "::";
                    csvReader.Configuration.HasHeaderRecord = false;

                    _witFieldRecords = new List <WITFieldEntity>();
                    while (csvReader.Read())
                    {
                        var record = new WITFieldEntity
                        {
                            WorkItemType = csvReader.GetField <string>(0),
                            FieldName    = csvReader.GetField <string>(1),
                            FieldValue   = csvReader.GetField <string>(2)
                        };

                        _witFieldRecords.Add(record);
                    }

                    return(1);
                }
        }
Пример #3
0
        public WorkItem GetWITByID(int WIId)
        {
            WorkItemTrackingHttpClient witHttpClient = this.Connection.GetClient <WorkItemTrackingHttpClient>();

            try
            {
                WorkItem result = witHttpClient.GetWorkItemAsync(WIId, expand: WorkItemExpand.Links | WorkItemExpand.Relations).Result;
                return(result);
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("The work item id # {0} does not exist. Error - {1}", WIId, ex.InnerException.Message);
                WriteLog.WriteLogLine("The work item id # {0} does not exist. Error - {1}", WIId, ex.InnerException.Message);
                return(null);
            }
        }
Пример #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sourceWIId">Parent work item</param>
        /// <param name="targetWIId">Child work item</param>
        /// <returns></returns>
        public WorkItem LinkToOtherWorkItem(int sourceWIId, int targetWIId)
        {
            WorkItemTrackingHttpClient workItemTrackingClient = this.Connection.GetClient <WorkItemTrackingHttpClient>();

            // Get work target work item
            WorkItem targetWorkItem = workItemTrackingClient.GetWorkItemAsync(targetWIId).Result;

            if (targetWorkItem == null)
            {
                Console.WriteLine("Tried to link two work items, but target work item # {0} does not exist.", targetWIId);
                WriteLog.WriteLogLine("Tried to link two work items, but target work item # {0} does not exist.", targetWIId);
                return(null);
            }
            JsonPatchDocument patchDocument = new JsonPatchDocument();

            patchDocument.Add(
                new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/relations/-",
                Value     = new
                {
                    rel        = "System.LinkTypes.Hierarchy-Forward",
                    url        = targetWorkItem.Url,
                    attributes = new
                    {
                        comment = "Making a new link for the dependency"
                    }
                }
            }
                );
            try
            {
                WorkItem result = workItemTrackingClient.UpdateWorkItemAsync(patchDocument, sourceWIId).Result;
                Console.WriteLine("Successfully linked source item # {0} to target item # {1}.", sourceWIId, targetWIId);
                WriteLog.WriteLogLine("Successfully linked source item # {0} to target item # {1}.", sourceWIId, targetWIId);
                return(result);
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("Failed to link source item # {0} to target item # {1}.", sourceWIId, targetWIId);
                WriteLog.WriteLogLine("Failed to link source item # {0} to target item # {1}.", sourceWIId, targetWIId);
                return(null);
            }
        }
Пример #5
0
        /// <summary>
        /// Bypass rules incase there are required fields are not provided
        /// </summary>
        /// <param name="document">/ Construct the object containing field values required for updating work item</param>
        /// <param name="WIId">The specific work item Id need to update</param>
        /// <returns></returns>
        public WorkItem UpdateWIT(JsonPatchDocument document, int WIId)
        {
            WorkItemTrackingHttpClient witHttpClient = this.Connection.GetClient <WorkItemTrackingHttpClient>();

            try
            {
                WorkItem result = witHttpClient.UpdateWorkItemAsync(document, WIId, bypassRules: true).Result;
                Console.WriteLine("Successfully Updated Work Item: # {0}", result.Id);
                WriteLog.WriteLogLine("Successfully Updated Work Item: # {0}", result.Id);
                return(result);
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("Failed to Update Work Item: # {0} - Error Message: {1}", WIId, ex.InnerException.Message);
                WriteLog.WriteLogLine("Failed to Update Work Item: # {0} - Error Message: {1}", WIId, ex.InnerException.Message);
                return(null);
            }
        }
Пример #6
0
        // WIT Operations
        /// <summary>
        /// Bypass rules incase there are required fields are not provided
        /// </summary>
        /// <param name="document">// Construct the object containing field values required for the new work item</param>
        /// <param name="WITypeName">Work Item Type Name: Learning Path / Module / Unit</param>
        /// <returns></returns>
        public WorkItem CreateWIT(JsonPatchDocument document, string WITypeName)
        {
            WorkItemTrackingHttpClient witHttpClient = this.Connection.GetClient <WorkItemTrackingHttpClient>();

            try
            {
                WorkItem result = witHttpClient.CreateWorkItemAsync(document, this.Project, WITypeName, bypassRules: true).Result;
                Console.WriteLine("{0} Successfully Created: # {1}", WITypeName, result.Id);
                WriteLog.WriteLogLine("{0} Successfully Created: # {1}", WITypeName, result.Id);
                return(result);
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("Error creating {0}: {1}", WITypeName, ex.InnerException.Message);
                WriteLog.WriteLogLine("Error creating {0}: {1}", WITypeName, ex.InnerException.Message);
                return(null);
            }
        }
Пример #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="WIId">The specific work item Id need to delete</param>
        /// <returns></returns>
        public WorkItemDelete DeleteWITByID(int WIId)
        {
            WorkItemTrackingHttpClient witHttpClient = this.Connection.GetClient <WorkItemTrackingHttpClient>();

            try
            {
                WorkItemDelete result = witHttpClient.DeleteWorkItemAsync(WIId, destroy: false).Result;
                Console.WriteLine("Successfully Deleted Work Item: # {0}", result.Id);
                WriteLog.WriteLogLine("Successfully Deleted Work Item: # {0}", result.Id);
                return(result);
            }
            catch (AggregateException ex)
            {
                Console.WriteLine("Failed to delete Work Item: # {0} - Error Message: {1}", WIId, ex.InnerException.Message);
                WriteLog.WriteLogLine("Failed to delete Work Item: # {0} - Error Message: {1}", WIId, ex.InnerException.Message);
                return(null);
            }
        }
Пример #8
0
        private int ReadLearningPathInformation()
        {
            string csvDelimiter = ",";

            if (!File.Exists(_learningPathFile))
            {
                Console.WriteLine("Failed to read input file - {0}", _learningPathFile);
                WriteLog.WriteLogLine("Failed to read input file - {0}", _learningPathFile);
                return(0);
            }

            _operation = new OperationInformation();

            using (StreamReader reader = new StreamReader(_learningPathFile))
            {
                string csvContent = reader.ReadToEnd();
                if (!string.IsNullOrEmpty(csvContent))
                {
                    string[] csvLines   = csvContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    string[] parameters = csvLines[0].Split(new string[] { csvDelimiter }, StringSplitOptions.RemoveEmptyEntries);
                    if (parameters.Length > 0)
                    {
                        string operation = parameters[0];

                        switch (operation.Trim().ToLower())
                        {
                        case "a":
                        case "add":
                        {
                            int colCount = parameters.Length;
                            if (colCount == 2)
                            {
                                // Add a single Learning Path only
                                _operation.Operation = OperationType.Add;
                                _operation.LearningPathDisplayName = parameters[1];
                                _operation.LearningPathModulesInfo = null;
                            }
                            else if (colCount >= 3)
                            {
                                int  moduleCount             = 0;
                                bool parseModuleCountSuccess = Int32.TryParse(parameters[2], out moduleCount);
                                if (!parseModuleCountSuccess)
                                {
                                    Console.WriteLine("Invalid input file - {0}, the module count(third column) for 'Add' should be an integer.", _learningPathFile);
                                    WriteLog.WriteLogLine("Invalid input file - {0}, the module count(third column) for 'Add' should be an integer.", _learningPathFile);
                                    return(0);
                                }
                                if (colCount == 3)
                                {
                                    // Add a learning path with modules only
                                    _operation.Operation = OperationType.Add;
                                    _operation.LearningPathDisplayName = parameters[1];
                                    _operation.LearningPathModulesInfo = new int[moduleCount][];

                                    for (int i = 0; i < moduleCount; i++)
                                    {
                                        _operation.LearningPathModulesInfo[i] = null;
                                    }
                                }
                                else
                                {
                                    // Add a learning path with modules, with units
                                    if (moduleCount + 3 == colCount)
                                    {
                                        bool passValidation = true;
                                        for (int i = 0; i < moduleCount; i++)
                                        {
                                            // Match 1.20
                                            string pattern = @"\d*[.]\d*";
                                            bool   isMatch = Regex.IsMatch(parameters[i + 3], pattern);
                                            if (!isMatch)
                                            {
                                                passValidation = false;
                                                Console.WriteLine("Invalid input file - {0}, parameter {1} does not match parttern {digits}.{digits}.", _learningPathFile, parameters[i + 3]);
                                                WriteLog.WriteLogLine("Invalid input file - {0}, parameter {1} does not match parttern {digits}.{digits}.", _learningPathFile, parameters[i + 3]);
                                                return(0);
                                            }
                                        }
                                        if (passValidation)
                                        {
                                            _operation.Operation = OperationType.Add;
                                            _operation.LearningPathDisplayName = parameters[1];
                                            _operation.LearningPathModulesInfo = new int[moduleCount][];
                                            for (int i = 0; i < moduleCount; i++)
                                            {
                                                string[] digits = parameters[i + 3].Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
                                                _operation.LearningPathModulesInfo[i] = new int[Int32.Parse(digits[1])];
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, at least 2 input values are required for 'Add'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, at least 2 input values are required for 'Add'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        case "u":
                        case "update":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 3)
                            {
                                _operation.Operation = OperationType.Update;
                                //_operation.IsCascadeUpdating = false;
                                _operation.SetValue <bool>("$isCascadeUpdating", false);
                                var updateWorkItems   = new List <int>();
                                var isCascadingUpdate = parameters[1].Trim().ToLower();
                                if (isCascadingUpdate == "false" || isCascadingUpdate == "true")
                                {
                                    //_operation.IsCascadeUpdating = Boolean.Parse(isCascadingUpdate);
                                    _operation.SetValue <bool>("$isCascadeUpdating", Boolean.Parse(isCascadingUpdate));
                                }

                                for (int i = 2; i < colCount; i++)
                                {
                                    int theId = 0;
                                    Int32.TryParse(parameters[i], out theId);

                                    //add to list
                                    if (theId != 0)
                                    {
                                        updateWorkItems.Add(theId);
                                    }
                                }
                                _operation.SetValue <List <int> >("$updateWorkItemIds", updateWorkItems);
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, at least 3 input values are required for 'Update'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, at least 3 input values are required for 'Update'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        case "d":
                        case "delete":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 3)
                            {
                                _operation.Operation = OperationType.Delete;
                                _operation.SetValue <bool>("$isCascadeDeleting", false);
                                //_operation.IsCascadeDeleting = false;
                                var deleteWorkItems   = new List <int>();
                                var isCascadingDelete = parameters[1].Trim().ToLower();
                                if (isCascadingDelete == "false" || isCascadingDelete == "true")
                                {
                                    //_operation.IsCascadeDeleting = Boolean.Parse(isCascadingDelete);
                                    _operation.SetValue <bool>("$isCascadeDeleting", Boolean.Parse(isCascadingDelete));
                                }
                                for (int i = 2; i < colCount; i++)
                                {
                                    int theId = 0;
                                    Int32.TryParse(parameters[i], out theId);

                                    //add to list
                                    if (theId != 0)
                                    {
                                        deleteWorkItems.Add(theId);
                                    }
                                }

                                _operation.SetValue <List <int> >("$deleteWorkItemIds", deleteWorkItems);
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, at least 3 input values are required for 'Delete'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, at least 3 input values are required for 'Delete'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        case "q":
                        case "query":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 2)
                            {
                                List <WITFieldEntity> clauses = new List <WITFieldEntity>();
                                // retrieve the query clause
                                for (int i = 1; i < colCount; i++)
                                {
                                    string    text     = parameters[i];
                                    string [] keyValue = text.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                                    if (keyValue.Length == 2)
                                    {
                                        WITFieldEntity entity = new WITFieldEntity()
                                        {
                                            FieldName  = keyValue[0].Trim(),
                                            FieldValue = keyValue[1].Trim()
                                        };
                                        clauses.Add(entity);
                                    }
                                }
                                _operation.Operation = OperationType.Query;
                                _operation.SetValue <List <WITFieldEntity> >("$QueryClauses", clauses);
                            }
                        }; break;

                        case "qu":
                        case "queryupdate":
                        {
                            int colCount = parameters.Length;
                            if (colCount >= 2)
                            {
                                List <WITFieldEntity> clauses = new List <WITFieldEntity>();
                                // retrieve the query clause
                                for (int i = 1; i < colCount; i++)
                                {
                                    string   text     = parameters[i];
                                    string[] keyValue = text.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                                    if (keyValue.Length == 2)
                                    {
                                        WITFieldEntity entity = new WITFieldEntity()
                                        {
                                            FieldName  = keyValue[0].Trim(),
                                            FieldValue = keyValue[1].Trim()
                                        };
                                        clauses.Add(entity);
                                    }
                                }
                                _operation.Operation = OperationType.QueryUpdate;
                                _operation.SetValue <List <WITFieldEntity> >("$QueryClauses", clauses);
                            }
                        }; break;

                        case "g":
                        case "get":
                        {
                            int colCount = parameters.Length;
                            if (colCount == 2)
                            {
                                int theId = 0;
                                Int32.TryParse(parameters[1], out theId);

                                if (theId != 0)
                                {
                                    _operation.Operation = OperationType.Get;
                                    //_operation.WorkItemIdToDisplay = theId;
                                    _operation.SetValue <int>("$workItemId", theId);
                                }
                                else
                                {
                                    Console.WriteLine("Invalid input file - {0}, second paremeter should be integer for 'Get'.", _learningPathFile);
                                    WriteLog.WriteLogLine("Invalid input file - {0}, second paremeter should be integer for 'Get'.", _learningPathFile);
                                    return(0);
                                }
                            }
                            else
                            {
                                Console.WriteLine("Invalid input file - {0}, only 2 input values are required for 'Get'.", _learningPathFile);
                                WriteLog.WriteLogLine("Invalid input file - {0}, only 2 input values are required for 'Get'.", _learningPathFile);
                                return(0);
                            }
                        }; break;

                        default:
                        {
                            Console.WriteLine("Invalid input file - {0}, operation '{1}' is not support yet.", _learningPathFile, operation);
                            WriteLog.WriteLogLine("Invalid input file - {0}, operation '{1}' is not support yet.", _learningPathFile, operation);
                            return(0);
                        };
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Invalid input file - {0}, content is empty.", _learningPathFile);
                    WriteLog.WriteLogLine("Invalid input file - {0}, content is empty.", _learningPathFile);
                    return(0);
                }
            }

            return(0);
        }