Skip to content

Commit 2d04c30

Browse files
Add rule for checking CommitHash
1 parent feda5f4 commit 2d04c30

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

src/NuGetPackageVerifier/CompositeRules/AdxVerificationCompositeRule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class AdxVerificationCompositeRule : IPackageVerifierRule
1212
{
1313
IPackageVerifierRule[] _rules = new IPackageVerifierRule[]
1414
{
15+
new AssemblyHasCommitHashAttributeRule(),
1516
new AssemblyHasCompanyAttributeRule(),
1617
new AssemblyHasCopyrightAttributeRule(),
1718
new AssemblyHasCorrectJsonNetVersionRule(),

src/NuGetPackageVerifier/CompositeRules/DefaultCompositeRule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class DefaultCompositeRule : IPackageVerifierRule
1212
{
1313
IPackageVerifierRule[] _rules = new IPackageVerifierRule[]
1414
{
15+
new AssemblyHasCommitHashAttributeRule(),
1516
new AssemblyHasCompanyAttributeRule(),
1617
new AssemblyHasCopyrightAttributeRule(),
1718
new AssemblyHasCorrectJsonNetVersionRule(),

src/NuGetPackageVerifier/PackageIssueFactory.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ public static PackageVerifierIssue AssemblyMissingServicingAttribute(string asse
1919
MyPackageIssueLevel.Error);
2020
}
2121

22+
public static PackageVerifierIssue AssemblyMissingHashAttribute(string assemblyPath)
23+
{
24+
return new PackageVerifierIssue(
25+
"ASSEMBLY_COMMIT_HASH",
26+
assemblyPath,
27+
string.Format(
28+
@"The managed assembly '{0}' in this package is missing the '[assembly: AssemblyMetadata(""CommitHash"", ""<text>"")]' attribute.",
29+
assemblyPath),
30+
MyPackageIssueLevel.Error);
31+
}
32+
2233
public static PackageVerifierIssue AssemblyMissingNeutralResourcesLanguageAttribute(string assemblyPath)
2334
{
2435
return new PackageVerifierIssue(

src/NuGetPackageVerifier/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public static int Main(string[] args)
6868

6969
// TODO: Look this up using reflection or something
7070
var allRules = new IPackageVerifierRule[] {
71+
new AssemblyHasCommitHashAttributeRule(),
7172
new AssemblyHasCompanyAttributeRule(),
7273
new AssemblyHasCopyrightAttributeRule(),
7374
new AssemblyHasCorrectJsonNetVersionRule(),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.Collections.Generic;
5+
using System.IO;
6+
using System.Linq;
7+
using System.Reflection;
8+
using Mono.Cecil;
9+
using Mono.Collections.Generic;
10+
11+
namespace NuGetPackageVerifier.Rules
12+
{
13+
public class AssemblyHasCommitHashAttributeRule : AssemblyHasAttributeRuleBase
14+
{
15+
public override IEnumerable<PackageVerifierIssue> ValidateAttribute(
16+
string currentFilePath,
17+
Collection<CustomAttribute> assemblyAttributes)
18+
{
19+
var fileName = Path.GetFileNameWithoutExtension(currentFilePath);
20+
var isSourcesPackage = fileName.EndsWith(".Sources", System.StringComparison.OrdinalIgnoreCase);
21+
if (!isSourcesPackage && !HasCommitHashInMetadataAttribute(assemblyAttributes))
22+
{
23+
yield return PackageIssueFactory.AssemblyMissingHashAttribute(currentFilePath);
24+
}
25+
}
26+
27+
private static bool HasCommitHashInMetadataAttribute(Collection<CustomAttribute> assemblyAttributes)
28+
{
29+
var hashAttribute = assemblyAttributes
30+
.SingleOrDefault(a =>
31+
a.AttributeType.FullName == typeof(AssemblyMetadataAttribute).FullName
32+
&& a.ConstructorArguments.Count == 2
33+
&& a.ConstructorArguments[0].Value as string == "CommitHash"
34+
&& !string.IsNullOrEmpty(a.ConstructorArguments[1].Value as string));
35+
36+
return hashAttribute != null;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)