简体   繁体   中英

How can I programmatically link a commit to a work item in TFS? (need second GUID in LinkedArtifactURI)

I am attempting to programmatically link a commit to a work item in TFS and I think I'm close. When creating a new ExternalLink , I'm struggling to form the LinkedArtifactUri property. I can run a simple query for a work item to see this property should like something like the following:

"vstfs:///Git/Commit/ef43137b-a906-47b1-8dd4-0438b26c20c4%2fd211ee62-075d-4419-9d0b-cb58b76ae9dd%2fedfe1d438a77c2a95afa30e625f5dabe2656dea0"

So I set out to determine where the GUIDs in this string come from and I was able to figure out the first two, but am currently stumped on the last one. The first GUID is the project ID (retrievable by doing something like _store.Value.Projects[TEAM_NAME].guid . The second is the Git Repo ID. Can someone please inform me as to what the third GUID might be?

Here is my code that should work if I can figure out where to get that last GUID:

var item = _store.Value.GetWorkItem(165464); //work item id #
var gitRepoService = _projectCollection.GetService<GitRepositoryService>();
var gitProjectRepoService = gitRepoService.QueryRepositories("TEAM_NAME");
var defaultGitRepo = gitProjectRepoService.Single(gr => 
gr.Name.Equals("REPO_NAME"));

var gitGuid = defaultGitRepo.Id;

var proj = _store.Value.Projects["TEAM_NAME"];
var id = proj.Guid;

var linkToAdd = new ExternalLink(_store.Value.RegisteredLinkTypes[ArtifactLinkIds.Commit], $"vstfs:///Git/Commit/{id}%{gitGuid}%{Where_does_this_guid_come_from}");
item.Links.Add(linkToAdd);
item.Save();    

Thank you very much for your time. Please let me know if I am being unclear or if you need anything else from me.

The third GUID should be the commit ID , it's the full commit ID. You can get it via REST API

To link a commit to a work item, you can refer to this helpful article , it has the detail steps and code sample for your reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM