-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathgithub.rb
More file actions
104 lines (89 loc) · 2.66 KB
/
github.rb
File metadata and controls
104 lines (89 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# frozen_string_literal: true
class Github
def initialize
options = { client_id: CONFIG['github']['key'], client_secret: CONFIG['github']['secret'] }
if CONFIG['github']['auto_paginate']
options.merge! auto_paginate: true
else
options.merge! per_page: 100
end
@client = Octokit::Client.new(options)
end
attr_reader :client
def commits(project)
commits = if project.branch.blank?
client.commits project.full_name
else
client.commits project.full_name, sha: project.branch
end
last_response = client.last_response
pages = (CONFIG['github']['project_pages'][project.full_name] || CONFIG['github']['pages'] || 1).to_i
(pages - 1).times do
if last_response.rels[:next]
last_response = last_response.rels[:next].get
commits += last_response.data
end
end
commits
end
def repository_info(project)
case project
when String
client.repo project
when Project
if project.github_id.present?
client.get "/repositories/#{project.github_id}"
else
client.repo project.full_name
end
else
raise 'Unknown parameter class'
end
end
def find_or_create_project(project_name)
find_project(project_name) || create_project(project_name)
end
def find_project(project_name)
Project.find_by(host: 'github', full_name: project_name)
end
def create_project(project_name)
project_name =~ %r{\w+/\w+}
return unless project_name
repo = repository_info(project_name)
project = Project.find_or_create_by(host: 'github', full_name: repo.full_name)
project.update_repository_info(repo)
project
rescue Octokit::NotFound
nil
end
def collaborators_info(project)
begin
client.get("/repos/#{project.full_name}/collaborators").map(&:login)
rescue StandardError
[project.full_name.split('/').first]
end +
begin
client.get("/orgs/#{project.full_name.split('/').first}/members").map(&:login)
rescue StandardError
[]
end
end
def branches(project)
branches = client.get("/repos/#{project.full_name}/branches")
last_response = client.last_response
while last_response && last_response.rels[:next]
last_response = last_response.rels[:next].get
branches += last_response.data
end
branches.map(&:name)
end
def repository_url(project)
"https://github.com/#{project.full_name}"
end
def source_repository_url(project)
"https://github.com/#{project.source_full_name}"
end
def commit_url(project, commit)
"https://github.com/#{project.full_name}/commit/#{commit}"
end
end