|
| 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 Maestro.MergePolicyEvaluation; |
| 9 | +using Microsoft.DotNet.DarcLib; |
| 10 | +using Microsoft.DotNet.DarcLib.Helpers; |
| 11 | +using Microsoft.DotNet.DarcLib.Models.VirtualMonoRepo; |
| 12 | +using Microsoft.DotNet.DarcLib.VirtualMonoRepo; |
| 13 | + |
| 14 | +namespace Maestro.MergePolicies; |
| 15 | +internal class ForwardFlowMergePolicy : MergePolicy |
| 16 | +{ |
| 17 | + public override string DisplayName => "ForwardFlow"; |
| 18 | + |
| 19 | + private static readonly string configurationErrorsHeader = """ |
| 20 | + ### :x: Check Failed |
| 21 | +
|
| 22 | + The following error(s) were encountered: |
| 23 | +
|
| 24 | +
|
| 25 | + """; |
| 26 | + |
| 27 | + private static readonly string seekHelpMessage = $""" |
| 28 | +
|
| 29 | +
|
| 30 | + ### :exclamation: IMPORTANT |
| 31 | +
|
| 32 | + The `{VmrInfo.DefaultRelativeSourceManifestPath}` and `{VersionFiles.VersionDetailsXml}` files are managed by Maestro/darc. Outside of exceptional circumstances, these files should not be modified manually. |
| 33 | + **Unless you are sure that you know what you are doing, we recommend reaching out for help**. You can receive assistance by: |
| 34 | + - tagging the **@dotnet/product-construction** team in a PR comment |
| 35 | + - using the [First Responder channel](https://teams.microsoft.com/l/channel/19%3Aafba3d1545dd45d7b79f34c1821f6055%40thread.skype/First%20Responders?groupId=4d73664c-9f2f-450d-82a5-c2f02756606dhttps://teams.microsoft.com/l/channel/19%3Aafba3d1545dd45d7b79f34c1821f6055%40thread.skype/First%20Responders?groupId=4d73664c-9f2f-450d-82a5-c2f02756606d), |
| 36 | + - [opening an issue](https://github.com/dotnet/arcade-services/issues/new?template=BLANK_ISSUE) in the dotnet/arcade-services repo |
| 37 | + - contacting the [.NET Product Construction Services team via e-mail](mailto:[email protected]). |
| 38 | + """; |
| 39 | + |
| 40 | + |
| 41 | + public override async Task<MergePolicyEvaluationResult> EvaluateAsync(PullRequestUpdateSummary pr, IRemote remote) |
| 42 | + { |
| 43 | + SourceManifest sourceManifest; |
| 44 | + try |
| 45 | + { |
| 46 | + sourceManifest = await remote.GetSourceManifestAsync(pr.TargetRepoUrl, pr.HeadBranch); |
| 47 | + } |
| 48 | + catch (Exception) |
| 49 | + { |
| 50 | + return Fail( |
| 51 | + "Error while retrieving source manifest", |
| 52 | + $"An issue occurred while retrieving the source manifest. This could be due to a misconfiguration of the `{VmrInfo.DefaultRelativeSourceManifestPath}` file, or because of a server error." |
| 53 | + + seekHelpMessage); |
| 54 | + } |
| 55 | + |
| 56 | + Dictionary<string, int?> repoNamesToBarIds; |
| 57 | + Dictionary<string, string> repoNamesToCommitSha; |
| 58 | + if (!TryCreateBarIdDictionaryFromSourceManifest(sourceManifest, out repoNamesToBarIds) || |
| 59 | + !TryCreateCommitShaDictionaryFromSourceManifest(sourceManifest, out repoNamesToCommitSha)) |
| 60 | + { |
| 61 | + return Fail( |
| 62 | + "The source manifest file is malformed", |
| 63 | + $"Duplicate repository URIs were found in {VmrInfo.DefaultRelativeSourceManifestPath}." + seekHelpMessage); |
| 64 | + } |
| 65 | + |
| 66 | + List<string> configurationErrors = CalculateConfigurationErrors(pr, repoNamesToBarIds, repoNamesToCommitSha); |
| 67 | + |
| 68 | + if (configurationErrors.Any()) |
| 69 | + { |
| 70 | + string failureMessage = string.Concat( |
| 71 | + configurationErrorsHeader, |
| 72 | + string.Join(Environment.NewLine, configurationErrors), |
| 73 | + seekHelpMessage); |
| 74 | + return Fail($"Missing or mismatched values found in {VmrInfo.DefaultRelativeSourceManifestPath}", failureMessage); |
| 75 | + } |
| 76 | + |
| 77 | + return Succeed($"Forward-flow checks succeeded."); |
| 78 | + } |
| 79 | + |
| 80 | + private List<string> CalculateConfigurationErrors( |
| 81 | + PullRequestUpdateSummary pr, |
| 82 | + Dictionary<string, int?> repoNamesToBarIds, |
| 83 | + Dictionary<string, string> repoNamesToCommitSha) |
| 84 | + { |
| 85 | + List<string> configurationErrors = new(); |
| 86 | + foreach (SubscriptionUpdateSummary PRUpdateSummary in pr.ContainedUpdates) |
| 87 | + { |
| 88 | + if (!repoNamesToBarIds.TryGetValue(PRUpdateSummary.SourceRepo, out int? sourceManifestBarId) || sourceManifestBarId == null) |
| 89 | + { |
| 90 | + configurationErrors.Add($""" |
| 91 | + #### {configurationErrors.Count + 1}. Missing BAR ID in `{VmrInfo.DefaultRelativeSourceManifestPath}` |
| 92 | + - **Source Repository**: {PRUpdateSummary.SourceRepo} |
| 93 | + - **Error**: The BAR ID for the current update from the source repository is not found in the source manifest. |
| 94 | + """); |
| 95 | + } |
| 96 | + if (sourceManifestBarId != null && sourceManifestBarId != PRUpdateSummary.BuildId) |
| 97 | + { |
| 98 | + configurationErrors.Add($""" |
| 99 | + #### {configurationErrors.Count + 1}. BAR ID Mismatch in `{VmrInfo.DefaultRelativeSourceManifestPath}` |
| 100 | + - **Source Repository**: {PRUpdateSummary.SourceRepo} |
| 101 | + - **Error**: BAR ID `{sourceManifestBarId}` found in the source manifest does not match the build ID of the current update (`{PRUpdateSummary.BuildId}`). |
| 102 | + """); |
| 103 | + } |
| 104 | + if (!repoNamesToCommitSha.TryGetValue(PRUpdateSummary.SourceRepo, out string sourceManifestCommitSha) || string.IsNullOrEmpty(sourceManifestCommitSha)) |
| 105 | + { |
| 106 | + configurationErrors.Add($""" |
| 107 | + #### {configurationErrors.Count + 1}. Missing Commit SHA in `{VmrInfo.DefaultRelativeSourceManifestPath}` |
| 108 | + - **Source Repository**: {PRUpdateSummary.SourceRepo} |
| 109 | + - **Error**: The commit SHA for the current update from the source repository is not found in the source manifest. |
| 110 | + """); |
| 111 | + } |
| 112 | + if (!string.IsNullOrEmpty(sourceManifestCommitSha) && sourceManifestCommitSha != PRUpdateSummary.CommitSha) |
| 113 | + { |
| 114 | + configurationErrors.Add($""" |
| 115 | + #### {configurationErrors.Count + 1}. Commit SHA Mismatch in `{VmrInfo.DefaultRelativeSourceManifestPath}` |
| 116 | + - **Source Repository**: {PRUpdateSummary.SourceRepo} |
| 117 | + - **Error**: Commit SHA `{sourceManifestCommitSha}` found in the source manifest does not match the commit SHA of the current update (`{PRUpdateSummary.CommitSha}`). |
| 118 | + """); |
| 119 | + } |
| 120 | + } |
| 121 | + return configurationErrors; |
| 122 | + } |
| 123 | + |
| 124 | + private bool TryCreateBarIdDictionaryFromSourceManifest(SourceManifest sourceManifest, out Dictionary<string, int?> repoNamesToBarIds) |
| 125 | + { |
| 126 | + repoNamesToBarIds = new Dictionary<string, int?>(); |
| 127 | + foreach (var repo in sourceManifest.Repositories) |
| 128 | + { |
| 129 | + if (repoNamesToBarIds.ContainsKey(repo.RemoteUri)) |
| 130 | + { |
| 131 | + return false; |
| 132 | + } |
| 133 | + repoNamesToBarIds.Add(repo.RemoteUri, repo.BarId); |
| 134 | + } |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + private bool TryCreateCommitShaDictionaryFromSourceManifest(SourceManifest sourceManifest, out Dictionary<string, string> repoNamesToCommitSha) |
| 140 | + { |
| 141 | + repoNamesToCommitSha = new Dictionary<string, string>(); |
| 142 | + foreach (var repo in sourceManifest.Repositories) |
| 143 | + { |
| 144 | + if (repoNamesToCommitSha.ContainsKey(repo.RemoteUri)) |
| 145 | + { |
| 146 | + return false; |
| 147 | + } |
| 148 | + repoNamesToCommitSha.Add(repo.RemoteUri, repo.CommitSha); |
| 149 | + } |
| 150 | + return true; |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +public class ForwardFlowMergePolicyBuilder : IMergePolicyBuilder |
| 155 | +{ |
| 156 | + public string Name => MergePolicyConstants.ForwardFlowMergePolicyName; |
| 157 | + |
| 158 | + public Task<IReadOnlyList<IMergePolicy>> BuildMergePoliciesAsync(MergePolicyProperties properties, PullRequestUpdateSummary pr) |
| 159 | + { |
| 160 | + return Task.FromResult<IReadOnlyList<IMergePolicy>>([new ForwardFlowMergePolicy()]); |
| 161 | + } |
| 162 | +} |
| 163 | + |
0 commit comments