Beispiel #1
0
        public async Task <IActionResult> UpdateWorkItem([FromBody] WorkTask wt)
        {
            CurrentUser cUser = new CurrentUser(HttpContext, _configuration);

            var uriLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_Uri");

            Uri accountUri = new Uri(uriLookup.Lvalue);

            var tokenLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_AccessToken");

            String personalAccessToken = tokenLookup.Lvalue;

            var projectLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_Project");

            wt.Project = projectLookup.Lvalue;

            // Create a connection to the account
            VssConnection connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));

            // Get an instance of the work item tracking client
            WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>();

            JsonPatchDocument patchDocument = new JsonPatchDocument
            {
                new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.History",
                    Value     = wt.Comment + "<p>By : " + cUser.Email + "</p>"
                }
            };

            if (wt.Priority != string.Empty && wt.Priority != null)
            {
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Replace,
                    Path      = "/fields/Microsoft.VSTS.Common.Priority",
                    Value     = wt.Priority
                });
            }

            try
            {
                if (wt.ScreenShot != string.Empty && wt.ScreenShot != null)
                {
                    string convert = wt.ScreenShot.Replace("data:image/png;base64,", String.Empty);
                    var    stream  = new MemoryStream(Convert.FromBase64String(convert));
                    AttachmentReference attachment = witClient.CreateAttachmentAsync(stream, wt.Project, "ScreenShot.jpg", null, null, null).Result;

                    patchDocument.Add(new JsonPatchOperation()
                    {
                        Operation = Operation.Add,
                        Path      = "/relations/-",
                        Value     = new
                        {
                            rel        = "AttachedFile",
                            url        = attachment.Url,
                            attributes = new { comment = "Emaintenance Attachment by " + cUser.Email }
                        }
                    });
                }
                WorkItem result = witClient.UpdateWorkItemAsync(patchDocument, Int32.Parse(wt.Id)).Result;
                return(Ok(result));
            }
            catch (AggregateException ex)
            {
                throw ex;
            }
        }
Beispiel #2
0
        public async Task <IActionResult> CreateWorkItem([FromBody] WorkTask wt)
        {
            CurrentUser cUser = new CurrentUser(HttpContext, _configuration);

            var uriLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_Uri");

            Uri accountUri = new Uri(uriLookup.Lvalue);

            var tokenLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_AccessToken");

            String personalAccessToken = tokenLookup.Lvalue;

            var projectLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_Project");

            wt.Project = projectLookup.Lvalue;

            // Create a connection to the account
            VssConnection connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken));

            // Get an instance of the work item tracking client
            WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>();

            JsonPatchDocument patchDocument = new JsonPatchDocument
            {
                //add fields and their values to your patch document
                new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.Title",
                    Value     = wt.Title
                },
                new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = (wt.Type == "Bug" ? "/fields/Microsoft.VSTS.TCM.ReproSteps" : "/fields/System.Description"),
                    Value     = wt.Description
                }
            };

            var iterationLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_IterationPath");

            if (iterationLookup != null)
            {
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.IterationPath",
                    Value     = iterationLookup.Lvalue
                });
            }

            var parentRelationLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_ParentRelation");

            if (parentRelationLookup != null)
            {
                int      id       = Int32.Parse(parentRelationLookup.Lvalue);
                WorkItem workitem = witClient.GetWorkItemAsync(id).Result;

                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/relations/-",
                    Value     = new
                    {
                        rel = "System.LinkTypes.Hierarchy-Reverse",
                        url = workitem.Url
                              //url = "https://dev.azure.com/inser13/SKF-Emaintenance/_apis/wit/workItems/2308"
                    }
                });
            }

            var areaPathLookup = await lookupRepo.GetLookupConfigByName(0, "Y", "DevOps_AreaPath");

            if (areaPathLookup != null)
            {
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.AreaPath",
                    Value     = areaPathLookup.Lvalue
                });
            }

            if (wt.Priority != string.Empty && wt.Priority != null)
            {
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/Microsoft.VSTS.Common.Priority",
                    Value     = wt.Priority
                });
            }

            patchDocument.Add(new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path      = "/fields/System.Tags",
                Value     = wt.Tags + "," + cUser.Email
            });

            //if (wt.Comment != string.Empty && wt.Comment != null)
            //{
            //    patchDocument.Add(new JsonPatchOperation()
            //    {
            //        Operation = Operation.Add,
            //        Path = "/fields/System.History",
            //        Value = wt.Comment + "<p>By :" + cUser.Email + "</p>"
            //    });
            //}

            if (wt.DeviceInfo != string.Empty && wt.DeviceInfo != null)
            {
                string Comment = null;
                if (wt.Comment != string.Empty && wt.Comment != null)
                {
                    Comment = wt.Comment + "<p>By :" + cUser.Email + "</p>";
                }
                Comment = Comment + "</p>" + wt.DeviceInfo;
                patchDocument.Add(new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path      = "/fields/System.History",
                    Value     = Comment
                });
            }

            try
            {
                if (wt.ScreenShot != string.Empty && wt.ScreenShot != null)
                {
                    string convert = wt.ScreenShot.Replace("data:image/png;base64,", String.Empty);
                    var    stream  = new MemoryStream(Convert.FromBase64String(convert));
                    AttachmentReference attachment = witClient.CreateAttachmentAsync(stream, wt.Project, "ScreenShot.jpg", null, null, null).Result;

                    patchDocument.Add(new JsonPatchOperation()
                    {
                        Operation = Operation.Add,
                        Path      = "/relations/-",
                        Value     = new
                        {
                            rel        = "AttachedFile",
                            url        = attachment.Url,
                            attributes = new { comment = "Emaintenance Attachment by " + cUser.Email }
                        }
                    });
                }
                WorkItem result = witClient.CreateWorkItemAsync(patchDocument, wt.Project, wt.Type).Result;
                return(Ok(result));
            }
            catch (AggregateException ex)
            {
                throw ex;
            }
        }