Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit 940d5b0

Browse files
Refactor Checker classes to unify additional information handling and improve configuration file reading
1 parent c4dd299 commit 940d5b0

11 files changed

Lines changed: 127 additions & 86 deletions

RepositoryLinter/Checks/Checker.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,23 @@ public abstract class Checker
1717
/// </summary>
1818
public required string TipToFix { get; init; }
1919

20+
protected string AdditionalInformation { get; set; } = string.Empty;
21+
22+
/// <summary>
23+
/// Concatenates the given information to the AdditionalInformation property.
24+
/// </summary>
25+
/// <param name="information">A string</param>
26+
public void AddAdditionalInformationLine(string information)
27+
{
28+
if (string.IsNullOrWhiteSpace(AdditionalInformation))
29+
{
30+
AdditionalInformation = information;
31+
return;
32+
}
33+
34+
AdditionalInformation += '\n' + information;
35+
}
36+
2037
/// <summary>
2138
/// Current status of the check.
2239
/// </summary>

RepositoryLinter/Checks/DirectoryExistsCheck.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class DirectoryExistsCheck(string relativeDirectoryPath, string pathToGit
1414
/// </summary>
1515
public CheckStatus StatusWhenEmpty { get; init; } = CheckStatus.Red;
1616

17-
private string _additionalInfo = string.Empty;
1817
public List<string> ShouldContainFiles { get; init; } = [];
1918
public SearchOption SearchOption { get; init; } = SearchOption.TopDirectoryOnly;
2019
private readonly List<string> _directoryContent = [];
@@ -31,7 +30,7 @@ public override void Run()
3130
if (empty)
3231
{
3332
Status = StatusWhenEmpty;
34-
_additionalInfo = $"Directory {relativeDirectoryPath} is empty.";
33+
AdditionalInformation = $"Directory {relativeDirectoryPath} is empty.";
3534
return;
3635
}
3736

@@ -45,7 +44,7 @@ public override void Run()
4544
if (_directoryContent.Count == 0)
4645
{
4746
Status = StatusWhenEmpty;
48-
_additionalInfo =
47+
AdditionalInformation =
4948
$"Directory {path} does not contain any of {string.Join(',', ShouldContainFiles)}";
5049
return;
5150
}
@@ -59,10 +58,10 @@ public override string ToString()
5958
{
6059
var builder = new StringBuilder(base.ToString());
6160

62-
if (_additionalInfo != string.Empty)
61+
if (AdditionalInformation != string.Empty)
6362
{
6463
builder.Append(Environment.NewLine);
65-
builder.Append(_additionalInfo);
64+
builder.Append(AdditionalInformation);
6665
}
6766

6867
if (_directoryContent.Count == 0) return builder.ToString();

RepositoryLinter/Checks/FileExistsCheck.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ public class FileExistsCheck(string relativeFilePath, string pathToGitDirectory)
1414
/// </summary>
1515
public CheckStatus StatusWhenEmpty { get; init; } = CheckStatus.Green;
1616

17-
private string? _additionalInfo;
18-
1917
/// <summary>
2018
/// Recursively search for the file in the directory. Default is false.
2119
/// </summary>
@@ -44,7 +42,7 @@ public override void Run()
4442
if (files.Count > 1)
4543
{
4644
Status = CheckStatus.Yellow;
47-
_additionalInfo = $"\nMultiple files matching {fileName} found in the directory {directory}.";
45+
AdditionalInformation = $"\nMultiple files matching {fileName} found in the directory {directory}.";
4846
return;
4947
}
5048

@@ -55,7 +53,7 @@ public override void Run()
5553

5654
if (exists && _isEmpty)
5755
{
58-
_additionalInfo += $"File {fileName} is empty.";
56+
AdditionalInformation += $"File {fileName} is empty.";
5957
Status = StatusWhenEmpty;
6058
return;
6159
}
@@ -105,9 +103,9 @@ public override string ToString()
105103
str += "\nFile is empty";
106104
}
107105

108-
if (_additionalInfo != "")
106+
if (AdditionalInformation != "")
109107
{
110-
str += _additionalInfo;
108+
str += '\n' + AdditionalInformation;
111109
}
112110

113111
return str;

RepositoryLinter/Checks/FilePathContainsStringChecker.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,24 @@ public class FilePathContainsStringChecker(string stringToFind, string pathToGit
1010
public CheckStatus StatusWhenNotFound { get; init; } = CheckStatus.Red;
1111

1212
private readonly GitIgnoreHandler _gitIgnore = new(pathToGitRepo);
13-
13+
1414
public override void Run()
1515
{
1616
var paths = GetAllFilePaths();
17-
17+
1818
foreach (var path in paths)
1919
{
2020
var relativePath = Path.GetRelativePath(pathToGitRepo, path);
2121
var found = relativePath.Contains(stringToFind);
22-
22+
2323
var ignored = _gitIgnore.IsIgnored(relativePath);
2424

2525
if (found && !ignored)
2626
{
2727
_foundPaths.Add(relativePath);
2828
}
2929
}
30-
30+
3131
// Set status depending on if files are found
3232
Status = _foundPaths.Count != 0 ? StatusWhenFound : StatusWhenNotFound;
3333
}
@@ -50,7 +50,13 @@ public override string ToString()
5050
builder.Append(Environment.NewLine);
5151
builder.Append(path);
5252
}
53-
53+
54+
if (AdditionalInformation != string.Empty)
55+
{
56+
builder.Append(Environment.NewLine);
57+
builder.Append(AdditionalInformation);
58+
}
59+
5460
return builder.ToString();
5561
}
5662
}
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
using System.Text;
2+
13
namespace RepositoryLinter.Checks;
24

