-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrepository.rb
More file actions
284 lines (239 loc) · 9.9 KB
/
repository.rb
File metadata and controls
284 lines (239 loc) · 9.9 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require 'find'
require 'pathname'
require 'shellwords'
require 'bundler'
require 'git'
require 'octokit'
require 'bundler'
require 'fileutils'
require 'logger'
module AwestructWebEditor
class Repository
attr_reader :name, :uri
attr_accessor :relative_path
def initialize(content = [])
@name = content['name'] || content[:name] || ''
@uri = content['uri'] || content[:uri] || ''
@relative_path = content['relative_path'] || content[:relative_path] || nil
log_file = File.new(File.join((ENV['OPENSHIFT_RUBY_LOG_DIR'] || 'log'), 'application.log' ), 'a+')
log_file.sync = true
@logger = Logger.new(log_file, 'daily')
if ENV['OPENSHIFT_DATA_DIR']
@base_repo_dir = File.join(ENV['OPENSHIFT_DATA_DIR'], 'repos', content[:username])
FileUtils.mkdir(File.join @base_repo_dir) unless File.exists? @base_repo_dir
elsif ENV['RACK_ENV'] =~ /test/
@base_repo_dir = "tmp/repos/#{content[:username]}"
elsif content['base_repo_dir'] || content[:base_repo_dir]
@base_repo_dir = content['base_repo_dir'] || content[:base_repo_dir]
else
@base_repo_dir = "repos/#{content[:username]}"
end
@git_repo = Git.open File.join @base_repo_dir, @name if (File.exists?(File.join @base_repo_dir, @name))
@settings = File.open(File.join(@base_repo_dir, 'github-settings'), 'r') { |f| JSON.load(f) } if File.exists? File.join(@base_repo_dir, 'github-settings') || {}
@settings['oauth_token'] = content['token'] || content[:token] || nil
create_github_client.user
end
def init_empty
Dir.chdir(File.join @base_repo_dir) do |_|
@git_repo = Git.init(@name)
end
end
def add_creds
doc = <<CONFIG
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[credential "https://github.com"]
username = #{@settings['oauth_token']}
helper = "!f() { echo 'password=x-oauth-basic'; }; f"
CONFIG
Dir.chdir(File.join @base_repo_dir, @name, '.git') do
File.open('config', 'w') { |f| f.write(doc) }
end
end
def clone_repo
github = create_github_client
begin
@logger.info 'creating github fork'
fork_response = github.fork(URI(@settings['repo']).path[1..-1])
rescue Exception => e
return [500, e.message]
end
Dir.chdir(File.join @base_repo_dir) do
git = Git.open(@name)
@logger.debug "Cloning fork - #{fork_response.clone_url}"
git.add_remote('origin', fork_response.clone_url)
@logger.debug "Adding upstream fork - #{fork_response.parent.git_url}"
git.add_remote('upstream', fork_response.parent.clone_url)
@logger.debug 'pulling from git'
Open3.popen3("git pull upstream master") do |_, _, stderr, wait_thr|
exit_value = wait_thr.value
@logger.debug "pull exit status: #{exit_value}"
error = stderr.readlines.join "\n"
@logger.debug "pulling error: #{error}" unless error.empty?
end
end
@git_repo = Git.open File.join @base_repo_dir, @name
@logger.debug 'Starting bundle install'
Bundler.with_clean_env do
Open3.popen3('bundle install', :chdir => File.absolute_path(base_repository_path)) do |_, _, stderr, wait_thr|
exit_status = wait_thr.value.exitstatus
[exit_status, stderr.readlines().join("\n")]
end
end
end
def all_files(allows = [], dir = '')
@logger.info "Finding all files, additional allows #{allows}"
default_allows = [%r!(.ad)|(.adoc)|(.adoc)|(.jpg)|(.jpeg)|(.png)|(.gif)!]
default_allows << allows.join unless allows.empty?
regexp_ignores = Regexp.union default_allows
default_ignores = [%r!(.gitignore$)|(.git$)|(_site$)|(.awestruct$)|(.awestruct_ignore$)|(_config$)|(_ext$)|(.git$)|(.travis.yml$)|(_tmp$)|(.sass-cache$)!]
files = []
if File.exists?(File.join(base_repository_path, dir))
Find.find(File.join(base_repository_path, dir)) do |path|
if Regexp.union(default_ignores).match(path.to_s)
Find.prune
end
if regexp_ignores.match(path.to_s) || File.directory?(path)
if File.basename(path.to_s) != @name
files << file_info(path, dir)
end
end
end
end
files
end
def base_repository_path
File.join @base_repo_dir, @name
end
def save_file(name, content)
@logger.info "Saving file #{name}"
if content.is_a? Hash
@logger.debug 'Saving new file'
IO.copy_stream(content[:tempfile], File.join(base_repository_path, name))
content[:tempfile].unlink
content[:tempfile].close
else
File.open(File.join(base_repository_path, name), 'w') do |f|
f.write content
end
end
@logger.debug 'Adding file to git'
@git_repo.add(Shellwords.escape name)
end
def remove_file(name)
@logger.info "Removing file #{name}"
@git_repo.remove(Shellwords.escape name)
path_to_file = File.join(base_repository_path, Shellwords.escape(name))
File.delete(path_to_file) if File.exists? path_to_file
!File.exists? path_to_file
end
def commit(message)
@logger.info "Commiting with message #{message}"
current_user = create_github_client.user
@git_repo.commit_all(message, :author => "current_user['name'] <#{current_user['email']}>")
@git_repo.log(1).first # Give us back a commit object so we can actually query it
end
def fetch_remote(remote = 'upstream')
@logger.info "Fetching remote #{remote}"
Open3.popen3("git fetch #{remote}",
:chdir => File.absolute_path(base_repository_path)) do |_, _, stderr, wait_thr|
exit_value = wait_thr.value
@logger.debug "fetch exit status: #{exit_value}"
error = stderr.readlines.join "\n"
@logger.debug "fetch error: #{error}" unless error.empty?
end
end
def create_branch(branch_name)
upstream_repo = create_github_client.repository(Octokit::Repository.from_url @settings['repo'])
fetch_remote
@logger.info "creating branch #{branch_name} based on 'upstream/#{upstream_repo.master_branch}'"
system("git checkout -b #{branch_name} upstream/#{upstream_repo.master_branch}")
end
def switch_branch(branch_name)
@git_repo.checkout branch_name, {:force => true}
[200, '']
end
def rebase(overwrite, remote = 'upstream')
fetch_remote remote
upstream_repo = create_github_client.repository(Octokit::Repository.from_url @settings['repo'])
Dir.chdir(File.join @base_repo_dir) do
if overwrite
@logger.debug 'overwriting our files during the rebase'
successful_return = system("git rebase upstream/#{upstream_repo.master_branch} -X ours")
else
@logger.debug 'rebasing'
successful_return = system("git rebase upstream/#{upstream_repo.master_branch}")
end
if successful_return
[200, 'Successful merge']
else
[500, 'Merge conflict detected']
end
end
end
def remove_branch(branch_name)
@logger.info "removing branch #{branch_name}"
@git_repo.branch('master').checkout
@git_repo.branch(branch_name).delete
end
def branches
@logger.debug "Fetching local branches and notes"
Open3.popen3('git show --format="||%d||%h||%N" --notes=pull $(git notes --ref=pull | awk "{ print $2 }") | awk "{ if ($1 ~ /\|\|/) { print $0 } }',
:chdir => File.absolute_path(base_repository_path)) do |_, stdout, stderr, wait_thr|
exit_value = wait_thr.value
@logger.debug "fetch exit status: #{exit_value}"
error = stderr.readlines.join "\n"
@logger.debug "fetch error: #{error}" unless error.empty?
returns = []
stdout.readlines.each do |line|
_, branches, pull = line.split '||'
branches = branches.strip.gsub(/[()]/, '').split(',').last.strip
returns << {:pull => pull, :branch => branches}
end
returns
end
end
def push(remote = 'origin')
@logger.info "pushing to #{remote}"
system("git push #{remote} HEAD")
end
def pull_request(title, body)
github = create_github_client
upstream_repo = Octokit::Repository.from_url @settings['repo']
upstream_response = github.repository(upstream_repo)
@logger.info "Issuing a pull request with title - #{title} and body #{body}"
pull_request_result = github.create_pull_request(upstream_repo,
"#{upstream_response.owner.login}:#{upstream_response.master_branch}",
"#{@settings['username']}:#{@git_repo.lib.branch_current}", title, body)
@git_repo.branch(upstream_response.master_branch).checkout
pull_request_result['html_url']
end
def file_content(file)
@logger.info "reading contents of file #{file.to_s}"
file_path = File.join(base_repository_path, file)
return '' unless File.exists? file_path
if `file --mime-encoding -b #{file_path}` =~ /binary/
"data:#{`file --mime-type -b #{file_path}`.strip};base64,#{Base64.encode64(File.open(file_path, 'rb').read)}"
else
File.open(file_path, 'r').read
end
end
def file_info(path, dir = '')
path = Pathname.new(path)
path = Pathname.new(File.join(base_repository_path, dir, path.basename)) unless File.exists? path
{ :location => path.basename.to_s, :directory => path.directory?,
:path_to_root => path.relative_path_from(Pathname.new base_repository_path).dirname.to_s }
end
def log(count = 30)
@logger.info "retrieving the last #{count} log entries"
@git_repo.log count
end
private
def create_github_client
Octokit::Client.new(:login => @settings['username'], :oauth_token => @settings['oauth_token'])
end
end
end