From 6c1ba2f2a0abedfd5969afc38bc4ebc16531336d Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Wed, 6 Mar 2024 09:59:55 -0600 Subject: [PATCH] Trim trailing .git from sourcelink repo urls in container generation (#39161) --- .../Microsoft.NET.Build.Containers.targets | 11 +++++--- .../TargetsTests.cs | 25 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets index ff0b034dba94..ecb8dee489c7 100644 --- a/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets +++ b/src/Containers/packaging/build/Microsoft.NET.Build.Containers.targets @@ -35,13 +35,13 @@ Returns="$(ContainerBaseImage)"> + For builds that have a RID, we default to that RID. Otherwise, we default to the Linux RID matching the architecture of the currently-executing SDK. --> $(RuntimeIdentifier) linux-$(NETCoreSdkPortableRuntimeIdentifier.Split('-')[1]) <_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' == ''">true <_ContainerIsUsingMicrosoftDefaultImages Condition="'$(ContainerBaseImage)' != ''">false - + + + + <_TrimmedRepositoryUrl Condition="'$(RepositoryType)' == 'git' and '$(PrivateRepositoryUrl)' != '' and $(PrivateRepositoryUrl.EndsWith('.git'))">$(PrivateRepositoryUrl.Substring(0, $(PrivateRepositoryUrl.LastIndexOf('.git')))) + <_TrimmedRepositoryUrl Condition="'$(_TrimmedRepositoryUrl)' == '' and '$(PrivateRepositoryUrl)' != ''">$(PrivateRepositoryUrl) + - + diff --git a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs index 88aa4498356d..a00f2fd47554 100644 --- a/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs +++ b/src/Tests/Microsoft.NET.Build.Containers.IntegrationTests/TargetsTests.cs @@ -150,6 +150,31 @@ public void ShouldNotIncludeSourceControlLabelsUnlessUserOptsIn(bool includeSour }; } + [InlineData("https://git.cosmere.com/shard/whimsy.git", "https://git.cosmere.com/shard/whimsy")] + [InlineData("https://repos.git.cosmere.com/shard/whimsy.git", "https://repos.git.cosmere.com/shard/whimsy")] + [Theory] + public void ShouldTrimTrailingGitSuffixFromRepoUrls(string repoUrl, string expectedLabel) + { + var commitHash = "abcdef"; + + static string NormalizeString(string s) => s.Replace(':', '_').Replace('/', '_'); + + var (project, logger, d) = ProjectInitializer.InitProject(new() + { + ["PublishRepositoryUrl"] = true.ToString(), + ["PrivateRepositoryUrl"] = repoUrl, + ["SourceRevisionId"] = commitHash, + ["RepositoryType"] = "git" + }, projectName: $"{nameof(ShouldNotIncludeSourceControlLabelsUnlessUserOptsIn)}_{NormalizeString(repoUrl)}_{NormalizeString(expectedLabel)}"); + using var _ = d; + var instance = project.CreateProjectInstance(global::Microsoft.Build.Execution.ProjectInstanceSettings.None); + instance.Build(new[] { ComputeContainerConfig }, new[] { logger }, null, out var outputs).Should().BeTrue("Build should have succeeded but failed due to {0}", String.Join("\n", logger.AllMessages)); + var labels = instance.GetItems(ContainerLabel); + + labels.Should().NotBeEmpty("Should have evaluated some labels by default") + .And.ContainSingle(label => LabelMatch("org.opencontainers.image.source", expectedLabel, label), String.Join(",", logger.AllMessages)); + } + [InlineData("7.0.100", "v7.0", "7.0")] [InlineData("7.0.100-preview.7", "v7.0", "7.0")] [InlineData("7.0.100-rc.1", "v7.0", "7.0")]