github-bridge 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +22 -0
- data/README.md +59 -0
- data/Rakefile +17 -0
- data/bin/ghb +7 -0
- data/lib/github-bridge.rb +164 -0
- data/test/test_github-bridge.rb +34 -0
- metadata +91 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2013 Branky Shao
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
'Software'), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
github-bridge
|
2
|
+
===========
|
3
|
+
|
4
|
+
The bridge between public GitHub (github.com) and private GitHub (GitHub Enterprise, github.mycompany.com).
|
5
|
+
|
6
|
+
Features
|
7
|
+
--------
|
8
|
+
|
9
|
+
* Fork a repopsitory on github.com, clone it to local, then create it on GitHub Enterprise. All remote repositories are set properly.
|
10
|
+
|
11
|
+
Examples
|
12
|
+
--------
|
13
|
+
|
14
|
+
$ ghb fork https://github.com/branky/github-bridge
|
15
|
+
|
16
|
+
Requirements
|
17
|
+
------------
|
18
|
+
* git
|
19
|
+
* ruby 1.9.x
|
20
|
+
* gem "github_api"
|
21
|
+
* gem "parseconfig"
|
22
|
+
|
23
|
+
Install
|
24
|
+
-------
|
25
|
+
|
26
|
+
* FIXME (sudo gem install, anything else)
|
27
|
+
|
28
|
+
Author
|
29
|
+
------
|
30
|
+
|
31
|
+
Original author: Branky Shao (branky34@gmail.com)
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
License
|
36
|
+
-------
|
37
|
+
|
38
|
+
The MIT License
|
39
|
+
|
40
|
+
Copyright (c) 2013 Branky Shao
|
41
|
+
|
42
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
43
|
+
a copy of this software and associated documentation files (the
|
44
|
+
'Software'), to deal in the Software without restriction, including
|
45
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
46
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
47
|
+
permit persons to whom the Software is furnished to do so, subject to
|
48
|
+
the following conditions:
|
49
|
+
|
50
|
+
The above copyright notice and this permission notice shall be
|
51
|
+
included in all copies or substantial portions of the Software.
|
52
|
+
|
53
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
54
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
55
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
56
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
57
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
58
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
59
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
begin
|
3
|
+
require 'bones'
|
4
|
+
rescue LoadError
|
5
|
+
abort '### Please install the "bones" gem ###'
|
6
|
+
end
|
7
|
+
|
8
|
+
task :default => 'test:run'
|
9
|
+
task 'gem:release' => 'test:run'
|
10
|
+
|
11
|
+
Bones {
|
12
|
+
name 'github-bridge'
|
13
|
+
authors 'Branky Shao'
|
14
|
+
email 'branky34@gmail.com'
|
15
|
+
url 'http://github.com/branky/github-bridge'
|
16
|
+
}
|
17
|
+
|
data/bin/ghb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
require 'parseconfig'
|
2
|
+
require 'github_api'
|
3
|
+
|
4
|
+
class GithubBridge
|
5
|
+
|
6
|
+
def initialize(args)
|
7
|
+
@command = args.shift
|
8
|
+
@args = args
|
9
|
+
@config = ParseConfig.new(Dir.home + '/.github/bridge.conf')
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
def self.start(args)
|
14
|
+
GithubBridge.new(args).run
|
15
|
+
end
|
16
|
+
|
17
|
+
def run
|
18
|
+
if @command && self.respond_to?(@command)
|
19
|
+
self.send @command
|
20
|
+
elsif %w(-h --help).include?(@command)
|
21
|
+
usage
|
22
|
+
else
|
23
|
+
help
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.github_user_repo(url)
|
28
|
+
# Get name of user (or organization) and repository from any valid github url
|
29
|
+
# html_url, ssh_url, git_url, https_url
|
30
|
+
m = /github\.com.(.*?)\/(.*)/.match(url)
|
31
|
+
if m
|
32
|
+
return m[1], m[2].sub(/\.git\Z/, "")
|
33
|
+
end
|
34
|
+
return nil, nil
|
35
|
+
end
|
36
|
+
|
37
|
+
## COMMANDS start ##
|
38
|
+
|
39
|
+
def help
|
40
|
+
puts "No command: #{@command}"
|
41
|
+
puts "Try: fork"
|
42
|
+
puts "or call with '-h' for usage information"
|
43
|
+
end
|
44
|
+
|
45
|
+
def fork
|
46
|
+
url = @args.shift
|
47
|
+
user, repo = self.class.github_user_repo url
|
48
|
+
if user and repo
|
49
|
+
#read config
|
50
|
+
github_login = @config['github']['login']
|
51
|
+
github_password = @config['github']['password']
|
52
|
+
local_repo_base = @config['local']['path']
|
53
|
+
enterprise_host = @config['enterprise']['host']
|
54
|
+
enterprise_login = @config['enterprise']['login']
|
55
|
+
enterprise_password = @config['enterprise']['password']
|
56
|
+
enterprise_name = @config['enterprise']['name']
|
57
|
+
|
58
|
+
github = self.class.login_github(github_login, github_password)
|
59
|
+
fork_res = fork_on_github(github, user, repo)
|
60
|
+
clone_to_local(local_repo_base, fork_res.clone_url)
|
61
|
+
|
62
|
+
add_remote("#{local_repo_base}/#{fork_res.name}", "upstream", fork_res.parent.clone_url)
|
63
|
+
|
64
|
+
github_enterprise = self.class.login_github(enterprise_login, enterprise_password, enterprise_host)
|
65
|
+
|
66
|
+
create_res = new_repo_github(github_enterprise, fork_res.name,
|
67
|
+
"#{fork_res.parent.description} | Forked from #{fork_res.parent.html_url}",
|
68
|
+
fork_res.parent.homepage)
|
69
|
+
|
70
|
+
add_remote("#{local_repo_base}/#{fork_res.name}", enterprise_name, create_res.ssh_url)
|
71
|
+
|
72
|
+
push_remote("#{local_repo_base}/#{fork_res.name}", enterprise_name)
|
73
|
+
else
|
74
|
+
puts "No user and repo info from url: #{url}"
|
75
|
+
usage
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
## COMMANDS - end ##
|
80
|
+
|
81
|
+
def fork_on_github(github, user, repo)
|
82
|
+
begin
|
83
|
+
fork_res = github.repos.forks.create user, repo
|
84
|
+
if fork_res.status == 202
|
85
|
+
puts "#{user}/#{repo} forked on github.com"
|
86
|
+
return fork_res
|
87
|
+
else
|
88
|
+
puts fork_res.status
|
89
|
+
#TODO handle fork error
|
90
|
+
return fork_res
|
91
|
+
end
|
92
|
+
rescue Github::Error::GithubError => e
|
93
|
+
puts e.message
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def clone_to_local(base_path, url)
|
98
|
+
puts "Cloning #{url} under #{base_path}"
|
99
|
+
Dir.chdir(base_path) {
|
100
|
+
cmd = "git clone #{url}"
|
101
|
+
clone_res = `#{cmd}`
|
102
|
+
puts clone_res
|
103
|
+
}
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
def add_remote(repo_path, remote_name, url)
|
108
|
+
Dir.chdir(repo_path) {
|
109
|
+
cmd = "git remote add #{remote_name} #{url}"
|
110
|
+
addremote_res = `#{cmd}`
|
111
|
+
puts addremote_res
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
115
|
+
def push_remote(repo_path, remote_name)
|
116
|
+
puts "Pushing to #{remote_name}"
|
117
|
+
Dir.chdir(repo_path) {
|
118
|
+
cmd = "git push -u --all #{remote_name}"
|
119
|
+
push_res = `#{cmd}`
|
120
|
+
puts push_res
|
121
|
+
}
|
122
|
+
|
123
|
+
end
|
124
|
+
|
125
|
+
def new_repo_github(github, name, description, homepage)
|
126
|
+
begin
|
127
|
+
create_res = github.repos.create :name => name,
|
128
|
+
:description => description,
|
129
|
+
:homepage => homepage
|
130
|
+
if create_res.status == 201
|
131
|
+
puts "#{name} created on GitHub Enterprise"
|
132
|
+
return create_res
|
133
|
+
else
|
134
|
+
puts create_res.status
|
135
|
+
#TODO
|
136
|
+
return create_res
|
137
|
+
end
|
138
|
+
rescue Github::Error::GithubError => e
|
139
|
+
puts e.message
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def self.login_github(username, password, host = nil)
|
144
|
+
if host
|
145
|
+
puts "Login to #{host} ..."
|
146
|
+
return Github.new do |attr|
|
147
|
+
attr.endpoint = "https://#{host}/api/v3"
|
148
|
+
attr.login = username
|
149
|
+
attr.password = password
|
150
|
+
end
|
151
|
+
else
|
152
|
+
puts "Login to github.com ..."
|
153
|
+
return Github.new login: username, password: password
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def usage
|
158
|
+
puts <<-USAGE
|
159
|
+
Usage:
|
160
|
+
ghb fork <url: e.g. https://github.com/branky/github-bridge>
|
161
|
+
USAGE
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'github-bridge'
|
3
|
+
|
4
|
+
class TestBridge < Test::Unit::TestCase
|
5
|
+
def test_github_user_repo
|
6
|
+
expected = ['test-user','test-repo']
|
7
|
+
#html_url
|
8
|
+
result = GithubBridge.github_user_repo "https://github.com/test-user/test-repo"
|
9
|
+
assert_equal expected, result
|
10
|
+
#http_url
|
11
|
+
result = GithubBridge.github_user_repo "https://github.com/test-user/test-repo.git"
|
12
|
+
assert_equal expected, result
|
13
|
+
#ssh_url
|
14
|
+
result = GithubBridge.github_user_repo "git@github.com:test-user/test-repo.git"
|
15
|
+
assert_equal expected, result
|
16
|
+
|
17
|
+
result = GithubBridge.github_user_repo "http://a.invalid.url"
|
18
|
+
assert_equal [nil, nil], result
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_usage
|
23
|
+
GithubBridge.new(['-h']).usage
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_help
|
27
|
+
GithubBridge.new(['unknown']).help
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_login_github
|
31
|
+
github = GithubBridge.login_github('githubbridge', 'Github4me', 'github.com')
|
32
|
+
puts github.client
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: github-bridge
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 1.0.2
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Branky Shao
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2013-02-21 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bones
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: github_api
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: parseconfig
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
description: The bridge between public GitHub (github.com) and private GitHub (GitHub Enterprise, github.mycompany.com).
|
49
|
+
email: branky34@gmail.com
|
50
|
+
executables:
|
51
|
+
- ghb
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- LICENSE
|
60
|
+
- lib/github-bridge.rb
|
61
|
+
- bin/ghb
|
62
|
+
- test/test_github-bridge.rb
|
63
|
+
homepage: http://github.com/branky/github-bridge
|
64
|
+
licenses: []
|
65
|
+
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: "0"
|
83
|
+
requirements: []
|
84
|
+
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.25
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: The bridge between public GitHub (github.com) and private GitHub (GitHub Enterprise, github.mycompany.com).
|
90
|
+
test_files: []
|
91
|
+
|