|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Microsoft.DotNet.DarcLib; |
| 9 | +using Microsoft.DotNet.ProductConstructionService.Client.Models; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | + |
| 12 | +namespace Dotnet.Docker; |
| 13 | + |
| 14 | +internal interface IBuildUpdaterService |
| 15 | +{ |
| 16 | + Task<int> UpdateFrom(Build build, CreatePullRequestOptions pullRequestOptions); |
| 17 | +} |
| 18 | + |
| 19 | +internal class BuildUpdaterService( |
| 20 | + IBuildAssetService buildAssetService, |
| 21 | + IBasicBarClient barClient, |
| 22 | + ILogger<BuildUpdaterService> logger) : IBuildUpdaterService |
| 23 | +{ |
| 24 | + private readonly IBuildAssetService _buildAssetService = buildAssetService; |
| 25 | + private readonly IBasicBarClient _barClient = barClient; |
| 26 | + private readonly ILogger<BuildUpdaterService> _logger = logger; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Updates product versions according to a specific BAR build of the VMR. This will update the |
| 30 | + /// manifest.versions.json file, generate Dockerfiles and Readmes from the templates, and if |
| 31 | + /// credentials are provided, submit a pull request. |
| 32 | + /// </summary> |
| 33 | + /// <param name="build"> |
| 34 | + /// A build of the VMR repo (dotnet/dotnet) |
| 35 | + /// </param> |
| 36 | + /// <param name="pullRequestOptions"> |
| 37 | + /// Options for creating a pull request. If credentials are provided, a pull request will be created. |
| 38 | + /// </param> |
| 39 | + /// <returns>Exit code (0 for success)</returns> |
| 40 | + public async Task<int> UpdateFrom(Build build, CreatePullRequestOptions pullRequestOptions) |
| 41 | + { |
| 42 | + _logger.LogInformation("Updating to build {build.Id} with commit {options.Repo}@{build.Commit}", |
| 43 | + build.Id, build.AzureDevOpsRepository ?? build.GitHubRepository, build.Commit); |
| 44 | + |
| 45 | + if (!IsVmrBuild(build)) |
| 46 | + { |
| 47 | + throw new InvalidOperationException( |
| 48 | + "Expected a build of the VMR, but got a build of " + |
| 49 | + $"{build.AzureDevOpsRepository ?? build.GitHubRepository} instead."); |
| 50 | + } |
| 51 | + |
| 52 | + IEnumerable<Asset> assets = await _barClient.GetAssetsAsync(buildId: build.Id); |
| 53 | + |
| 54 | + Asset productCommitsAsset = assets.FirstOrDefault(a => ProductCommits.SdkAssetRegex.IsMatch(a.Name)) |
| 55 | + ?? throw new InvalidOperationException($"Could not find product version commit in assets."); |
| 56 | + |
| 57 | + string productCommitsJson = await _buildAssetService.GetAssetTextContentsAsync(productCommitsAsset); |
| 58 | + ProductCommits productCommits = ProductCommits.FromJson(productCommitsJson); |
| 59 | + |
| 60 | + Version dockerfileVersion = ResolveMajorMinorVersion(productCommits.Sdk.Version); |
| 61 | + |
| 62 | + // Run old update-dependencies command using the resolved versions |
| 63 | + var updateDependencies = new SpecificCommand(); |
| 64 | + var updateDependenciesOptions = new SpecificCommandOptions() |
| 65 | + { |
| 66 | + DockerfileVersion = dockerfileVersion.ToString(), |
| 67 | + ProductVersions = new Dictionary<string, string?>() |
| 68 | + { |
| 69 | + // "dotnet" version is also required. It sets the "dotnet|*|product-version" |
| 70 | + // variable which is used for runtime-deps, runtime, and aspnet tags. |
| 71 | + { "dotnet", productCommits.Runtime.Version }, |
| 72 | + { "runtime", productCommits.Runtime.Version }, |
| 73 | + { "aspnet", productCommits.AspNetCore.Version }, |
| 74 | + { "aspnet-composite", productCommits.AspNetCore.Version }, |
| 75 | + { "sdk", productCommits.Sdk.Version }, |
| 76 | + }, |
| 77 | + |
| 78 | + // Pass through all properties of CreatePullRequestOptions |
| 79 | + User = pullRequestOptions.User, |
| 80 | + Email = pullRequestOptions.Email, |
| 81 | + Password = pullRequestOptions.Password, |
| 82 | + AzdoOrganization = pullRequestOptions.AzdoOrganization, |
| 83 | + AzdoProject = pullRequestOptions.AzdoProject, |
| 84 | + AzdoRepo = pullRequestOptions.AzdoRepo, |
| 85 | + VersionSourceName = pullRequestOptions.VersionSourceName, |
| 86 | + SourceBranch = pullRequestOptions.SourceBranch, |
| 87 | + TargetBranch = pullRequestOptions.TargetBranch, |
| 88 | + }; |
| 89 | + |
| 90 | + return await updateDependencies.ExecuteAsync(updateDependenciesOptions); |
| 91 | + } |
| 92 | + |
| 93 | + private static Version ResolveMajorMinorVersion(string versionString) |
| 94 | + { |
| 95 | + string[] versionParts = versionString.Split('.'); |
| 96 | + if (versionParts.Length < 2) |
| 97 | + { |
| 98 | + throw new InvalidOperationException($"Could not parse major-minor version from '{versionString}'."); |
| 99 | + } |
| 100 | + |
| 101 | + return new Version(major: int.Parse(versionParts[0]), minor: int.Parse(versionParts[1])); |
| 102 | + } |
| 103 | + |
| 104 | + private static bool IsVmrBuild(Build build) |
| 105 | + { |
| 106 | + string repo = build.GitHubRepository ?? build.AzureDevOpsRepository; |
| 107 | + return repo == "https://github.com/dotnet/dotnet" |
| 108 | + || repo == "https://dev.azure.com/dnceng/internal/_git/dotnet-dotnet"; |
| 109 | + } |
| 110 | +} |
0 commit comments