-
Notifications
You must be signed in to change notification settings - Fork 207
/
base.rb
151 lines (121 loc) · 3.81 KB
/
base.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
# frozen_string_literal: true
module RepositoryOwner
class Base
attr_reader :owner
def initialize(owner)
@owner = owner
end
def avatar_url(size = 60)
raise NotImplementedError
end
def repository_url
raise NotImplementedError
end
def top_favourite_projects
Project.visible.where(id: top_favourite_project_ids).maintained.order(Arel.sql("position(','||projects.id::text||',' in '#{top_favourite_project_ids.join(',')}')"))
end
def top_contributors
RepositoryUser.where(id: top_contributor_ids).order(Arel.sql("position(','||repository_users.id::text||',' in '#{top_contributor_ids.join(',')}')"))
end
def to_s
owner.name.presence || owner.login
end
def to_param
{
host_type: owner.host_type.downcase,
login: owner.login,
}
end
def github_id
owner.uuid
end
def download_orgs
raise NotImplementedError
end
def download_repos
raise NotImplementedError
end
def api_client(_token = nil)
self.class.api_client
end
def self.format(host_type)
case host_type.try(:downcase) # rubocop: disable Style/HashLikeCase
when "github"
"GitHub"
when "gitlab"
"GitLab"
when "bitbucket"
"Bitbucket"
end
end
def check_status
response = Typhoeus.head(repository_url)
if response.response_code == 404
owner.repositories.each do |repo|
CheckRepoStatusWorker.perform_async(repo.host_type, repo.full_name)
end
owner.destroy
end
end
def formatted_host
self.class.format(owner.host_type)
end
def download_user_from_host
download_user_from_host_by(owner.uuid)
rescue StandardError
download_user_from_host_by_login
end
def download_user_from_host_by_login
download_user_from_host_by(owner.login)
end
def download_user_from_host_by(id_or_login)
self.class.download_user_from_host(owner.host_type, id_or_login)
end
def self.download_user_from_host(host_type, id_or_login)
RepositoryUser.create_from_host(host_type, fetch_user(id_or_login))
end
def download_org_from_host
download_org_from_host_by(owner.uuid)
rescue StandardError
download_org_from_host_by_login
end
def download_org_from_host_by_login
download_org_from_host_by(owner.login)
end
def download_org_from_host_by(id_or_login)
self.class.download_org_from_host(owner.host_type, id_or_login)
end
def self.download_org_from_host(host_type, id_or_login)
RepositoryOrganisation.create_from_host(host_type, fetch_org(id_or_login))
end
def self.fetch_user(id_or_login)
raise NotImplementedError
end
def self.fetch_org(id_or_login)
raise NotImplementedError
end
# Return a HashWithIndifferentAccess, where null bytes are removed from string values,
# for each repositories' client response. The possible response Hash wrappers are:
#
# * Bitbucket returns Hashie::Mash
# * GitHub returns Sawyer::Resource
# * GitLab returns Gitlab::ObjectifiedHash
def self.sanitized_hash_with_indifferent_access_from_client_response(response)
response
.to_hash
.with_indifferent_access
.transform_values { |v| v.is_a?(String) ? StringUtils.strip_null_bytes(v) : v }
end
private
def top_favourite_project_ids
Rails.cache.fetch [owner, "top_favourite_project_ids"], expires_in: 1.week, race_condition_ttl: 2.minutes do
owner.favourite_projects.limit(10).pluck(:id)
end
end
def top_contributor_ids
Rails.cache.fetch [owner, "top_contributor_ids"], expires_in: 1.week, race_condition_ttl: 2.minutes do
owner.contributors.visible.limit(50).pluck(:id)
end
end
end
end