35
public class LicenseFileChecker : Checker
46
{
57
private readonly string _directory;
6-
private string _additionalInfo = string.Empty;
78
public CheckStatus StatusWhenEmpty { get; init; } = CheckStatus.Red;
9+
810
public LicenseFileChecker(string directory)
911
{
1012
_directory = directory;
1113
Status = CheckStatus.Red;
1214
}
13-
15+
1416
public override void Run()
1517
{
1618
var licenseFiles = Directory.EnumerateFiles(_directory, "LICENSE*", SearchOption.TopDirectoryOnly).ToList();
@@ -21,67 +23,71 @@ public override void Run()
2123
Status = CheckStatus.Green;
2224
return;
2325
}
24-
26+
2527
if (licenseFiles.Count == 1 && FilesAreEmpty(licenseFiles))
2628
{
2729
// License file found but is empty
2830
Status = StatusWhenEmpty;
29-
_additionalInfo = "License file is empty.";
31+
AdditionalInformation = "License file is empty.";
3032
return;
3133
}
32-
34+
3335
if (licenseFiles.Count > 1)
3436
{
3537
// Multiple license files found
3638
Status = CheckStatus.Yellow;
37-
_additionalInfo = "Multiple license files found.";
39+
AdditionalInformation = "Multiple license files found.";
3840
return;
3941
}
40-
42+
4143
// Check if there is a LICENSE directory that contains the license file
42-
var licenseDirectories = Directory.EnumerateDirectories(_directory, "LICENSE*", SearchOption.TopDirectoryOnly).ToList();
43-
44+
var licenseDirectories = Directory.EnumerateDirectories(_directory, "LICENSE*", SearchOption.TopDirectoryOnly)
45+
.ToList();
46+
4447
if (licenseDirectories.Count == 0) return; // No LICENSE directory found
45-
48+
4649
// Check if the license file is in the LICENSE directory
4750
var licenseDirectory = licenseDirectories[0];
48-
var licenseFilesInDirectory = Directory.EnumerateFiles(licenseDirectory, "LICENSE*", SearchOption.TopDirectoryOnly).ToList();
49-
51+
var licenseFilesInDirectory =
52+
Directory.EnumerateFiles(licenseDirectory, "LICENSE*", SearchOption.TopDirectoryOnly).ToList();
53+
5054
var filesAreEmpty = FilesAreEmpty(licenseFilesInDirectory);
51-
55+
5256
if (licenseFilesInDirectory.Count != 0 && !filesAreEmpty)
5357
{
5458
// License file found in LICENSE directory and is not empty
5559
Status = CheckStatus.Green;
5660
}
57-
61+
5862
if (licenseFilesInDirectory.Count != 0 && filesAreEmpty)
5963
{
6064
// License file found in LICENSE directory but is empty
6165
Status = StatusWhenEmpty;
62-
_additionalInfo = $"License file in {licenseDirectory} is empty.";
66+
AdditionalInformation = $"License file in {licenseDirectory} is empty.";
6367
}
6468
}
65-
69+
6670
private static bool FilesAreEmpty(List<string> files)
6771
{
6872
return files.Any(file => new FileInfo(file).Length == 0);
6973
}
70-
74+
7175
public override string ToString()
7276
{
73-
var output = base.ToString();
74-
77+
var builder = new StringBuilder(base.ToString());
78+
7579
switch (Status)
7680
{
7781
case CheckStatus.Green:
78-
return output;
82+
return builder.ToString();
7983
case CheckStatus.Yellow:
80-
output += "\n" + _additionalInfo;
81-
return output;
84+
builder.Append(Environment.NewLine);
85+
builder.Append(AdditionalInformation);
86+
return builder.ToString();
8287
default:
83-
output += "\nLicense file not found.";
84-
return output;
88+
builder.Append(Environment.NewLine);
89+
builder.Append("License file not found.");
90+
return builder.ToString();
8591
}
8692
}
8793
}

