Skip to content

Commit

Permalink
Map SourceBranchName from sourcelink to RepositoryBranch for NuGet pa…
Browse files Browse the repository at this point in the history
…ck (#5923)
  • Loading branch information
MattKotsenas committed Aug 6, 2024
1 parent 043ed8c commit e9b7d1c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,10 @@ Copyright (c) .NET Foundation. All rights reserved.
DependsOnTargets="InitializeSourceControlInformation"
Condition="'$(SourceControlInformationFeatureSupported)' == 'true'">
<PropertyGroup>
<!-- The project must specify PublishRepositoryUrl=true in order to publish the URL, in order to prevent inadvertent leak of internal URL. -->
<!-- The project must specify PublishRepositoryUrl=true in order to publish the URL or branch, in order to prevent inadvertent leak of internal data. -->
<RepositoryUrl Condition="'$(RepositoryUrl)' == '' and '$(PublishRepositoryUrl)' == 'true'">$(PrivateRepositoryUrl)</RepositoryUrl>
<RepositoryCommit Condition="'$(RepositoryCommit)' == ''">$(SourceRevisionId)</RepositoryCommit>
<RepositoryBranch Condition="'$(RepositoryBranch)' == '' and '$(PublishRepositoryUrl)' == 'true' and '$(SourceBranchName)' != ''">$(SourceBranchName)</RepositoryBranch>
</PropertyGroup>
</Target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3667,6 +3667,7 @@ public void PackCommand_PackWithSourceControlInformation_Unsupported_VerifyNuspe
<PropertyGroup>
<SourceRevisionId>e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1</SourceRevisionId>
<PrivateRepositoryUrl>https://github.com/NuGet/NuGet.Client.git</PrivateRepositoryUrl>
<SourceBranchName>refs/heads/main</SourceBranchName>
</PropertyGroup>
</Target>
</Project>");
Expand All @@ -3693,8 +3694,10 @@ public void PackCommand_PackWithSourceControlInformation_Unsupported_VerifyNuspe
}
}

[PlatformFact(Platform.Windows)]
public void PackCommand_PackWithSourceControlInformation_PrivateUrl_VerifyNuspec()
[PlatformTheory(Platform.Windows)]
[InlineData(true)]
[InlineData(false)]
public void PackCommand_PackWithSourceControlInformation_PrivateUrl_VerifyNuspec(bool shouldEmitPublishProperty)
{
using (var testDirectory = _dotnetFixture.CreateTestDirectory())
{
Expand All @@ -3711,6 +3714,10 @@ public void PackCommand_PackWithSourceControlInformation_PrivateUrl_VerifyNuspec
var ns = xml.Root.Name.Namespace;

ProjectFileUtils.AddProperty(xml, "RepositoryType", "git");
if (shouldEmitPublishProperty)
{
ProjectFileUtils.AddProperty(xml, "PublishRepositoryUrl", "false");
}

xml.Root.Add(
new XElement(ns + "PropertyGroup",
Expand All @@ -3725,6 +3732,7 @@ public void PackCommand_PackWithSourceControlInformation_PrivateUrl_VerifyNuspec
<PropertyGroup>
<SourceRevisionId>e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1</SourceRevisionId>
<PrivateRepositoryUrl>https://github.com/NuGet/NuGet.Client.git</PrivateRepositoryUrl>
<SourceBranchName>refs/heads/abc</SourceBranchName>
</PropertyGroup>
</Target>
</Project>";
Expand Down Expand Up @@ -3787,6 +3795,7 @@ public void PackCommand_PackWithSourceControlInformation_PublishedUrl_VerifyNusp
<PropertyGroup>
<SourceRevisionId>e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1</SourceRevisionId>
<PrivateRepositoryUrl>https://github.com/NuGet/NuGet.Client.git</PrivateRepositoryUrl>
<SourceBranchName>refs/heads/main</SourceBranchName>
</PropertyGroup>
</Target>
</Project>";
Expand All @@ -3809,7 +3818,7 @@ public void PackCommand_PackWithSourceControlInformation_PublishedUrl_VerifyNusp
var repositoryMetadata = nuspecReader.GetRepositoryMetadata();
repositoryMetadata.Type.Should().Be("git");
repositoryMetadata.Url.Should().Be("https://github.com/NuGet/NuGet.Client.git");
repositoryMetadata.Branch.Should().Be("");
repositoryMetadata.Branch.Should().Be("refs/heads/main");
repositoryMetadata.Commit.Should().Be("e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1");
}
}
Expand All @@ -3836,14 +3845,16 @@ public void PackCommand_PackWithSourceControlInformation_ProjectOverride_VerifyN
ProjectFileUtils.AddProperty(xml, "PublishRepositoryUrl", "true");
ProjectFileUtils.AddProperty(xml, "RepositoryCommit", "1111111111111111111111111111111111111111");
ProjectFileUtils.AddProperty(xml, "RepositoryUrl", "https://github.com/Overridden");
ProjectFileUtils.AddProperty(xml, "RepositoryBranch", "refs/heads/overwritten");

// mock implementation of InitializeSourceControlInformation common targets:
xml.Root.Add(
new XElement(ns + "Target",
new XAttribute("Name", "InitializeSourceControlInformation"),
new XElement(ns + "PropertyGroup",
new XElement("SourceRevisionId", "e1c65e4524cd70ee6e22abe33e6cb6ec73938cb1"),
new XElement("PrivateRepositoryUrl", "https://github.com/NuGet/NuGet.Client"))));
new XElement("PrivateRepositoryUrl", "https://github.com/NuGet/NuGet.Client"),
new XElement("SourceBranchName", "refs/heads/main"))));

xml.Root.Add(
new XElement(ns + "PropertyGroup",
Expand All @@ -3868,7 +3879,7 @@ public void PackCommand_PackWithSourceControlInformation_ProjectOverride_VerifyN
var repositoryMetadata = nuspecReader.GetRepositoryMetadata();
repositoryMetadata.Type.Should().Be("git");
repositoryMetadata.Url.Should().Be("https://github.com/Overridden");
repositoryMetadata.Branch.Should().Be("");
repositoryMetadata.Branch.Should().Be("refs/heads/overwritten");
repositoryMetadata.Commit.Should().Be("1111111111111111111111111111111111111111");
}
}
Expand Down

0 comments on commit e9b7d1c

Please sign in to comment.