Skip to content

Commit

Permalink
Bugfix: when looking for a match in CODEOWNERS, do not emit error m…
Browse files Browse the repository at this point in the history
…essage for commented out paths (#5366)
  • Loading branch information
Konrad Jamrozik authored Feb 13, 2023
1 parent 56ccb6a commit 2c09d93
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
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.

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

0 comments on commit 2c09d93

Please sign in to comment.