Azure Repos REST API获取合并请求中已更改文件的列表

时间:2019-12-19 10:24:26

标签: azure azure-devops azure-devops-rest-api

目标:每当批准了请求请求并且审阅者在Azure仓库中完成(合并)请求时,我都想确定该合并请求中的文件列表。基于文件扩展名,我必须触发相应的构建管道。

问题:到目前为止,从azure devOps门户看来这是不可能的,而我需要REST API。请为我指出可以帮助我确定合并请求中更改文件列表的API / API

分析:我也见过Get Merge Requests,但不确定在管道中从何处获取mergeOperationId。

2 个答案:

答案 0 :(得分:0)

作为解决方法,您可以找到完成的PR并检查其提交。带有.NET client libraries for Azure DevOps Services的GitHttpClient的示例。

private static void ViewPullRequests(string TeamProjectName, string GitRepo, bool ClosedPRs = false, string TargetRef = "")
{
    if (ClosedPRs && TargetRef == "")
    {
        Console.WriteLine("Define a target branch reference");
        return;
    }

    var pullRequests = (ClosedPRs)? 
        GitClient.GetPullRequestsAsync(TeamProjectName, GitRepo, new GitPullRequestSearchCriteria { Status = PullRequestStatus.Completed, TargetRefName = TargetRef }, top: 10).Result : 
        GitClient.GetPullRequestsAsync(TeamProjectName, GitRepo, null ).Result;

    foreach (var pullRequest in pullRequests)
    {
        var commits = GitClient.GetPullRequestCommitsAsync(TeamProjectName, GitRepo, pullRequest.PullRequestId).Result;

        foreach (var commit in commits)
        {
            Console.WriteLine("{0} {1}", commit.CommitId.Substring(0, 8), commit.Comment);
            GitCommitChanges changes = GitClient.GetChangesAsync(TeamProjectName, commit.CommitId, GitRepo).Result;

            foreach(var change in changes.Changes)
                Console.WriteLine("{0}: {1}", change.ChangeType, change.Item.Path);
        }
    }
}

Rest Api方法:

  1. Pull Requests - Get Pull Requests

  2. Pull Request Commits - Get Pull Request Commits

  3. Commits - Get Changes

答案 1 :(得分:0)

您可以从UI页面获取合并请求中已更改文件的列表。

enter image description here

如果要通过rest api获取已更改文件的列表,则需要使用 Shamrai Aleksander 提供的方法。

首先,您需要使用Pull Request Commits rest api来获取请求请求Commits。

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/commits?api-version=5.1

然后根据Commits-Get Changes rest api读取path参数以获取已更改的文件,并循环获取已更改文件的列表。

GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits/{commitId}/changes?api-version=5.1

enter image description here