cleo_codeowners 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/cleo_codeowners/version.rb +1 -1
- data/lib/codeowners/generator.rb +32 -22
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 95dabb73fd9d44328c2c7baed5ffbf4f91996ef2bad026d2f79175b98ba252f9
|
|
4
|
+
data.tar.gz: 4a65dca6d6efef99d01587612451b72293283a30aa0a99f3ada7f192b05added
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 55b1e75f9bfbf6affb08442adb631c5fb96ab8a7ac3a1a31a433fc44235f5875964a98d4d77038965cd7c20db7cf022c5a8989db3c7a1acdfe80f32a864c792a
|
|
7
|
+
data.tar.gz: c69046a48d695beb1129a005fff5b87163b0cd4935fbb625017a123b1bc2f1ed291ee02d833cf6a2ee4ef0448d14a388a2f5324547cb7fb5fe79bfbe680b3f98
|
data/lib/codeowners/generator.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
# typed: false
|
|
2
2
|
# frozen_string_literal: true
|
|
3
3
|
|
|
4
|
+
require 'pathname'
|
|
5
|
+
|
|
4
6
|
module Codeowners
|
|
5
7
|
class Generator
|
|
6
8
|
DEFAULT_OUTPUT_PATH = '.github/CODEOWNERS'
|
|
@@ -105,32 +107,40 @@ module Codeowners
|
|
|
105
107
|
calculated_owners.push({ path:, owner: })
|
|
106
108
|
end
|
|
107
109
|
|
|
110
|
+
# GitHub applies the *last* matching CODEOWNERS rule, so rules are listed
|
|
111
|
+
# least-specific first: by number of path segments (`/app/` is broader than
|
|
112
|
+
# `/app/models/user.rb`), then by path so a wildcard such as `/foo/*` sorts
|
|
113
|
+
# ahead of the `/foo/bar/` it would otherwise shadow.
|
|
108
114
|
def output_in_machine_format
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
115
|
+
merge_owners_by_path(calculated_owners)
|
|
116
|
+
.sort_by { |path, _owners| [path_depth(path), path] }
|
|
117
|
+
.each { |path, rule_owners| output_file << "#{path} #{rule_owners.join(' ')}\n" }
|
|
112
118
|
end
|
|
113
119
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
end
|
|
120
|
+
# Combines the owners of rules that share a path onto one line.
|
|
121
|
+
def merge_owners_by_path(rules)
|
|
122
|
+
rules_by_path = rules.group_by { |owner| sanitised_path(owner[:path]) }
|
|
123
|
+
reject_trailing_slash_conflicts(rules_by_path.keys)
|
|
124
|
+
rules_by_path.map do |path, owners_for_path|
|
|
125
|
+
[path, owners_for_path.map { |owner| owner[:owner] }.uniq.sort]
|
|
121
126
|
end
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# A path written both with and without a trailing slash is rejected rather
|
|
130
|
+
# than guessed at: `/foo` and `/foo/` are effectively same rule, but on disk
|
|
131
|
+
# they differ (a directory needs its slash to match recursively, a file must
|
|
132
|
+
# not have one), so the definitions must pick one.
|
|
133
|
+
def reject_trailing_slash_conflicts(paths)
|
|
134
|
+
paths.group_by { |path| path.chomp('/') }.each_value do |variants|
|
|
135
|
+
next if variants.uniq.size == 1
|
|
136
|
+
|
|
137
|
+
raise "#{variants.min} is declared both with and without a trailing slash " \
|
|
138
|
+
"(#{variants.uniq.sort.join(', ')}); pick one so it is clear whether it is a file or a directory."
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def path_depth(path)
|
|
143
|
+
Pathname.new(path).each_filename.count
|
|
134
144
|
end
|
|
135
145
|
|
|
136
146
|
def blurb
|