Skip to content

Commit

Permalink
updated code to use libraries and proper patching mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
Lloyd Philbrook committed Mar 5, 2012
1 parent 64ccdea commit 4020b00
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 140 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.DS_Store .DS_Store
*.swp *.swp
.project
39 changes: 1 addition & 38 deletions app/controllers/github_hook_controller.rb
Expand Up @@ -8,7 +8,7 @@ def index
repository = find_repository repository = find_repository


# Fetch the changes from Github # Fetch the changes from Github
update_repository(repository) Git.new.update_repository(repository)


# Fetch the new changesets into Redmine # Fetch the new changesets into Redmine
repository.fetch_changesets repository.fetch_changesets
Expand All @@ -18,42 +18,6 @@ def index


private private


# Executes shell command. Returns true if the shell command exits with a success status code
def exec(command)
logger.debug { "GithubHook: Executing command: '#{command}'" }

# Get a path to a temp file
logfile = Tempfile.new('github_hook_exec')
logfile.close

success = system("#{command} > #{logfile.path} 2>&1")
output_from_command = File.readlines(logfile.path)
if success
logger.debug { "GithubHook: Command output: #{output_from_command.inspect}"}
#p "GithubHook: Command output: #{output_from_command.inspect}"
else
logger.error { "GithubHook: Command '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}"}
#p "GithubHook: Command '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}"
end

return success
ensure
logfile.unlink
end

def git_command(command, repository)
"git --git-dir='#{repository.url}' #{command}"
end

# Fetches updates from the remote repository
def update_repository(repository)
command = git_command('fetch origin', repository)
if exec(command)
command = git_command("fetch origin '+refs/heads/*:refs/heads/*'", repository)
exec(command)
end
end

# Gets the project identifier from the querystring parameters and if that's not supplied, assume # Gets the project identifier from the querystring parameters and if that's not supplied, assume
# the Github repository name is the same as the project identifier. # the Github repository name is the same as the project identifier.
def get_identifier def get_identifier
Expand All @@ -79,5 +43,4 @@ def find_repository
raise TypeError, "Repository for project '#{project.to_s}' ('#{project.identifier}') is not a Git repository" unless repository.is_a?(Repository::Git) raise TypeError, "Repository for project '#{project.to_s}' ('#{project.identifier}') is not a Git repository" unless repository.is_a?(Repository::Git)
return repository return repository
end end

end end
72 changes: 13 additions & 59 deletions app/models/repository_observer.rb
@@ -1,64 +1,18 @@
class RepositoryObserver < ActiveRecord::Observer class RepositoryObserver < ActiveRecord::Observer
#observe Repository
#p 'hi, observe Repository'
def before_save(repository) def before_save(repository)
if Setting.plugin_redmine_github_hook[:enabled] && repository.type == 'Git' && repository.url.match('.*github.com') if Setting.plugin_redmine_github_hook[:enabled] && repository.type == 'Git' && repository.url.match('.*github.com')
base_dir_name = repository.url[/[\/][^\/]+.git/] respistory_name = repository.url[/[\/][^\/]+.git/]
url = repository.url repository_url = repository.url
#p 'basedir=', base_dir_name git_dir = Setting.plugin_redmine_github_hook[:git_dir].to_s
git_dir = Setting.plugin_redmine_github_hook[:git_dir].to_s repository = git_dir + respistory_name
git_dir = git_dir + base_dir_name
#git_dir = '/opt/data/git_repos' + base_dir_name #repository.project.identifier
if Dir[git_dir] == []
cmd = 'git clone --bare ' + url + ' ' + git_dir
if exec(cmd)
cmd = git_command('remote add origin '+url, git_dir)
exec(cmd)
cmd = git_command('fetch -v', git_dir)
exec(cmd)
cmd = git_command('fetch origin', git_dir)
exec(cmd)
cmd = git_command('reset --soft refs/remotes/origin/master', git_dir)
exec(cmd)
repository.url = git_dir
else
return false
end
end
end
end #defined before_save --------------


private if !File.exists?(repository)
def git_command(command, git_dir) if Git.new.mirror_repsitory(url, repository)
"git --git-dir='#{git_dir}' #{command}" repository.url = repository
end else

return false
# Executes shell command. Returns output from command if the shell command exits with a success status code end
def exec(command)
#logger.debug { "Github: Executing command: '#{command}'" }
#p "Github: Executing command: '#{command}'"

# Get a path to a temp file
#logfile = Tempfile.new('github__exec')
#logfile.close

