简体   繁体   English

如何从本地TFS服务器2018使用REST Api获取多个提交?

[英]How to GET multiple commits with REST Api from an on-premises TFS server 2018?

Consider the GetCommits API. 考虑GetCommits API。 It has an optional parameter searchCriteria.ids which is a list of commit Ids. 它有一个可选参数searchCriteria.ids ,它是提交ID的列表。

But how to pass this list? 但是如何通过这份名单呢? For example, I have 2 commits A and B. I tried: 例如,我有2次提交A和B。我尝试过:

  1. A,B A,B
  2. AB AB
  3. [A,B] [A,B]

The response I get is: 我得到的答复是:

An object ID must be 40 characters long and only have hex digits. Passed in object ID: 0533232917cc37fb743d282c467eb41bafac15ee,063a6805640221285db15f75c3b3ca6f3fe619c

I get the same response for all the three variants - only the reported object Id differs. 对于所有三个变体,我都得到相同的响应-仅报告的对象ID不同。

So, how do I pass an array to the Azure Devops REST Api in a GET request? 因此,如何在GET请求中将数组传递给Azure Devops REST Api?

I use powershell. 我使用Powershell。 The statement is simply Invoke-RestMethod -UseDefaultCredentials -Uri $Url 该语句只是Invoke-RestMethod -UseDefaultCredentials -Uri $Url

Variant 1 - use comma 变体1-使用逗号

http://tfsserver:8080/tfs/defaultcollection/code/_apis/git/repositories/xyz/commits?api-version=4.1&searchCriteria.ids=0533232917cc37fb743d282c467eb41bafac15ee,063a6805640221285db15f75c3b3ca6f3fe619c0

Results in 结果是

{"$id":"1","innerException":null,"message":"An object ID must be 40 characters long and only have hex digits. Passed in object ID: 0533232917cc37fb743d282c467eb41bafac15ee,063a6805640221285db15f75c3b3ca6f3fe619c0.","typeName":"System.ArgumentException, mscorlib","typeKey":"ArgumentException","errorCode":0,"eventId":0}

Variant 2 - use space 形式2-使用空间

http://tfsserver:8080/tfs/defaultcollection/code/_apis/git/repositories/xyz/commits?api-version=4.1&searchCriteria.ids=0533232917cc37fb743d282c467eb41bafac15ee%20063a6805640221285db15f75c3b3ca6f3fe619c0

Results in the same error. 结果相同。

Variant 3 - use JavaScript array [,] 变体3-使用JavaScript数组[,]

http://tfsserver:8080/tfs/defaultcollection/code/_apis/git/repositories/xyz/commits?api-version=4.1&searchCriteria.ids=[0533232917cc37fb743d282c467eb41bafac15ee,063a6805640221285db15f75c3b3ca6f3fe619c0]

Same error. 同样的错误。

It does work when I just pass one commit without any extra symbols. 当我只传递一次提交而没有任何其他符号时,它确实起作用。

在此处输入图片说明

Using in instead of = in代替=

There is a suggestion to use ' in ' instead of '='. 有建议使用'in'代替'='。 This does accept comma, but does not seem to work correctly. 这确实接受逗号,但似乎无法正常工作。 Please, observe: 请注意:

在此处输入图片说明

So, using '=' yields one commit - as expected. 因此,使用'='会产生一次提交-如预期的那样。 But using ' in ' yields 100? 但是使用'in'会产生100? I tried passing two commits separated with a comma and it does not fail, but returns 100 commits as well. 我尝试传递两个用逗号分隔的提交,它不会失败,但也会返回100个提交。 This is wrong. 错了

Based on my test, comma way is working for the rest API. 根据我的测试,逗号方式适用于其余的API。 The following is demo and test result with Powershell code.You could refer to it. 以下是Powershell代码的演示和测试结果,您可以参考它。 I test it with Azure Devops API. 我使用Azure Devops API对其进行了测试。

If there is a space between the ids, I also can reproduce the issue that you mentioned. 如果ID之间有空格,我也可以重现您提到的问题。

$pat = "PATToken"
$organization = "organization name"
$project = "project Id"
$repositoryId = "repository Id"
$ids = "266295d9adbxxx,f345970123bxxxx" # Ids of commits
$apiUrl= "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/commits?searchCriteria.ids=$ids&api-version=5.0-preview.1"

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $pat)))

$Result = Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}  -Method GET 

foreach($item in $Result.value)
{
  Write-Host "$item"
  Write-Host "---------------"
}

Note: I also try to test it with API version 4.1, it is also working on my side. 注意:我也尝试使用API​​版本4.1对其进行测试,它也可以正常工作。

Test Result: 测试结果:

在此处输入图片说明

Update : 更新

If I use the TFS API I also can reproduce the issue that you mentioned with the code I mentioned. 如果我使用TFS API,我还可以重现您提到的代码所提到的问题。

I can get the result with following way. 我可以通过以下方式获得结果。 Please have a try to use in to instead of "=" 请尝试使用in代替“ =”

The following is my test code: 以下是我的测试代码:

$ids = '0cc4e954c082372379xxxx','9b5983436c2f81b5fae6a72xxx'

$apiUrl= "http://$tfsserver:8080/tfs/$organization/$project/_apis/git/repositories/$repositoryId/commits?searchCriteria.ids in $ids&api-version=4.1"

Test Result: 测试结果:

在此处输入图片说明

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

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