Думаю, мне удалось добиться того, чего я хочу, с помощью следующего кода:
private async Task<GitCommitRef> GetLastCommitOnBranch(TfsProject project, string repositoryId, string branchName)
{
GitHttpClient gitHttpClient = GetGitHttpClient(project);
GitQueryCommitsCriteria gitQueryCommitsCriteria = new GitQueryCommitsCriteria();
gitQueryCommitsCriteria.Top = 1;
GitVersionDescriptor gitVersionDescriptor = new GitVersionDescriptor();
gitVersionDescriptor.Version = branchName.Contains("refs/heads/") ? branchName.Substring("refs/heads/".Length) : branchName;
gitQueryCommitsCriteria.ItemVersion = gitVersionDescriptor;
List<GitCommitRef> commits = await gitHttpClient.GetCommitsAsync(repositoryId, gitQueryCommitsCriteria);
GitCommitRef commitRef = commits.FirstOrDefault();
return commitRef;
}
private async Task<IEnumerable<Build>> GetBuildsForLastCommitOnBranch(TfsProject project, string repositoryId, string branchName)
{
GitCommitRef commitRef = await GetLastCommitOnBranch(project, repositoryId, branchName);
BuildHttpClient buildHttpClient = GetBuildHttpClient(project);
List<Build> builds = await buildHttpClient.GetBuildsAsync(project.TfsProjectName, branchName: branchName, minFinishTime: commitRef.Author.Date, repositoryType: RepositoryTypes.Git);
IEnumerable<Build> buildsContainingLastCommit = builds.Where(b => b.SourceVersion == commitRef.CommitId);
return buildsContainingLastCommit;
}
public async Task<byte[]> GetActionScreenshotForLastCommitInBranch(TfsProject project, string repositoryId, string targetBranch, string currentBuildUrl, int testPlanId, int testCaseId, int testPointId, int iterationId, int actionId)
{
// Try to retrieve the builds containing the latest commit on target branch
IEnumerable<Build> buildsContainingLastCommit = await GetBuildsForLastCommitOnBranch(project, repositoryId, targetBranch);
ITestManagementTeamProject testManagementTeamProject = GetTeamProject(project);
ITestCase testCase = testManagementTeamProject.TestCases.Find(testCaseId);
ITestCaseResultCollection testCaseResultCollection = testManagementTeamProject.TestResults.ByTestId(testCaseId);
// Try to find a result for the test point that has been generated by a build related to the latest commit of the target branch
ITestCaseResult testCaseResult = testCaseResultCollection.FirstOrDefault(tcr => tcr.TestPointId == testPointId && tcr.TestCaseRevision == testCase.Revision && buildsContainingLastCommit.Count(b => b.BuildNumber == tcr.BuildNumber) > 0);
if (testCaseResult != null)
return GetActionScreenshotFromTestCaseResult(testCaseResult, iterationId, actionId);
return null;
}