diff --git a/.circleci/config.yml b/.circleci/config.yml index 8f1654d..40c6693 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 diff --git a/tests/BUILD b/tests/BUILD index 5e45330..156d2f6 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -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.", diff --git a/tests/github_codeowners_auto_sort_golden b/tests/github_codeowners_auto_sort_golden new file mode 100644 index 0000000..a58425b --- /dev/null +++ b/tests/github_codeowners_auto_sort_golden @@ -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 diff --git a/tests/hey/sub/sub2/BUILD b/tests/hey/sub/sub2/BUILD new file mode 100644 index 0000000..1cb243f --- /dev/null +++ b/tests/hey/sub/sub2/BUILD @@ -0,0 +1,7 @@ +load("//tools:codeowners.bzl", "codeowners") + +codeowners( + name = "codeowners", + team = "@org/foo2", + visibility = ["//visibility:public"], +) diff --git a/tests/hey/sub/sub3/BUILD b/tests/hey/sub/sub3/BUILD new file mode 100644 index 0000000..1cb243f --- /dev/null +++ b/tests/hey/sub/sub3/BUILD @@ -0,0 +1,7 @@ +load("//tools:codeowners.bzl", "codeowners") + +codeowners( + name = "codeowners", + team = "@org/foo2", + visibility = ["//visibility:public"], +) diff --git a/tools/codeowners.bzl b/tools/codeowners.bzl index 5c953bd..799bcf3 100644 --- a/tools/codeowners.bzl +++ b/tools/codeowners.bzl @@ -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 @@ -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",