Пример #1
0
        public string GetWorkItemsByQuery()
        {
            var project = _configuration.Project;
            var path    = _configuration.Query; // path to the query

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

                // if you already know the query id, then you can skip this step
                HttpResponseMessage queryHttpResponseMessage = client.GetAsync(project + "/_apis/wit/queries/" + path + "?api-version=2.2").Result;

                if (queryHttpResponseMessage.IsSuccessStatusCode)
                {
                    // bind the response content to the queryResult object
                    QueryResult queryResult = queryHttpResponseMessage.Content.ReadAsAsync <QueryResult>().Result;
                    string      queryId     = queryResult.id;

                    // using the queryId in the url, we can execute the query
                    HttpResponseMessage httpResponseMessage = client.GetAsync(project + "/_apis/wit/wiql/" + queryId + "?api-version=2.2").Result;

                    if (httpResponseMessage.IsSuccessStatusCode)
                    {
                        WorkItemQueryResult workItemQueryResult = httpResponseMessage.Content.ReadAsAsync <WorkItemQueryResult>().Result;

                        // now that we have a bunch of work items, build a list of id's so we can get details
                        var builder = new System.Text.StringBuilder();
                        foreach (var item in workItemQueryResult.workItems)
                        {
                            builder.Append(item.id.ToString()).Append(",");
                        }

                        // clean up string of id's
                        string ids = builder.ToString().TrimEnd(new char[] { ',' });

                        HttpResponseMessage getWorkItemsHttpResponse = client.GetAsync("_apis/wit/workitems?ids=" + ids + "&fields=System.Id,System.Title,System.State&asOf=" + workItemQueryResult.asOf + "&api-version=2.2").Result;

                        if (getWorkItemsHttpResponse.IsSuccessStatusCode)
                        {
                            var result = getWorkItemsHttpResponse.Content.ReadAsStringAsync().Result;
                            return("success");
                        }

                        return("failed");
                    }

                    return("failed");
                }

                return("failed");
            }
        }
Пример #2
0
        public string GetWorkItemsByWiql()
        {
            string project = _configuration.Project;

            // create wiql object
            var wiql = new
            {
                query = "Select [State], [Title] " +
                        "From WorkItems " +
                        "Where [Work Item Type] = 'Bug' " +
                        "And [System.TeamProject] = '" + project + "' " +
                        "And [System.State] = 'New' " +
                        "Order By [State] Asc, [Changed Date] Desc"
            };

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

                // serialize the wiql object into a json string
                var postValue = new StringContent(JsonConvert.SerializeObject(wiql), Encoding.UTF8, "application/json"); // mediaType needs to be application/json for a post call

                // set the httpmethod to PPOST
                var method = new HttpMethod("POST");

                // send the request
                var httpRequestMessage = new HttpRequestMessage(method, _configuration.UriString + "_apis/wit/wiql?api-version=2.2")
                {
                    Content = postValue
                };
                var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;

                if (httpResponseMessage.IsSuccessStatusCode)
                {
                    WorkItemQueryResult workItemQueryResult = httpResponseMessage.Content.ReadAsAsync <WorkItemQueryResult>().Result;

                    // now that we have a bunch of work items, build a list of id's so we can get details
                    var builder = new System.Text.StringBuilder();
                    foreach (var item in workItemQueryResult.workItems)
                    {
                        builder.Append(item.id.ToString()).Append(",");
                    }

                    // clean up string of id's
                    string ids = builder.ToString().TrimEnd(new char[] { ',' });

                    HttpResponseMessage getWorkItemsHttpResponse = client.GetAsync("_apis/wit/workitems?ids=" + ids + "&fields=System.Id,System.Title,System.State&asOf=" + workItemQueryResult.asOf + "&api-version=2.2").Result;

                    if (getWorkItemsHttpResponse.IsSuccessStatusCode)
                    {
                        var result = getWorkItemsHttpResponse.Content.ReadAsStringAsync().Result;
                        return("success");
                    }

                    return("failed");
                }

                return("failed");
            }
        }