Skip to content

Commit 5e9a32e

Browse files
Backport of auditSources fix (#42826)
Co-authored-by: Viktor Hofer <[email protected]>
1 parent 5fbab6e commit 5e9a32e

File tree

1 file changed

+49
-28
lines changed

1 file changed

+49
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
#nullable enable
6+
57
using System;
68
using System.Collections.Generic;
79
using System.IO;
@@ -21,7 +23,7 @@ namespace Microsoft.DotNet.UnifiedBuild.Tasks
2123
public class RemoveInternetSourcesFromNuGetConfig : Task
2224
{
2325
[Required]
24-
public string NuGetConfigFile { get; set; }
26+
public required string NuGetConfigFile { get; set; }
2527

2628
/// <summary>
2729
/// Whether to work in offline mode (remove all internet sources) or online mode (remove only authenticated sources)
@@ -33,54 +35,73 @@ public class RemoveInternetSourcesFromNuGetConfig : Task
3335
/// example, a source named 'darc-pub-dotnet-aspnetcore-e81033e' will be kept if the prefix
3436
/// 'darc-pub-dotnet-aspnetcore-' is in this list.
3537
/// </summary>
36-
public string[] KeepFeedPrefixes { get; set; }
38+
public string[] KeepFeedPrefixes { get; set; } = [];
39+
40+
private readonly string[] Sections = [ "packageSources", "auditSources" ];
3741

3842
public override bool Execute()
3943
{
4044
string xml = File.ReadAllText(NuGetConfigFile);
4145
string newLineChars = FileUtilities.DetectNewLineChars(xml);
4246
XDocument d = XDocument.Parse(xml);
43-
XElement packageSourcesElement = d.Root.Descendants().First(e => e.Name == "packageSources");
44-
XElement disabledPackageSourcesElement = d.Root.Descendants().FirstOrDefault(e => e.Name == "disabledPackageSources");
47+
XElement? disabledPackageSourcesElement = d.Root?.Descendants().FirstOrDefault(e => e.Name == "disabledPackageSources");
48+
49+
foreach (string sectionName in Sections)
50+
{
51+
ProcessSection(d, sectionName);
52+
}
53+
54+
// Remove disabledPackageSources element so if any internal packages remain, they are used in source-build
55+
disabledPackageSourcesElement?.ReplaceNodes(new XElement("clear"));
56+
57+
using (var w = XmlWriter.Create(NuGetConfigFile, new XmlWriterSettings { NewLineChars = newLineChars, Indent = true }))
58+
{
59+
d.Save(w);
60+
}
61+
62+
return true;
63+
}
4564

46-
IEnumerable<XElement> local = packageSourcesElement.Descendants().Where(e =>
65+
private void ProcessSection(XDocument d, string sectionName)
66+
{
67+
XElement? sectionElement = d.Root?.Descendants().FirstOrDefault(e => e.Name == sectionName);
68+
if (sectionElement == null)
69+
{
70+
return;
71+
}
72+
73+
IEnumerable<XElement> local = sectionElement.Descendants().Where(e =>
4774
{
4875
if (e.Name == "add")
4976
{
50-
string feedName = e.Attribute("key").Value;
51-
if (KeepFeedPrefixes
77+
string? feedName = e.Attribute("key")?.Value;
78+
if (feedName != null &&
79+
KeepFeedPrefixes
5280
?.Any(prefix => feedName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
5381
== true)
5482
{
5583
return true;
5684
}
5785

58-
string feedUrl = e.Attribute("value").Value;
59-
if (BuildWithOnlineFeeds)
86+
string? feedUrl = e.Attribute("value")?.Value;
87+
if (feedUrl != null)
6088
{
61-
return !( feedUrl.StartsWith("https://pkgs.dev.azure.com/dnceng/_packaging", StringComparison.OrdinalIgnoreCase) ||
62-
feedUrl.StartsWith("https://pkgs.dev.azure.com/dnceng/internal/_packaging", StringComparison.OrdinalIgnoreCase) );
63-
}
64-
else
65-
{
66-
return !(feedUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || feedUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase));
89+
if (BuildWithOnlineFeeds)
90+
{
91+
return !(feedUrl.StartsWith("https://pkgs.dev.azure.com/dnceng/_packaging", StringComparison.OrdinalIgnoreCase) ||
92+
feedUrl.StartsWith("https://pkgs.dev.azure.com/dnceng/internal/_packaging", StringComparison.OrdinalIgnoreCase));
93+
}
94+
else
95+
{
96+
return !(feedUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase) || feedUrl.StartsWith("https://", StringComparison.OrdinalIgnoreCase));
97+
}
6798
}
6899
}
69100

70101
return true;
71102
});
72103

73-
packageSourcesElement.ReplaceNodes(local.ToArray());
74-
75-
// Remove disabledPackageSources element so if any internal packages remain, they are used in source-build
76-
disabledPackageSourcesElement?.ReplaceNodes(new XElement("clear"));
77-
78-
using (var w = XmlWriter.Create(NuGetConfigFile, new XmlWriterSettings { NewLineChars = newLineChars, Indent = true }))
79-
{
80-
d.Save(w);
81-
}
82-
83-
return true;
104+
sectionElement.ReplaceNodes(local.ToArray());
84105
}
85106
}
86-
}
107+
}

0 commit comments

Comments
 (0)