Class: CodeInventory::Source::GitHub

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token:, org:) ⇒ GitHub

Returns a new instance of GitHub.



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

def initialize(access_token:, org:)
  Octokit.auto_paginate = true
  @access_token = access_token
  @org = org
end

Instance Attribute Details

#orgObject

Returns the value of attribute org.



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

def org
  @org
end

Instance Method Details

#clientObject



42
43
44
# File 'lib/codeinventory/source/github.rb', line 42

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

#projectsObject



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

def projects
  repos = client.organization_repositories(@org)
  projects = []
  repos.each do |repo|
    begin
       = client.contents(repo[:full_name], path: ".codeinventory.yml")
      type = :yaml
      raw_content = Base64.decode64([:content])
    rescue Octokit::NotFound
      begin
         = client.contents(repo[:full_name], path: ".codeinventory.json")
        type = :json
        raw_content = Base64.decode64([:content])
      rescue Octokit::NotFound
        # Ignore repositories that don't have a CodeInventory metadata file
      end
    end
    if type == :yaml
      projects << YAML.load(raw_content).to_hash
    elsif type == :json
      projects << JSON.parse(raw_content)
    end
  end
  projects
end