Skip to content

Add --json output with each file's owners and matching rule - #55

Closed
Mohith26 wants to merge 1 commit into
hmarr:mainfrom
Mohith26:json-output
Closed

Mohith26 wants to merge 1 commit into
hmarr:mainfrom
Mohith26:json-output

Conversation

@Mohith26

@Mohith26 Mohith26 commented Nov 9, 2026

Copy link
Copy Markdown

Closes #36. Adds -j/--json so the CLI can be used from scripts and CI without parsing the column-aligned text (the use case in #36, and the reason for the awk/YAML wrappers in #41).

$ codeowners --json *.md CODEOWNERS
[
  {"path":"DOCUMENTATION.md","owners":["@example/docs-writers"],"rule":{"pattern":"*.md","line":2}},
  {"path":"README.md","owners":["product-manager@example.com"],"rule":{"pattern":"README.md","line":3}},
  {"path":"CODEOWNERS","owners":[],"rule":null}
]
  • Each file reports its owners plus the rule that matched it (pattern, line). The rule makes it easy to answer "why does this file belong to this team?" and to spot rules that override earlier ones. A rule with no owners (an explicit unown) is still reported, and rule is null when nothing matched.
  • owners is always an array (empty for unowned files), so jq '.[].owners[]' never hits null.
  • --owner and --unowned filter the JSON exactly like the text output; no results prints [].
  • Output is streamed one object per line inside the array, so big monorepos don't need to fit in memory, and paths use forward slashes on Windows.
  • Text output is unchanged: the printing moved behind a small printer interface with a text and a JSON implementation, and a test pins the text format byte for byte.

Tests: new cmd/codeowners/main_test.go (7 tests: text output unchanged, JSON fields, null rule for unmatched files, --owner, --unowned, empty result, escaping). go vet ./... and go test ./... pass locally (Go 1.26, Apple Silicon); the README gets a short --json section with a jq example.

I used AI assistance while writing this and reviewed and tested it locally.

Adds -j/--json, which streams a JSON array with one object per file:
its path, owners (empty when unowned) and the rule that matched it
(pattern and line number, or null). --owner and --unowned filter it the
same way as the text output, which is unchanged.

Closes #36
@Mohith26 Mohith26 closed this by deleting the head repository Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request - automation output

1 participant