/// <summary>
        /// Add hyperlink to work item
        /// </summary>
        /// <param name="id"></param>
        /// <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem AddHyperLink(string id)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[1];

            // change some values on a few fields
            fields[0] = new WorkItemPatch.Field()
            {
                op    = "add",
                path  = "/relations/-",
                value = new WorkItemPatch.Value()
                {
                    rel        = "Hyperlink",
                    url        = "http://www.visualstudio.com/team-services",
                    attributes = new WorkItemPatch.Attributes()
                    {
                        comment = "Visual Studio Team Services"
                    }
                }
            };

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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, _configuration.UriString + "_apis/wit/workitems/" + id + "?api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                if (response.IsSuccessStatusCode)
                {
                    viewModel         = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                    viewModel.Message = "success";
                }
                else
                {
                    dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>();
                    Newtonsoft.Json.Linq.JContainer msg  = responseForInvalidStatusCode.Result;
                    viewModel.Message = msg.ToString();
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
        /// <summary>
        /// Add hyperlink to work item
        /// </summary>
        /// <param name="id"></param>
        /// <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem AddCommitLink(string id)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[1];

            // change some values on a few fields
            fields[0] = new WorkItemPatch.Field()
            {
                op    = "add",
                path  = "/relations/-",
                value = new WorkItemPatch.Value()
                {
                    rel        = "ArtifactLink",
                    url        = "vstfs:///Git/Commit/1435ac99-ba45-43e7-9c3d-0e879e7f2691%2Fd00dd2d9-55dd-46fc-ad00-706891dfbc48%2F3fefa488aac46898a25464ca96009cf05a6426e3",
                    attributes = new WorkItemPatch.Attributes()
                    {
                        name = "Fixed in Commit"
                    }
                }
            };

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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, _configuration.UriString + "_apis/wit/workitems/" + id + "?api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                if (response.IsSuccessStatusCode)
                {
                    viewModel         = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                    viewModel.Message = "success";
                }
                else
                {
                    dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>();
                    Newtonsoft.Json.Linq.JContainer msg  = responseForInvalidStatusCode.Result;
                    viewModel.Message = msg.ToString();
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
Exemple #3
0
        // / <summary>
        // / Create a bug
        // / </summary>
        // / <param name="projectName"></param>
        // / <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem CreateBug(string projectName)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[4];

            // set some field values like title and description
            fields[0] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.Title", value = "Authorization Errors"
            };
            fields[1] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/Microsoft.VSTS.TCM.ReproSteps", value = "Our authorization logic needs to allow for users with Microsoft accounts (formerly Live Ids) - http:// msdn.microsoft.com/en-us/library/live/hh826547.aspx"
            };
            fields[2] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = "1"
            };
            fields[3] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/Microsoft.VSTS.Common.Severity", value = "2 - High"
            };


            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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, _configuration.UriString + projectName + "/_apis/wit/workitems/$Bug?api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                var me = response.ToString();

                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
        // / <summary>
        // / update a specific work item by id and return that changed worked item
        // / </summary>
        // / <param name="id"></param>
        // / <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem UpdateWorkItemFields(string id)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[4];

            // change some values on a few fields
            fields[0] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.History", value = "adding some history"
            };
            fields[1] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/Microsoft.VSTS.Common.Priority", value = "2"
            };
            fields[2] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/Microsoft.VSTS.Common.BusinessValue", value = "100"
            };
            fields[3] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/Microsoft.VSTS.Common.ValueArea", value = "Architectural"
            };

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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, _configuration.UriString + "_apis/wit/workitems/" + id + "?api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
        // / <summary>
        // / create a work item using bypass rules
        // / </summary>
        // / <param name="projectName">name of project</param>
        // / <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem CreateWorkItemUsingByPassRules(string projectName)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[3];

            // add a title and add a field you normally cant add such as CreatedDate
            fields[0] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.Title", value = "hello world!"
            };
            fields[1] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.CreatedDate", value = "6/1/2016"
            };
            fields[2] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.CreatedBy", value = "Art Vandelay"
            };

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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");

                var url = _configuration.UriString + projectName + "/_apis/wit/workitems/$UserStory?api-version=2.2";

                // send the request
                var request = new HttpRequestMessage(method, _configuration.UriString + projectName + "/_apis/wit/workitems/$User Story?bypassRules=true&api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                var me = response.ToString();

                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
        // / <summary>
        // / add link to another work item
        // / </summary>
        // / <param name="id">work item id</param>
        // / <param name="linkToId">link to work item id</param>
        // / <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem AddAttachment(string id, string url)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[1];

            // change some values on a few fields
            fields[0] = new WorkItemPatch.Field()
            {
                op    = "add",
                path  = "/relations/-",
                value = new WorkItemPatch.Value()
                {
                    rel        = "AttachedFile",
                    url        = url,
                    attributes = new WorkItemPatch.Attributes()
                    {
                        comment = "adding attachment to work item"
                    }
                }
            };

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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, _configuration.UriString + "_apis/wit/workitems/" + id + "?api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
        // / <summary>
        // / move a work item from a project in agile to a project in scrum
        // / </summary>
        // / <param name="id">work item id</param>
        // / <param name="type">Bug or User Story</param>
        // / <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem ChangeType(string id, string type)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[3];

            // change the work item type, state and reason values in order to change the work item type
            fields[0] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.WorkItemType", value = type
            };
            fields[1] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.State", value = "New"
            };
            fields[2] = new WorkItemPatch.Field()
            {
                op = "add", path = "/fields/System.Reason", value = "New"
            };

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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, _configuration.UriString + "_apis/wit/workitems/" + id + "?api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                var someme = response.ToString();

                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }
