Ejemplo n.º 1
0
        public bool createNewOnTFS()
        {
            if (string.IsNullOrEmpty(this.ID))
            {
                WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();

                WorkItemPatch.Field[] fields = new WorkItemPatch.Field[7];

                fields[0] = new WorkItemPatch.Field()
                {
                    op = "add", path = "/fields/System.Title", value = this.title
                };
                fields[1] = new WorkItemPatch.Field()
                {
                    op = "add", path = "/fields/System.Description", value = this.description
                };
                fields[2] = new WorkItemPatch.Field()
                {
                    op = "add", path = "/fields/System.IterationPath", value = this.TFSIteration
                };
                fields[3] = new WorkItemPatch.Field()
                {
                    op = "add", path = "/fields/System.AreaPath", value = this.TFSArea
                };
                fields[4] = new WorkItemPatch.Field()
                {
                    op = "add", path = "/fields/System.AssignedTo", value = this.assignedTo
                };
                fields[5] = new WorkItemPatch.Field()
                {
                    op = "add", path = "/fields/Ext_RefId", value = this.wrappedElement.uniqueID
                };
                fields[6] = new WorkItemPatch.Field()
                {
                    op = "add", path = "/fields/State", value = this.state
                };

                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = _TFSOwnerProject.authorization;

                    // serialize the fields array into a json string
                    var patchValue = new StringContent(JsonConvert.SerializeObject(fields), Encoding.UTF8, "application/json-patch+json");     // mediaType needs to be application/json-patch+json for a patch call

                    // set the httpmethod to Patch
                    var method = new HttpMethod("PATCH");

                    // send the request
                    //var request = new HttpRequestMessage(method, _TFSOwnerProject.TFSUrl + Uri.EscapeDataString(_TFSOwnerProject.name) + "/_apis/wit/workitems/$" + this.type + "?api-version=2.2") { Content = patchValue };
                    var request = new HttpRequestMessage(method, _TFSOwnerProject.TFSUrl + Uri.EscapeDataString(_TFSOwnerProject.name) + "/_apis/wit/workitems/$" + this.type + "?api-version=2.2")
                    {
                        Content = patchValue
                    };
                    var response = _TFSOwnerProject.sendToTFS(client, request);

                    if (response != null)
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                            this.ID   = viewModel.id.ToString();
                            return(true);
                        }
                        Logger.logError("Could not create workitem on TFS with title: '" + this.title + " because of error \nStatuscode: "
                                        + response.StatusCode + " Reasonphrase: " + response.ReasonPhrase);
                    }
                    return(false);
                }
            }
            return(false);
        }