我有一个使用Git进行源代码管理的VSTS项目。该项目有多个存储库(准确地说是11个),每个存储库都有多个分支。
鉴于VSTS ID,我试图获取与该Id相关联的所有提交的列表。
我目前编码如下
VSTSHelper helper = new VSTSHelper(); // my helper for establishing a connection
ProjectHttpClient projectClient = helper.Connection.GetClient<ProjectHttpClient>();
GitHttpClient gitClient = helper.Connection.GetClient<GitHttpClient>();
Microsoft.TeamFoundation.Core.WebApi.TeamProject project = projectClient.GetProject(helper.Project).Result;
List<GitRepository> gitRepositoryList = gitClient.GetRepositoriesAsync(project.Id).Result;
GitQueryCommitsCriteria criteria = new GitQueryCommitsCriteria()
{
IncludeWorkItems = true
};
foreach (GitRepository repo in gitRepositoryList)
{
List<GitBranchStats> branchStatsList = gitClient.GetBranchesAsync(repo.Id).Result;
foreach (GitBranchStats branchStats in branchStatsList)
{
criteria.ItemVersion = new GitVersionDescriptor() { Version = branchStats.Name, VersionType = GitVersionType.Branch };
List<GitCommitRef> commits = gitClient.GetCommitsAsync(repo.Id, criteria).Result;
if (commits.Count > 0)
{
List<GitCommitRef> commitsWithWorkItems = commits.Where(pc => pc.WorkItems.Count > 0).ToList();
if (commitsWithWorkItems.Count > 0)
{
// WorkItemIds is a list of int
List<GitCommitRef> workItemCommits = projectCommitsWithWorkItems.Where(
pc => pc.WorkItems.Any(x => this.WorkItemIds.Contains(Convert.ToInt32(x.Id)))).ToList();
// get the changes associated with the commit here
if (workItemCommits.Count > 0)
{
foreach (GitCommitRef wiCommit in workItemCommits)
{
GitCommitChanges wiChanges = gitClient.GetChangesAsync(wiCommit.CommitId, repo.Id).Result;
}
}
}
}
}
}
传递给我的代码的工作项ID(例如Id = 810)具有最初针对另一个分支中的另一个工作项ID(例如675)提交的代码,然后移动到给定的Id。然后编辑代码,然后针对新的Id(810)
进行提交我上面的代码只发现对项目675的原始提交 - 并且所有代码更改都显示在此Id上 - 包括那些我期望看到810的内容.Id 810没有返回任何内容。
尽管有很多谷歌搜索,我发现自己很难过得很开心,我认为我误解了一些重要的时间!
非常感谢任何正确方向的帮助或指示。
答案 0 :(得分:0)
您可以使用以下代码获取链接到给定工作项的所有提交:
作为REST API GET a work item with Fully expanded,relations
对象下存在所有链接信息。然后,您可以通过过滤包含vstfs:///Git/Commit
的网址来获取提交。
int id = workitemID;
var token = "PAT";
String Uri = "https://account.visualstudio.com/DefaultCollection";
var Connection1 = new VssConnection(new Uri(Uri), new VssBasicCredential(string.Empty, token));
WorkItemTrackingHttpClient workItemTrackingClient = Connection1.GetClient<WorkItemTrackingHttpClient>();
WorkItem workitem = workItemTrackingClient.GetWorkItemAsync(id, expand: WorkItemExpand.All).Result;
Console.WriteLine("Related commits contain: ");
foreach (var relation in workitem.Relations)
{
if (relation.Url.Contains("vstfs:///Git/Commit"))
{
Console.WriteLine("commit: {0}", relation.Url);
}
}
要获取提交sha-1值(提交ID),它位于relation.Url
的最后40个字符中,它们与存储库ID分隔%2f
。因此,网址格式实际上是vstfs:///Git/Commit/{projectID}%2f{repositoryID}%2f{commitID}
。
例如relation.Url
如下:
vstfs:///Git/Commit/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4%2fad3acf8e-b269-48e5-81bc-354251856b51%2fb802c91e68f321676fe31eca9dda048e7032ea11"
b802c91e68f321676fe31eca9dda048e7032ea11
。ad3acf8e-b269-48e5-81bc-354251856b51
。f7855e29-6f8d-429d-8c9b-41fd4d7e70a4
。