@@ -4,8 +4,9 @@ class Project < ActiveRecord::Base
44
55 validates :full_name , uniqueness : true , presence : true
66 validates :github_id , uniqueness : true , presence : true
7+ validates :host , inclusion : [ "github" , "bitbucket" ] , presence : true
78
8- def update_github_info repo
9+ def update_repository_info repo
910 self . github_id = repo . id
1011 self . name = repo . name
1112 self . full_name = repo . full_name
@@ -16,22 +17,34 @@ def update_github_info repo
1617 self . save!
1718 end
1819
20+ def repository_client
21+ if host == "github"
22+ Github . new
23+ elsif host == "bitbucket"
24+ Bitbucket . new
25+ end
26+ end
27+
1928 def github_url
20- "https://github.com/ #{ full_name } "
29+ repository_client . repository_url self
2130 end
2231
2332 def source_github_url
24- "https://github.com/#{ source_full_name } "
33+ repository_client . source_repository_url self
34+ end
35+
36+ def raw_commits
37+ repository_client . commits self
38+ end
39+
40+ def repository_info
41+ repository_client . repository_info self
2542 end
2643
2744 def new_commits
2845 begin
2946 commits = Timeout ::timeout ( 90 ) do
30- client = Octokit ::Client . new \
31- :client_id => CONFIG [ 'github' ] [ 'key' ] ,
32- :client_secret => CONFIG [ 'github' ] [ 'secret' ] ,
33- :per_page => 100
34- client . commits ( full_name ) .
47+ raw_commits .
3548 # Filter merge request
3649 select { |c | !( c . commit . message =~ /^(Merge\s |auto\s merge)/ ) } .
3750 # Filter fake emails
@@ -132,26 +145,14 @@ def self.update_cache
132145 end
133146 end
134147
135- def github_info
136- client = Octokit ::Client . new \
137- :client_id => CONFIG [ 'github' ] [ 'key' ] ,
138- :client_secret => CONFIG [ 'github' ] [ 'secret' ]
139- if github_id . present?
140- client . get ( "/repositories/#{ github_id } " )
141- else
142- client . repo ( full_name )
143- end
144- end
145-
146148 def update_info
147149 begin
148- update_github_info ( github_info )
150+ update_repository_info ( repository_info )
149151 rescue Octokit ::BadGateway , Octokit ::NotFound , Octokit ::InternalServerError ,
150152 Errno ::ETIMEDOUT , Net ::ReadTimeout , Faraday ::Error ::ConnectionFailed => e
151153 Rails . logger . info "Project ##{ id } : #{ e . class } happened"
152154 rescue StandardError => e
153155 Airbrake . notify ( e )
154156 end
155157 end
156-
157158end
0 commit comments