Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: when looking for a match in CODEOWNERS, do not emit error message for commented out paths #5366

Merged
merged 5 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ private static void WriteOwnersToCsv(
}

// Possible future work:
// instead of returning lines with issues, consider returning the modified & fixed CODEOWNERS file.
// instead of returning lines with issues, consider returning the modified & fixed CODEOWNERS file.
// It could work by reading all the lines, then replacing the wrong
// lines by using dict replacement. Need to be careful about retaining spaces to not misalign,
// e.g.
Expand All @@ -352,6 +352,7 @@ private static List<string> PathsWithIssues(
outputLines.AddRange(PathsWithMissingPrefixSlash(entries));
outputLines.AddRange(PathsWithMissingSuffixSlash(targetDir, entries, paths));
outputLines.AddRange(InvalidPaths(entries));
// TODO: add a check here for CODEOWNERS paths that do not match any dir or file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need an issue tracking this?

Copy link
Contributor Author

@konrad-jamrozik konrad-jamrozik Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benbp not really. I put it here more as a reminder that this validation is currently missing, in the unlikely scenario I (or somebody else) will have to use it again. I forgot at first to include it. But I kind of expect for it to be actually picked up when working on:

This kind of validation is mentioned here:


return outputLines;
}
Expand Down Expand Up @@ -414,9 +415,15 @@ private static List<string> PathsWithMissingSuffixSlash(
private static bool MatchesNamePrefix(string[] paths, string trimmedPathExpression)
=> paths.Any(
path =>
path.TrimStart('/').StartsWith(trimmedPathExpression)
&& path.TrimStart('/').Length != trimmedPathExpression.Length
&& !path.TrimStart('/').Substring(trimmedPathExpression.Length).StartsWith('/'));
{
string trimmedPath = path.TrimStart('/');
bool pathIsChildDir = trimmedPath.Contains("/")
&& trimmedPath.Length > trimmedPathExpression.Length
&& trimmedPath.Substring(trimmedPathExpression.Length).StartsWith('/');
return trimmedPath.StartsWith(trimmedPathExpression)
&& trimmedPath.Length != trimmedPathExpression.Length
&& !pathIsChildDir;
});

private static bool MatchesDirExactly(string targetDir, string trimmedPathExpression)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public class MatchedCodeownersEntry
/// See the class comment to understand this method purpose.
/// </summary>
public static bool IsCodeownersPathValid(string codeownersPathExpression)
=> !ContainsUnsupportedCharacters(codeownersPathExpression)
=> !IsCommentedOutPath(codeownersPathExpression)
&& !ContainsUnsupportedCharacters(codeownersPathExpression)
&& !ContainsUnsupportedSequences(codeownersPathExpression);

/// <summary>
Expand Down Expand Up @@ -128,6 +129,16 @@ private CodeownersEntry GetMatchingCodeownersEntry(

private CodeownersEntry NoMatchCodeownersEntry { get; } = new CodeownersEntry();

/// <summary>
/// We do not output any error message in case the path is commented out,
/// as a) such paths are expected to be processed and discarded by this logic
/// and b) outputting error messages would possibly result in output
/// truncation, truncating the resulting json, and thus making the output of the
/// calling tool malformed.
/// </summary>
private static bool IsCommentedOutPath(string codeownersPathExpression)
=> codeownersPathExpression.Trim().StartsWith("#");

/// <summary>
/// See the comment on unsupportedChars.
/// </summary>
Expand Down