public WorkItemPatchResponse.WorkItem DeleteWorkItem(string id) { WorkItemPatchResponse.WorkItem viewModel = new WorkItemPatchResponse.WorkItem(); 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); var method = new HttpMethod("DELETE"); var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/workitems/" + id + "?api-version=2.2"); var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { 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); } }
public ViewModels.WorkItemTracking.AttachmentReference UploadAttachmentBinaryFile(string filePath) { Byte[] bytes = File.ReadAllBytes(@filePath); String[] breakApart = filePath.Split('\\'); int length = breakApart.Length; string fileName = breakApart[length - 1]; ViewModels.WorkItemTracking.AttachmentReference viewModel = new ViewModels.WorkItemTracking.AttachmentReference(); using (var client = new HttpClient()) { client.BaseAddress = new Uri(_configuration.UriString); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/octet-stream")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); ByteArrayContent content = new ByteArrayContent(bytes); content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); HttpResponseMessage response = client.PostAsync("_apis/wit/attachments?fileName=" + fileName + "&api-version=2.2", content).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <ViewModels.WorkItemTracking.AttachmentReference>().Result; } 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 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); } }
/// <summary> /// Update Iteration Dates /// </summary> /// <param name="project"></param> /// <param name="path"></param> /// <param name="startDate"></param> /// <param name="finishDate"></param> /// <returns></returns> public GetNodeResponse.Node UpdateIterationDates(string project, string path, DateTime startDate, DateTime finishDate) { try { CreateUpdateNodeViewModel.Node node = new CreateUpdateNodeViewModel.Node() { //name = path, attributes = new CreateUpdateNodeViewModel.Attributes() { startDate = startDate, finishDate = finishDate } }; GetNodeResponse.Node viewModel = new GetNodeResponse.Node(); using (var client = GetHttpClient()) { // serialize the fields array into a json string var patchValue = new StringContent(JsonConvert.SerializeObject(node), Encoding.UTF8, "application/json"); var method = new HttpMethod("PATCH"); // send the request var request = new HttpRequestMessage(method, project + "/_apis/wit/classificationNodes/iterations/" + path + "?api-version=" + _configuration.VersionNumber) { Content = patchValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetNodeResponse.Node>().Result; viewModel.Message = "success"; } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; viewModel.Message = msg.ToString(); var errorMessage = response.Content.ReadAsStringAsync(); string error = Utility.GeterroMessage(errorMessage.Result.ToString()); this.LastFailureMessage = error; } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } } catch (Exception ex) { logger.Debug(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + "\t" + ex.Message + "\t" + "\n" + ex.StackTrace + "\n"); } return(new GetNodeResponse.Node()); }
public GetRestoreMultipleWorkItemsResponse.Items PeremenentlyDeleteMultipleItems(string[] ids) { GetRestoreMultipleWorkItemsResponse.Items viewModel = new GetRestoreMultipleWorkItemsResponse.Items(); WorkItemBatchPost.BatchRequest[] postDocument = new WorkItemBatchPost.BatchRequest[3]; Dictionary <string, string> headers = new Dictionary <string, string>() { { "Content-Type", "application/json-patch+json" } }; var i = 0; foreach (var id in ids) { postDocument[i] = new WorkItemBatchPost.BatchRequest { method = "DELETE", uri = "/_apis/wit/recyclebin/" + id + "?api-version=3.0-preview", headers = headers }; i = i + 1; } ; 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); var postValue = new StringContent(JsonConvert.SerializeObject(postDocument), Encoding.UTF8, "application/json"); var method = new HttpMethod("POST"); var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/$batch?api-version=3.0-preview") { Content = postValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetRestoreMultipleWorkItemsResponse.Items>().Result; } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; viewModel.Message = msg.ToString(); } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } }
public GetOperationResponse.Operation CreateTeamProject(string name) { GetOperationResponse.Operation operation = new GetOperationResponse.Operation(); Object teamProject = new { name = name, description = "VanDelay Industries travel app", capabilities = new { versioncontrol = new { sourceControlType = "Git" }, processTemplate = new { templateTypeId = "6b724908-ef14-45cf-84f8-768b5384da45" } } }; 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(teamProject), Encoding.UTF8, "application/json"); var method = new HttpMethod("POST"); var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/projects?api-version=2.2") { Content = patchValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { operation = response.Content.ReadAsAsync <GetOperationResponse.Operation>().Result; } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; operation.Message = msg.ToString(); } operation.HttpStatusCode = response.StatusCode; return(operation); } }
/// <summary> /// Update Iteration Dates /// </summary> /// <param name="project"></param> /// <param name="path"></param> /// <param name="startDate"></param> /// <param name="finishDate"></param> /// <returns></returns> public GetNodeResponse.Node UpdateIterationDates(string project, string path, DateTime startDate, DateTime finishDate) { CreateUpdateNodeViewModel.Node node = new CreateUpdateNodeViewModel.Node() { //name = path, attributes = new CreateUpdateNodeViewModel.Attributes() { startDate = startDate, finishDate = finishDate } }; GetNodeResponse.Node viewModel = new GetNodeResponse.Node(); 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(node), Encoding.UTF8, "application/json"); var method = new HttpMethod("PATCH"); // send the request var request = new HttpRequestMessage(method, _configuration.UriString + project + "/_apis/wit/classificationNodes/iterations/" + path + "?api-version=" + _configuration.VersionNumber) { Content = patchValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetNodeResponse.Node>().Result; viewModel.Message = "success"; } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; viewModel.Message = msg.ToString(); var errorMessage = response.Content.ReadAsStringAsync(); string error = Utility.GeterroMessage(errorMessage.Result.ToString()); this.LastFailureMessage = error; } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } }
public GetNodeResponse.Node CreateIteration(string project, string path) { CreateUpdateNodeViewModel.Node node = new CreateUpdateNodeViewModel.Node() { name = path //attributes = new CreateUpdateNodeViewModel.Attributes() //{ // startDate = startDate, // finishDate = finishDate //} }; GetNodeResponse.Node viewModel = new GetNodeResponse.Node(); 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 postValue = new StringContent(JsonConvert.SerializeObject(node), Encoding.UTF8, "application/json"); var method = new HttpMethod("POST"); // send the request var request = new HttpRequestMessage(method, _configuration.UriString + project + "/_apis/wit/classificationNodes/iterations?api-version=2.2") { Content = postValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetNodeResponse.Node>().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); } }
public string CreateBugByPassingRules() { var projectName = _configuration.Project; Object[] patchDocument = new Object[6]; patchDocument[0] = new { op = "add", path = "/fields/System.Title", value = "Imported bug from my other system (rest api)" }; patchDocument[1] = new { 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" }; patchDocument[2] = new { op = "add", path = "/fields/System.CreatedBy", value = "Some User" }; patchDocument[3] = new { op = "add", path = "/fields/System.ChangedBy", value = "Some User" }; patchDocument[4] = new { op = "add", path = "/fields/System.CreatedDate", value = "4/15/2016" }; patchDocument[5] = new { op = "add", path = "/fields/System.History", value = "Data imported from source" }; 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(patchDocument), 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?bypassRules=true&api-version=2.2") { Content = patchValue }; var response = client.SendAsync(request).Result; patchDocument = null; if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsStringAsync().Result; return("success"); } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; return(msg.ToString()); } } }
public GetOperationResponse.Operation ChangeTeamProjectDescription(string projectId, string projectDescription) { GetOperationResponse.Operation opertion = new GetOperationResponse.Operation(); Object projectData = new { description = projectDescription }; 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(projectData), Encoding.UTF8, "application/json"); var method = new HttpMethod("PATCH"); var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/projects/" + projectId + "?api-version=2.2") { Content = patchValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { opertion = response.Content.ReadAsAsync <GetOperationResponse.Operation>().Result; } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; opertion.Message = msg.ToString(); } opertion.HttpStatusCode = response.StatusCode; return(opertion); } }
public GetRestoredWorkItemResponse.WorkItem RestoreItem(string id) { GetRestoredWorkItemResponse.WorkItem viewModel = new GetRestoredWorkItemResponse.WorkItem(); var patchDocument = new { IsDeleted = false }; 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); var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json"); var method = new HttpMethod("PATCH"); var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/recyclebin/" + id + "?api-version=3.0-preview") { Content = patchValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetRestoredWorkItemResponse.WorkItem>().Result; } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; viewModel.Message = msg.ToString(); } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } }
public string AddAttachmentToBug() { string _id = _configuration.WorkItemId; string _filePath = _configuration.FilePath; // get the file name from the full path String[] breakApart = _filePath.Split('\\'); int length = breakApart.Length; string fileName = breakApart[length - 1]; Byte[] bytes; try { bytes = System.IO.File.ReadAllBytes(@_filePath); } catch (System.IO.FileNotFoundException) { return(@"file not found: " + _filePath); } using (var client = new HttpClient()) { client.BaseAddress = new Uri(_configuration.UriString); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/octet-stream")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); ByteArrayContent content = new ByteArrayContent(bytes); content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); HttpResponseMessage uploadResponse = client.PostAsync("_apis/wit/attachments?fileName=" + fileName + "&api-version=2.2", content).Result; if (uploadResponse.IsSuccessStatusCode) { var attachmentReference = uploadResponse.Content.ReadAsAsync <AttachmentReference>().Result; Object[] patchDocument = new Object[1]; // add required attachment values patchDocument[0] = new { op = "add", path = "/relations/-", value = new { rel = "AttachedFile", url = attachmentReference.url, // url from uploadresult attributes = new { comment = "adding attachment to bug" } } }; 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(patchDocument), 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) { var result = response.Content.ReadAsStringAsync().Result; } else { dynamic responseForInvalidStatusCode = response.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; return(msg.ToString()); } return("success"); } else { dynamic responseForInvalidStatusCode = uploadResponse.Content.ReadAsAsync <dynamic>(); Newtonsoft.Json.Linq.JContainer msg = responseForInvalidStatusCode.Result; return(msg.ToString()); } } }