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
- #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.
-
#government_wide_reuse_project(repo, inventory_file_metadata) ⇒ Object
Provides a value for the governmentWideReuseProject 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.
-
#license(repo, inventory_file_metadata) ⇒ Object
Provides a value for the license field.
-
#name(repo, inventory_file_metadata) ⇒ Object
Provides a value for the name field.
-
#open_source_project(repo, inventory_file_metadata) ⇒ Object
Provides a value for the openSourceProject 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 repository field.
-
#tags(repo, inventory_file_metadata) ⇒ Object
Provides a value for the tags 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
#client ⇒ Object
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:
-
List of overrides
-
CodeInventory metadata file
-
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:
-
List of overrides
-
CodeInventory metadata file
-
GitHub repository description
-
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:
-
List of overrides
-
CodeInventory metadata file
-
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:
-
List of overrides
-
CodeInventory metadata file
-
LICENSE.md or LICENSE file in the repository
-
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 = client.repository_license_contents(repo[:full_name], headers) license = [:html_url] rescue Octokit::NotFound ; end license end |
#name(repo, inventory_file_metadata) ⇒ Object
Provides a value for the name field. Order of precedence:
-
CodeInventory metadata file
-
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:
-
List of overrides
-
CodeInventory metadata file
-
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:
-
List of overrides
-
CodeInventory metadata file
-
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"] = (repo, ) ["contact"] = { "email" => contact_email(repo, ) } ["repository"] = repository(repo, ) organization = organization(repo, ) ["organization"] = organization(repo, ) unless organization.nil? end end |
#projects ⇒ Object
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"] = (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:
-
List of overrides
-
CodeInventory metadata file
-
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:
-
List of overrides
-
CodeInventory metadata file
-
GitHub topics
-
A single tag consisting of the org name.
153 154 155 156 157 158 |
# File 'lib/codeinventory/github/source.rb', line 153 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 |