Class: GithubBridge
- Inherits:
-
Object
- Object
- GithubBridge
- Defined in:
- lib/github-bridge.rb
Class Method Summary collapse
- .github_user_repo(url) ⇒ Object
- .login_github(username, password, host = nil) ⇒ Object
- .start(args) ⇒ Object
Instance Method Summary collapse
- #add_remote(repo_path, remote_name, url) ⇒ Object
- #clone_to_local(base_path, url) ⇒ Object
- #fork ⇒ Object
-
#fork_on_github(github, user, repo) ⇒ Object
COMMANDS - end ##.
-
#help ⇒ Object
COMMANDS start ##.
-
#initialize(args) ⇒ GithubBridge
constructor
A new instance of GithubBridge.
- #new_repo_github(github, name, description, homepage) ⇒ Object
- #push_remote(repo_path, remote_name) ⇒ Object
- #run ⇒ Object
- #usage ⇒ Object
Constructor Details
#initialize(args) ⇒ GithubBridge
Returns a new instance of GithubBridge.
6 7 8 9 10 |
# File 'lib/github-bridge.rb', line 6 def initialize(args) @command = args.shift @args = args @config = ParseConfig.new(Dir.home + '/.github/bridge.conf') end |
Class Method Details
.github_user_repo(url) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/github-bridge.rb', line 27 def self.github_user_repo(url) # Get name of user (or organization) and repository from any valid github url # html_url, ssh_url, git_url, https_url m = /github\.com.(.*?)\/(.*)/.match(url) if m return m[1], m[2].sub(/\.git\Z/, "") end return nil, nil end |
.login_github(username, password, host = nil) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/github-bridge.rb', line 143 def self.login_github(username, password, host = nil) if host puts "Login to #{host} ..." return Github.new do |attr| attr.endpoint = "https://#{host}/api/v3" attr.login = username attr.password = password end else puts "Login to github.com ..." return Github.new login: username, password: password end end |
.start(args) ⇒ Object
13 14 15 |
# File 'lib/github-bridge.rb', line 13 def self.start(args) GithubBridge.new(args).run end |
Instance Method Details
#add_remote(repo_path, remote_name, url) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/github-bridge.rb', line 107 def add_remote(repo_path, remote_name, url) Dir.chdir(repo_path) { cmd = "git remote add #{remote_name} #{url}" addremote_res = `#{cmd}` puts addremote_res } end |
#clone_to_local(base_path, url) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/github-bridge.rb', line 97 def clone_to_local(base_path, url) puts "Cloning #{url} under #{base_path}" Dir.chdir(base_path) { cmd = "git clone #{url}" clone_res = `#{cmd}` puts clone_res } end |
#fork ⇒ Object
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 |
# File 'lib/github-bridge.rb', line 45 def fork url = @args.shift user, repo = self.class.github_user_repo url if user and repo #read config github_login = @config['github']['login'] github_password = @config['github']['password'] local_repo_base = @config['local']['path'] enterprise_host = @config['enterprise']['host'] enterprise_login = @config['enterprise']['login'] enterprise_password = @config['enterprise']['password'] enterprise_name = @config['enterprise']['name'] github = self.class.login_github(github_login, github_password) fork_res = fork_on_github(github, user, repo) clone_to_local(local_repo_base, fork_res.clone_url) add_remote("#{local_repo_base}/#{fork_res.name}", "upstream", fork_res.parent.clone_url) github_enterprise = self.class.login_github(enterprise_login, enterprise_password, enterprise_host) create_res = new_repo_github(github_enterprise, fork_res.name, "#{fork_res.parent.description} | Forked from #{fork_res.parent.html_url}", fork_res.parent.homepage) add_remote("#{local_repo_base}/#{fork_res.name}", enterprise_name, create_res.ssh_url) push_remote("#{local_repo_base}/#{fork_res.name}", enterprise_name) else puts "No user and repo info from url: #{url}" usage end end |
#fork_on_github(github, user, repo) ⇒ Object
COMMANDS - end ##
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/github-bridge.rb', line 81 def fork_on_github(github, user, repo) begin fork_res = github.repos.forks.create user, repo if fork_res.status == 202 puts "#{user}/#{repo} forked on github.com" return fork_res else puts fork_res.status #TODO handle fork error return fork_res end rescue Github::Error::GithubError => e puts e. end end |
#help ⇒ Object
COMMANDS start ##
39 40 41 42 43 |
# File 'lib/github-bridge.rb', line 39 def help puts "No command: #{@command}" puts "Try: fork" puts "or call with '-h' for usage information" end |
#new_repo_github(github, name, description, homepage) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/github-bridge.rb', line 125 def new_repo_github(github, name, description, homepage) begin create_res = github.repos.create :name => name, :description => description, :homepage => homepage if create_res.status == 201 puts "#{name} created on GitHub Enterprise" return create_res else puts create_res.status #TODO return create_res end rescue Github::Error::GithubError => e puts e. end end |
#push_remote(repo_path, remote_name) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/github-bridge.rb', line 115 def push_remote(repo_path, remote_name) puts "Pushing to #{remote_name}" Dir.chdir(repo_path) { cmd = "git push -u --all #{remote_name}" push_res = `#{cmd}` puts push_res } end |
#run ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/github-bridge.rb', line 17 def run if @command && self.respond_to?(@command) self.send @command elsif %w(-h --help).include?(@command) usage else help end end |
#usage ⇒ Object
157 158 159 160 161 162 |
# File 'lib/github-bridge.rb', line 157 def usage puts <<-USAGE Usage: ghb fork <url: e.g. https://github.com/branky/github-bridge> USAGE end |