Search code examples
c#gittfspull-requesttfs-sdk

How to get a web URL for a PullRequest using TFS client libraries?


Is there a way to get web URL(TeamWebAccess like https://tfshost/tfs/Collection/Project/_git/Repository/pullrequest/12345 for PR#12345, not the API url) for a PullRequest using TFS client libraries?

var prId = 12345;
var tpc = new TfsTeamProjectCollection(new Uri(""));
var git = tpc.GetClient<GitHttpClient>();
// GitHttpClient has no special methods to do it
// and GitPullRequest obtained through GitHttpClient.GetPullRequestByIdAsync contains only the API link
var prWebUrl = git.?????(prId);

Perhaps there is something analogous to TswaClientHyperlinkService that I missed?

Or do I have to resort to deducing web URL from API link/GitPullRequest properties?


Solution

  • It's not able to get the RemoteUrl from the client library direrctly, as the value is null. Check the screenshot below:

    enter image description here

    But you could get the RemoteUrl for a repository (https://tfshost/tfs/Collection/Project/_git/Repository), so you can use the following code to get the PR RemoteUrl:

    using Microsoft.TeamFoundation.SourceControl.WebApi;
    using Microsoft.VisualStudio.Services.Common;
    using Microsoft.VisualStudio.Services.WebApi;
    using System;
    
    namespace GetPullRequest
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                String collectionUri = "https://xxx.visualstudio.com";
                VssBasicCredential creds = new VssBasicCredential("", "6ztnrtjdd3i42juchu4xxxxxxxxxaslnseo277tgiuiq");
                VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
                var git = connection.GetClient<GitHttpClient>();
                var prId = 12345;
                var pr = git.GetPullRequestByIdAsync(prId).Result;
                var RepoUrl = pr.Repository.RemoteUrl;
                var prUrl = RepoUrl + "/pullrequest/" + prId;
                Console.WriteLine(prUrl);
    
            }
        }
    }