Class: CodeInventory::GitHub

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

Constant Summary collapse

VERSION =
"0.1.9"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



10
11
12
13
14
15
16
# File 'lib/codeinventory/github.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.rb', line 8

def exclude
  @exclude
end

#orgObject

Returns the value of attribute org.



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

def org
  @org
end

#overridesObject

Returns the value of attribute overrides.



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

def overrides
  @overrides
end

Instance Method Details

#clientObject



171
172
173
# File 'lib/codeinventory/github.rb', line 171

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



142
143
144
145
146
147
# File 'lib/codeinventory/github.rb', line 142

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



79
80
81
82
83
84
# File 'lib/codeinventory/github.rb', line 79

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)



120
121
122
123
124
# File 'lib/codeinventory/github.rb', line 120

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.



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

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/codeinventory/github.rb', line 92

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



68
69
70
71
# File 'lib/codeinventory/github.rb', line 68

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)



109
110
111
112
113
# File 'lib/codeinventory/github.rb', line 109

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



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

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

#projectsObject



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

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



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

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.



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

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