forked from chocolatey/choco
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chocolatey#3000) Add new rule service to handle custom rules
This commit adds a new rule service together with some base types that will be used by the service itself, and any implementing rule. This service takes a path to a `nupkg` file or to a `nuspec` file and passes along the appropriate values necessary to validate the metadata that is being used for the package.
- Loading branch information
1 parent
2cbac08
commit fbe4c0f
Showing
10 changed files
with
319 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/chocolatey/infrastructure.app/rules/MetadataRuleBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright © 2023-Present Chocolatey Software, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace chocolatey.infrastructure.app.rules | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
using chocolatey.infrastructure.rules; | ||
using NuGet.Packaging; | ||
|
||
public abstract class MetadataRuleBase : IMetadataRule | ||
{ | ||
public abstract IEnumerable<RuleResult> validate(NuspecReader reader); | ||
|
||
protected static bool has_element(NuspecReader reader, string name) | ||
{ | ||
var metadataNode = reader.Xml.Root.Elements().FirstOrDefault(e => StringComparer.Ordinal.Equals(e.Name.LocalName, "metadata")); | ||
|
||
return !(metadataNode is null || metadataNode.Elements(XName.Get(name, metadataNode.GetDefaultNamespace().NamespaceName)).FirstOrDefault() is null); | ||
} | ||
|
||
protected static string get_element_value(NuspecReader reader, string name) | ||
{ | ||
var metadataNode = reader.Xml.Root.Elements().FirstOrDefault(e => StringComparer.Ordinal.Equals(e.Name.LocalName, "metadata")); | ||
|
||
if (metadataNode is null) | ||
{ | ||
return null; | ||
} | ||
|
||
var element = metadataNode.Elements(XName.Get(name, metadataNode.GetDefaultNamespace().NamespaceName)).FirstOrDefault(); | ||
|
||
if (element is null) | ||
{ | ||
return null; | ||
} | ||
|
||
return element.Value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright © 2023-Present Chocolatey Software, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace chocolatey.infrastructure.app.services | ||
{ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using chocolatey.infrastructure.guards; | ||
using chocolatey.infrastructure.rules; | ||
using chocolatey.infrastructure.services; | ||
using NuGet.Configuration; | ||
using NuGet.Packaging; | ||
|
||
public sealed class RuleService : IRuleService | ||
{ | ||
private readonly IMetadataRule[] _rules; | ||
|
||
public RuleService(IMetadataRule[] rules) | ||
{ | ||
_rules = rules; | ||
} | ||
|
||
public IEnumerable<RuleResult> validate_rules(string filePath) | ||
{ | ||
Ensure.that(() => filePath) | ||
.is_not_null_or_whitespace() | ||
.has_any_extension(NuGetConstants.PackageExtension, NuGetConstants.ManifestExtension); | ||
|
||
var rules = filePath.EndsWith(NuGetConstants.PackageExtension) | ||
? get_rules_from_package_async(filePath).GetAwaiter().GetResult() | ||
: get_rules_from_metadata(filePath); | ||
|
||
return rules | ||
.OrderBy(r => r.Severity) | ||
.ThenBy(r => r.Message); | ||
} | ||
|
||
private async Task<IEnumerable<RuleResult>> get_rules_from_package_async(string filePath, CancellationToken token = default) | ||
{ | ||
using (var packageReader = new PackageArchiveReader(filePath)) | ||
{ | ||
var nuspecReader = await packageReader.GetNuspecReaderAsync(token); | ||
|
||
// We add ToList here to ensure that the package | ||
// reader hasn't been disposed of before we return | ||
// any results. | ||
return validate_nuspec(nuspecReader, _rules).ToList(); | ||
} | ||
} | ||
|
||
private IEnumerable<RuleResult> get_rules_from_metadata(string filePath) | ||
{ | ||
var nuspecReader = new NuspecReader(filePath); | ||
|
||
return validate_nuspec(nuspecReader, _rules); | ||
} | ||
|
||
private static IEnumerable<RuleResult> validate_nuspec(NuspecReader reader, IMetadataRule[] rules) | ||
{ | ||
foreach (var rule in rules) | ||
{ | ||
var validationResults = rule.validate(reader); | ||
|
||
foreach (var result in validationResults.Where(v => v.Severity != RuleType.None)) | ||
{ | ||
yield return result; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright © 2023-Present Chocolatey Software, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace chocolatey.infrastructure.rules | ||
{ | ||
using System.Collections.Generic; | ||
using chocolatey.infrastructure.app.attributes; | ||
using NuGet.Packaging; | ||
|
||
[MultiService] | ||
public interface IMetadataRule | ||
{ | ||
IEnumerable<RuleResult> validate(NuspecReader reader); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright © 2023-Present Chocolatey Software, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace chocolatey.infrastructure.rules | ||
{ | ||
public readonly struct RuleResult | ||
{ | ||
public static readonly RuleResult Success = new RuleResult(RuleType.None, string.Empty); | ||
|
||
public RuleResult(RuleType severity, string message) | ||
{ | ||
Severity = severity; | ||
Message = message; | ||
} | ||
|
||
public readonly RuleType Severity; | ||
|
||
public readonly string Message; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright © 2023-Present Chocolatey Software, Inc | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
namespace chocolatey.infrastructure.rules | ||
{ | ||
public enum RuleType | ||
{ | ||
None = 0, | ||
Error, | ||
Warning, | ||
Information | ||
} | ||
} |
Oops, something went wrong.