Class: OrganizationAudit::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/organization_audit/repo.rb

Constant Summary collapse

HOST =
"https://api.github.com"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, token = nil) ⇒ Repo

Returns a new instance of Repo.



9
10
11
12
# File 'lib/organization_audit/repo.rb', line 9

def initialize(data, token=nil)
  @data = data
  @token = token
end

Class Method Details

.all(options) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/organization_audit/repo.rb', line 34

def self.all(options)
  user = if options[:organization]
    "orgs/#{options[:organization]}"
  elsif options[:user]
    "users/#{options[:user]}"
  else
    "user"
  end
  url = File.join(HOST, user, "repos")

  token = options[:token]
  download_all_pages(url, headers(token)).map { |data| Repo.new(data, token) }
end

Instance Method Details

#clone_urlObject



75
76
77
78
79
80
81
# File 'lib/organization_audit/repo.rb', line 75

def clone_url
  if private?
    url.sub("https://", "git@").sub("/", ":") + ".git"
  else
    url + ".git"
  end
end

#content(file) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/organization_audit/repo.rb', line 48

def content(file)
  @content ||= {}
  @content[file] ||= begin
    if private?
      download_content_via_api(file)
    else
      download_content_via_raw(file)
    end
  end
rescue OpenURI::HTTPError => e
  raise "Error downloading #{file} from #{url} (#{e})" unless e.message.start_with?("404")
end

#directory?(folder) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/organization_audit/repo.rb', line 87

def directory?(folder)
  !!list(File.dirname(folder)).detect { |f| f["path"] == folder && f["type"] == "dir" }
end

#file_list(dir = ".") ⇒ Object



83
84
85
# File 'lib/organization_audit/repo.rb', line 83

def file_list(dir=".")
  list(dir).select { |f| f["type"] == "file" }.map { |file| file["path"] }
end

#gem?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/organization_audit/repo.rb', line 14

def gem?
  !!gemspec_file
end

#gemspec_contentObject



18
19
20
# File 'lib/organization_audit/repo.rb', line 18

def gemspec_content
  content(gemspec_file) if gem?
end

#last_commiterObject



69
70
71
72
73
# File 'lib/organization_audit/repo.rb', line 69

def last_commiter
  response = call_api("commits/#{branch}")
  committer = response["commit"]["committer"]
  "#{committer["name"]} <#{committer["email"]}>"
end

#nameObject



30
31
32
# File 'lib/organization_audit/repo.rb', line 30

def name
  api_url.split("/").last
end

#private?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/organization_audit/repo.rb', line 61

def private?
  @data["private"]
end

#public?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/organization_audit/repo.rb', line 65

def public?
  !private?
end

#to_sObject



26
27
28
# File 'lib/organization_audit/repo.rb', line 26

def to_s
  "#{url} -- #{last_commiter}"
end

#urlObject



22
23
24
# File 'lib/organization_audit/repo.rb', line 22

def url
  api_url.sub("api.", "").sub("/repos", "")
end