-
Notifications
You must be signed in to change notification settings - Fork 0
ARCH-4643 Add org-wide CODEOWNERS enforcement workflow #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| _extends: .github-private |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| name: Test scripts | ||
|
|
||
| on: | ||
| push: | ||
| paths: ["scripts/**"] | ||
| pull_request: | ||
| paths: ["scripts/**"] | ||
|
|
||
| jobs: | ||
| bats: | ||
| name: BATS | ||
| runs-on: | ||
| - runs-on=${{ github.run_id }} | ||
| - runner=tn | ||
| - env=production-eu | ||
| - tag=bats | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
|
|
||
| - name: Install bats-core | ||
| run: sudo apt-get update && sudo apt-get install -y bats | ||
|
|
||
| - name: Run tests | ||
| run: bats scripts/check-codeowners.bats |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .idea/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,31 @@ | ||
| # .github | ||
| A repo for org wide health files (e.g. PR template). | ||
| A repo for organization-wide configuration. | ||
|
|
||
| ## PULL_REQUEST_TEMPLATE.md | ||
|
|
||
| Global PR template. This must be followed for auditing purposes. | ||
| Teams may only set specific PR templates that deviate from this template with proper approval. | ||
|
|
||
| ## org-workflows/ | ||
|
|
||
| Workflows applied across the entire organization via **Required Workflows** | ||
| (Org Settings → Actions → Required workflows). Each workflow must be registered | ||
| there by an org admin, pointing at `collibra/.github/<path>@main`. | ||
|
|
||
| | File | Purpose | | ||
| |----------------------------------------------|-------------------------------------------------------------------| | ||
| | `org-workflows/enforce-codeowners-teams.yml` | Rejects CODEOWNERS entries that name individuals instead of teams | | ||
|
|
||
| ## .github/workflows/ | ||
|
|
||
| Regular workflows that run only within this repository (e.g. CI for the scripts | ||
| in `scripts/`). | ||
|
|
||
| ## scripts/ | ||
|
|
||
| Shell scripts used by the workflows in `org-workflows/`, with BATS tests alongside them. | ||
| Run tests locally with: | ||
|
|
||
| ```sh | ||
| bats scripts/check-codeowners.bats | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # This workflow is configured as a Required Workflow at the organization level | ||
| # (Org Settings → Actions → Required workflows) so that it runs on every pull | ||
| # request across all repositories. Do not remove the pull_request trigger or | ||
| # narrow its scope — doing so would break org-wide enforcement. | ||
| name: Enforce team-only CODEOWNERS | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: ["**"] | ||
|
|
||
| jobs: | ||
| check-codeowners: | ||
| name: CODEOWNERS — teams only | ||
| runs-on: | ||
| - runs-on=${{ github.run_id }} | ||
| - runner=tn | ||
| - env=production-eu | ||
| - tag=check-codeowners | ||
| steps: | ||
| - uses: actions/checkout@v6 | ||
|
|
||
| - uses: actions/checkout@v6 | ||
| with: | ||
| repository: ${{ github.repository_owner }}/.github | ||
| path: .github-shared | ||
|
|
||
| - run: bash .github-shared/scripts/check-codeowners.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| #!/usr/bin/env bats | ||
|
|
||
| setup() { | ||
| # shellcheck source=check-codeowners.sh | ||
| source "$BATS_TEST_DIRNAME/check-codeowners.sh" | ||
| FIXTURE_DIR="$(mktemp -d)" | ||
| } | ||
|
|
||
| teardown() { | ||
| rm -rf "$FIXTURE_DIR" | ||
| } | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # extract_individuals | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| @test "extract_individuals: empty output for team-only entries" { | ||
| printf "* @org/backend-team\n/src @org/frontend-team\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ "$status" -eq 0 ] | ||
| [ -z "$output" ] | ||
| } | ||
|
|
||
| @test "extract_individuals: detects a single individual" { | ||
| printf "* @johndoe\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ "$output" = "@johndoe" ] | ||
| } | ||
|
|
||
| @test "extract_individuals: detects individual mixed with teams on the same line" { | ||
| printf "* @org/team @johndoe\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ "$output" = "@johndoe" ] | ||
| } | ||
|
|
||
| @test "extract_individuals: detects multiple individuals across lines" { | ||
| printf "* @alice\n/docs @bob\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ "$output" = "@alice"$'\n'"@bob" ] | ||
| } | ||
|
|
||
| @test "extract_individuals: ignores comment lines" { | ||
| printf "# @individual-in-comment\n* @org/team\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ -z "$output" ] | ||
| } | ||
|
|
||
| @test "extract_individuals: ignores email addresses" { | ||
| printf "* user@example.com\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ -z "$output" ] | ||
| } | ||
|
|
||
| @test "extract_individuals: empty output for empty file" { | ||
| touch "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ -z "$output" ] | ||
| } | ||
|
|
||
| @test "extract_individuals: ignores pattern-only lines with no owner (unset ownership)" { | ||
| printf "*.lockfile\n/some/path\n* @org/team\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run extract_individuals "$FIXTURE_DIR/CODEOWNERS" | ||
| [ -z "$output" ] | ||
| } | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # find_codeowners | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| @test "find_codeowners: finds CODEOWNERS at root" { | ||
| touch "$FIXTURE_DIR/CODEOWNERS" | ||
| run find_codeowners "$FIXTURE_DIR" | ||
| [ "$status" -eq 0 ] | ||
| [ "$output" = "$FIXTURE_DIR/CODEOWNERS" ] | ||
| } | ||
|
|
||
| @test "find_codeowners: finds CODEOWNERS under .github/" { | ||
| mkdir -p "$FIXTURE_DIR/.github" | ||
| touch "$FIXTURE_DIR/.github/CODEOWNERS" | ||
| run find_codeowners "$FIXTURE_DIR" | ||
| [ "$status" -eq 0 ] | ||
| [ "$output" = "$FIXTURE_DIR/.github/CODEOWNERS" ] | ||
| } | ||
|
|
||
| @test "find_codeowners: finds CODEOWNERS under docs/" { | ||
| mkdir -p "$FIXTURE_DIR/docs" | ||
| touch "$FIXTURE_DIR/docs/CODEOWNERS" | ||
| run find_codeowners "$FIXTURE_DIR" | ||
| [ "$status" -eq 0 ] | ||
| [ "$output" = "$FIXTURE_DIR/docs/CODEOWNERS" ] | ||
| } | ||
|
|
||
| @test "find_codeowners: root takes priority over .github/" { | ||
| mkdir -p "$FIXTURE_DIR/.github" | ||
| touch "$FIXTURE_DIR/CODEOWNERS" "$FIXTURE_DIR/.github/CODEOWNERS" | ||
| run find_codeowners "$FIXTURE_DIR" | ||
| [ "$output" = "$FIXTURE_DIR/CODEOWNERS" ] | ||
| } | ||
|
|
||
| @test "find_codeowners: returns non-zero when no CODEOWNERS exists" { | ||
| run find_codeowners "$FIXTURE_DIR" | ||
| [ "$status" -ne 0 ] | ||
| } | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # main (integration) | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| @test "main: passes for team-only CODEOWNERS" { | ||
| printf "* @org/team\n/src @org/other-team\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run main "$FIXTURE_DIR" | ||
| [ "$status" -eq 0 ] | ||
| } | ||
|
|
||
| @test "main: fails for individual user in CODEOWNERS" { | ||
| printf "* @username\n" > "$FIXTURE_DIR/CODEOWNERS" | ||
| run main "$FIXTURE_DIR" | ||
| [ "$status" -eq 1 ] | ||
| } | ||
|
|
||
| @test "main: passes when no CODEOWNERS file exists" { | ||
| run main "$FIXTURE_DIR" | ||
| [ "$status" -eq 0 ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| # Locate a CODEOWNERS file under ROOT (defaults to cwd). | ||
| # GitHub resolves CODEOWNERS from three locations in priority order. | ||
| find_codeowners() { | ||
| local root="${1:-.}" | ||
| for path in CODEOWNERS .github/CODEOWNERS docs/CODEOWNERS; do | ||
| if [[ -f "$root/$path" ]]; then | ||
| echo "$root/$path" | ||
| return 0 | ||
| fi | ||
| done | ||
| return 1 | ||
| } | ||
|
|
||
| # Print individual-user @mentions from FILE (one per line). | ||
| # Team references (@org/team) and email addresses (user@host) are excluded. | ||
| # Splits on whitespace first so that embedded @ (e.g. user@host) is never matched. | ||
| extract_individuals() { | ||
| local file="$1" | ||
| grep -v '^\s*#' "$file" \ | ||
| | tr ' \t' '\n' \ | ||
| | grep -E '^@[A-Za-z0-9_.-]+(/[A-Za-z0-9_.-]+)?$' \ | ||
| | grep -v '/' \ | ||
| || true | ||
| } | ||
|
|
||
| main() { | ||
| local root="${1:-.}" | ||
| local codeowners_file | ||
|
|
||
| if ! codeowners_file=$(find_codeowners "$root"); then | ||
| echo "No CODEOWNERS file found — nothing to check." | ||
| exit 0 | ||
| fi | ||
|
|
||
| echo "Checking $codeowners_file ..." | ||
|
|
||
| local individuals | ||
| individuals=$(extract_individuals "$codeowners_file") | ||
|
|
||
| if [[ -n "$individuals" ]]; then | ||
| echo "::error file=$codeowners_file::Individual users are not permitted in CODEOWNERS — use @org/team references instead." | ||
| echo "" | ||
| echo "Offending entries:" | ||
| echo "$individuals" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "All CODEOWNERS entries are team references." | ||
| } | ||
|
|
||
| if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | ||
| set -euo pipefail | ||
| main "$@" | ||
| fi |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.