code_owners 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 24ab19a06481e09c20a3f95fea27072d53295a71fbc1bde5be7eac2e9eb35c23
4
- data.tar.gz: a944a07f298772b8cbe20526a8e32415601a29fba24d5f8943602cfbbbe68e6a
3
+ metadata.gz: b742a1fbaa41ae70ad7acc4f258ce685517c74d91855fe554ce29fbb02ca9d99
4
+ data.tar.gz: 9535aa63120da63e000b8fee5500d696e9d0ee097a12a9bfb052aab5f6b2fb00
5
5
  SHA512:
6
- metadata.gz: f1436b88d8d02715528137180e920d55f944a9d81fd8c6f5e4795da75d45efd221818fb79c2276e73399a81378bf33491bd2183ad570a4ab9bade020020b404d
7
- data.tar.gz: e4d28dd008b14ba0c42c04ae588f1753d90efe9fa3425b521175013f450b27f76c25d03420d18a4805a426f7fb17efb096e58ae5fb07e3a6c9b476f46d118aa4
6
+ metadata.gz: f2bed6c4f9a8991d6319b31b9d41e6fddc63bdb9cb584a6629106f90514944384f9d67a18134a0afd3c68ff13fb46c8c994af08ad8a1cfebec040d360343ca4f
7
+ data.tar.gz: bad817126846774d0e62706229351b2abd189bfeb570d16d994d2e01cdc7998bee78dab90979f41fcb3546824f744714a644c28fcc09fe0e8bfb78a2ba29d2b0
@@ -1,27 +1,27 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- code_owners (1.0.5)
4
+ code_owners (1.0.6)
5
5
  rake
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
9
9
  specs:
10
10
  diff-lcs (1.3)
11
- rake (12.3.2)
12
- rspec (3.7.0)
13
- rspec-core (~> 3.7.0)
14
- rspec-expectations (~> 3.7.0)
15
- rspec-mocks (~> 3.7.0)
16
- rspec-core (3.7.1)
17
- rspec-support (~> 3.7.0)
18
- rspec-expectations (3.7.0)
11
+ rake (12.3.3)
12
+ rspec (3.8.0)
13
+ rspec-core (~> 3.8.0)
14
+ rspec-expectations (~> 3.8.0)
15
+ rspec-mocks (~> 3.8.0)
16
+ rspec-core (3.8.2)
17
+ rspec-support (~> 3.8.0)
18
+ rspec-expectations (3.8.4)
19
19
  diff-lcs (>= 1.2.0, < 2.0)
20
- rspec-support (~> 3.7.0)
21
- rspec-mocks (3.7.0)
20
+ rspec-support (~> 3.8.0)
21
+ rspec-mocks (3.8.1)
22
22
  diff-lcs (>= 1.2.0, < 2.0)
23
- rspec-support (~> 3.7.0)
24
- rspec-support (3.7.1)
23
+ rspec-support (~> 3.8.0)
24
+ rspec-support (3.8.2)
25
25
 
26
26
  PLATFORMS
27
27
  ruby
@@ -13,14 +13,22 @@ module CodeOwners
13
13
  # -v -> verbose, outputs details about the matching pattern (if any) for each given pathname
14
14
  # -n -> non-matching, shows given paths which don't match any pattern
15
15
 
16
- def ownerships
17
- patterns, owners = pattern_owners.transpose
16
+ def log(message)
17
+ puts message
18
+ end
18
19
 
19
- git_owner_info(patterns).map do |line, pattern, file|
20
+ def ownerships
21
+ patterns = pattern_owners
22
+ git_owner_info(patterns.map { |p| p[0] }).map do |line, pattern, file|
20
23
  if line.empty?
21
- { file: file, owner: "UNOWNED" }
24
+ { file: file, owner: "UNOWNED", line: nil, pattern: nil }
22
25
  else
23
- { file: file, owner: owners[line.to_i - 1], line: line, pattern: pattern }
26
+ {
27
+ file: file,
28
+ owner: patterns.fetch(line.to_i-1)[1],
29
+ line: line,
30
+ pattern: pattern
31
+ }
24
32
  end
25
33
  end
26
34
  end
@@ -35,11 +43,23 @@ module CodeOwners
35
43
  end
36
44
 
37
45
  # read the github file and spit out a slightly formatted list of patterns and their owners
46
+ # Empty/invalid/commented lines are still included in order to preserve line numbering
38
47
  def pattern_owners
39
48
  codeowner_path = search_codeowners_file
