-
Notifications
You must be signed in to change notification settings - Fork 204
/
gitlab.rb
182 lines (152 loc) · 5.27 KB
/
gitlab.rb
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# frozen_string_literal: true
module RepositoryHost
class Gitlab < Base
IGNORABLE_EXCEPTIONS = [::Gitlab::Error::NotFound,
::Gitlab::Error::Forbidden,
::Gitlab::Error::Unauthorized,
::Gitlab::Error::InternalServerError,
::Gitlab::Error::Parsing].freeze
def self.api_missing_error_class
::Gitlab::Error::NotFound
end
def avatar_url(_size = 60)
repository.logo_url
end
def domain
"https://gitlab.com"
end
def forks_url
"#{url}/forks"
end
def contributors_url
"#{url}/graphs/#{repository.default_branch}"
end
def blob_url(sha = nil)
sha ||= repository.default_branch
"#{url}/blob/#{sha}/"
end
def commits_url(_author = nil)
"#{url}/commits/#{repository.default_branch}"
end
def download_contributions(token = nil)
# not implemented yet
end
def download_forks(token = nil)
# not implemented yet
end
def retrieve_commits
# not implemented yet
end
def download_owner
return if repository.owner && repository.repository_user_id && repository.owner.login == repository.owner_name
namespace = api_client.project(repository.full_name).try(:namespace)
return unless namespace
case namespace.kind
when "group"
o = RepositoryOwner::Gitlab.api_client.group(namespace.path)
org = RepositoryOrganisation.create_from_host("GitLab", o)
if org
repository.repository_organisation_id = org.id
repository.repository_user_id = nil
repository.save
end
when "user"
o = RepositoryOwner::Gitlab.fetch_user(namespace.path)
u = RepositoryUser.create_from_host("GitLab", o)
if u
repository.repository_user_id = u.id
repository.repository_organisation_id = nil
repository.save
end
end
rescue *IGNORABLE_EXCEPTIONS
nil
end
def create_webhook(token = nil)
# not implemented yet
end
def get_file_list(token = nil)
tree = api_client(token).tree(repository.full_name, recursive: true)
tree.select { |item| item.type == "blob" }.map(&:path)
rescue *IGNORABLE_EXCEPTIONS
nil
end
def get_file_contents(path, token = nil)
file = api_client(token).get_file(repository.full_name, path, repository.default_branch)
{
sha: file.commit_id,
content: Base64.decode64(file.content),
}
rescue *IGNORABLE_EXCEPTIONS
nil
end
def download_readme(token = nil)
files = api_client(token).tree(repository.full_name)
paths = files.map(&:path)
readme_path = paths.select { |path| path.match(/^readme/i) }.min { |path| Readme.supported_format?(path) ? 0 : 1 }
return if readme_path.nil?
file = get_file_contents(readme_path, token)
return unless file.present?
content = Readme.format_markup(readme_path, file[:content])
return unless content.present?
if repository.reload_readme.nil?
repository.create_readme(html_body: content)
else
repository.readme.update(html_body: content)
end
rescue *IGNORABLE_EXCEPTIONS
nil
end
def download_tags(token = nil)
existing_tag_names = repository.tags.pluck(:name)
remote_tags = api_client(token).tags(repository.full_name).auto_paginate do |tag|
next if existing_tag_names.include?(tag.name)
next if tag.commit.nil?
repository.tags.create({
name: tag.name,
kind: "tag",
sha: tag.commit.id,
published_at: tag.commit.committed_date,
})
end
repository.projects.find_each(&:forced_save) if remote_tags.present?
rescue *IGNORABLE_EXCEPTIONS
nil
end
def self.recursive_gitlab_repos(page_number = 1, limit = 5, order = "created_asc")
return if limit.zero?
r = Typhoeus.get("https://gitlab.com/explore/projects?&page=#{page_number}&sort=#{order}")
if r.code == 500
recursive_gitlab_repos(page_number.to_i + 1, limit, order)
else
page = Nokogiri::HTML(r.body)
names = page.css("a.project").map { |project| project.attributes["href"].value[1..] }
names.each do |name|
if (r = Repository.host("GitLab").find_by_full_name(name))
RepositoryDownloadWorker.perform_async(r.id)
else
CreateRepositoryWorker.perform_async("GitLab", name)
end
end
end
if names.any?
limit -= 1
REDIS.set "gitlab-page", page_number
recursive_gitlab_repos(page_number.to_i + 1, limit, order)
end
end
def self.fetch_repo(full_name, token = nil)
project = api_client(token).project(full_name, { license: true })
RawUpstreamDataConverter.convert_from_gitlab_api(project)
rescue *IGNORABLE_EXCEPTIONS
nil
end
def self.api_client(token = nil)
::Gitlab.client(endpoint: "https://gitlab.com/api/v4", private_token: token || Rails.configuration.gitlab_key)
end
private
def api_client(_token = nil)
self.class.api_client
end
end
end