繁体   English   中英

TFS Rest API 查询

[英]TFS Rest API Queries

我们正在使用 TFS 2015,并将转向 TFS 2017 版本。

由于我们有很多集合(大约 17 个),有时我们必须手动检查所有集合的值。 为了缓解这种情况,我计划查询 TFS Rest API,以获取值。

我对如何使用 Rest API 有一些疑问,因为我们可以使用以下方法:

1.) 使用 Nuget 包,如:

Microsoft.TeamFoundationServer.Client

Microsoft.VisualStudio.Services.Client

Microsoft.VisualStudio.Services.InteractiveClient

这需要以下代码:

//Prompt user for credential
VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssBasicCredential(string.Empty, pat));

//create a wiql object and build our query
Wiql wiql = new Wiql()
{
    Query = "Select [State], [Title] " +
            "From WorkItems " +
            "Where [Work Item Type] = 'Bug' " +
            "And [System.TeamProject] = '" + project + "' " +
            "And [System.State] <> 'Closed' " +
            "Order By [State] Asc, [Changed Date] Desc"
};

//create http client and query for resutls
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
Wiql query = new Wiql() { Query = "SELECT [Id], [Title], [State] FROM workitems WHERE [Work Item Type] = 'Bug' AND [Assigned To] = @Me" };
WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;

//Display reults in console
if (queryResults == null || queryResults.WorkItems.Count() == 0)
{
    Console.WriteLine("Query did not find any results");
}

2.) 另一种方法是使用带有 PAT 或备用凭据的 HttpClient 访问 Rest API,如下所示:

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Accept.Add(
        new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
        "Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(
            $"{username}:{password}")));

    using (HttpResponseMessage response = client.GetAsync(
        $"https://dev.azure.com/{account}/_apis/projects").Result)
    {
        response.EnsureSuccessStatusCode();
        repsonseBody = await response.Content.ReadAsStringAsync();
    }
}

两种方法之间有什么区别,哪种方法是推荐的方法并且可以与 TFS 2017 版本一起使用?

任何帮助或建议或链接都​​会很棒。

OAuth 身份验证是否与 TFS 一起使用,就像在 Azure Devops 中一样?

nuget 包中的*HttpClient类是 REST API 的包装器。 所以它们的级别更高,应该更容易使用。 他们还为所有结果和参数对象定义了类。 由于它们针对相同的 API,因此得到了同等的支持。


OAuth 身份验证是否与 TFS 一起使用,就像它与 Azure Devops 一样?

不,它不是按照https://docs.microsoft.com/en-us/azure/devops/integrate/get-started/authentication/oauth?view=azure-devops

以下指南适用于 Azure DevOps Services 用户,因为 Team Foundation Server 或 Azure DevOps Server 不支持 OAuth 2.0。

我在我的项目中使用第一个选项。 在 MS 支持下,您可以提示用户提供凭据或使用 PAT。 此外,您可以使用具有已知结果的 clear 方法,而无需定义自己的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM