Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add posibility to define global_owners #14

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
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
44 changes: 44 additions & 0 deletions tests/global_approvers/BUILD
Original file line number Diff line number Diff line change
@@ -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",
],
)
6 changes: 6 additions & 0 deletions tests/global_approvers/github_codeowners_golden
Original file line number Diff line number Diff line change
@@ -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
29 changes: 24 additions & 5 deletions tools/codeowners.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"
},
)

Expand All @@ -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
Expand All @@ -50,17 +58,28 @@ 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
""",
)

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",
Expand Down