-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
195 lines (181 loc) · 6.42 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
require 'git'
require 'csv'
require 'fileutils'
require 'travis'
require 'open4'
require 'date'
require 'travis/client'
@number_of_repositories=1
@client = Travis::Client.new
@github_username='ChenZhangg'
def clone_repo(user_dir,repo_url,repo_dir)
i=0
while(i<600)
FileUtils.rm_rf(repo_dir) if File.exist?(repo_dir)
result=%x"cd #{user_dir} && git clone #{repo_url}"
if $?.to_i!=0
puts "********************The #{i} of 600 try to clone the project from #{repo_url}. $?=#{$?.to_i}*******************************"
i+=1
sleep 60
next
else
puts "*******************Clone the project from #{repo_url} successfully.**************************"
break
end
end
end
def create_github_repo(repo_dir,user_name,repo_name,i)
k=0
while(i<600)
%x"cd #{repo_dir} && hub create #{user_name}_#{repo_name}_#{i}"
if $?.to_i!=0
puts "*******************The #{k} of 600 try to hub create #{user_name}_#{repo_name}_#{i}. $?=#{$?.to_i}*******************************"
k+=1
sleep 60
next
else
puts "*******************Create repo #{@github_username}/#{user_name}_#{repo_name}_#{i} on Github successfully.**************************"
break
end
end
end
def open_travis(repo_dir,user_name,repo_name,i)
k=0
while(k<600)
%x"cd #{repo_dir} && travis enable -r #{@github_username}/#{user_name}_#{repo_name}_#{i}"
if $?.to_i!=0
puts "*******************The #{k} of 600 try to enable #{@github_username}/#{user_name}_#{repo_name}_#{i}. $?=#{$?.to_i}*******************************"
k+=1
sleep 60
next
else
puts "*******************Enable travis #{@github_username}/#{user_name}_#{repo_name}_#{i} successfully.**************************"
break
end
end
end
def link_remote(repo_dir,user_name,repo_name,i)
k=0
while(k<600)
%x"cd #{repo_dir} && git remote add #{user_name}_#{repo_name}_#{i} git@github.com:#{@github_username}/#{user_name}_#{repo_name}_#{i}.git"
if $?.to_i==0
puts "*******************Remote add #{@github_username}/#{user_name}_#{repo_name}_#{i} successfully.**************************"
break
elsif $?.to_i==128
puts "*******************Already add #{@github_username}/#{user_name}_#{repo_name}_#{i}.**************************"
break
else
puts "*******************The #{k} of 600 try to remote add #{@github_username}/#{user_name}_#{repo_name}_#{i}. $?=#{$?.to_i}*******************************"
k+=1
sleep 60
next
end
end
end
def enable_travis(repo_dir,user_name,repo_name)
(0...@number_of_repositories).each do |i|
create_github_repo(repo_dir,user_name,repo_name,i)
sleep 60
open_travis(repo_dir,user_name,repo_name,i)
link_remote(repo_dir,user_name,repo_name,i)
end
end
def get_last_build(user_name,repo_name,i)
k=0
begin
@client.clear_cache!
travis_repo= @client.repo("#{@github_username}/#{user_name}_#{repo_name}_#{i}")
last=travis_repo.last_build
rescue
puts $!
travis_repo=nil
last=nil
puts "*******************The #{k} of 10 try to get last build of #{@github_username}/#{user_name}_#{repo_name}_#{i}.**************************"
sleep 60
k+=1
retry if k<10
end
return last
end
def pre_build_completed(user_name,repo_name,i)
sleep 300
last=get_last_build(user_name,repo_name,i)
puts "*******************The information of last build of #{@github_username}/#{user_name}_#{repo_name}_#{i} is #{last ? last.number : '0'}********************"
while(last && last.finished? != true)
puts "*******************The last build #{last ? last.number : '0'} of #{@github_username}/#{user_name}_#{repo_name}_#{i} is not finished.********************"
sleep 300
last=get_last_build(user_name,repo_name,i)
end
end
def push(repo_dir,user_name,repo_name,l,i)
k=0
while(k<5)
%x"cd #{repo_dir} && git push #{user_name}_#{repo_name}_#{i} #{l.sha}:refs/heads/master"
if $?.to_i==0
puts "*******************Push #{l.sha} #{l.date} to Github repo #{@github_username}/#{user_name}_#{repo_name}_#{i}*******************"
break
elsif $?.to_i==256
puts "*******************Push Error $?=#{$?} #{l.sha} #{l.date} to Github repo #{@github_username}/#{user_name}_#{repo_name}_#{i}*******************"
k+=1
sleep 60
next
elsif $?.to_i==32768
puts "*******************Push Error $?=#{$?.to_i} #{l.sha} #{l.date} to Github repo #{@github_username}/#{user_name}_#{repo_name}_#{i}*******************"
k+=1
sleep 60
next
else
puts "*******************Push Error $?=#{$?.to_i} #{l.sha} #{l.date} to Github repo #{@github_username}/#{user_name}_#{repo_name}_#{i}*******************"
k+=1
sleep 60
next
end
end
end
def git_push(repo_dir,user_name,repo_name,first_commit_time)
days=(Time.now.to_date-first_commit_time.getlocal.to_date).to_i
g=Git.open(repo_dir)
g_log=g.log(nil).since("#{days} days ago")
g_log.reverse_each do |l|
(0...@number_of_repositories).each do |i|
push(repo_dir,user_name,repo_name,l,i)
end
(0...@number_of_repositories).each do |i|
pre_build_completed(user_name,repo_name,i)
end
end
end
def create_dir(user_name,repo_name,repo_url,first_commit_time)
user_dir=File.join('repositories',user_name)
repo_dir=File.join('repositories',user_name,repo_name)
return if File.exist?(repo_dir)
FileUtils.mkdir_p(user_dir) unless File.exist?(user_dir)
clone_repo(user_dir,repo_url,repo_dir)
enable_travis(repo_dir,user_name,repo_name)
git_push(repo_dir,user_name,repo_name,first_commit_time)
end
def use_travis(user_name,repo_name,repo_url)
i=0
begin
travis_repo=@client.repo("#{user_name}/#{repo_name}")
rescue
travis_repo=nil
puts $!
puts "****************#{user_name}/#{repo_name} don't use travis service, the #{i} of 10 try.****************"
sleep 60
i+=1
retry if i<10
end
if travis_repo && travis_repo.last_build && travis_repo.last_build.number.to_i>10
first_commit_time=travis_repo.build(1).commit.committed_at
create_dir(user_name,repo_name,repo_url,first_commit_time)
end
puts "****************The project #{user_name}/#{repo_name} finished.***************************"
end
def csv_traverse(csv_file)
CSV.foreach(csv_file,headers:false,col_sep:',') do |row|
use_travis(row[0],row[1],row[2])
end
puts '***********************CSV TRAVERSE OVER**************************'
end
csv_traverse('repoAbove1000WithTravis.csv')