Skip to content

Commit

Permalink
(chocolatey#3000) Add back old validation messages for custom rules
Browse files Browse the repository at this point in the history
This commit adds back the validation messages that was
already being used in v1 but is missing in the uplifted
v2 version.
Additionally, this commit also updates the validation
messages to be more user friendly.
  • Loading branch information
AdmiringWorm committed Jan 26, 2023
1 parent 815c3e6 commit 08cc2f0
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/chocolatey/chocolatey.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props" Condition="Exists('..\packages\Microsoft.CodeAnalysis.BannedApiAnalyzers.3.3.3\build\Microsoft.CodeAnalysis.BannedApiAnalyzers.props')" />
<PropertyGroup>
Expand Down Expand Up @@ -216,12 +216,15 @@
<Compile Include="infrastructure.app\nuget\ChocolateyNuGetSettings.cs" />
<Compile Include="infrastructure.app\nuget\ChocolateySourceCacheContext.cs" />
<Compile Include="infrastructure.app\rules\MetadataRuleBase.cs" />
<Compile Include="infrastructure.app\rules\RequiredMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\EmpyOrInvalidUrlMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\FrameWorkReferencesMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\IconMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\LicenseMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\PackageTypesMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\ReadmeMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\RepositoryMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\RequireLicenseAcceptanceMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\ServicableMetadataRule.cs" />
<Compile Include="infrastructure.app\rules\VersionMetadataRule.cs" />
<Compile Include="infrastructure.app\services\RuleService.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class EmpyOrInvalidUrlMetadataRule : MetadataRuleBase
{
public override IEnumerable<RuleResult> validate(NuspecReader reader)
{
var items = new[]
{
"projectUrl",
"projectSourceUrl",
"docsUrl",
"bugTrackerUrl",
"mailingListUrl",
"iconUrl",
"licenseUrl"
};

foreach (var item in items)
{
if (has_element(reader, item))
{
var value = get_element_value(reader, item);

if (string.IsNullOrWhiteSpace(value))
{
yield return new RuleResult(RuleType.Error, "The {0} element in the package nuspec file cannot be empty.".format_with(item));
}
else if (!Uri.TryCreate(value, UriKind.Absolute, out _))
{
yield return new RuleResult(RuleType.Error, "'{0}' is not a valid URL for the {1} element in the package nuspec file.".format_with(value, item));
}
}
}
}
}
}
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.app.rules
{
using System.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class RequireLicenseAcceptanceMetadataRule : IMetadataRule
{
public IEnumerable<RuleResult> validate(NuspecReader reader)
{
if (string.IsNullOrWhiteSpace(reader.GetLicenseUrl()) && reader.GetRequireLicenseAcceptance())
{
yield return new RuleResult(RuleType.Error, "Enabling license acceptance requires a license url.");
}
}
}
}
43 changes: 43 additions & 0 deletions src/chocolatey/infrastructure.app/rules/RequiredMetadataRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;

internal sealed class RequiredMetadataRule : MetadataRuleBase
{
public override IEnumerable<RuleResult> validate(NuspecReader reader)
{
var requiredItems = new[]
{
"id",
"version",
"authors",
"description"
};

foreach (var item in requiredItems)
{
if (string.IsNullOrWhiteSpace(get_element_value(reader, item)))
{
yield return new RuleResult(RuleType.Error, "{0} is a required element in the package nuspec file.".format_with(item));
}
}
}
}
}
37 changes: 37 additions & 0 deletions src/chocolatey/infrastructure.app/rules/VersionMetadataRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// 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.Collections.Generic;
using chocolatey.infrastructure.rules;
using NuGet.Packaging;
using NuGet.Versioning;

internal sealed class VersionMetadataRule : MetadataRuleBase
{
public override IEnumerable<RuleResult> validate(NuspecReader reader)
{
var version = get_element_value(reader, "version");

// We need to check for the $version$ substitution value, as it in not replaced
// at this stage yet.
if (!string.IsNullOrEmpty(version) && !version.is_equal_to("$version$") && !NuGetVersion.TryParse(version, out _))
{
yield return new RuleResult(RuleType.Error, "'{0}' is not a valid version string in the package nuspec file.".format_with(version));
}
}
}
}

0 comments on commit 08cc2f0

Please sign in to comment.