/// <summary> /// Returns the build definition for the <paramref name="settings"/>. /// </summary> /// <param name="log">The Cake log context.</param> /// <param name="settings">Settings for accessing AzureDevOps.</param> /// <returns>The build definition for the BuildDefinitionName on <paramref name="settings"/>. /// <c>null</c> if the BuildDefinitionName was not set or no build definition was found.</returns> private static AzureDevOpsBuildDefinition GetBuildDefinition( ICakeLog log, AzureDevOpsBuildsSettings settings) { log.NotNull(nameof(log)); settings.NotNull(nameof(settings)); var buildDefinitions = AzureDevOpsBuildsDefinitionHelper .GetAzureDevOpsBuildDefinitions( log, settings); var buildDefinition = buildDefinitions .SingleOrDefault(x => x.Name.Equals(settings.BuildDefinitionName, StringComparison.InvariantCultureIgnoreCase)); if (buildDefinition == null) { log.Verbose( "Build definition '{0}' not found", settings.BuildDefinitionName); } log.Verbose( "Build definition '{0}' found", settings.BuildDefinitionName); return(buildDefinition); }
/// <summary> /// Initializes a new instance of the <see cref="AzureDevOpsBuildsSettings"/> class /// based on the instance of a <see cref="AzureDevOpsBuildsSettings"/> class. /// </summary> /// <param name="settings">Settings containing the parameters.</param> public AzureDevOpsBuildsSettings(AzureDevOpsBuildsSettings settings) { settings.NotNull(nameof(settings)); this.CollectionUrl = settings.CollectionUrl; this.ProjectGuid = settings.ProjectGuid; this.ProjectName = settings.ProjectName; this.Credentials = settings.Credentials; }
/// <summary> /// Initializes a new instance of the <see cref="AzureDevOpsBuild"/> class. /// </summary> /// <param name="log">The Cake log context.</param> /// <param name="settings">Settings for accessing AzureDevOps.</param> /// <param name="build">The build.</param> internal AzureDevOpsBuild(ICakeLog log, AzureDevOpsBuildsSettings settings, Build build) { log.NotNull(nameof(log)); settings.NotNull(nameof(settings)); build.NotNull(nameof(build)); this.log = log; this.build = build; this.buildClientFactory = new BuildClientFactory(); this.credentials = settings.Credentials; this.CollectionUrl = settings.CollectionUrl; }
/// <summary> /// Returns the build definition for the <paramref name="settings"/>. /// </summary> /// <param name="log">The Cake log context.</param> /// <param name="buildHttpClient">The Http build client.</param> /// <param name="settings">Settings for accessing AzureDevOps.</param> /// <returns>The build definition for the BuildDefinitionName on <paramref name="settings"/>. /// <c>null</c> if the BuildDefinitionName was not set or no build definition was found.</returns> private static BuildDefinitionReference GetBuildDefinition( ICakeLog log, BuildHttpClient buildHttpClient, AzureDevOpsBuildsSettings settings) { log.NotNull(nameof(log)); buildHttpClient.NotNull(nameof(buildHttpClient)); settings.NotNull(nameof(settings)); List <BuildDefinitionReference> buildDefinitions = null; if (settings.ProjectGuid != Guid.Empty) { buildDefinitions = buildHttpClient .GetDefinitionsAsync(settings.ProjectGuid) .ConfigureAwait(false) .GetAwaiter() .GetResult(); } else if (!string.IsNullOrWhiteSpace(settings.ProjectName)) { buildDefinitions = buildHttpClient .GetDefinitionsAsync(settings.ProjectName) .ConfigureAwait(false) .GetAwaiter() .GetResult(); } else { throw new ArgumentOutOfRangeException( nameof(settings), "Either ProjectGuid or ProjectName needs to be set"); } var buildDefinition = buildDefinitions .SingleOrDefault(x => x.Name.Equals(settings.BuildDefinitionName, StringComparison.InvariantCultureIgnoreCase)); if (buildDefinition == null) { log.Verbose( "Build definition '{0}' not found", settings.BuildDefinitionName); } log.Verbose( "Build definition '{0}' found", settings.BuildDefinitionName); return(buildDefinition); }
/// <summary> /// Returns the build definitions for the <paramref name="settings"/>. /// </summary> /// <param name="log">The Cake log context.</param> /// <param name="settings">Settings for accessing Azure DevOps.</param> /// <returns>The build definitions for the the <paramref name="settings"/>.</returns> internal static IEnumerable <AzureDevOpsBuildDefinition> GetAzureDevOpsBuildDefinitions( ICakeLog log, AzureDevOpsBuildsSettings settings) { log.NotNull(nameof(log)); settings.NotNull(nameof(settings)); List <BuildDefinitionReference> buildDefinitions = null; using (var buildHttpClient = new BuildClientFactory().CreateBuildClient(settings.CollectionUrl, settings.Credentials)) { if (settings.ProjectGuid != Guid.Empty) { buildDefinitions = buildHttpClient .GetDefinitionsAsync(settings.ProjectGuid) .ConfigureAwait(false) .GetAwaiter() .GetResult(); } else if (!string.IsNullOrWhiteSpace(settings.ProjectName)) { buildDefinitions = buildHttpClient .GetDefinitionsAsync(settings.ProjectName) .ConfigureAwait(false) .GetAwaiter() .GetResult(); } else { throw new ArgumentOutOfRangeException( nameof(settings), "Either ProjectGuid or ProjectName needs to be set"); } log.Verbose( "{0} Build definitions found", buildDefinitions.Count); return(buildDefinitions .Select(x => x.ToAzureDevOpsBuildDefinition()) .ToList()); } }
/// <summary> /// Gets the builds for the parameter <paramref name="settings"/>. /// </summary> /// <param name="log">The Cake log context.</param> /// <param name="settings">Settings for getting the build.</param> /// <returns>The builds or an empty list of builds if no builds were found for the <paramref name="settings"/>.</returns> /// <exception cref="InvalidOperationException">If no build definition was found for the <paramref name="settings"/>.</exception> internal static IEnumerable <AzureDevOpsBuild> GetAzureDevOpsBuilds( ICakeLog log, AzureDevOpsBuildsSettings settings) { log.NotNull(nameof(log)); settings.NotNull(nameof(settings)); var azureDevOpsBuilds = new List <AzureDevOpsBuild>(); using (var buildHttpClient = new BuildClientFactory().CreateBuildClient(settings.CollectionUrl, settings.Credentials, out var authorizedIdenity)) { log.Verbose( "Authorized Identity:\n Id: {0}\n DisplayName: {1}", authorizedIdenity.Id, authorizedIdenity.DisplayName); AzureDevOpsBuildDefinition buildDefinition = null; if (!string.IsNullOrEmpty(settings.BuildDefinitionName)) { buildDefinition = GetBuildDefinition(log, settings); if (buildDefinition == null) { throw new InvalidOperationException($"Build definition '{settings.BuildDefinitionName}' not found"); } } List <int> buildsDefinitionIds = buildDefinition == null ? null : new List <int>() { buildDefinition.Id }; List <Build> builds = null; if (settings.ProjectGuid != Guid.Empty) { builds = buildHttpClient .GetBuildsAsync( settings.ProjectGuid, definitions: buildsDefinitionIds, statusFilter: settings.BuildStatus?.ToBuildStatus(), resultFilter: settings.BuildResult?.ToBuildResult(), queryOrder: settings.BuildQueryOrder?.ToBuildQueryOrder(), branchName: settings.BranchName, top: settings.Top, maxBuildsPerDefinition: settings.MaxBuildsPerDefinition) .ConfigureAwait(false) .GetAwaiter() .GetResult(); } else if (!string.IsNullOrWhiteSpace(settings.ProjectName)) { builds = buildHttpClient .GetBuildsAsync( settings.ProjectName, definitions: buildsDefinitionIds, statusFilter: settings.BuildStatus?.ToBuildStatus(), resultFilter: settings.BuildResult?.ToBuildResult(), queryOrder: settings.BuildQueryOrder?.ToBuildQueryOrder(), branchName: settings.BranchName, top: settings.Top, maxBuildsPerDefinition: settings.MaxBuildsPerDefinition) .ConfigureAwait(false) .GetAwaiter() .GetResult(); } else { throw new ArgumentOutOfRangeException( nameof(settings), "Either ProjectGuid or ProjectName needs to be set"); } azureDevOpsBuilds.AddRange(builds.Select(x => new AzureDevOpsBuild(log, settings, x))); } log.Verbose( "{0} builds found", azureDevOpsBuilds.Count); return(azureDevOpsBuilds); }
/// <summary> /// Initializes a new instance of the <see cref="AzureDevOpsBuildsSettings"/> class /// based on the instance of a <see cref="AzureDevOpsBuildsSettings"/> class. /// </summary> /// <param name="settings">Settings containing the parameters.</param> public AzureDevOpsBuildsSettings(AzureDevOpsBuildsSettings settings) : base(settings) { }