40
- File.read(codeowner_path).split("\n").map do |line|
41
- line.gsub(/#.*/, '').gsub(/^$/, " @").split(/\s+@/, 2)
42
- end
49
+ patterns = []
50
+ File.read(codeowner_path).split("\n").each_with_index { |line, i|
51
+ path_owner = line.split(/\s+@/, 2)
52
+ if line.match(/^\s*(?:#.*)?$/)
53
+ patterns.push ['', ''] # Comment/empty line
54
+ elsif path_owner.length != 2 || (path_owner[0].empty? && !path_owner[1].empty?)
55
+ log "Parse error line #{(i+1).to_s}: \"#{line}\""
56
+ patterns.push ['', ''] # Invalid line
57
+ else
58
+ path_owner[1] = '@'+path_owner[1]
59
+ patterns.push path_owner
60
+ end
61
+ }
62
+ return patterns
43
63
  end
44
64
 
45
65
  def git_owner_info(patterns)
@@ -1,3 +1,3 @@
1
1
  module CodeOwners
2
- VERSION = "1.0.5"
2
+ VERSION = "1.0.6"
3
3
  end
@@ -1,38 +1,71 @@
1
1
  require 'code_owners'
2
+ require 'tmpdir'
2
3
 
3
- RSpec.describe CodeOwners do
4
+ RSpec.describe CodeOwners do |rspec|
4
5
  describe ".ownerships" do
5
6
  it "assigns owners to things" do
6
- expect(CodeOwners).to receive(:pattern_owners).and_return([["pat1", "own1"], ["pat2", "own2"], ["pat3", "own3"]])
7
+ expect(CodeOwners).to receive(:pattern_owners).and_return([["pat1", "own1"], ["pat2*", "own2"], ["pat3", "own3"]])
7
8
  expect(CodeOwners).to receive(:git_owner_info).and_return(
8
9
  [
9
- ["2", "whatever/pattern/thing", "this/is/a/file"],
10
+ ["2", "pat2*", "pat2file"],
10
11
  ["", "", "unowned/file"]
11
12
  ]
12
13
  )
13
14
  expect(CodeOwners.ownerships).to eq(
14
15
  [
15
- { file: "this/is/a/file", owner: "own2", line: "2", pattern: "whatever/pattern/thing" },
16
- { file: "unowned/file", owner: "UNOWNED"}
16
+ { file: "pat2file", owner: "own2", line: "2", pattern: "pat2*" },
17
+ { file: "unowned/file", owner: "UNOWNED", line: nil, pattern: nil }
17
18
  ]
18
19
  )
19
20
  end
20
21
  end
21
22
 
22
23
  describe ".pattern_owners" do
24
+ around(:each) do |example|
25
+ Dir.mktmpdir { |d|
26
+ @d = d
27
+ f = File.new(File.join(d, 'CODEOWNERS'), 'w+')
28
+ f.write <<-CODEOWNERS
29
+ lib/* @jcheatham
30
+ some/path/** @someoneelse
31
+ other/path/* @someoneelse @anotherperson
32
+ invalid/codeowners/line
33
+ @AnotherInvalidLine
34
+ #comment-line (empty line next)
35
+
36
+ # another comment line
37
+ CODEOWNERS
38
+ f.close
39
+ example.run
40
+ }
41
+ end
42
+
23
43
  it "returns a list of patterns and owners" do
24
- patterns, owners = CodeOwners.pattern_owners.transpose
25
- expect(owners).to include("jcheatham")
26
- expect(patterns).to include("lib/*")
44
+ expect(CodeOwners).to receive(:current_repo_path).and_return(@d)
45
+ expect(CodeOwners).to receive(:log).twice
46
+ pattern_owners = CodeOwners.pattern_owners
47
+ expect(pattern_owners).to include(["other/path/*", "@someoneelse @anotherperson"])
27
48
  end
28
49
 
29
50
  it "works when invoked in a repo's subdirectory" do
30
- Dir.chdir("spec") do
31
- patterns, owners = CodeOwners.pattern_owners.transpose
32
- expect(owners).to include("jcheatham")
33
- expect(patterns).to include("lib/*")
51
+ expect(CodeOwners).to receive(:current_repo_path).and_return(@d)
52
+ expect(CodeOwners).to receive(:log).twice
53
+ subdir = File.join(@d, 'spec')
54
+ Dir.mkdir(subdir)
55
+ Dir.chdir(subdir) do
56
+ pattern_owners = CodeOwners.pattern_owners
57
+ expect(pattern_owners).to include(["lib/*", "@jcheatham"])
34
58
  end
35
59
  end
60
+
61
+ it "prints validation errors and skips lines that aren't the expected format" do
62
+ expect(CodeOwners).to receive(:current_repo_path).and_return(@d)
63
+ expect(CodeOwners).to receive(:log).with("Parse error line 4: \"invalid/codeowners/line \"")
64
+ expect(CodeOwners).to receive(:log).with("Parse error line 5: \" @AnotherInvalidLine\"")
65
+ pattern_owners = CodeOwners.pattern_owners
66
+ expect(pattern_owners).not_to include(["", "@AnotherInvalidLine"])
67
+ expect(pattern_owners).to include(["", ""])
68
+ end
36
69
  end
37
70
 
38
71
  describe ".git_owner_info" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_owners
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan Cheatham