sah 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -0
- data/lib/sah/api.rb +79 -0
- data/lib/sah/cli.rb +76 -140
- data/lib/sah/config.rb +8 -2
- data/lib/sah/version.rb +1 -1
- data/lib/sah.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df17335238e88fbccf43783368514990177ae205
|
4
|
+
data.tar.gz: 6391cde337e7e556bfb44d045a5cd86496c44c35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 10ee1527f31c4b973133b43c3c28cb68686ea73d6289510de7e58663ef123024fa6023967ea2ebf926cae8195970f0abcc9ce4942876ea048050d47578adeab8
|
7
|
+
data.tar.gz: 99db2c7d06eb40ed66f92f8f7fa618ffa8f6384163a1c2bcfdee7890a521181a4e645a4935bb3b2e495ed78706fe00ad8b52fd6eb63b2d7077e40856aa9abad4
|
data/README.md
CHANGED
@@ -103,6 +103,8 @@ If you use multiple stash, define profile(s) and specify it.
|
|
103
103
|
- `git config --global sah.config.upstream-prevent-push true`
|
104
104
|
(the same as `--prevent-push` option)
|
105
105
|
Setting this option to true will Prevent push to upstream repository.
|
106
|
+
- `git config --global sah.config.sah.git-protocol [ssh|http]`
|
107
|
+
Setting this option to specify git protocol. (default: ssh)
|
106
108
|
|
107
109
|
### user
|
108
110
|
|
data/lib/sah/api.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require "sah"
|
2
|
+
require 'faraday'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
|
5
|
+
module Sah
|
6
|
+
class API
|
7
|
+
attr_accessor :config, :conn
|
8
|
+
|
9
|
+
def initialize(config)
|
10
|
+
@config = config
|
11
|
+
|
12
|
+
base_url = (config.url).to_s.sub(/#{config.url.path}$/, '')
|
13
|
+
@conn = Faraday.new(url: base_url) do |faraday|
|
14
|
+
faraday.response :json
|
15
|
+
# faraday.response :logger
|
16
|
+
faraday.adapter Faraday.default_adapter
|
17
|
+
faraday.basic_auth config.user, config.password
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def fork_repo(project, repo, name=nil)
|
22
|
+
body = {slug: repo}
|
23
|
+
body = body.merge(name: name) if name
|
24
|
+
|
25
|
+
@conn.post do |req|
|
26
|
+
req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos/#{repo}"
|
27
|
+
req.headers['Content-Type'] = 'application/json'
|
28
|
+
req.body = body.to_json
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_repo(project, repo)
|
33
|
+
@conn.post do |req|
|
34
|
+
req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos"
|
35
|
+
req.headers['Content-Type'] = 'application/json'
|
36
|
+
req.body = {name: repo, scmId: "git", forkable: true}.to_json
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def list_project
|
41
|
+
@conn.get do |req|
|
42
|
+
req.url @config.url.path + "/rest/api/1.0/projects"
|
43
|
+
req.params['limit'] = 1000
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def show_project(project)
|
48
|
+
@conn.get do |req|
|
49
|
+
req.url @config.url.path + "/rest/api/1.0/projects/#{project}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def list_user
|
54
|
+
@conn.get do |req|
|
55
|
+
req.url @config.url.path + "/rest/api/1.0/users"
|
56
|
+
req.params['limit'] = 1000
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def show_user(user)
|
61
|
+
@conn.get do |req|
|
62
|
+
req.url @config.url.path + "/rest/api/1.0/users/#{user}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def list_repository(project)
|
67
|
+
@conn.get do |req|
|
68
|
+
req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos"
|
69
|
+
req.params['limit'] = 1000
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def show_repository(project, repository)
|
74
|
+
@conn.get do |req|
|
75
|
+
req.url @config.url.path + "/rest/api/1.0/projects/#{project}/repos/#{repository}"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
data/lib/sah/cli.rb
CHANGED
@@ -1,112 +1,9 @@
|
|
1
1
|
require "sah"
|
2
2
|
require "thor"
|
3
|
-
require 'faraday'
|
4
|
-
require 'faraday_middleware'
|
5
3
|
require 'hirb'
|
6
4
|
require 'hirb-unicode'
|
7
5
|
|
8
6
|
module Sah
|
9
|
-
class API
|
10
|
-
attr_accessor :conn
|
11
|
-
|
12
|
-
def initialize(config)
|
13
|
-
@conn = Faraday.new(url: config.url) do |faraday|
|
14
|
-
faraday.response :json
|
15
|
-
# faraday.response :logger
|
16
|
-
faraday.adapter Faraday.default_adapter
|
17
|
-
faraday.basic_auth config.user, config.password
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def fork_repo(project, repo, name=nil)
|
22
|
-
body = {slug: repo}
|
23
|
-
body = body.merge(name: name) if name
|
24
|
-
|
25
|
-
res = @conn.post do |req|
|
26
|
-
req.url "/rest/api/1.0/projects/#{project}/repos/#{repo}"
|
27
|
-
req.headers['Content-Type'] = 'application/json'
|
28
|
-
req.body = body.to_json
|
29
|
-
end
|
30
|
-
if res.status != 201
|
31
|
-
puts res.body["errors"].first["message"]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def create_repo(project, repo)
|
36
|
-
res = @conn.post do |req|
|
37
|
-
req.url "/rest/api/1.0/projects/#{project}/repos"
|
38
|
-
req.headers['Content-Type'] = 'application/json'
|
39
|
-
req.body = {name: repo, scmId: "git", forkable: true}.to_json
|
40
|
-
end
|
41
|
-
if res.status != 201
|
42
|
-
puts res.body["errors"].first["message"]
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def list_project
|
47
|
-
res = @conn.get do |req|
|
48
|
-
req.url "/rest/api/1.0/projects"
|
49
|
-
req.params['limit'] = 1000
|
50
|
-
end
|
51
|
-
if res.status != 200
|
52
|
-
puts res.body["errors"].first["message"]
|
53
|
-
end
|
54
|
-
res.body["values"].sort_by{|e| e["id"].to_i }
|
55
|
-
end
|
56
|
-
|
57
|
-
def show_project(project)
|
58
|
-
res = @conn.get do |req|
|
59
|
-
req.url "/rest/api/1.0/projects/#{project}"
|
60
|
-
end
|
61
|
-
if res.status != 200
|
62
|
-
puts res.body["errors"].first["message"]
|
63
|
-
end
|
64
|
-
res.body
|
65
|
-
end
|
66
|
-
|
67
|
-
def list_user
|
68
|
-
res = @conn.get do |req|
|
69
|
-
req.url "/rest/api/1.0/users"
|
70
|
-
req.params['limit'] = 1000
|
71
|
-
end
|
72
|
-
if res.status != 200
|
73
|
-
puts res.body["errors"].first["message"]
|
74
|
-
end
|
75
|
-
users = res.body["values"].sort_by{|e| e["id"].to_i }
|
76
|
-
end
|
77
|
-
|
78
|
-
def show_user(user)
|
79
|
-
res = @conn.get do |req|
|
80
|
-
req.url "/rest/api/1.0/users/#{user}"
|
81
|
-
end
|
82
|
-
if res.status != 200
|
83
|
-
puts res.body["errors"].first["message"]
|
84
|
-
end
|
85
|
-
res.body
|
86
|
-
end
|
87
|
-
|
88
|
-
def list_repository(project)
|
89
|
-
res = @conn.get do |req|
|
90
|
-
req.url "/rest/api/1.0/projects/#{project}/repos"
|
91
|
-
req.params['limit'] = 1000
|
92
|
-
end
|
93
|
-
if res.status != 200
|
94
|
-
puts res.body["errors"].first["message"]
|
95
|
-
end
|
96
|
-
repositories = (res.body["values"] || []).sort_by{|e| e["id"].to_i }
|
97
|
-
end
|
98
|
-
|
99
|
-
def show_repository(project, repository)
|
100
|
-
res = @conn.get do |req|
|
101
|
-
req.url "/rest/api/1.0/projects/#{project}/repos/#{repository}"
|
102
|
-
end
|
103
|
-
if res.status != 200
|
104
|
-
puts res.body["errors"].first["message"]
|
105
|
-
end
|
106
|
-
res.body
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
7
|
class CLI < Thor
|
111
8
|
class_option :profile,
|
112
9
|
type: :string, default: (ENV["SAH_DEFAULT_PROFILE"] || :default),
|
@@ -123,13 +20,16 @@ module Sah
|
|
123
20
|
sah clone ~USERNAME/REPO
|
124
21
|
\x5> git clone ssh://git@example.com:7999/~USERNAME/REPO
|
125
22
|
LONG_DESCRIPTION
|
126
|
-
def clone(
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
23
|
+
def clone(arg)
|
24
|
+
repository_slug, project_key = arg.split("/").reverse
|
25
|
+
project_key ||= "~#{config.user}"
|
26
|
+
res = api.show_repository(project_key, repository_slug)
|
27
|
+
if res.body.key? "errors"
|
28
|
+
abort res.body["errors"].first["message"]
|
29
|
+
end
|
30
|
+
repository = res.body
|
131
31
|
remote_url =
|
132
|
-
|
32
|
+
repository["links"]["clone"].find{ |e| e["name"] == config.git_protocol }["href"]
|
133
33
|
system "git", "clone", remote_url
|
134
34
|
end
|
135
35
|
|
@@ -147,12 +47,18 @@ module Sah
|
|
147
47
|
\x5# repository name is same as the current repository
|
148
48
|
LONG_DESCRIPTION
|
149
49
|
method_option :name, aliases: "-n", desc: "Set repository name"
|
150
|
-
def create(
|
151
|
-
|
152
|
-
|
50
|
+
def create(arg=nil)
|
51
|
+
project_key = (arg || "~#{config.user}")
|
52
|
+
repository_slug = (
|
153
53
|
options[:name] || File.basename(`git rev-parse --show-toplevel`).chomp
|
154
54
|
)
|
155
|
-
api.create_repo(
|
55
|
+
res = api.create_repo(project_key, repository_slug)
|
56
|
+
if res.body.key? "errors"
|
57
|
+
abort res.body["errors"].first["message"]
|
58
|
+
end
|
59
|
+
remote_url =
|
60
|
+
res.body["links"]["clone"].find{ |e| e["name"] == config.git_protocol }["href"]
|
61
|
+
system "git", "remote", "add", "origin", remote_url
|
156
62
|
end
|
157
63
|
|
158
64
|
desc "fork [REPO] [--name REPO_NAME]", "Fork repository"
|
@@ -171,15 +77,18 @@ module Sah
|
|
171
77
|
\x5# fork from ~USERNAME/REPO to ~YOUR_NAME/REPO
|
172
78
|
LONG_DESCRIPTION
|
173
79
|
method_option :name, aliases: "-n", desc: "Set repository name"
|
174
|
-
def fork(
|
175
|
-
if
|
176
|
-
|
80
|
+
def fork(arg=nil)
|
81
|
+
if arg
|
82
|
+
project_key, repository_slug = arg.split("/")
|
177
83
|
else
|
178
84
|
remote_url = `git config --get remote.origin.url`.chomp
|
179
85
|
remote_url.match %r%/([^/]+)/([^/]+?)(?:\.git)?$%
|
180
|
-
|
86
|
+
project_key, repository_slug = $1, $2
|
87
|
+
end
|
88
|
+
res = api.fork_repo(project_key, repository_slug, options[:name])
|
89
|
+
if res.body.key? "errors"
|
90
|
+
abort res.body["errors"].first["message"]
|
181
91
|
end
|
182
|
-
api.fork_repo(project, repo, options[:name])
|
183
92
|
end
|
184
93
|
|
185
94
|
desc "project [PROJECT]", "Show project information"
|
@@ -190,13 +99,21 @@ module Sah
|
|
190
99
|
sah project PROJECT
|
191
100
|
\x5# show project detail
|
192
101
|
LONG_DESCRIPTION
|
193
|
-
def project(
|
194
|
-
if
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
102
|
+
def project(arg=nil)
|
103
|
+
if arg
|
104
|
+
res = api.show_project(arg)
|
105
|
+
if res.body.key? "errors"
|
106
|
+
abort res.body["errors"].first["message"]
|
107
|
+
end
|
108
|
+
project = res.body
|
199
109
|
puts Hirb::Helpers::AutoTable.render(project, headers: false)
|
110
|
+
else
|
111
|
+
res = api.list_project
|
112
|
+
if res.body.key? "errors"
|
113
|
+
abort res.body["errors"].first["message"]
|
114
|
+
end
|
115
|
+
projects = res.body["values"].sort_by{|e| e["id"].to_i }
|
116
|
+
puts Hirb::Helpers::AutoTable.render(projects, fields: %w(id key name description))
|
200
117
|
end
|
201
118
|
end
|
202
119
|
|
@@ -208,14 +125,22 @@ module Sah
|
|
208
125
|
sah repository PROJECT/REPO
|
209
126
|
\x5# show repository detail
|
210
127
|
LONG_DESCRIPTION
|
211
|
-
def repository(
|
212
|
-
|
213
|
-
if
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
128
|
+
def repository(arg)
|
129
|
+
project_key, repository_slug = arg.split("/")
|
130
|
+
if repository_slug
|
131
|
+
res = api.show_repository(project_key, repository_slug)
|
132
|
+
if res.body.key? "errors"
|
133
|
+
abort res.body["errors"].first["message"]
|
134
|
+
end
|
135
|
+
repository = res.body
|
218
136
|
puts Hirb::Helpers::AutoTable.render(repository, headers: false)
|
137
|
+
else
|
138
|
+
res = api.list_repository(project_key)
|
139
|
+
if res.body.key? "errors"
|
140
|
+
abort res.body["errors"].first["message"]
|
141
|
+
end
|
142
|
+
repositories = (res.body["values"] || []).sort_by{|e| e["id"].to_i }
|
143
|
+
puts Hirb::Helpers::AutoTable.render(repositories, fields: %w(id slug name))
|
219
144
|
end
|
220
145
|
end
|
221
146
|
|
@@ -235,10 +160,13 @@ module Sah
|
|
235
160
|
remote_url = `git config --get remote.origin.url`.chomp
|
236
161
|
remote_url.match %r%/([^/]+)/([^/]+?)(?:\.git)?$%
|
237
162
|
project, repository = $1, $2
|
238
|
-
|
239
|
-
|
163
|
+
res = api.show_repository(project, repository)
|
164
|
+
if res.body.key? "errors"
|
165
|
+
abort res.body["errors"].first["message"]
|
166
|
+
end
|
167
|
+
repository = res.body
|
240
168
|
upstream_url =
|
241
|
-
|
169
|
+
repository["origin"]["links"]["clone"].find{ |e| e["name"] == config.git_protocol }["href"]
|
242
170
|
if options[:"add-remote"]
|
243
171
|
system "git", "remote", "add", "upstream", upstream_url
|
244
172
|
if config.upstream_fetch_pull_request || options[:"fetch-pull-request"]
|
@@ -261,13 +189,21 @@ module Sah
|
|
261
189
|
sah user USER
|
262
190
|
\x5# show user detail
|
263
191
|
LONG_DESCRIPTION
|
264
|
-
def user(
|
265
|
-
if
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
192
|
+
def user(arg=nil)
|
193
|
+
if arg
|
194
|
+
res = api.show_user(arg)
|
195
|
+
if res.body.key? "errors"
|
196
|
+
abort res.body["errors"].first["message"]
|
197
|
+
end
|
198
|
+
user = res.body
|
270
199
|
puts Hirb::Helpers::AutoTable.render(user, headers: false)
|
200
|
+
else
|
201
|
+
res = api.list_user
|
202
|
+
if res.body.key? "errors"
|
203
|
+
abort res.body["errors"].first["message"]
|
204
|
+
end
|
205
|
+
users = (res.body["values"] || []).sort_by{|e| e["id"].to_i }
|
206
|
+
puts Hirb::Helpers::AutoTable.render(users, fields: %w(id slug name displayName))
|
271
207
|
end
|
272
208
|
end
|
273
209
|
|
data/lib/sah/config.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
1
3
|
module Sah
|
2
4
|
class Config
|
3
5
|
attr_accessor :user, :password, :url,
|
4
|
-
:upstream_fetch_pull_request, :upstream_prevent_push
|
6
|
+
:upstream_fetch_pull_request, :upstream_prevent_push,
|
7
|
+
:git_protocol
|
5
8
|
|
6
9
|
def initialize(profile)
|
7
10
|
@user, @password, @url = nil, nil, nil
|
8
11
|
@upstream_fetch_pull_request = false
|
9
12
|
@upstream_prevent_push = false
|
13
|
+
@git_protocol = "ssh"
|
10
14
|
|
11
15
|
profile_prefix = "sah\.profile\.#{profile}"
|
12
16
|
config_prefix = "sah\.config"
|
@@ -18,11 +22,13 @@ module Sah
|
|
18
22
|
when /#{profile_prefix}\.password (.*)$/
|
19
23
|
@password = $1
|
20
24
|
when /#{profile_prefix}\.url (.*)$/
|
21
|
-
@url = $1
|
25
|
+
@url = URI.parse $1
|
22
26
|
when /#{config_prefix}\.upstream-fetch-pull-request (.*)$/
|
23
27
|
@upstream_fetch_pull_request = ($1 == "true")
|
24
28
|
when /#{config_prefix}\.upstream-prevent-push (.*)$/
|
25
29
|
@upstream_prevent_push = ($1 == "true")
|
30
|
+
when /#{config_prefix}\.git-protocol (.*)$/
|
31
|
+
@git_protocol = $1
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
data/lib/sah/version.rb
CHANGED
data/lib/sah.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sah
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- f440
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -154,6 +154,7 @@ files:
|
|
154
154
|
- bin/setup
|
155
155
|
- exe/sah
|
156
156
|
- lib/sah.rb
|
157
|
+
- lib/sah/api.rb
|
157
158
|
- lib/sah/cli.rb
|
158
159
|
- lib/sah/config.rb
|
159
160
|
- lib/sah/version.rb
|