以编程方式完成 TFS 合并请求

Complete TFS Pull Request programmatically

使用 Microsoft.TeamFoundationServer.Client (15.112.1) 连接到 TFS 2017 Update 2 服务器我们可以获得详细信息关于像这样的现有 PR:

var connection = new VssConnection(collectionUri, credentials);
var client = connection.GetClient<GitHttpClient>();
var pr = await client.GetPullRequestByIdAsync(pullRequestId);

此外,我们可以像这样创建新的 PR:

var pr = await client.CreatePullRequestAsync(
        new GitPullRequest
        {
          SourceRefName = "master",
          TargetRefName = "develop",
          Title = "[Automatic Merge]"
        },
        projectName, repositoryName);

另外,我们可以这样给PR投票:

var pr = await client.CreatePullRequestReviewerAsync(
            reviewer, projectName, repositoryName, pullRequestId, authorizedIdenity.Id.ToString());

Is there any way to complete the PR (overriding or not existing branch policies) and proceed with the merge operation?

GitHttpClient 有一个 UpdatePullRequestAsync 方法。

要完成拉取请求,您需要更新拉取请求的 状态 属性。并使用 UpdatePullRequestAsync 方法来完成您的 PR。

请确保设置 CompletionOptions 属性 以指定是否合并提交、删除源分支等

因此您的代码如下所示

pr.Status = PullRequestStatus.Completed
pr.CompletionOption = new GitPullRequestCompletionOption() { SquashMerge = true };
client.UpdatePullRequest(pr, repositoryId, pullRequestId);

编辑:

Microsoft.TeamFoundationServer.ExtendedClient 的发布版本尚不能使用 ByPassPolicy。

但是,如果您安装库 Microsoft.TeamFoundationServer.ExtendedClient 的预发布 NuGet 包 v15.122.1-preview,您将在 GitPullrequestCompletionOptions class 中看到选项 ByPassPolicy 作为 属性。您可以将其设置为 true 以绕过策略。