#success = system("#{command} > #{logfile.path} 2>&1")
#output_from_command = File.readlines(logfile.path)
#output_from_command = %x[command]
shell = Shell.new(command)
shell.run
success = (shell.exitstatus == 0)
output_from_command = shell.output
if success
#logger.debug { "Github: Command output: #{output_from_command.inspect}"}
#p "Github: Command output: #{output_from_command.inspect}"
return output_from_command
else
#logger.error { "Github: Command '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}"}
p "Github: Command failed '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}"
end

#ensure
#logfile.unlink
end end
end
end
end end
44 changes: 1 addition & 43 deletions app/views/repositories/show.rhtml
@@ -1,49 +1,7 @@
<%= call_hook(:view_repositories_show_contextual, { :repository => @repository, :project => @project }) %> <%= call_hook(:view_repositories_show_contextual, { :repository => @repository, :project => @project }) %>
<%
def git_command(command, repository)
"git --git-dir='#{repository.url}' #{command}"
end

# Executes shell command. Returns output from command if the shell command exits with a success status code
def exec(command)
logger.debug { "Github: Executing command: '#{command}'" }

# Get a path to a temp file
logfile = Tempfile.new('github__exec')
logfile.close

success = system("#{command} > #{logfile.path} 2>&1")
output_from_command = File.readlines(logfile.path)
if success
logger.debug { "Github: Command output: #{output_from_command.inspect}"}
#p "Github: Command output: #{output_from_command.inspect}"
return output_from_command
else
logger.error { "Github: Command '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}"}
#p "Github: Command '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}"
end

ensure
logfile.unlink
end

url = @project.repository.url

if Setting.plugin_redmine_github_hook[:enabled] && @project.repository.type == 'Git'
command = git_command('remote -v', @repository)
result = exec(command)
if result and result.is_a?(Array) and result.length == 2
remote_name = result[0].split("\t")[0].strip
remote_url = result[0].split("\t")[1]
if remote_name == 'origin'
url = result[0].split("\t")[1].split(' ')[0]
end
end
end
%>
<div class="contextual"> <div class="contextual">
<% if @project.is_public or @project.users.include?(User.current) %> <% if @project.is_public or @project.users.include?(User.current) %>
<%= @project.repository.type %> URL: <input type="text" value="<%= url %>" size="30"> <%= @project.repository.type %> URL: <input type="text" value="<%= @project.repostory.remote_url %>" size="30">
<% end %> <% end %>
<%= render :partial => 'navigation' %> <%= render :partial => 'navigation' %>
</div> </div>
Expand Down
39 changes: 39 additions & 0 deletions lib/git.rb
@@ -0,0 +1,39 @@
class Git

# Mirror remote repository.
def mirror_repsitory(url, repository)
exec(git_command("clone --mirror #{url}", repository))
end

# Fetches updates from the remote repository
def update_repository(repository)
command = git_command('fetch origin', repository)
if exec(command)
command = git_command("fetch origin '+refs/heads/*:refs/heads/*'", repository)
exec(command)
end
end

# Get remote url
def get_remote_url(repository)
exec(git_command("remote -v", repository))
end

private

def git_command(command, git_dir)
"git --git-dir='#{git_dir}' #{command}"
end

def exec(command)
shell = Shell.new(command)
shell.run
success = (shell.exitstatus == 0)
output_from_command = shell.output
if success
return output_from_command
else
p "Github: Command failed '#{command}' didn't exit properly. Full output: #{output_from_command.inspect}"
end
end
end
39 changes: 39 additions & 0 deletions lib/repository_patch.rb
@@ -0,0 +1,39 @@
require_dependency 'repository'

# Patches Redmine's Repository dynamically.
module RepositoryPatch
def self.included(base) # :nodoc:
base.extend(ClassMethods)

base.send(:include, InstanceMethods)

# Same as typing in the class
base.class_eval do
unloadable # Send unloadable so it will not be unloaded in development
end

end

module ClassMethods

end

module InstanceMethods
# Get the remote url from the repoisotory.
def remote_url
if Setting.plugin_redmine_github_hook[:enabled] and self.type == 'Git'
result = Git.new.get_remote_url(self)
if result and result.is_a?(Array) and result.length == 2
remote_name = result[0].split("\t")[0].strip
remote_url = result[0].split("\t")[1]
if remote_name == 'origin'
result[0].split("\t")[1].split(' ')[0]
end
end
end
end
end
end

# Add module to Repository
Respository.send(:include, RepositoryPatch)

0 comments on commit 4020b00

Please sign in to comment.