Skip to content
Draft
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
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
docker:
- image: circleci/openjdk:11-jdk-stretch
steps:
- run: sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python
- run: sudo apt-get update && sudo apt-get install pkg-config zip g++ zlib1g-dev unzip python
- run: wget -O /tmp/bazel-installer.sh https://github.com/bazelbuild/bazel/releases/download/0.27.0/bazel-0.27.0-installer-linux-x86_64.sh
- run: chmod +x /tmp/bazel-installer.sh
- run: /tmp/bazel-installer.sh --user
Expand Down
27 changes: 27 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,33 @@ sh_test(
],
)

generate_codeowners(
name = "github_codeowners_auto_sort",
owners = [
":py_files",
":multi_team",
"//tests/hey/sub/sub2:codeowners",
"//tests/hey/sub/sub3:codeowners",
"//tests/heyoo:codeowners",
"//tests/hey/sub:codeowners",
"//tests/hey:codeowners",
],
sort = True,
)

sh_test(
name = "validate_codeowners_auto_sort",
srcs = ["//tools:diff.sh"],
args = [
"$(location :github_codeowners_auto_sort.out)",
"$(location github_codeowners_auto_sort_golden)",
],
data = [
"github_codeowners_auto_sort_golden",
":github_codeowners_auto_sort.out",
],
)

failure_testing_test(
name = "both_team_and_teams_set",
expected = "Both team and teams can not be set at the same time.",
Expand Down
10 changes: 10 additions & 0 deletions tests/github_codeowners_auto_sort_golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file was generated by rules_codeowners / Bazel
# Don't edit it directly

/tests/*.html @the-css-team @the-html-team
/tests/*.py @the-python-dev
/tests/hey/ @org/foo
/tests/hey/sub/ @org/foo2
/tests/hey/sub/sub2/ @org/foo2
/tests/hey/sub/sub3/ @org/foo2
/tests/heyoo/*.js @org/bar
7 changes: 7 additions & 0 deletions tests/hey/sub/sub2/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//tools:codeowners.bzl", "codeowners")

codeowners(
name = "codeowners",
team = "@org/foo2",
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions tests/hey/sub/sub3/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("//tools:codeowners.bzl", "codeowners")

codeowners(
name = "codeowners",
team = "@org/foo2",
visibility = ["//visibility:public"],
)
18 changes: 15 additions & 3 deletions tools/codeowners.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ def _generate_codeowners_impl(ctx):
all_ownerships.append(owner.files.to_list()[0].path)
all_ownerships_files.append(owner.files.to_list()[0])

unsorted_tmp_file = ctx.actions.declare_file(ctx.label.name + "_unsorted")

ctx.actions.run_shell(
outputs = [ctx.outputs.outfile],
outputs = [ctx.outputs.outfile, unsorted_tmp_file],
inputs = all_ownerships_files,
arguments = all_ownerships,
env = {
"OUTFILE": ctx.outputs.outfile.path,
"UNSORTED_FILE": unsorted_tmp_file.path,
"SORT": "true" if ctx.attr.sort else "false",
},
command = """
set -euo pipefail
Expand All @@ -70,15 +74,23 @@ echo "" >> "$OUTFILE"

for file in "$@"
do
cat "$file" >> "$OUTFILE"
cat "$file" >> "$UNSORTED_FILE"
done
""",

# Optional sorting, the most specific rule will appear last in the output
if "$SORT"; then
cat "$UNSORTED_FILE" | sort >> "$OUTFILE"
else
cat "$UNSORTED_FILE" >> "$OUTFILE"
fi
""",
)

generate_codeowners = rule(
implementation = _generate_codeowners_impl,
attrs = {
"owners": attr.label_list(),
"sort": attr.bool(default = False, mandatory = False),
},
outputs = {
"outfile": "%{name}.out",
Expand Down