Exemple #8
0
        /// <summary>
        /// Create Work items bypassing all rules
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public WorkItemPatchResponse.WorkItem CreateWorkItemUsingByPassRules(string json)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[3];


            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _credentials);

                // 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

                var jsonContent = new StringContent(json, Encoding.UTF8, "application/json-patch+json");
                // set the httpmethod to Patch
                var method = new HttpMethod("PATCH");

                // send the request
                var request = new HttpRequestMessage(method, _configuration.UriString + "TestProject" + "/_apis/wit/workitems/$" + "User Story" + "?bypassRules=true&api-version=2.2")
                {
                    Content = jsonContent
                };
                var response = client.SendAsync(request).Result;

                var me = response.ToString();

                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                }
                else
                {
                    var    errorMessage = response.Content.ReadAsStringAsync();
                    string error        = Utility.GeterroMessage(errorMessage.Result.ToString());
                    this.LastFailureMessage = error;
                }

                viewModel.HttpStatusCode = response.StatusCode;
                return(viewModel);
            }
        }
        // / <summary>
        // / update fields on work item using bypass rules
        // / </summary>
        // / <param name="id">work item id</param>
        // / <returns>WorkItemPatchResponse.WorkItem</returns>
        public WorkItemPatchResponse.WorkItem UpdateWorkItemFieldsWithByPassRules(string id)
        {
            WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem();
            WorkItemPatch.Field[]          fields    = new WorkItemPatch.Field[1];

            // replace value on a field that you normally cannot change, like system.createdby
            fields[0] = new WorkItemPatch.Field()
            {
                op = "replace", path = "/fields/System.CreatedBy", value = "Foo <*****@*****.**>"
            };

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials);

                // 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, _configuration.UriString + "_apis/wit/workitems/" + id + "?bypassRules=true&api-version=2.2")
                {
                    Content = patchValue
                };
                var response = client.SendAsync(request).Result;

                if (response.IsSuccessStatusCode)
                {
                    viewModel = response.Content.ReadAsAsync <WorkItemPatchResponse.WorkItem>().Result;
                }

                viewModel.HttpStatusCode = response.StatusCode;

                return(viewModel);
            }
        }