public void CreatePatchTest()
        {
            var original = JsonConvert.DeserializeObject <WorkItem>(TestResources.JsonPatchTestSideA);
            var update   = JsonConvert.DeserializeObject <WorkItem>(TestResources.JsonPatchSideB);

            var patchDoc = JsonHelpers.CreatePatch(original, update);

            Assert.AreEqual("5.9166", patchDoc.First(p => p.Operation == Operation.Replace && p.Path == "/fields/Microsoft.VSTS.Scheduling.RemainingWork").Value.ToString());
            Assert.AreEqual("0.0833", patchDoc.First(p => p.Operation == Operation.Replace && p.Path == "/fields/Microsoft.VSTS.Scheduling.CompletedWork").Value.ToString());
        }
Example #2
0
        /// <summary>
        /// When overridden in the derived class, performs execution
        /// of the command.
        /// </summary>
        /// <exception cref="T:System.Management.Automation.PipelineStoppedException">The pipeline has already been terminated, or was terminated
        /// during the execution of this method.
        /// The Cmdlet should generally just allow PipelineStoppedException
        /// to percolate up to the caller of ProcessRecord etc.</exception>
        // ReSharper disable once StyleCop.SA1650
        protected override void ProcessCmdletRecord()
        {
            var request = new RestRequest($"wit/workitems/{this.Id}");

            //// request.RequestFormat = DataFormat.Json;

            if (this.OriginalWorkItem == null)
            {
                var getRequest  = new RestRequest($"wit/workitems/{this.Id}");
                var getResponse = this.Client.Execute <WorkItem>(getRequest);

                if (getResponse.IsSuccessful)
                {
                    this.OriginalWorkItem = getResponse.Data;
                }
                else
                {
                    this.ProcessErrorResponse(
                        getResponse,
                        DevOpsModelTarget.WorkItem,
                        ErrorCategory.NotSpecified,
                        this);
                }
            }

            var patchDocument = JsonHelpers.CreatePatch(this.OriginalWorkItem, this.UpdatedWorkItem);

            request.AddJsonBody(patchDocument);
            request.AddParameter("Content-Type", "application/json-patch+json", ParameterType.HttpHeader);
            var restResponse   = this.Client.Patch(request);
            var updateWorkItem = JsonConvert.DeserializeObject <WorkItem>(restResponse.Content);

            if (this.IsDebug)
            {
                // ReSharper disable once ExceptionNotDocumented
                this.SetPsVariable("UpdateRestResponse", restResponse);
            }

            if (!restResponse.IsSuccessful)
            {
                this.ProcessErrorResponse(restResponse, DevOpsModelTarget.WorkItem, ErrorCategory.NotSpecified, this);
            }
            else if (this.UpdatedWorkItem.Rev == updateWorkItem.Rev)
            {
                this.ThrowTerminatingError(new ErrorRecord(new PatchOperationFailedException("The update was not applied the selected work item"), "AzureDevOpsMgmt.Core.Cmdlet.UpdateWorkItem.RevisionNumberIsEqual", ErrorCategory.InvalidResult, request));
            }

            this.WriteObject("The operation has completed successfully");
        }