回答您提出的问题:
似乎不太容易找到为拉取请求提供单一确定性构建 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");
在您提到的问题中,您提到想要为拉取请求构建(单数),这意味着您只有一个构建定义充当策略门。此方法可以根据目标分支上的策略配置返回多个 Builds。但是,如果这是您的设置,那么您的问题将是询问您等待完成 PR 的所有相关构建,这似乎是合乎逻辑的。
我正在查看 Policy Evaluations 以查看是否有更直接的方法来获取正在通过策略运行的构建的 ID,但我无法按照以下方式正确格式化请求:
使用唯一标识拉取请求的工件 ID 检索评估。要为拉取请求生成工件 ID,请使用此模板:
vstfs:///CodeReview/CodeReviewId/{projectId}/{pullRequestId}
即使使用 GetById 方法在 PR 上的 artifactId 字段中返回的值也会导致 Doesn't exist or Don't have access 响应,因此如果其他人知道如何使用此方法并且如果它给出了正在评估的准确构建 ID对于策略配置,我很高兴听到它。
获得您真正想要的东西的替代方法
听起来分支策略的唯一用途是在完成合并之前运行“门构建”。
为什么不用create the PR 自动完成。
名称 - autoCompleteSetBy
类型 - IdentityRef
描述 - 如果设置,则为此拉取请求启用自动完成,这是启用它的身份。
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);