forked from snowyu/redmine_github_hook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated code to use libraries and proper patching mechanism
- Loading branch information
Lloyd Philbrook
committed
Mar 5, 2012
1 parent
64ccdea
commit 4020b00
Showing
6 changed files
with
94 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
*.swp | ||
.project |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,18 @@ | ||
class RepositoryObserver < ActiveRecord::Observer | ||
#observe Repository | ||
#p 'hi, observe Repository' | ||
def before_save(repository) | ||
if Setting.plugin_redmine_github_hook[:enabled] && repository.type == 'Git' && repository.url.match('.*github.com') | ||
base_dir_name = repository.url[/[\/][^\/]+.git/] | ||
url = repository.url | ||
#p 'basedir=', base_dir_name | ||
git_dir = Setting.plugin_redmine_github_hook[:git_dir].to_s | ||
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 -------------- | ||
if Setting.plugin_redmine_github_hook[:enabled] && repository.type == 'Git' && repository.url.match('.*github.com') | ||
respistory_name = repository.url[/[\/][^\/]+.git/] | ||
repository_url = repository.url | ||
git_dir = Setting.plugin_redmine_github_hook[:git_dir].to_s | ||
repository = git_dir + respistory_name | ||
|
||
private | ||
def git_command(command, git_dir) | ||
"git --git-dir='#{git_dir}' #{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}'" } | ||
#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 | ||
if !File.exists?(repository) | ||
if Git.new.mirror_repsitory(url, repository) | ||
repository.url = repository | ||
else | ||
return false | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |