private static IEnumerable <VssCredentials> CredentialsFactory(AuthenticationTypes t) { if (t.HasFlag(AuthenticationTypes.OpenAuthorization)) { foreach (var cred in Qwiq.Credentials.CredentialsFactory.GetOAuthCredentials()) { yield return(cred); } } if (t.HasFlag(AuthenticationTypes.PersonalAccessToken)) { foreach (var cred in Qwiq.Credentials.CredentialsFactory.GetServiceIdentityPatCredentials()) { yield return(cred); } } if (t.HasFlag(AuthenticationTypes.Windows)) { foreach (var cred in Qwiq.Credentials.CredentialsFactory.GetServiceIdentityCredentials()) { yield return(cred); } } if (t.HasFlag(AuthenticationTypes.Basic)) { foreach (var cred in Qwiq.Credentials.CredentialsFactory.GetBasicCredentials()) { yield return(cred); } } if (t.HasFlag(AuthenticationTypes.Windows)) { var storage = new VssClientCredentialStorage(); // User did not specify a username or a password, so use the process identity yield return(new VssClientCredentials(new WindowsCredential(false)) { Storage = storage, PromptType = CredentialPromptType.DoNotPrompt }); // Use the Windows identity of the logged on user yield return(new VssClientCredentials(true) { Storage = storage, PromptType = CredentialPromptType.PromptIfNeeded }); } }
static void Main(string[] args) { var prDict = new Dictionary <Guid, Dictionary <int, GitPullRequest> >(); var ccs = new VssClientCredentialStorage(); var pullRequestBuildStatuses = new Dictionary <int, PullRequestBuildStatus>(); VssCredentials cred = new VssClientCredentials(false); cred.Storage = ccs; cred.PromptType = CredentialPromptType.PromptIfNeeded; var conn = new VssConnection(ServerUri, cred); var buildServer = conn.GetClient <BuildHttpClient>(); var gitClient = conn.GetClient <GitHttpClient>(); var builds = buildServer.GetBuildsAsync("Zoodata Inspect").Result; foreach (var build in builds) { var projectId = build.Project.Id; if (build.Reason == BuildReason.ValidateShelveset) { var type = build.Repository.Type; if (type == "TfsGit") { if (!prDict.ContainsKey(projectId)) { prDict.Add(build.Project.Id, new Dictionary <int, GitPullRequest>()); int offset = 0; int resultCount; do { var results = gitClient.GetPullRequestsByProjectAsync(projectId, new GitPullRequestSearchCriteria() { Status = PullRequestStatus.All }, skip: offset).Result; foreach (var gitPullRequest in results) { prDict[gitPullRequest.Repository.ProjectReference.Id][gitPullRequest.PullRequestId] = gitPullRequest; } resultCount = results.Count; offset = offset + resultCount; } while (resultCount > 0); } var prId = Convert.ToInt32(build.SourceBranch.Split('/')[2]); var pullRequest = prDict[projectId][prId]; if (pullRequest == null) { pullRequest = gitClient.GetPullRequestAsync(projectId, build.Repository.Id, prId).Result; prDict[projectId].Add(prId, pullRequest); } if (!pullRequestBuildStatuses.ContainsKey(prId)) { pullRequestBuildStatuses.Add(prId, pullRequest.ToBuildStatus(build)); } } } } foreach (var status in pullRequestBuildStatuses.Values.Where(pr => pr.Status == PullRequestStatus.Active).OrderBy(p => p.Id)) { Console.WriteLine(status.DisplayString); } Console.ReadLine(); }
/// <summary> /// Connect to TFS - this may ask the user for credentials, depending on the status of the connection /// </summary> /// <returns></returns> public async Task Connect() { /* * eg: https://company.visualstudio.com/defaultcollection/_apis/projects?api-version=1.0 * * // Create instance of VssConnection using AD Credentials for AD backed account * VssConnection connection = new VssConnection(new Uri(collectionUri), new VssAadCredential()); * * // VssCredentials - NTLM against an on-prem server * VssConnection connection = new VssConnection(new Uri(tfsUri), new VssCredentials()); * * // VssConnection - VS sign in prompt * VssConnection connection = new VssConnection(new Uri(collectionUri), new VssClientCredentials()); * */ var tfsUri = _configManager.TfsDefaultCollectionUrl; if (tfsUri == null) { return; } // Use VS client credentails, including default windows credential store for caching VssClientCredentialStorage storage = new VssClientCredentialStorage(); var creds = new VssClientCredentials(); creds.Storage = storage; // attempt to connect try { _currentConnection = new VssConnection(tfsUri, creds); await _currentConnection.ConnectAsync(VssConnectMode.Automatic); } catch (TaskCanceledException) { //todo - set some application state to reflect not logged in status return; } catch (VssServiceException) { // something bad happened - can't do a lot here... return; } if (_currentConnection.HasAuthenticated) { // update app with logged in credentials, if present. var details = new UserDetails(); details.DisplayName = _currentConnection.AuthorizedIdentity?.DisplayName; string connectedAccount = null; if (_currentConnection.AuthorizedIdentity?.Properties?.TryGetValue("Account", out connectedAccount) == true) { details.Account = connectedAccount; } _configManager.SetUserDetails(details); // get an REST wrapper for the build APIs _BuildClient = await _currentConnection.GetClientAsync <Microsoft.TeamFoundation.Build.WebApi.BuildHttpClient>(); } }