我们正在尝试收集有关拉取请求的信息,以便为我们的部门建立某些指标(完成拉取请求需要多长时间......)。
Azure DevOps 提供了一个 API 来查询很多东西,包括拉取请求。
我在这里查了一下用法:https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20request?view=azure-devops-rest-6.0 https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pull%20requests/get%20pull%20request?view=azure-devops-rest-6.0我还研究了微软提供的这个客户端:https://learn.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.sourcecontrol.webapi.githttpclient?view=azure-devops-dotnet https://learn.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.sourcecontrol.webapi.githttpclient?view=azure-devops-dotnet
我有一个问题,我通过 API 和 GitHttpClient 获得的结果似乎与我的信息不匹配think应该存在。
我们最新的拉取请求之一的 ID 为 1086。我认为,这意味着之前已经有 1085 个拉取请求,这是第 1086 个拉取请求。
当我查询 API 时,我得到了奇怪的结果:
使用 API,在没有任何过滤条件的情况下,我收到 5 个拉取请求。使用过滤条件“状态已完成”,我收到 101 个拉取请求。使用 top = 5000 仍然会输出 101。
使用 GitHttpClient 我得到不同的结果。没有任何过滤器,我得到 4。使用过滤条件“状态已完成”,我得到 101 个拉取请求。使用 top = 5000 我得到 490。
这仍然不是 1086。我不想手动检查 Azure DevOps 中的所有拉取请求并检查 API 是否返回正确数量的拉取请求。
那么,pull request id 是如何计算的呢?难道只是算数吗?实际上是否有 1086 个拉取请求,而 api/客户端只是交付了更少?拉取请求 ID 中是否存在间隙,而实际上并不存在 1086 个拉取请求?
我正在使用的个人访问令牌具有所需的权限。我什至尝试过使用具有所有权限的访问令牌,但结果仍然相同。所以这不应该是问题。
那么有没有一种好的、简单/快速的方法来验证我从 API / GitHttpClient 获取的数据是否完整?
另外为什么我必须输入 top=x 才能获得超过 101 个结果。据我了解,这不是其余 api 的工作方式......
代码示例
应用程序编程接口:
public class Client
{
private const string _azureDevopsUri = "https://dev.azure.com/companyName";
private const string _query = "projectName/_apis/git/pullrequests?searchCriteria.status=completed&api-version=6.0";
public async Task<PullRequests> GetPullRequestReport(DateTime? fromTime, DateTime? toTime)
{
string personalAccessToken = "HereIsMyToken";
string credentials = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalAccessToken)));
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(_azureDevopsUri);
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
var response = await httpClient.GetAsync(_query);
if (response.IsSuccessStatusCode)
{
var responseContentAsJsonString = await response.Content.ReadAsStringAsync();
var pullRequests = JsonConvert.DeserializeObject<PullRequests>(responseContentAsJsonString);
GitHttp客户端
public class Client
{
private const string _azureDevopsUri = "https://dev.azure.com/companyName";
public async Task<PullRequests> GetPullRequestReport(DateTime? fromTime, DateTime? toTime)
{
string personalAccessToken = "HereIsMyToken";
var connection = new VssConnection(new Uri(_azureDevopsUri), new VssBasicCredential(string.Empty, personalAccessToken));
var gitHttpClient = connection.GetClient<GitHttpClient>();
var pullRequestsGit = await gitHttpClient.GetPullRequestsByProjectAsync(projectName", new GitPullRequestSearchCriteria { Status = PullRequestStatus.Completed }, top: 5000);