Skip to content

Commit

Permalink
Make table output more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Pizzo committed Jul 16, 2024
1 parent bc3168a commit 9fa93f8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
16 changes: 16 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright (c) 2024 Vincent Pizzo
#
# 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.
#

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
Expand Down
16 changes: 16 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright (c) 2024 Vincent Pizzo
#
# 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.
#

- id: codeowners-cli
name: codeowners-cli
description: Runs codeowners-cli to list or validate code owners.
Expand Down
2 changes: 1 addition & 1 deletion hooks/codeowners-cli-pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ fi
if !(codeowners-cli $@)
then
echo
echo "Error: new CODEOWNErS errors found. Please fix them and retry the commit."
echo "Error: new CODEOWNERS errors found. Please fix them and retry the commit."
exit 1
fi
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public Integer call() {
});

try {
GitIgnore gitIgnore = buildGitIgnore();
final GitIgnore gitIgnore = buildGitIgnore();

CodeOwners codeOwners = new CodeOwners(codeownersFile.toFile());
final CodeOwners codeOwners = new CodeOwners(codeownersFile.toFile());
if (codeOwners.hasStructuralProblems()) {
throw new RuntimeException("CodeOwners has structural issues!");
}
Expand All @@ -123,7 +123,7 @@ public Integer call() {
.stream()
.sorted();

final List<Map.Entry<Path, List<String>>> matchEntries = allPotentialFiles
final List<Map.Entry<Path, String>> matchEntries = allPotentialFiles
.map(filePath -> Map.entry(filePath, codeOwners.getAllApprovers(filePath.toString())))
.filter(entry -> {
final List<String> approvers = entry.getValue();
Expand All @@ -137,29 +137,30 @@ else if (unownedFilesOnly) {
return true;
}
})
.map(entry -> Map.entry(
entry.getKey(),
entry.getValue().isEmpty() ? "(Unowned)" : String.join(", ", entry.getValue())))
.toList();

if (matchEntries.isEmpty()) {
System.out.println("No matching owners");
return 0;
}
else {
int maxFileLength = matchEntries
final int maxFileLength = Math.max(matchEntries
.stream()
.mapToInt(entry -> entry.getKey().toString().length())
.max()
.getAsInt();
int maxCodeOwnersLength = matchEntries
.getAsInt(), 12);
final int maxCodeOwnersLength = Math.max(matchEntries
.stream()
.mapToInt(entry -> String.join(", ", entry.getValue()).length())
.mapToInt(entry -> entry.getValue().length())
.max()
.getAsInt();
.getAsInt(), 12);

String format = "%" + maxFileLength + "s | %" + maxCodeOwnersLength + "s\n";
final String format = "%" + maxFileLength + "s | %" + maxCodeOwnersLength + "s\n";
System.out.printf(format, "File", "Approvers");
matchEntries.forEach(entry -> {
System.out.printf(format, entry.getKey().toString(), String.join(", ", entry.getValue()));
});
matchEntries.forEach(entry -> System.out.printf(format, entry.getKey().toString(), entry.getValue()));

return failOnOutput ? 1 : 0;
}
Expand Down

0 comments on commit 9fa93f8

Please sign in to comment.