From 672839884c5c3baff8da11c232655cd9bc8510a0 Mon Sep 17 00:00:00 2001 From: Gustav Westling Date: Thu, 26 Sep 2019 23:58:02 +0200 Subject: [PATCH] Add posibility to define global_owners The global_owners attribute can be set on the generate_codeowners rule to automatically add that team to the owner of all codeowners. The include_global_owners attribute can be set on the codeowners rule to out out of the global owners for that specific rule/pattern/path. --- tests/global_approvers/BUILD | 44 +++++++++++++++++++ .../global_approvers/github_codeowners_golden | 6 +++ tools/codeowners.bzl | 29 +++++++++--- 3 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 tests/global_approvers/BUILD create mode 100644 tests/global_approvers/github_codeowners_golden diff --git a/tests/global_approvers/BUILD b/tests/global_approvers/BUILD new file mode 100644 index 0000000..c4e002a --- /dev/null +++ b/tests/global_approvers/BUILD @@ -0,0 +1,44 @@ +load("//tools:codeowners.bzl", "codeowners", "generate_codeowners") +load("//tools:codeowners_test.bzl", "failure_testing_test") + +generate_codeowners( + name = "github_codeowners", + owners = [ + ":a", + ":b", + ":c", + ], + global_owners = "@epic-team", +) + +codeowners( + name = "a", + pattern = "*.a", + team = "@team-a", +) + +codeowners( + name = "b", + pattern = "*.b", + team = "@team-b", + include_global_owners = False, +) + +codeowners( + name = "c", + pattern = "*.c", + team = "@team-c", +) + +sh_test( + name = "validate_codeowners", + srcs = ["//tools:diff.sh"], + args = [ + "$(location :github_codeowners.out)", + "$(location github_codeowners_golden)", + ], + data = [ + "github_codeowners_golden", + ":github_codeowners.out", + ], +) diff --git a/tests/global_approvers/github_codeowners_golden b/tests/global_approvers/github_codeowners_golden new file mode 100644 index 0000000..5b02d03 --- /dev/null +++ b/tests/global_approvers/github_codeowners_golden @@ -0,0 +1,6 @@ +# This file was generated by rules_codeowners / Bazel +# Don't edit it directly + +/tests/global_approvers/*.a @team-a @epic-team +/tests/global_approvers/*.b @team-b +/tests/global_approvers/*.c @team-c @epic-team diff --git a/tools/codeowners.bzl b/tools/codeowners.bzl index 88c854d..1c18486 100644 --- a/tools/codeowners.bzl +++ b/tools/codeowners.bzl @@ -17,7 +17,12 @@ def _codeowners_impl(ctx): ctx.actions.write( output = ctx.outputs.outfile, - content = "%s%s %s\n" % (path, ctx.attr.pattern, " ".join(teams)) + content = "%s%s %s" % (path, ctx.attr.pattern, " ".join(teams)) + ) + + ctx.actions.write( + output = ctx.outputs.include_global_owners, + content = "%d" % (1 if ctx.attr.include_global_owners else 0), ) codeowners = rule( @@ -26,9 +31,11 @@ codeowners = rule( "team": attr.string(mandatory = False, doc = "The GitHub team that should get ownership of the matching files. One of team and teams must be set."), "teams": attr.string_list(mandatory = False, doc = "A list of the GitHub teams that should get ownership of the matching files. One of team and teams must be set."), "pattern": attr.string(mandatory = False), + "include_global_owners": attr.bool(mandatory = False, default = True), }, outputs = { "outfile": "%{name}.out", + "include_global_owners": "%{name}.include_global_owners" }, ) @@ -42,6 +49,7 @@ def _generate_codeowners_impl(ctx): arguments = all_ownership_paths, env = { "OUTFILE": ctx.outputs.outfile.path, + "GLOBAL_APPROVERS": ctx.attr.global_owners, }, command = """ set -euo pipefail @@ -50,9 +58,19 @@ echo "# This file was generated by rules_codeowners / Bazel" >> "$OUTFILE" echo "# Don't edit it directly" >> "$OUTFILE" echo "" >> "$OUTFILE" -for file in "$@" -do - cat "$file" >> "$OUTFILE" +while [ "$#" -gt 0 ]; do + default_rule=$1 + include_global_owner=$2 + shift + shift + + cat "$default_rule" >> "$OUTFILE" + + if [ ! -z "$GLOBAL_APPROVERS" ] && [ $(cat "$include_global_owner") == "1" ]; then + echo -n " $GLOBAL_APPROVERS" >> "$OUTFILE" + fi + + echo >> "$OUTFILE" done """, ) @@ -60,7 +78,8 @@ done generate_codeowners = rule( implementation = _generate_codeowners_impl, attrs = { - "owners": attr.label_list(), + "owners": attr.label_list(mandatory = True), + "global_owners": attr.string(mandatory = False), }, outputs = { "outfile": "%{name}.out",