/// <summary> /// method to get list of work items /// </summary> /// <param name="workItemType"></param> /// <returns></returns> public WorkItemFetchResponse.WorkItems GetWorkItemsfromSource(string workItemType) { GetWorkItemsResponse.Results viewModel = new GetWorkItemsResponse.Results(); WorkItemFetchResponse.WorkItems fetchedWIs; try { // create wiql object Object wiql = new { query = "Select [State], [Title] ,[Effort]" + "From WorkItems " + "Where [Work Item Type] = '" + workItemType + "'" + "And [System.TeamProject] = '" + Project + "' " + "Order By [Stack Rank] Desc, [Backlog Priority] Desc" }; using (var client = GetHttpClient()) { var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call // set the httpmethod to Patch var method = new HttpMethod("POST"); // send the request var request = new HttpRequestMessage(method, _configuration.UriString + "/_apis/wit/wiql?api-version=" + _configuration.VersionNumber) { Content = postValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode && response.StatusCode == System.Net.HttpStatusCode.OK) { viewModel = response.Content.ReadAsAsync <GetWorkItemsResponse.Results>().Result; } else { var errorMessage = response.Content.ReadAsStringAsync(); string error = Utility.GeterroMessage(errorMessage.Result.ToString()); LastFailureMessage = error; } viewModel.HttpStatusCode = response.StatusCode; string workitemIDstoFetch = ""; int WICtr = 0; foreach (GetWorkItemsResponse.Workitem WI in viewModel.workItems) { workitemIDstoFetch = WI.id + "," + workitemIDstoFetch; WICtr++; } workitemIDstoFetch = workitemIDstoFetch.TrimEnd(','); fetchedWIs = GetWorkItemsDetailInBatch(workitemIDstoFetch); return(fetchedWIs); } } catch (Exception ex) { logger.Info(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + ex.Message + "\n" + ex.StackTrace + "\n"); string error = ex.Message; LastFailureMessage = error; } return(new WorkItemFetchResponse.WorkItems()); }
/// <summary> /// method to get list of work items /// </summary> /// <param name="workItemType"></param> /// <returns></returns> public WorkItemFetchResponse.WorkItems getWorkItemsfromSource(string workItemType) { GetWorkItemsResponse.Results viewModel = new GetWorkItemsResponse.Results(); WorkItemFetchResponse.WorkItems fetchedWIs; // create wiql object Object wiql = new { query = "Select [State], [Title] ,[Effort]" + "From WorkItems " + "Where [Work Item Type] = '" + workItemType + "'" + "And [System.TeamProject] = '" + _sourceConfig.Project + "' " + "Order By [Stack Rank] Desc, [Backlog Priority] Desc" }; 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", _sourceCredentials); var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call // set the httpmethod to Patch var method = new HttpMethod("POST"); // send the request var request = new HttpRequestMessage(method, _sourceConfig.UriString + "/_apis/wit/wiql?api-version=2.2") { Content = postValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetWorkItemsResponse.Results>().Result; } viewModel.HttpStatusCode = response.StatusCode; string workitemIDstoFetch = ""; int WICtr = 0; foreach (GetWorkItemsResponse.Workitem WI in viewModel.workItems) { workitemIDstoFetch = WI.id + "," + workitemIDstoFetch; WICtr++; } Console.WriteLine("Total {0} {1} Work Items read from source", WICtr, workItemType); workitemIDstoFetch = workitemIDstoFetch.TrimEnd(','); fetchedWIs = GetWorkItemsDetailinBatch(workitemIDstoFetch); //update the work items in target if specified } return(fetchedWIs); }
/// <summary> /// Method to Get the list of all workItems /// </summary> /// <param name="project"></param> /// <param name="workItemType"></param> /// <returns></returns> public GetWorkItemsResponse.Results GetListOfWorkItems_ByWiql(string project, string workItemType) { try { GetWorkItemsResponse.Results viewModel = new GetWorkItemsResponse.Results(); //create wiql object Object wiql = new { query = "Select [Work Item Type],[State], [Title],[Created By] " + "From WorkItems " + "Where [Work Item Type] = '" + workItemType + "' " + "And [System.TeamProject] = '" + project + "' " + "And [System.State] = 'New' " + "Order By [Stack Rank] Desc, [Backlog Priority] Desc" }; using (var client = GetHttpClient()) { var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call // set the httpmethod to Patch var method = new HttpMethod("POST"); // send the request var request = new HttpRequestMessage(method, "_apis/wit/wiql?api-version=" + _configuration.VersionNumber) { Content = postValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetWorkItemsResponse.Results>().Result; } else { 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.Info(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + ex.Message + "\n" + ex.StackTrace + "\n"); } return(new GetWorkItemsResponse.Results()); }
/// <summary> /// Method to Get the list of all workItems /// </summary> /// <param name="project"></param> /// <param name="WorkItemType"></param> /// <returns></returns> public GetWorkItemsResponse.Results GetListOfWorkItems_ByWiql(string project, string WorkItemType) { GetWorkItemsResponse.Results viewModel = new GetWorkItemsResponse.Results(); //create wiql object Object wiql = new { query = "Select [Work Item Type],[State], [Title],[Created By] " + "From WorkItems " + "Where [Work Item Type] = '" + WorkItemType + "' " + "And [System.TeamProject] = '" + project + "' " + "And [System.State] = 'New' " + "Order By [State] Asc" }; 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); var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call // set the httpmethod to Patch var method = new HttpMethod("POST"); // send the request var request = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/wiql?api-version=" + _configuration.VersionNumber) { Content = postValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetWorkItemsResponse.Results>().Result; } else { var errorMessage = response.Content.ReadAsStringAsync(); string error = Utility.GeterroMessage(errorMessage.Result.ToString()); this.lastFailureMessage = error; } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } }
public void WIQL_GetListOfWorkItemsByWiql_Success() { // arrange WIQL request = new WIQL(_configuration); // act GetWorkItemsResponse.Results response = request.GetListOfWorkItems_ByWiql(_configuration.Project); // assert if (response.HttpStatusCode == HttpStatusCode.NotFound) { Assert.Inconclusive("no query results found"); } else { Assert.AreEqual(HttpStatusCode.OK, response.HttpStatusCode); } request = null; }
// / <summary> // / get list of work item by query id // / </summary> // / <param name="id">query id</param> // / <returns></returns> public GetWorkItemsResponse.Results GetListOfWorkItems_ByQueryId(string project, string id) { GetWorkItemsResponse.Results viewModel = new GetWorkItemsResponse.Results(); 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/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", _credentials); HttpResponseMessage response = client.GetAsync(project + "/_apis/wit/wiql/" + id + "?api-version=2.2").Result; if (response.IsSuccessStatusCode) { viewModel = response.Content.ReadAsAsync <GetWorkItemsResponse.Results>().Result; } viewModel.HttpStatusCode = response.StatusCode; return(viewModel); } }
public static List <WorkItems> GetWorkItemsfromSource(string workItemType, UrlParameters parameters) { GetWorkItemsResponse.Results viewModel = new GetWorkItemsResponse.Results(); List <WorkItemFetchResponse.WorkItems> fetchedWIs; try { // create wiql object Object wiql = new { //select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.IterationPath], [Microsoft.VSTS.Scheduling.CompletedWork] from WorkItems where [System.TeamProject] = @project and [System.WorkItemType] = 'Task' and [System.State] <> '' and [System.IterationPath] = 'unisys\\Iteration 1' query = "select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.IterationPath], [Microsoft.VSTS.Scheduling.CompletedWork] from WorkItems where [System.TeamProject] = '" + parameters.Project + "' and [System.WorkItemType] = 'Task' and [System.State] <> '' and [System.IterationPath] under '" + parameters.Project + "'" }; using (var client = new HttpClient()) { client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", parameters.PatBase); var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); // mediaType needs to be application/json-patch+json for a patch call // set the httpmethod to Patch var method = new HttpMethod("POST"); // send the request var request = new HttpRequestMessage(method, parameters.UriString + "/_apis/wit/wiql?api-version=5.0") { Content = postValue }; var response = client.SendAsync(request).Result; if (response.IsSuccessStatusCode && response.StatusCode == System.Net.HttpStatusCode.OK) { string res = response.Content.ReadAsStringAsync().Result; viewModel = JsonConvert.DeserializeObject <GetWorkItemsResponse.Results>(res); } else { var errorMessage = response.Content.ReadAsStringAsync(); } List <string> witIds = new List <string>(); string workitemIDstoFetch = ""; int WICtr = 0; foreach (GetWorkItemsResponse.Workitem WI in viewModel.workItems) { workitemIDstoFetch = WI.id + "," + workitemIDstoFetch; WICtr++; if (WICtr >= 199) { witIds.Add(workitemIDstoFetch = workitemIDstoFetch.Trim(',')); workitemIDstoFetch = ""; } } if (witIds.Count == 0) { workitemIDstoFetch = workitemIDstoFetch.TrimEnd(','); witIds.Add(workitemIDstoFetch); } fetchedWIs = GetWorkItemsDetailInBatch(witIds, parameters); return(fetchedWIs); } } catch (Exception ex) { string error = ex.Message; WriteFileToDisk("GetWorkItemsfromSource", ex.Message + Environment.NewLine + ex.StackTrace); } return(new List <WorkItemFetchResponse.WorkItems>()); }