如何针对使用 Git 作为源代码管理的 VSTS WorkItem 获取提交
How to get the Commits against a VSTS WorkItem that uses Git as source control
我有一个使用 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 没有返回任何内容。
尽管谷歌搜索了很多,但我发现自己很挣扎,而且我认为我误会了很长时间!
任何正确方向的帮助或指示将不胜感激。
您可以使用以下代码获取对给定工作项 link 的所有提交:
作为REST API GET a work item with Fully expanded,所有link的信息都存在于relations
对象下。然后,您可以通过过滤包含 vstfs:///Git/Commit
.
的 url 来获取提交
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
。所以 url 格式实际上是 vstfs:///Git/Commit/{projectID}%2f{repositoryID}%2f{commitID}
.
例如下面的relation.Url
:
vstfs:///Git/Commit/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4%2fad3acf8e-b269-48e5-81bc-354251856b51%2fb802c91e68f321676fe31eca9dda048e7032ea11"
- 提交的 sha-1 值为
b802c91e68f321676fe31eca9dda048e7032ea11
。
- 存储库 ID 为
ad3acf8e-b269-48e5-81bc-354251856b51
。
- 项目 ID 为
f7855e29-6f8d-429d-8c9b-41fd4d7e70a4
。
我有一个使用 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 没有返回任何内容。
尽管谷歌搜索了很多,但我发现自己很挣扎,而且我认为我误会了很长时间!
任何正确方向的帮助或指示将不胜感激。
您可以使用以下代码获取对给定工作项 link 的所有提交:
作为REST API GET a work item with Fully expanded,所有link的信息都存在于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
。所以 url 格式实际上是 vstfs:///Git/Commit/{projectID}%2f{repositoryID}%2f{commitID}
.
例如下面的relation.Url
:
vstfs:///Git/Commit/f7855e29-6f8d-429d-8c9b-41fd4d7e70a4%2fad3acf8e-b269-48e5-81bc-354251856b51%2fb802c91e68f321676fe31eca9dda048e7032ea11"
- 提交的 sha-1 值为
b802c91e68f321676fe31eca9dda048e7032ea11
。 - 存储库 ID 为
ad3acf8e-b269-48e5-81bc-354251856b51
。 - 项目 ID 为
f7855e29-6f8d-429d-8c9b-41fd4d7e70a4
。