我们正在使用TFS内部部署。
TFS版本: Microsoft Visual Studio Team Foundation Server 版本16.122.27409.2 (2018)。
我们需要执行TFS源代码控制(代码搜索) 根据{{3}}这种方式使用TFS REST API。 Build和Release API正在运行,但搜索API返回404。 已安装搜索代码扩展,并且可以从TFS门户中正常工作。
API网址:
POST :http:// {DNS}:8080 / tfs / {Collection} / {Project} /_apis/search/codesearchresults?api-version=4.1-preview.1
请帮忙,我做错了什么?
答案 0 :(得分:1)
您无法在浏览器中打开它。您必须提供请求正文,如API示例中明确表达的那样:
{
"searchText": "CodeSearchController",
"$skip": 0,
"$top": 1,
"filters": {
"Project": [
"MyFirstProject"
],
"Repository": [
"MyFirstProject"
],
"Path": [
"/"
],
"Branch": [
"master"
],
"CodeElement": [
"def",
"class"
]
},
"$orderBy": [
{
"field": "filename",
"sortOrder": "ASC"
}
],
"includeFacets": true
}
答案 1 :(得分:0)
正如丹尼尔所说" You can't just open it in a browser. You have to provide a request body
"
因此,您可以使用Postman等工具向请求正文发送请求,或者您可以使用PowerShell通过请求正文调用REST API。
此外,根据我的测试,您提到的REST API(Code Search Results)似乎不适用于内部部署TFS 。我在TFS 2018 Update2(Version 16.131.27701.1
)上进行了测试,它总是返回"count": 0,
。
但是,您可以使用以下REST API来搜索代码:
POST http://server:8080/tfs/DefaultCollection/{Project}/_api/_search/postCodeQuery?api-version=4.1-preview.1
请求正文:
{"searchText":"<test1>",
"scope":"Team Foundation Server",
"filters":"{\"ProjectFilters\":[\"0511ScrumTFVC\"]}",
"skipResults":0,
"takeResults":50,
"sortOptions":""
}
以下PowerShell示例供您参考:
Param(
[string]$baseurl = "http://server:8080/tfs/DefaultCollection",
[string]$projectName = "ProjectName",
[string]$user = "username",
[string]$token = "password"
)
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
function CreateJsonBody
{
$value = @"
{"searchText":"<test1>",
"scope":"Team Foundation Server",
"filters":"{\"ProjectFilters\":[\"ProjectName\"]}",
"skipResults":0,
"takeResults":50,
"sortOptions":""
}
"@
return $value
}
$json = CreateJsonBody
$uri = "$baseurl/$($projectName)/_api/_search/postCodeQuery?api-version=4.1-preview.1"
Write-Host $uri
$result = Invoke-RestMethod -Uri $uri -Method POST -Body $json -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$result = $result | convertto-json
Write-host $result