public virtual async Task SetUp() { var collectionUrl = Environment.GetEnvironmentVariable("WILINQ_TEST_TPCURL"); if (string.IsNullOrWhiteSpace(collectionUrl)) { throw new InvalidOperationException("Environment variable 'WILINQ_TEST_TPCURL' is missing"); } var personnalAccessToken = Environment.GetEnvironmentVariable("WILINQ_TEST_PAT"); VssCredentials vssCredentials; if (string.IsNullOrWhiteSpace(personnalAccessToken)) { vssCredentials = new VssClientCredentials { Storage = new VssClientCredentialStorage() }; } else { vssCredentials = new VssBasicCredential(string.Empty, personnalAccessToken); } var connection = new VssConnection(new Uri(collectionUrl), vssCredentials); await connection.ConnectAsync(); Client = await connection.GetClientAsync <WorkItemTrackingHttpClient>(); var projectHttpClient = await connection.GetClientAsync <ProjectHttpClient>(); Project = await projectHttpClient.GetProject("WiLinqTestProject"); TestSessionId = Guid.NewGuid().ToString(); }
public void PrepairProjectForCreation(Project project) { ProjectHttpClient projClient; TeamProjectReference onlineProject; Uri tfsUri = new Uri(project.ProjectAreaPath); VssBasicCredential credentials = new VssBasicCredential(project.UserName, project.Password); VssConnection connection = new VssConnection(tfsUri, credentials); _teamClient = connection.GetClientAsync <TeamHttpClient>().Result; projClient = connection.GetClientAsync <ProjectHttpClient>().Result; _workItemTrackingClient = connection.GetClient <WorkItemTrackingHttpClient>(); IPagedList <TeamProjectReference> projects = projClient.GetProjects().Result; onlineProject = projects.FirstOrDefault(pro => pro.Name.ToUpperInvariant().Equals(project.ProjectName.ToUpperInvariant(), StringComparison.InvariantCulture)); if (onlineProject != null) { project.CreationDate = DateTime.Now; OnlineTfsTeamProjectName = project.ProjectName; OnlineTfsProjectId = onlineProject.Id.ToString(); project.ProjectType = Projects.Enums.ProjectType.Online.ToString(); project.Password = project.AuthType == 1 ? encryption.DecryptString(project.Password, DecryptionKey) : project.Password; ConnectToOnLineTfsAndCreateQuries(project.ProjectName); } else { throw new UserFriendlyException($"Project Path: '{project.ProjectAreaPath}' Was Not Found!"); } }
public static async Task Main(string[] args) { var client = new SecretClient( vaultUri: new Uri("https://roslyninfra.vault.azure.net:443"), credential: new DefaultAzureCredential(includeInteractiveCredentials: true)); var azureDevOpsSecret = await client.GetSecretAsync("vslsnap-vso-auth-token"); using var connection = new VssConnection( new Uri("https://devdiv.visualstudio.com/DefaultCollection"), new WindowsCredential(new NetworkCredential("vslsnap", azureDevOpsSecret.Value.Value))); using var gitClient = await connection.GetClientAsync <GitHttpClient>(); using var buildClient = await connection.GetClientAsync <BuildHttpClient>(); var visualStudioReleases = await GetVisualStudioReleasesAsync(gitClient); var roslynRepository = new Repository(args[0]); var existingTags = roslynRepository.Tags.ToImmutableArray(); foreach (var visualStudioRelease in visualStudioReleases) { var roslynTagName = TryGetRoslynTagName(visualStudioRelease); if (roslynTagName != null) { if (!existingTags.Any(t => t.FriendlyName == roslynTagName)) { Console.WriteLine($"Tag {roslynTagName} is missing."); var roslynBuild = await TryGetRoslynBuildForReleaseAsync(visualStudioRelease, gitClient, buildClient); if (roslynBuild != null) { Console.WriteLine($"Tagging {roslynBuild.CommitSha} as {roslynTagName}."); string message = $"Build Branch: {roslynBuild.SourceBranch}\r\nInternal ID: {roslynBuild.BuildId}\r\nInternal VS ID: {visualStudioRelease.BuildId}"; roslynRepository.ApplyTag(roslynTagName, roslynBuild.CommitSha, new Signature("dotnet bot", "*****@*****.**", when: visualStudioRelease.CreationTime), message); } else { Console.WriteLine($"Unable to find the build for {roslynTagName}."); } } else { Console.WriteLine($"Tag {roslynTagName} already exists."); } } } }
public async Task <bool> ArePendingBuilds(string projectName, string buildDefinitionName) { var buildClient = await _connection.GetClientAsync <BuildHttpClient>(); var buildDefinitionId = await GetBuildDefinitionId(projectName, buildDefinitionName); var pendingBuilds = await buildClient.GetBuildsAsync( project : projectName, definitions : new[] { buildDefinitionId }, statusFilter : BuildStatus.NotStarted | BuildStatus.Postponed); return(pendingBuilds.Count != 0); }
static void CloneRepositories(Options options) { VssCredentials creds = new VssBasicCredential(string.Empty, options.AccessToken); // Connect to Azure DevOps Services VssConnection connection = new VssConnection(options.CollectionUri, creds); ProjectHttpClient projClient = connection.GetClientAsync <ProjectHttpClient>().Result; var projects = projClient.GetProjects().Result; var cloneOptions = new CloneOptions() { CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = options.UserName, Password = options.AccessToken, } }; GitHttpClient gitClient = connection.GetClient <GitHttpClient>(); foreach (var project in projects) { var repositories = gitClient.GetRepositoriesAsync(project.Name).Result; if (repositories != null) { foreach (var repo in repositories) { string cloneDir = Path.Combine(options.OutputPath, project.Name, repo.Name); Console.WriteLine($"Cloning: {repo.RemoteUrl} to {cloneDir}"); Repository.Clone(repo.RemoteUrl, cloneDir, cloneOptions); } } } }
/// <summary> /// Retrieves all active pull requests this user has created. /// </summary> /// <returns>An async stream of <see cref="PullRequestViewElement"/></returns> public async IAsyncEnumerable <PullRequestViewElement> FetchCreatedPullRequests() { foreach (var accountGroup in m_config.AccountsByUri) { Uri organizationUri = accountGroup.Key; using VssConnection connection = await GetConnectionAsync(organizationUri, accountGroup.Value); using GitHttpClient client = await connection.GetClientAsync <GitHttpClient>(); { // Capture the currentUserId so it can be used to filter PR's later. // Guid userId = connection.AuthorizedIdentity.Id; // Only fetch pull requests which are active, and assigned to this user. // GitPullRequestSearchCriteria criteria = new GitPullRequestSearchCriteria { CreatorId = userId, Status = PullRequestStatus.Active, IncludeLinks = false, }; foreach (AccountConfig account in accountGroup.Value) { List <GitPullRequest> requests = await client.GetPullRequestsByProjectAsync(account.Project, criteria); foreach (var request in requests) { yield return(new PullRequestViewElement(request, account.Handler !) { CreatedMode = true });
/// <summary> /// Gets all team members for the given <paramref name="projectId" /> and <paramref name="teamId"/>. /// </summary> /// <param name="client">The <see cref="TeamHttpClient" /> to use.</param> /// <param name="connection">The connection for the <paramref name="client"/> that will be used to retrieve the identities for the team members.</param> /// <param name="projectId">The project identifier.</param> /// <param name="teamId">The team identifier whose members to retrieve.</param> /// <param name="pageSize">Page size to use while retrieving the projects.</param> /// <param name="userState">The user state object.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"> /// </exception> /// <exception cref="System.ArgumentException">$The '{nameof(connection)}' parameter must be for the given '{nameof(client)}'</exception> public static async Task <IReadOnlyCollection <Identity> > GetAllTeamMembers(this TeamHttpClient client, VssConnection connection, string projectId, string teamId, int pageSize = 10, object userState = null, CancellationToken cancellationToken = default(CancellationToken)) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (connection == null) { throw new ArgumentNullException(nameof(connection)); } if (projectId == null) { throw new ArgumentNullException(nameof(projectId)); } if (teamId == null) { throw new ArgumentNullException(nameof(teamId)); } if (Equals(client.BaseAddress, connection.Uri)) { throw new ArgumentException($"The '{nameof(connection)}' parameter must be for the base uri of the VSTS / TFS system", nameof(connection)); } var result = new List <Identity>(); var identityReferences = new List <IdentityRef>(); int currentPage = 0; var teamMembersForCurrentPage = (await client.GetTeamMembersAsync(projectId, teamId, pageSize, currentPage, userState, cancellationToken).ConfigureAwait(false)).ToList(); while (teamMembersForCurrentPage.Count > 0) { cancellationToken.ThrowIfCancellationRequested(); identityReferences.AddRange(teamMembersForCurrentPage); // check whether the recently returned item(s) were less than the max page size if (teamMembersForCurrentPage.Count < pageSize) { break; // if so, break the loop as we've read all instances } // otherwise continue cancellationToken.ThrowIfCancellationRequested(); teamMembersForCurrentPage = (await client.GetTeamMembersAsync(projectId, teamId, pageSize, currentPage, userState, cancellationToken).ConfigureAwait(false)).ToList(); } cancellationToken.ThrowIfCancellationRequested(); if (identityReferences.Count > 0) { using (var identityHttpClient = await connection.GetClientAsync <IdentityHttpClient>(cancellationToken).ConfigureAwait(false)) { result.AddRange(await identityHttpClient.ReadIdentitiesAsync(identityReferences.Select(identityReference => new Guid(identityReference.Id)).ToList(), cancellationToken: cancellationToken).ConfigureAwait(false)); } } cancellationToken.ThrowIfCancellationRequested(); return(result); }
public override async Task Connect(string personalAccessToken) { Uri accountUri = new Uri(this.AccountOrOrganization, UriKind.Absolute); VssConnection connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken)); await connection.ConnectAsync(); _witClient = await connection.GetClientAsync <WorkItemTrackingHttpClient>(); }
/// <summary> /// Parametrized method which returns http client of TFS /// </summary> /// <typeparam name="T">Http client type</typeparam> /// <param name="orgName">TFS organization name</param> /// <param name="PatToken">TFS PAT token</param> /// <returns></returns> public async static Task <T> GetTFSHttpClient <T>(string orgName, string PatToken) where T : VssHttpClientBase { var u = new Uri($"https://dev.azure.com/" + orgName); VssCredentials c = new VssCredentials(new VssBasicCredential(string.Empty, PatToken)); var connection = new VssConnection(u, c); // check connection await connection.ConnectAsync(); return(await connection.GetClientAsync <T>()); }
public async Task <IEnumerable <string> > GetAllWorkItemTypesAsync() { var vssConnection = new VssConnection(_versionControlServer.TeamProjectCollection.Uri, _versionControlServer.TeamProjectCollection.ClientCredentials); using (var projectClientHttpClient = await vssConnection.GetClientAsync <ProjectHttpClient>()) using (var workItemTrackingHttpClient = await vssConnection.GetClientAsync <WorkItemTrackingHttpClient>()) { var projects = await projectClientHttpClient.GetProjects(ProjectState.All, null, null, null, null); var projectGuids = projects.Select(x => x.Id).ToList(); var result = await QueuingTask.WhenAll(projectGuids, x => workItemTrackingHttpClient.GetWorkItemTypesAsync(x)); return(result .Select(x => x.Name) .Distinct() .OrderBy(x => x) .ToList()); } }
public async Task <IList <Commit> > GetPullRequestCommitsAsync(string pullRequestUrl) { VssConnection connection = CreateConnection(pullRequestUrl); GitHttpClient client = await connection.GetClientAsync <GitHttpClient>(); (string team, string repo, int id) = ParsePullRequestUri(pullRequestUrl); List <GitCommitRef> commits = await client.GetPullRequestCommitsAsync(team, repo, id); return(commits.Select(c => new Commit(c.Author.Name, c.CommitId)).ToList()); }
public async Task DownloadArtifacts(string basePath, string project, string definitionName, string artifactName, string sourceBranch, string targetBranchName, int buildId, int runLimit) { Directory.CreateDirectory(basePath); var connection = new VssConnection(new Uri(_collectionUri), new VssBasicCredential(string.Empty, _pat)); var client = await connection.GetClientAsync <BuildHttpClient>(); if (!targetBranchName.StartsWith("refs/heads/")) { targetBranchName = "refs/heads/" + targetBranchName; } Console.WriteLine($"Getting definitions ({basePath}, {project}, {definitionName}, {artifactName}, {sourceBranch}, {targetBranchName}, {buildId}, {runLimit})"); var definitions = await client.GetDefinitionsAsync(project, name : definitionName); Console.WriteLine("Getting builds"); var builds = await client.GetBuildsAsync(project, definitions : new[] { definitions.First().Id }, branchName : targetBranchName, top : runLimit, queryOrder : BuildQueryOrder.FinishTimeDescending, statusFilter : BuildStatus.Completed); var currentBuild = await client.GetBuildAsync(project, buildId); foreach (var build in builds.Concat(new[] { currentBuild }).Distinct(new BuildComparer())) { var tempFile = Path.GetTempFileName(); Console.WriteLine($"Getting artifact for build {build.Id}"); using (var stream = await client.GetArtifactContentZipAsync(project, build.Id, artifactName)) { using (var f = File.OpenWrite(tempFile)) { await stream.CopyToAsync(f); } } var fullPath = Path.Combine(basePath, $"{build.LastChangedDate:yyyyMMdd-hhmmss}-{build.Id}"); Console.WriteLine($"Extracting artifact for build {build.Id}"); ZipFile.ExtractToDirectory(tempFile, fullPath); foreach (var file in Directory.EnumerateFiles(fullPath, "*.*", SearchOption.AllDirectories)) { if (Path.GetDirectoryName(file) != fullPath) { var destFileName = Path.Combine(fullPath, Path.GetFileName(file)); if (!File.Exists(destFileName)) { File.Move(file, destFileName); } } } } }
public override async Task Connect(Uri projectUri, string personalAccessToken) { _projectName = projectUri.GetProjectName(); string accountUriString = projectUri.GetAccountUriString(); Uri accountUri = new Uri(accountUriString, UriKind.Absolute); VssConnection connection = new VssConnection(accountUri, new VssBasicCredential(string.Empty, personalAccessToken)); await connection.ConnectAsync(); _witClient = await connection.GetClientAsync <WorkItemTrackingHttpClient>(); }
static async Task Main(string[] args) { if (args.Length == 2) { Uri orgUrl = new Uri("https://dev.azure.com/dnceng"); string personalAccessToken = args[0]; int numberOfBuilds = int.Parse(args[1]); // Create a connection VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken)); var buildClient = await connection.GetClientAsync <BuildHttpClient>(); var definition = await buildClient.GetDefinitionsAsync("internal", name : "dotnet-runtime-official"); var builds = await buildClient.GetBuildsAsync("internal", definitions : definition.Select(def => def.Id), branchName : "refs/heads/master", statusFilter : BuildStatus.Completed, resultFilter : BuildResult.Succeeded, top : numberOfBuilds); var timelines = await Task.WhenAll(builds.Select(async build => (timeline: await buildClient.GetBuildTimelineAsync(build.Project.Name, build.Id), project: build.Project.Name, id: build.Id))); var signingTimes = await Task.WhenAll(timelines.Select(timeline => CalculateSigningTime(buildClient, timeline.timeline, timeline.project, timeline.id))); var validationDurations = timelines.Select(t => t.timeline.Records.First(r => r.Name == "Validate").GetDuration()); var futurePromotionTimes = signingTimes.Zip(validationDurations, (sign, validate) => sign + validate); var buildWithoutSignOrValidate = builds.Select(build => build.FinishTime - build.StartTime).Zip(futurePromotionTimes, (buildTime, promotionTime) => buildTime.Value - promotionTime).ToArray(); var publishingDurations = timelines.Select(t => t.timeline.Records.First(r => r.Name == ".NET Core 5 Dev Publishing" || r.Name == ".NET 5 Dev Publishing").GetDuration()); Array.Sort(signingTimes); TimeSpan averageSigning = TimeSpan.FromMilliseconds(signingTimes.Average(time => time.TotalMilliseconds)); TimeSpan averageBuildNoSignValidate = TimeSpan.FromMilliseconds(buildWithoutSignOrValidate.Average(time => time.TotalMilliseconds)); TimeSpan averagePublishTime = TimeSpan.FromMilliseconds(publishingDurations.Average(time => time.TotalMilliseconds)); TimeSpan median = signingTimes[signingTimes.Length / 2]; Console.WriteLine($"Time spent signing over the last {numberOfBuilds} official builds for dotnet/runtime"); Console.WriteLine($"Average time spent signing in the build: {averageSigning}"); Console.WriteLine($"Median time spent signing in the build: {median}"); Console.WriteLine(); Console.WriteLine($"Average build time without signing/validation: {averageBuildNoSignValidate}"); Console.WriteLine($"Average time spent publishing : {averagePublishTime}. We have on average {TimeSpan.FromMinutes(45) - averagePublishTime} left for build."); Console.WriteLine($"On average, product build (no signing, validation, or publishing) takes about {averageBuildNoSignValidate - averagePublishTime}."); } else { Console.WriteLine("Usage: SigningTime {personalAccessToken} {numberOfBuilds}"); } }
private async Task <PipelineCacheClient> CreateClientAsync( BlobStoreClientTelemetry blobStoreClientTelemetry, AgentTaskPluginExecutionContext context, VssConnection connection) { var tracer = context.CreateArtifactsTracer(); IClock clock = UtcClock.Instance; var pipelineCacheHttpClient = await connection.GetClientAsync <PipelineCacheHttpClient>(); var pipelineCacheClient = new PipelineCacheClient(blobStoreClientTelemetry, pipelineCacheHttpClient, clock, tracer); return(pipelineCacheClient); }
protected override async Task OnInitializedAsync() { var creds = new VssBasicCredential(string.Empty, Config.GetSection("DevOpsPAT").Value); var connection = new VssConnection(new Uri(Config.GetSection("DevOpsURL").Value), creds); try { var projectclient = await connection.GetClientAsync <ProjectHttpClient>(); projects = await projectclient.GetProjects(); buildclient = await connection.GetClientAsync <BuildHttpClient>(); relclient = await connection.GetClientAsync <ReleaseHttpClient>(); if (Builds == null) { Builds = new(); } if (BuildRelease == null) { BuildRelease = new(); } if (Releases == null) { Releases = new(); } await LoadData(); Connected = true; } catch { Connected = false; } }
/// <summary> /// Attempts to set up a connection to the VSO URL specified by the <see cref="VSOOutput"/> /// </summary> /// <returns>True if the connection was set up successfully</returns> public async Task <bool> Connect() { try { var creds = new VssClientCredentials { PromptType = CredentialPromptType.PromptIfNeeded }; _connection = new VssConnection(_output.URL, creds); _workItemClient = await _connection.GetClientAsync <WorkItemTrackingHttpClient>(); _projectClient = await _connection.GetClientAsync <ProjectHttpClient>(); _workClient = await _connection.GetClientAsync <WorkHttpClient>(); _buildClient = await _connection.GetClientAsync <BuildHttpClient>(); return(true); } catch (Exception) { return(false); } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { // Get off the calling thread immediately await Task.Yield(); try { _logger.LogInformation(new EventId(0, "StartingScanner"), "Starting pipeline scanner loop. Interval: {ScanInterval}.", _options.CurrentValue.ScanInterval); _buildClient = await _connection.GetClientAsync <BuildHttpClient>(stoppingToken); while (!stoppingToken.IsCancellationRequested) { var options = _options.CurrentValue; using (var scope = _scopeFactory.CreateScope()) { var context = new TestResultsScrapeContext(scope.ServiceProvider.GetRequiredService <TestResultsDbContext>(), _buildClient, options, _loggerFactory.CreateLogger <TestResultsScrapeContext>()); _logger.LogInformation(new EventId(0, "PrimingCaches"), "Priming caches..."); await context.PrimeCachesAsync(stoppingToken); _logger.LogInformation(new EventId(0, "PrimedCaches"), "Primed caches."); _logger.LogInformation(new EventId(0, "RunningScanLoop"), "Running scan loop."); foreach (var config in context.Options.Pipelines) { using (_logger.BeginScope("Pipeline: {PipelineProject}/{PipelineName}", config.Project, config.Name)) { _logger.LogInformation(new EventId(0, "ProcessingPipeline"), "Processing pipeline {PipelineProject}/{PipelineName}...", config.Project, config.Name); await ProcessPipelineAsync(context, config, stoppingToken); _logger.LogInformation(new EventId(0, "ProcessedPipeline"), "Processed pipeline {PipelineProject}/{PipelineName}.", config.Project, config.Name); } } } _logger.LogInformation("Sleeping for {ScanInterval}...", options.ScanInterval); await Task.Delay(options.ScanInterval); } } catch (OperationCanceledException) { _logger.LogInformation(new EventId(0, "CancellingSync"), "Cancelling pipeline sync."); } _logger.LogInformation(new EventId(0, "Stopped"), "Service Stopped."); }
public async Task ConnectToTfsServer2015Async(string hostname, string teamCollection, string projectName, Regex buildDefinitionNameFilter = null) { _hostname = hostname; _isWebServer = _hostname.Contains("://"); try { string url; if (_isWebServer) { _hostname = _hostname.TrimEnd('\\', '/'); url = _hostname + "/" + teamCollection; _urlPrefix = hostname + "/" + teamCollection + "/" + projectName + "/_build?_a=summary&buildId="; } else { url = "http://" + _hostname + ":8080/tfs/" + teamCollection; _urlPrefix = "http://" + hostname + ":8080/tfs/" + (string.IsNullOrEmpty(teamCollection) ? "" : teamCollection + "/") + projectName + "/_build?_a=summary&buildId="; } var connection = new VssConnection(new Uri(url), new VssCredentials(true)); connection.Settings.BypassProxyOnLocal = false; BuildHttpClient buildClient = await connection.GetClientAsync <BuildHttpClient>().ConfigureAwait(false); var definitions = await buildClient.GetDefinitionsAsync(project : projectName).ConfigureAwait(false); var buildDefinitions = new List <DefinitionReference>(); foreach (var def in definitions) { if (string.IsNullOrWhiteSpace(buildDefinitionNameFilter.ToString()) || buildDefinitionNameFilter.IsMatch(def.Name)) { buildDefinitions.Add(def); } } _buildDefinitions2015 = buildDefinitions.ToArray(); _buildClient = buildClient; _projectName = projectName; } catch (Exception ex) { Trace.WriteLine(ex.Message); } }
public async Task <IWorkItemClient> Create(string projectCollectionUri, string patToken) { if (string.IsNullOrWhiteSpace(projectCollectionUri)) { throw new ArgumentException("This argument is required.", nameof(projectCollectionUri)); } if (string.IsNullOrWhiteSpace(patToken)) { throw new ArgumentException("This argument is required.", nameof(patToken)); } var connection = new VssConnection(new Uri(projectCollectionUri), new VssBasicCredential(string.Empty, patToken)); var witClient = await connection.GetClientAsync <WorkItemTrackingHttpClient>(); return(new WorkItemClientAdapter(witClient)); }
public async Task <PullRequest> GetPullRequestAsync(string pullRequestUrl) { VssConnection connection = CreateConnection(pullRequestUrl); GitHttpClient client = await connection.GetClientAsync <GitHttpClient>(); (string team, string repo, int id) = ParsePullRequestUri(pullRequestUrl); GitPullRequest pr = await client.GetPullRequestAsync(team, repo, id); return(new PullRequest { Title = pr.Title, Description = pr.Description, BaseBranch = pr.TargetRefName, HeadBranch = pr.SourceRefName }); }
public async Task UpdatePullRequestAsync(string pullRequestUri, PullRequest pullRequest) { VssConnection connection = CreateConnection(pullRequestUri); GitHttpClient client = await connection.GetClientAsync <GitHttpClient>(); (string team, string repo, int id) = ParsePullRequestUri(pullRequestUri); await client.UpdatePullRequestAsync( new GitPullRequest { Title = pullRequest.Title, Description = pullRequest.Description }, team, repo, id); }
public async Task DownloadSampleAsync() { // Get the audit log client VssConnection connection = Context.Connection; AuditHttpClient auditClient = await connection.GetClientAsync <AuditHttpClient>(); // Download the log to a file foreach (string format in new[] { "json", "csv" }) { string fileName = $"{Path.GetTempFileName()}.{format}"; using (FileStream fileStream = File.Create(fileName)) using (Stream logStream = await auditClient.DownloadLogAsync(format)) { await logStream.CopyToAsync(fileStream); } Context.Log($"Log downloaded to {fileName}"); } }
public static async Task <Build> QueueBuild(ExecutionContext context, ILogger log, string channel = "beta") { var config = ConfigHelper.GetConfig(context); var pat = config[ConfigHelper.DevOpsPersonalAccessToken]; var collectionUri = config[ConfigHelper.DevOpsCollectionUri]; var project = config[ConfigHelper.DevOpsProject]; if (!int.TryParse(config[ConfigHelper.DevOpsBuildDefinitionId], out int buildDefinitionId)) { var message = $"Could not parse {ConfigHelper.DevOpsBuildDefinitionId} to an int."; log.LogCritical(message); throw new InvalidOperationException(message); } using (var devOpsConnection = new VssConnection(new Uri(collectionUri), new VssBasicCredential(string.Empty, pat))) { var buildClient = await devOpsConnection.GetClientAsync <BuildHttpClient>(); var buildDefinition = await buildClient.GetDefinitionAsync(project, buildDefinitionId); if (buildDefinition == null) { var message = $"A build definition with the value {ConfigHelper.DevOpsBuildDefinitionId} was not found."; log.LogCritical(message); throw new InvalidOperationException(message); } var buildParameters = new Dictionary <string, string> { { "V8_CHANNEL", channel } }; var build = await buildClient.QueueBuildAsync(new Build { Definition = new DefinitionReference { Id = buildDefinition.Id, }, Project = buildDefinition.Project, Parameters = JsonConvert.SerializeObject(buildParameters) }); return(build); } }
public async Task InitializeAsync() { try { _logger.LogInformation($"Initializing Azure DevOps Code Search Client for {_projectUri}"); var creds = new VssClientCredentials(); var connection = new VssConnection(_projectUri, creds); _searchClient = await connection.GetClientAsync <SearchHttpClient>(); _logger.LogInformation($"Successfully initialized Azure DevOps Code Search Client for {_projectUri}"); // Kick off a warm up query await SearchCodeAsync(new SearchRequest { Filter = "_FastCodeNav_VSCode_Extension_WarmUp_", MaxResults = 1 }); } catch (Exception e) { _logger.LogError(e, $"Failed to initialize Azure DevOps Code Search Client for {_projectUri}"); } }
static async Task Main(string[] args) { if (args.Length == 2) { Uri orgUrl = new Uri("https://dev.azure.com/dnceng"); string personalAccessToken = args[0]; int numberOfBuilds = int.Parse(args[1]); // Create a connection VssConnection connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken)); var buildClient = await connection.GetClientAsync <BuildHttpClient>(); var definition = await buildClient.GetDefinitionsAsync("internal", name : "dotnet-runtime-official"); var builds = await buildClient.GetBuildsAsync("internal", definitions : definition.Select(def => def.Id), branchName : "refs/heads/master", statusFilter : BuildStatus.Completed, resultFilter : BuildResult.Succeeded, top : numberOfBuilds); var timelines = await Task.WhenAll(builds.Select(build => buildClient.GetBuildTimelineAsync(build.Project.Name, build.Id))); var buildTimeCurrent = timelines.Select(t => t.Records.First(r => r.Name == "Build" && r.RecordType == "Stage")).Select(r => r.GetDuration()).ToArray(); Array.Sort(buildTimeCurrent); OutputStatistics("Current product build times (including incremental signing steps):", buildTimeCurrent); var timeCurrentManifest = timelines.Select(t => SpeculativelyParallelizeTimeline_CurrentPlatformManifest(t)).ToArray(); Array.Sort(timeCurrentManifest); OutputStatistics("Likely best parallelization with current manifest generation:", timeCurrentManifest); var timeGeneratedManifest = timelines.Select(t => SpeculativelyParallelizeTimeline_GeneratedPlatformManifest(t)).ToArray(); Array.Sort(timeGeneratedManifest); OutputStatistics("Likely best parallelization with generated manifest:", timeGeneratedManifest); } else { Console.WriteLine("Usage: SpeculativeParallelization {personalAccessToken} {numberOfBuilds}"); } }
/// <summary> /// Generate an azure devops PAT with a given name, target organization set, and scopes. /// </summary> /// <param name="name"></param> /// <param name="targetScopes"></param> /// <param name="targetOrganizationNames"></param> /// <param name="validTo"></param> /// <returns>New PAT</returns> /// <exception cref="ArgumentException"></exception> /// <exception cref="Exception"></exception> public async Task <SessionToken> GeneratePATAsync( string name, AzureDevOpsPATScopes targetScopes, IEnumerable <string> targetOrganizationNames, DateTime validTo) { ValidateParameters(name, targetScopes, targetOrganizationNames, validTo); var minimalScopesList = targetScopes.GetMinimizedScopeList(); var scopesWithPrefixes = minimalScopesList.Select(scope => $"vso.{scope}"); string scopes = string.Join(" ", scopesWithPrefixes); try { using var tokenConnection = new VssConnection( new Uri("https://vssps.dev.azure.com/"), credentials); using var tokenClient = await tokenConnection.GetClientAsync <TokenHttpClient>(); var organizations = await GetAccountsByNameAsync(targetOrganizationNames); var tokenInfo = new SessionToken() { DisplayName = name, Scope = scopes, TargetAccounts = organizations.Select(account => account.AccountId).ToArray(), ValidFrom = DateTime.UtcNow, ValidTo = validTo, }; SessionToken pat = await tokenClient.CreateSessionTokenAsync( tokenInfo, SessionTokenType.Compact, isPublic : false); return(pat); } catch (Exception ex) { throw new Exception($"Failed to generate a PAT named '{name}' for organizatons '{string.Join(", ", targetOrganizationNames)}' and scopes '{scopes}'", ex); } }
public async Task <string> CreatePullRequestAsync(string repoUri, PullRequest pullRequest) { VssConnection connection = CreateConnection(repoUri); GitHttpClient client = await connection.GetClientAsync <GitHttpClient>(); (string team, string repo) = ParseRepoUri(repoUri); GitPullRequest createdPr = await client.CreatePullRequestAsync( new GitPullRequest { Title = pullRequest.Title, Description = pullRequest.Description, SourceRefName = "refs/heads/" + pullRequest.HeadBranch, TargetRefName = "refs/heads/" + pullRequest.BaseBranch }, team, repo); return(createdPr.Url); }
public async Task <Repository> Setup(IConfiguration config) { try { VssConnection connection = new VssConnection( new Uri(config.CollectionUri), new VssBasicCredential(config.MyEmail, config.Token)); // Get a GitHttpClient to talk to the Git endpoints this.gitClient = await connection.GetClientAsync <GitHttpClient>(); this.gitRepository = await this.gitClient.GetRepositoryAsync(config.ProjectName, this.project); return(this); } catch (Exception e) { Console.WriteLine(e.Message); return(this); } }
private async Task <T> GetClientAsync <T>() where T : VssHttpClientBase { var type = typeof(T); T result; await clientCacheSemaphore.WaitAsync(); if (clientCache.ContainsKey(type)) { result = (T)clientCache[type]; } else { result = await connection.GetClientAsync <T>(); clientCache.Add(type, result); } clientCacheSemaphore.Release(); return(result); }
/// <summary> /// Gets all team members for the given <paramref name="projectId" /> and <paramref name="teamId"/>. /// </summary> /// <param name="client">The <see cref="TeamHttpClient" /> to use.</param> /// <param name="connection">The connection for the <paramref name="client"/> that will be used to retrieve the identities for the team members.</param> /// <param name="projectId">The project identifier.</param> /// <param name="teamId">The team identifier whose members to retrieve.</param> /// <param name="pageSize">Page size to use while retrieving the projects.</param> /// <param name="userState">The user state object.</param> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> /// <exception cref="System.ArgumentNullException"> /// </exception> /// <exception cref="System.ArgumentException">$The '{nameof(connection)}' parameter must be for the given '{nameof(client)}'</exception> public static async Task<IReadOnlyCollection<Identity>> GetAllTeamMembers(this TeamHttpClient client, VssConnection connection, string projectId, string teamId, int pageSize = 10, object userState = null, CancellationToken cancellationToken = default(CancellationToken)) { if (client == null) throw new ArgumentNullException(nameof(client)); if (connection == null) throw new ArgumentNullException(nameof(connection)); if (projectId == null) throw new ArgumentNullException(nameof(projectId)); if (teamId == null) throw new ArgumentNullException(nameof(teamId)); if(Equals(client.BaseAddress, connection.Uri)) throw new ArgumentException($"The '{nameof(connection)}' parameter must be for the base uri of the VSTS / TFS system", nameof(connection)); var result = new List<Identity>(); var identityReferences = new List<IdentityRef>(); int currentPage = 0; var teamMembersForCurrentPage = (await client.GetTeamMembersAsync(projectId, teamId, pageSize, currentPage, userState, cancellationToken).ConfigureAwait(false)).ToList(); while (teamMembersForCurrentPage.Count > 0) { cancellationToken.ThrowIfCancellationRequested(); identityReferences.AddRange(teamMembersForCurrentPage); // check whether the recently returned item(s) were less than the max page size if (teamMembersForCurrentPage.Count < pageSize) break; // if so, break the loop as we've read all instances // otherwise continue cancellationToken.ThrowIfCancellationRequested(); teamMembersForCurrentPage = (await client.GetTeamMembersAsync(projectId, teamId, pageSize, currentPage, userState, cancellationToken).ConfigureAwait(false)).ToList(); } cancellationToken.ThrowIfCancellationRequested(); if (identityReferences.Count > 0) { using (var identityHttpClient = await connection.GetClientAsync<IdentityHttpClient>(cancellationToken).ConfigureAwait(false)) { result.AddRange(await identityHttpClient.ReadIdentitiesAsync(identityReferences.Select(identityReference => new Guid(identityReference.Id)).ToList(), cancellationToken: cancellationToken).ConfigureAwait(false)); } } cancellationToken.ThrowIfCancellationRequested(); return result; }