프로그래밍 방식으로 쿼리를 사용하여 작업 항목 가져오기
Azure DevOps Services
Azure DevOps Services의 일반적인 시나리오는 쿼리를 사용하여 작업 항목을 가져오는 것입니다. 이 문서에서는 REST API 또는 .NET 클라이언트 라이브러리를 사용하여 프로그래밍 방식으로 해당 시나리오를 구현하는 방법을 자세히 설명합니다.
필수 조건
다음 항목이 있어야 합니다.
- Azure DevOps Services의 조직입니다. 없는 경우 무료로 만들 수 있습니다 .
- 개인용 액세스 토큰, 토큰을 만드는 방법 알아보기
- C# 개발 환경에서 Visual Studio를 사용할 수 있습니다.
Visual Studio에서 C# 프로젝트 만들기
Visual Studio 내의 C# 프로그래밍에 대한 자세한 내용은 Visual Studio C# 프로그래밍 설명서를 참조 하세요.
C# 코드 콘텐츠
다음 작업은 다음 코드 샘플에서 발생합니다.
- 인증
- PAT를 사용하여 자격 증명 만들기
- 클라이언트 생성
- 작업 항목 가져오기
- 사용하려는 쿼리 만들기
- 해당 쿼리에 대한 결과 가져오기
- ID별로 각 작업 항목 가져오기
C# 코드 조각
// nuget:Microsoft.TeamFoundationServer.Client
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;
using Microsoft.VisualStudio.Services.Common;
public class QueryExecutor
{
private readonly Uri uri;
private readonly string personalAccessToken;
/// <summary>
/// Initializes a new instance of the <see cref="QueryExecutor" /> class.
/// </summary>
/// <param name="orgName">
/// An organization in Azure DevOps Services. If you don't have one, you can create one for free:
/// <see href="https://go.microsoft.com/fwlink/?LinkId=307137" />.
/// </param>
/// <param name="personalAccessToken">
/// A Personal Access Token, find out how to create one:
/// <see href="/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops" />.
/// </param>
public QueryExecutor(string orgName, string personalAccessToken)
{
this.uri = new Uri("https://dev.azure.com/" + orgName);
this.personalAccessToken = personalAccessToken;
}
/// <summary>
/// Execute a WIQL (Work Item Query Language) query to return a list of open bugs.
/// </summary>
/// <param name="project">The name of your project within your organization.</param>
/// <returns>A list of <see cref="WorkItem"/> objects representing all the open bugs.</returns>
public async Task<IList<WorkItem>> QueryOpenBugs(string project)
{
var credentials = new VssBasicCredential(string.Empty, this.personalAccessToken);
// create a wiql object and build our query
var wiql = new Wiql()
{
// NOTE: Even if other columns are specified, only the ID & URL are available in the WorkItemReference
Query = "Select [Id] " +
"From WorkItems " +
"Where [Work Item Type] = 'Bug' " +
"And [System.TeamProject] = '" + project + "' " +
"And [System.State] <> 'Closed' " +
"Order By [State] Asc, [Changed Date] Desc",
};
// create instance of work item tracking http client
using (var httpClient = new WorkItemTrackingHttpClient(this.uri, credentials))
{
// execute the query to get the list of work items in the results
var result = await httpClient.QueryByWiqlAsync(wiql).ConfigureAwait(false);
var ids = result.WorkItems.Select(item => item.Id).ToArray();
// some error handling
if (ids.Length == 0)
{
return Array.Empty<WorkItem>();
}
// build a list of the fields we want to see
var fields = new[] { "System.Id", "System.Title", "System.State" };
// get work items for the ids found in query
return await httpClient.GetWorkItemsAsync(ids, fields, result.AsOf).ConfigureAwait(false);
}
}
/// <summary>
/// Execute a WIQL (Work Item Query Language) query to print a list of open bugs.
/// </summary>
/// <param name="project">The name of your project within your organization.</param>
/// <returns>An async task.</returns>
public async Task PrintOpenBugsAsync(string project)
{
var workItems = await this.QueryOpenBugs(project).ConfigureAwait(false);
Console.WriteLine("Query Results: {0} items found", workItems.Count);
// loop though work items and write to console
foreach (var workItem in workItems)
{
Console.WriteLine(
"{0}\t{1}\t{2}",
workItem.Id,
workItem.Fields["System.Title"],
workItem.Fields["System.State"]);
}
}
}
관련된 문서
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기