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



194
195
196
# File 'lib/codeinventory/github/source.rb', line 194

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



165
166
167
168
169
170
# File 'lib/codeinventory/github/source.rb', line 165

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



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

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)



141
142
143
144
145
# File 'lib/codeinventory/github/source.rb', line 141

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.



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

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



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

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



89
90
91
92
# File 'lib/codeinventory/github/source.rb', line 89

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)



130
131
132
133
134
# File 'lib/codeinventory/github/source.rb', line 130

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



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

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
36
# File 'lib/codeinventory/github/source.rb', line 18

def project(repo_name)
  headers = { accept: "application/vnd.github.mercy-preview+json" } # for GitHub topics preview
  repo = client.repository(repo_name, headers)
   = 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



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

def projects
  headers = { accept: "application/vnd.github.mercy-preview+json" } # for GitHub topics preview
  repos = client.organization_repositories(@org, headers)
  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



177
178
179
180
181
# File 'lib/codeinventory/github/source.rb', line 177

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. GitHub topics

  4. A single tag consisting of the org name.



153
154
155
156
157
158
# File 'lib/codeinventory/github/source.rb', line 153

def tags(repo, )
  return @overrides[:tags] if @overrides[:tags]
  return ["tags"] if ["tags"]
  return repo[:topics] unless (repo[:topics].nil? || repo[:topics].empty?)
  [repo[:owner][:login]]
end