Microsoft.TeamFoundation.Build.WebApi 获取 PR 政策启动的构建状态

Microsoft.TeamFoundation.Build.WebApi Get Build Status Launched by PR policy

在我们的管道中,我们以编程方式创建拉取请求 (PR)。被合并到的分支有一个启动构建的策略。此构建需要可变的时间。我需要查询构建状态直到它完成(或长时间超时),以便我可以完成 PR,并清理临时分支。

我正在尝试弄清楚如何获取 PR 启动的构建,以便我可以使用 Microsoft.TeamFoundation.Build.WebApi 检查状态,但是 BuildHttpClientBase.GetBuildAsync 的所有重载都需要一个构建我没有的ID。我想避免使用 Azure Build REST API。有谁知道我如何使用 BuildHttpClientBase 在没有构建 ID 的情况下通过 PR 启动构建?

遗憾的是,该文档没有提供很多有关功能的详细信息。

正在回答您提出的问题:

找到一个为拉取请求提供单一确定性构建 ID 的调用似乎并不容易获得。

如前所述,您可以使用 BuldHttpClient.GetBuildsAsync() 根据分支、存储库、请求用户和原因过滤构建。

根据您需要传递的分支,在请求中添加 BuildReason.PullRequest 值可能是多余的。

var pr = new GitPullRequest(); // the PR you've received after creation
var requestedFor = pr.CreatedBy.DisplayName;
var repo = pr.Repository.Id.ToString();
var branch = $"refs/pull/{pr.PullRequestId}/merge";
var reason = BuildReason.PullRequest;

var buildClient = c.GetClient<BuildHttpClient>();
var blds = await buildClient.GetBuildsAsync("myProject", 
                                            branchName: branch,
                                            repositoryId: repo,
                                            requestedFor: requestedFor,
                                            reasonFilter: reason,
                                            repositoryType: "TfsGit");

在您的问题中,您提到要为拉取请求构建(单一),这意味着您只有一个构建定义作为策略门。此方法可以根据目标分支上的策略配置 return 多个 Builds。但是,如果那是您的设置,那么您的问题将是要求您等待完成 PR 的所有相关构建似乎是合乎逻辑的。

我正在研究 Policy Evaluations 看看是否有更直接的方法来通过策略获取构建的 ID 运行,但我无法格式化按照以下方式正确请求:

Evaluations are retrieved using an artifact ID which uniquely identifies the pull request. To generate an artifact ID for a pull request, use this template:

vstfs:///CodeReview/CodeReviewId/{projectId}/{pullRequestId}

即使使用 GetById 方法在 PR 的 artifactId 字段中使用 returned 的值也会导致 Doesn't exist or Don't have access 响应,因此如果其他人知道如何使用这个方法,如果它给出了为策略配置评估的确切构建 ID,我会很高兴听到它。


获得您真正想要的东西的替代方法

听起来分支策略的唯一用途是 运行 在完成合并之前进行“门构建”。

为什么不 create the PR 自动完成。

Name - autoCompleteSetBy

Type - IdentityRef

Description - If set, auto-complete is enabled for this pull request and this is the identity that enabled it.

var me = new IdentityRef(); // you obviously need to populate this with real values
var prClient = connection.GetClient<GitHttpClient>();
await prClient.CreatePullRequestAsync(new GitPullRequest()
                                      {
                                          CreatedBy = me,
                                          AutoCompleteSetBy = me,
                                          Commits = new GitCommitRef[0],
                                          SourceRefName = "feature/myFeature",
                                          TargetRefName = "master",
                                          Title = "Some good title for my PR"
                                      },
                                      "myBestRepository",
                                      true);