Class: CodeInventory::GitHub::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/codeinventory/github/source.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auth, org, overrides: {}, exclude: []) ⇒ Source

Returns a new instance of Source.



10
11
12
13
14
15
16
# File 'lib/codeinventory/github/source.rb', line 10

def initialize(auth, org, overrides: {}, exclude: [])
  Octokit.auto_paginate = true
  @auth = auth
  @org = org
  @overrides = overrides
  @exclude = exclude
end

Instance Attribute Details

#excludeObject

Returns the value of attribute exclude.



8
9
10
# File 'lib/codeinventory/github/source.rb', line 8

def exclude
  @exclude
end

#orgObject

Returns the value of attribute org.



8
9
10
# File 'lib/codeinventory/github/source.rb', line 8

def org
  @org
end

#overridesObject

Returns the value of attribute overrides.



8
9
10
# File 'lib/codeinventory/github/source.rb', line 8

def overrides
  @overrides
end

Instance Method Details

#clientObject



190
191
192
# File 'lib/codeinventory/github/source.rb', line 190

def client
  @client ||= Octokit::Client.new(@auth)
end

#contact_email(repo, inventory_file_metadata) ⇒ Object

Provides a value for the contact.email field. Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. GitHub organization email



161
162
163
164
165
166
# File 'lib/codeinventory/github/source.rb', line 161

def contact_email(repo, )
  return @overrides[:contact][:email] if @overrides.dig(:contact, :email)
  return ["contact"]["email"] if .dig("contact", "email")
  org = client.organization(@org)
  org[:email]
end

#description(repo, inventory_file_metadata) ⇒ Object

Provides a value for the description field. Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. GitHub repository description

  4. GitHub repository name



98
99
100
101
102
103
# File 'lib/codeinventory/github/source.rb', line 98

def description(repo, )
  return @overrides[:description] if @overrides[:description]
  return ["description"] if ["description"]
  return repo[:description] if repo[:description]
  repo[:name]
end

#government_wide_reuse_project(repo, inventory_file_metadata) ⇒ Object

Provides a value for the governmentWideReuseProject field. Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. 1 (assume government-wide reuse)



139
140
141
142
143
# File 'lib/codeinventory/github/source.rb', line 139

def government_wide_reuse_project(repo, )
  return @overrides[:governmentWideReuseProject] if @overrides[:governmentWideReuseProject]
  return ["governmentWideReuseProject"] if ["governmentWideReuseProject"]
  1
end

#inventory_file(repo) ⇒ Object

Checks if the repo has an inventory file. If so, loads its metadata.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/codeinventory/github/source.rb', line 63

def inventory_file(repo)
   = {}
  return  if repo[:size] == 0 # Empty repo
  filenames = [ ".codeinventory.yml", "codeinventory.yml", ".codeinventory.json", "codeinventory.json"]
  repo_contents = client.contents(repo[:full_name], path: "/")
  inventory_file = repo_contents.select { |file| filenames.include? file[:name] }.first
  unless inventory_file.nil?
    file_content = client.contents(repo[:full_name], path: inventory_file[:path])
    raw_content = Base64.decode64(file_content[:content])
    # Remove UTF-8 BOM if there is one; it throws off JSON.parse
    raw_content.sub!("\xEF\xBB\xBF".force_encoding("ASCII-8BIT"), "")
    if inventory_file[:name].end_with? ".yml"
       = YAML.load(raw_content).to_hash
    elsif inventory_file[:name].end_with? ".json"
       = JSON.parse(raw_content)
    end
  end
  
end

#license(repo, inventory_file_metadata) ⇒ Object

Provides a value for the license field. Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. LICENSE.md or LICENSE file in the repository

  4. nil



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/codeinventory/github/source.rb', line 111

def license(repo, )
  return @overrides[:license] if @overrides[:license]
  return ["license"] if ["license"]
  # Need to set header to quiet warning about using a GitHub preview feature
  headers = { accept: "application/vnd.github.drax-preview+json" }
  begin
    license_meta = client.repository_license_contents(repo[:full_name], headers)
    license = license_meta[:html_url]
  rescue Octokit::NotFound ; end
  license
end

#name(repo, inventory_file_metadata) ⇒ Object

Provides a value for the name field. Order of precedence:

  1. CodeInventory metadata file

  2. GitHub repository name



87
88
89
90
# File 'lib/codeinventory/github/source.rb', line 87

def name(repo, )
  return ["name"] if ["name"]
  repo[:name]
end

#open_source_project(repo, inventory_file_metadata) ⇒ Object

Provides a value for the openSourceProject field. Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. GitHub repository public/private status (public=1; private=0)



128
129
130
131
132
# File 'lib/codeinventory/github/source.rb', line 128

def open_source_project(repo, )
  return @overrides[:openSourceProject] if @overrides[:openSourceProject]
  return ["openSourceProject"] if ["openSourceProject"]
  repo[:private] ? 0 : 1
end

#organization(repo, inventory_file_metadata) ⇒ Object

Provies a value for the organization field (optional). Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. No organization field



184
185
186
187
188
# File 'lib/codeinventory/github/source.rb', line 184

def organization(repo, )
  return @overrides[:organization] if @overrides[:organization]
  return ["organization"] if ["organization"]
  nil
end

#project(repo_name) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/codeinventory/github/source.rb', line 18

def project(repo_name)
  repo = client.repository(repo_name)
   = inventory_file(repo)
  unless .dig("codeinventory", "exclude")
     = {}
    ["name"] = name(repo, )
    ["description"] = description(repo, )
    ["license"] = license(repo, )
    ["openSourceProject"] = open_source_project(repo, )
    ["governmentWideReuseProject"] = government_wide_reuse_project(repo, )
    ["tags"] = tags(repo, )
    ["contact"] = { "email" => contact_email(repo, ) }
    ["repository"] = repository(repo, )
    organization = organization(repo, )
    ["organization"] = organization(repo, ) unless organization.nil?
    
  end
end

#projectsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/codeinventory/github/source.rb', line 37

def projects
  repos = client.organization_repositories(@org)
  repos.delete_if { |repo| exclude.include? repo[:name] }
  projects = []
  repos.each do |repo|
     = inventory_file(repo)
    unless .dig("codeinventory", "exclude")
       = {}
      ["name"] = name(repo, )
      ["description"] = description(repo, )
      ["license"] = license(repo, )
      ["openSourceProject"] = open_source_project(repo, )
      ["governmentWideReuseProject"] = government_wide_reuse_project(repo, )
      ["tags"] = tags(repo, )
      ["contact"] = { "email" => contact_email(repo, ) }
      ["repository"] = repository(repo, )
      organization = organization(repo, )
      ["organization"] = organization(repo, ) unless organization.nil?
      projects << 
      yield  if block_given?
    end
  end
  projects
end

#repository(repo, inventory_file_metadata) ⇒ Object

Provies a value for the repository field. Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. If repo is public, GitHub repository URL, otherwise nil



173
174
175
176
177
# File 'lib/codeinventory/github/source.rb', line 173

def repository(repo, )
  return @overrides[:repository] if @overrides[:repository]
  return ["repository"] if ["repository"]
  repo[:private] ? nil : repo[:html_url]
end

#tags(repo, inventory_file_metadata) ⇒ Object

Provides a value for the tags field. Order of precedence:

  1. List of overrides

  2. CodeInventory metadata file

  3. A single tag consisting of the org name.



150
151
152
153
154
# File 'lib/codeinventory/github/source.rb', line 150

def tags(repo, )
  return @overrides[:tags] if @overrides[:tags]
  return ["tags"] if ["tags"]
  [repo[:owner][:login]]
end