RepositoryLinter/Checks/SearchForStringCheck.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ public class SearchForStringCheck(string searchString, string gitRepoPath, Globa
1111
/// Invert the result of the check. Default is false.
1212
/// </summary>
1313
public bool InvertResult { get; init; }
14-
14+
1515
private readonly List<string> _files = [];
16-
17-
private string _additionalInfo = "";
16+
1817
public override void Run()
1918
{
2019
var files = Directory.EnumerateFiles(gitRepoPath, "*.*", SearchOption.AllDirectories);
@@ -28,7 +27,7 @@ public override void Run()
2827
{
2928
continue; // Skip files that are ignored by .gitignore if we have already found a file that is ignored.
3029
}
31-
30+
3231
// Read the file in chunks to avoid reading the entire file into memory
3332
const int chunkSize = 1024;
3433
using var fileReader = File.OpenText(file);
@@ -37,16 +36,17 @@ public override void Run()
3736
while ((bytesRead = fileReader.Read(buffer, 0, chunkSize)) > 0)
3837
{
3938
var chunk = new string(buffer, 0, bytesRead);
40-
39+
4140
if (!chunk.Contains(searchString)) continue;
42-
41+
4342
if (fileIsIgnored)
4443
{
45-
_additionalInfo = "Found string in files that are ignored by .gitignore. If you want to search in these files, run the program with the --ignore-gitignore flag.";
44+
AdditionalInformation =
45+
"Found string in files that are ignored by .gitignore. If you want to search in these files, run the program with the --ignore-gitignore flag.";
4646
hasIgnoredFiles = true;
4747
break;
4848
}
49-
49+
5050
_files.Add(file);
5151
break;
5252
}
@@ -57,6 +57,7 @@ public override void Run()
5757
{
5858
found = !found;
5959
}
60+
6061
Status = found ? CheckStatus.Green : StatusWhenFailed;
6162
}
6263

@@ -66,7 +67,8 @@ public override string ToString()
6667
{
6768
return base.ToString();
6869
}
69-
70-
return base.ToString() + $"\nSearch string: {searchString} found in following files:\n{string.Join("\n", _files)}\nTotal number of files: {_files.Count}\n{_additionalInfo}";
70+
71+
return base.ToString() +
72+
$"\nSearch string: {searchString} found in following files:\n{string.Join("\n", _files)}\nTotal number of files: {_files.Count}\n{AdditionalInformation}";
7173
}
7274
}

RepositoryLinter/Checks/SecretsCheck.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ public class SecretsCheck(string pathToGitRepo, GlobalConfiguration config) : Ch
2222
/// </summary>
2323
private bool _fileHasBeenIgnored;
2424

25-
/// <summary>
26-
/// Additional information about the check. This is set if secrets are found in files that are ignored by .gitignore.
27-
/// </summary>
28-
private string _additionalInfo = "";
29-
3025
private string? _lastRunCommand;
3126

3227
public override void Run()
@@ -47,7 +42,7 @@ public override string ToString()
4742
outputBuilder.Append($"Ran Trufflehog with command \"{_lastRunCommand}\" and no secrets where found.");
4843
return outputBuilder.ToString();
4944
case CheckStatus.Yellow:
50-
outputBuilder.Append(_additionalInfo);
45+
outputBuilder.Append(AdditionalInformation);
5146
return outputBuilder.ToString();
5247
}
5348

@@ -71,7 +66,7 @@ public override string ToString()
7166
if (!_fileHasBeenIgnored) return outputBuilder.ToString();
7267

7368
outputBuilder.Append(Environment.NewLine);
74-
outputBuilder.Append(_additionalInfo);
69+
outputBuilder.Append(AdditionalInformation);
7570

7671
return outputBuilder.ToString();
7772
}
@@ -194,7 +189,7 @@ private void RemoveIgnoredFiles()
194189
if (_foundSecretsJson.Count == 0 && _fileHasBeenIgnored)
195190
{
196191
Status = CheckStatus.Yellow;
197-
_additionalInfo = "All secrets found are in files that are ignored by .gitignore.";
192+
AdditionalInformation = "All secrets found are in files that are ignored by .gitignore.";
198193
}
199194

200195
// If some secrets are found in ignored files, set status to red

0 commit comments

Comments
 (0)