Skip to content

Commit

Permalink
Add examples of how to generate and validate generate_codeowners
Browse files Browse the repository at this point in the history
  • Loading branch information
zegl committed Oct 13, 2019
1 parent 826b742 commit b5ced4f
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,60 @@ sh_test(
],
)
```

## Automation tip: Automatically populate the `generate_codeowners`

To automatically set all `codeowners` as owners in a `generate_codeowners`, the following script that utilizes `bazel query` and [`buildozer`](https://github.com/bazelbuild/buildtools/tree/master/buildozer) can be used.

```bash
#!/usr/bin/env bash

# target to the generate_codeowners, eg "//.github:gen_codeowners"
readonly target=$1

readonly BAZEL=$(which bazel)
readonly BUILDOZER=$(which buildozer)

readonly new_owners=$(
# Query for all codeowners() rules (anchor at the front to avoid match on generate_codeowners rule)
$BAZEL query --output=label 'kind("^codeowners rule", //...)' |
# Print the length of each label at the front of the line
awk '{ print length, $0 }' |
# Sort shortest-first, so that the root //:OWNERS ends up first in CODEOWNERS
sort -n -s |
# 43 label -> "label"
awk '{ print "\x22" $2 "\x22" }' |
# comma-separated
tr '\n' ','
)

readonly command="set owners [${new_owners}]|${target}"

echo "$command" | $BUILDOZER -f -
```

## Automation tip: Verify that all codeowners are transiently depended on a `generate_codeowners`

To verify that all `codeowners` are targeted by a `generate_codeowners`, a script like the one below can be used.

```bash
#!/usr/bin/env bash

# target to the generate_codeowners, eg "//.github:gen_codeowners"
readonly target=$1

readonly TEEFILE=$(mktemp)
readonly BAZEL=$(which bazel)

$BAZEL query "kind(codeowners, //...) except deps(${target})" 2>&1 | tee "$TEEFILE" | grep "Empty results"
EXIT=$?

if [ "$EXIT" -eq 1 ]; then
cat "$TEEFILE"
echo "--> All codeowners rules must be depended on by ${target}"
echo "--> Add the codeowners as a dependency, or delete the target"
exit 1
fi

exit 0
```

0 comments on commit b5ced4f

Please sign in to comment.