Class: CodeInventory::GitHub::Source
- Inherits:
-
Object
- Object
- CodeInventory::GitHub::Source
- Defined in:
- lib/codeinventory/github/source.rb
Instance Attribute Summary collapse
-
#exclude ⇒ Object
Returns the value of attribute exclude.
-
#org ⇒ Object
Returns the value of attribute org.
-
#overrides ⇒ Object
Returns the value of attribute overrides.
Instance Method Summary collapse
- #build_metadata(repo, inventory_file_metadata) ⇒ Object
- #client ⇒ Object
-
#contact_email(repo, inventory_file_metadata) ⇒ Object
Provides a value for the contact.email field.
-
#description(repo, inventory_file_metadata) ⇒ Object
Provides a value for the description field.
-
#exemption_text(repo, inventory_file_metadata) ⇒ Object
Provides a value for the permissions.exemptionText field.
-
#initialize(auth, org, overrides: {}, exclude: []) ⇒ Source
constructor
A new instance of Source.
-
#inventory_file(repo) ⇒ Object
Checks if the repo has an inventory file.
-
#labor_hours(repo, inventory_file_metadata) ⇒ Object
Provies a value for the laborHours field.
-
#licenses(repo, inventory_file_metadata) ⇒ Object
Provides a value for the permissions.licenses field.
-
#name(repo, inventory_file_metadata) ⇒ Object
Provides a value for the name field.
-
#organization(repo, inventory_file_metadata) ⇒ Object
Provies a value for the organization field (optional).
- #project(repo_name) ⇒ Object
- #projects ⇒ Object
-
#repository(repo, inventory_file_metadata) ⇒ Object
Provies a value for the repositoryURL field.
-
#tags(repo, inventory_file_metadata) ⇒ Object
Provides a value for the tags field.
-
#usage_type(repo, inventory_file_metadata) ⇒ Object
Provides a value for the permissions.usageType field.
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
#exclude ⇒ Object
Returns the value of attribute exclude.
8 9 10 |
# File 'lib/codeinventory/github/source.rb', line 8 def exclude @exclude end |
#org ⇒ Object
Returns the value of attribute org.
8 9 10 |
# File 'lib/codeinventory/github/source.rb', line 8 def org @org end |
#overrides ⇒ Object
Returns the value of attribute overrides.
8 9 10 |
# File 'lib/codeinventory/github/source.rb', line 8 def overrides @overrides end |
Instance Method Details
#build_metadata(repo, inventory_file_metadata) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/codeinventory/github/source.rb', line 51 def (repo, ) = {} ["name"] = name(repo, ) ["description"] = description(repo, ) usage_type = usage_type(repo, ) ["permissions"] = { "licenses" => licenses(repo, ), "usageType" => usage_type, "exemptionText" => exemption_text(repo, ) } ["tags"] = (repo, ) ["contact"] = { "email" => contact_email(repo, ) } ["repositoryURL"] = repository(repo, ) ["laborHours"] = labor_hours(repo, ) organization = organization(repo, ) ["organization"] = organization unless organization.nil? end |
#client ⇒ Object
210 211 212 |
# File 'lib/codeinventory/github/source.rb', line 210 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:
-
List of overrides
-
CodeInventory metadata file
-
GitHub organization email
170 171 172 173 174 175 |
# File 'lib/codeinventory/github/source.rb', line 170 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:
-
List of overrides
-
CodeInventory metadata file
-
GitHub repository description
-
GitHub repository name
106 107 108 109 110 111 |
# File 'lib/codeinventory/github/source.rb', line 106 def description(repo, ) return @overrides[:description] if @overrides[:description] return ["description"] if ["description"] return repo[:description] if repo[:description] repo[:name] end |
#exemption_text(repo, inventory_file_metadata) ⇒ Object
Provides a value for the permissions.exemptionText field. Order of precedence:
-
List of overrides
-
CodeInventory metadata file
-
nil
146 147 148 149 150 |
# File 'lib/codeinventory/github/source.rb', line 146 def exemption_text(repo, ) return @overrides[:permissions][:exemptionText] if @overrides.dig(:permissions, :exemptionText) return ["permissions"]["exemptionText"] if .dig("permissions", "exemptionText") nil end |
#inventory_file(repo) ⇒ Object
Checks if the repo has an inventory file. If so, loads its metadata.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/codeinventory/github/source.rb', line 71 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 |
#labor_hours(repo, inventory_file_metadata) ⇒ Object
Provies a value for the laborHours field. Order of precedence:
-
List of overrides
-
CodeInventory metadata file
-
0
193 194 195 196 197 |
# File 'lib/codeinventory/github/source.rb', line 193 def labor_hours(repo, ) return @overrides[:laborHours] if @overrides[:laborHours] return ["laborHours"] if ["laborHours"] 0 end |
#licenses(repo, inventory_file_metadata) ⇒ Object
Provides a value for the permissions.licenses field. Order of precedence:
-
List of overrides
-
CodeInventory metadata file
-
GitHub repository license
-
nil
119 120 121 122 123 124 125 126 127 |
# File 'lib/codeinventory/github/source.rb', line 119 def licenses(repo, ) return @overrides[:permissions][:licenses] if @overrides.dig(:permissions, :licenses) return ["permissions"]["licenses"] if .dig("permissions", "licenses") require 'pp' if repo[:license] && repo[:license][:url] && repo[:license][:spdx_id] return [ { "URL": repo[:license][:url], "name": repo[:license][:spdx_id] } ] end nil end |
#name(repo, inventory_file_metadata) ⇒ Object
Provides a value for the name field. Order of precedence:
-
CodeInventory metadata file
-
GitHub repository name
95 96 97 98 |
# File 'lib/codeinventory/github/source.rb', line 95 def name(repo, ) return ["name"] if ["name"] repo[:name] end |
#organization(repo, inventory_file_metadata) ⇒ Object
Provies a value for the organization field (optional). Order of precedence:
-
List of overrides
-
CodeInventory metadata file
-
No organization field
204 205 206 207 208 |
# File 'lib/codeinventory/github/source.rb', line 204 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 |
# File 'lib/codeinventory/github/source.rb', line 18 def project(repo_name) # mercy = GitHub topics preview # drax = GitHub license preview headers = { accept: [ "application/vnd.github.mercy-preview+json", "application/vnd.github.drax-preview+json" ] } repo = client.repository(repo_name, headers) = inventory_file(repo) unless .dig("codeinventory", "exclude") (repo, ) end end |
#projects ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/codeinventory/github/source.rb', line 31 def projects # mercy = GitHub topics preview # drax = GitHub license preview headers = { accept: [ "application/vnd.github.mercy-preview+json", "application/vnd.github.drax-preview+json" ] } 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") = (repo, ) projects << yield if block_given? end end projects end |
#repository(repo, inventory_file_metadata) ⇒ Object
Provies a value for the repositoryURL field. Order of precedence:
-
List of overrides
-
CodeInventory metadata file
-
If repo is public, GitHub repository URL, otherwise nil
182 183 184 185 186 |
# File 'lib/codeinventory/github/source.rb', line 182 def repository(repo, ) return @overrides[:repositoryURL] if @overrides[:repositoryURL] return ["repositoryURL"] if ["repositoryURL"] repo[:private] ? nil : repo[:html_url] end |
#tags(repo, inventory_file_metadata) ⇒ Object
Provides a value for the tags field. Order of precedence:
-
List of overrides
-
CodeInventory metadata file
-
GitHub topics
-
A single tag consisting of the org name.
158 159 160 161 162 163 |
# File 'lib/codeinventory/github/source.rb', line 158 def (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 |
#usage_type(repo, inventory_file_metadata) ⇒ Object
Provides a value for the permissions.usageType field. Order of precedence:
-
List of overrides
-
CodeInventory metadata file
-
GitHub repository public/private status (public=openSource; private=governmentWideReuse) Note: exempt* values must be set either in overrides or the metadata file.
135 136 137 138 139 |
# File 'lib/codeinventory/github/source.rb', line 135 def usage_type(repo, ) return @overrides[:permissions][:usageType] if @overrides.dig(:permissions, :usageType) return ["permissions"]["usageType"] if .dig("permissions", "usageType") repo[:private] ? "governmentWideReuse" : "openSource" end |