Class: Codeowners::Checker::OwnersList

Inherits:
Object
  • Object
show all
Defined in:
lib/codeowners/checker/owners_list.rb

Overview

Manage OWNERS file reading, re-writing and fetching

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, _config = nil) ⇒ OwnersList

Returns a new instance of OwnersList.



13
14
15
16
17
18
# File 'lib/codeowners/checker/owners_list.rb', line 13

def initialize(repo, _config = nil)
  @validate_owners = true
  # doing gsub here ensures the files are always in the same directory
  @filename = CodeOwners.filename(repo).gsub('CODEOWNERS', 'OWNERS')
  @config ||= Codeowners::Config.new
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



10
11
12
# File 'lib/codeowners/checker/owners_list.rb', line 10

def filename
  @filename
end

#ownersObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/codeowners/checker/owners_list.rb', line 36

def owners
  return [] unless @validate_owners

  @owners ||=
    if github_credentials_exist?
      Codeowners::GithubFetcher.get_owners(@config.default_organization, ENV['GITHUB_TOKEN'])
    else
      FileAsArray.new(@filename).content
    end
end

#validate_ownersObject

Returns the value of attribute validate_owners.



10
11
12
# File 'lib/codeowners/checker/owners_list.rb', line 10

def validate_owners
  @validate_owners
end

Class Method Details

.persist!(repo, owners, config = nil) ⇒ Object



20
21
22
23
24
# File 'lib/codeowners/checker/owners_list.rb', line 20

def self.persist!(repo, owners, config = nil)
  owner_list = new(repo, config)
  owner_list.owners = owners
  owner_list.persist!
end

Instance Method Details

#<<(owner) ⇒ Object



64
65
66
67
68
# File 'lib/codeowners/checker/owners_list.rb', line 64

def <<(owner)
  return if @owners.include?(owner)

  @owners << owner
end

#github_credentials_exist?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
# File 'lib/codeowners/checker/owners_list.rb', line 47

def github_credentials_exist?
  token = ENV['GITHUB_TOKEN']
  organization = @config.default_organization
  token && !organization.empty?
end

#invalid_owners(codeowners) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/codeowners/checker/owners_list.rb', line 53

def invalid_owners(codeowners)
  return [] unless @validate_owners

  codeowners.each_with_object([]) do |line, acc|
    next unless line.pattern?

    missing = line.owners - owners
    acc.push([line, missing]) if missing.any?
  end
end

#persist!Object



26
27
28
29
30
# File 'lib/codeowners/checker/owners_list.rb', line 26

def persist!
  owners_file = FileAsArray.new(@filename)
  owners_file.content = @owners
  owners_file.persist!
end

#valid_owner?(owner) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/codeowners/checker/owners_list.rb', line 32

def valid_owner?(owner)
  !@validate_owners || owners.include?(owner)
end