通过 REST 列出 VSTS 工作项 API
VSTS work items list through REST API
如何使用 REST API 从 VSTS 获取工作项列表?
根据 documentation,ids
参数是可选的,但是当我省略它时,会出现 404
错误。
如果我添加 ids
参数,我可以获得项目。
请求失败:
GET https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems?api-version=1.0
请求成功:
GET https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems?ids=252&api-version=1.0
两者的身份验证相同。
要解决的完整问题是:获取特定 VSTS 项目中的所有功能
关键是使用 API 的 WIQL 部分,而不是工作项。
例如,要获取某种类型的工作项的平面列表,请使用以下命令:
https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql#a-flat-query
PowerShell 示例(显示所有处于关闭状态的用户故事):
# using env vars passed from VSTS build
$collectionuri = $Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
$token = $Env:SYSTEM_ACCESSTOKEN # need to configure build to allow passing OAuth tokens
$basicAuth = "{0}:{1}"-f "ivan-the-terrible", $token
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}"-f $basicAuth)}
$WorkItemType = 'User Story'
$url = $collectionuri + 'DefaultCollection/_apis/wit/wiql?api-version=1.0'
$WIQL_query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = '" + $WorkItemType + "' AND [State] = 'Closed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
$body = @{ query = $WIQL_query }
$bodyJson=@($body) | ConvertTo-Json
$response = Invoke-RestMethod -Uri $url -headers $headers -Method Post -ContentType "application/json" -Body $bodyJson
$workitems = $response.workItems
Write-Host "Found" $workitems.Count "work items of type:" $WorkItemType
这是我用 C# 编写的一个类似的解决方案,用于 return 类型为 Ticket 的所有工作项的列表。我能够利用接受的答案中提供的示例并查看文档 here
来构建此结果
public QueryResult GetTickets()
{
try
{
var token = "****";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", token))));
var query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Ticket' AND [State] <> 'Closed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate]";
var content = new StringContent("{ \"query\": \"" + query + "\" }", Encoding.UTF8, "application/json");
var url = "https://{account}.visualstudio.com/_apis/wit/wiql?api-version=4.1";
using (HttpResponseMessage response = client.PostAsync(url, content).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<QueryResult>(responseBody);
return result;
}
}
}
catch(Exception ex)
{
return null;
}
}
如何使用 REST API 从 VSTS 获取工作项列表?
根据 documentation,ids
参数是可选的,但是当我省略它时,会出现 404
错误。
如果我添加 ids
参数,我可以获得项目。
请求失败:
GET https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems?api-version=1.0
请求成功:
GET https://{account}.visualstudio.com/DefaultCollection/_apis/wit/workitems?ids=252&api-version=1.0
两者的身份验证相同。
要解决的完整问题是:获取特定 VSTS 项目中的所有功能
关键是使用 API 的 WIQL 部分,而不是工作项。 例如,要获取某种类型的工作项的平面列表,请使用以下命令: https://www.visualstudio.com/en-us/docs/integrate/api/wit/wiql#a-flat-query
PowerShell 示例(显示所有处于关闭状态的用户故事):
# using env vars passed from VSTS build
$collectionuri = $Env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI
$token = $Env:SYSTEM_ACCESSTOKEN # need to configure build to allow passing OAuth tokens
$basicAuth = "{0}:{1}"-f "ivan-the-terrible", $token
$basicAuth = [System.Text.Encoding]::UTF8.GetBytes($basicAuth)
$basicAuth = [System.Convert]::ToBase64String($basicAuth)
$headers = @{Authorization=("Basic {0}"-f $basicAuth)}
$WorkItemType = 'User Story'
$url = $collectionuri + 'DefaultCollection/_apis/wit/wiql?api-version=1.0'
$WIQL_query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = '" + $WorkItemType + "' AND [State] = 'Closed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate] desc"
$body = @{ query = $WIQL_query }
$bodyJson=@($body) | ConvertTo-Json
$response = Invoke-RestMethod -Uri $url -headers $headers -Method Post -ContentType "application/json" -Body $bodyJson
$workitems = $response.workItems
Write-Host "Found" $workitems.Count "work items of type:" $WorkItemType
这是我用 C# 编写的一个类似的解决方案,用于 return 类型为 Ticket 的所有工作项的列表。我能够利用接受的答案中提供的示例并查看文档 here
来构建此结果public QueryResult GetTickets()
{
try
{
var token = "****";
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "", token))));
var query = "Select [System.Id], [System.Title], [System.State] From WorkItems Where [System.WorkItemType] = 'Ticket' AND [State] <> 'Closed' order by [Microsoft.VSTS.Common.Priority] asc, [System.CreatedDate]";
var content = new StringContent("{ \"query\": \"" + query + "\" }", Encoding.UTF8, "application/json");
var url = "https://{account}.visualstudio.com/_apis/wit/wiql?api-version=4.1";
using (HttpResponseMessage response = client.PostAsync(url, content).Result)
{
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<QueryResult>(responseBody);
return result;
}
}
}
catch(Exception ex)
{
return null;
}
}