gitee-cli 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3152b7535b6e704388456ee6d86f841afa554e54722141e8b7dff4e6b17b9d51
4
- data.tar.gz: a3c514a46ad51ceae1c315c74154a3ac51c56160e133612da4895d0d24ab4b65
3
+ metadata.gz: 39860a8249020b6e74c1eacd9946717adb47c63dfb8f70f9deaa258f600a7448
4
+ data.tar.gz: b3a7435c1690810d877c32ba94d0e9f3b00dca2bf822f4675d4a7828248f2f79
5
5
  SHA512:
6
- metadata.gz: ecc8fa0820605bedb7af26f7a9227e2a91a654d2a0a01ad342793dfbd761cb975dbef826e1e749ab6eed3d66d39fe37f98079dfefaddae67d0131a30e197718c
7
- data.tar.gz: ed461d6cac655cf23daad68b271b53b36d2818103d25e93cc1f63aec598cfd042b23dbaa69efb78c00eaedad96e4486a2b2ec0b098c24d18c7b1f6d41ff50197
6
+ metadata.gz: 257b5dd80bd85e09afade814b48ae956398a8e861f157016e889c1ffe46c3f6d3f459baf188c330b29bc9ea3e7c290ab4cc73454fae7ff5e22812d163cdab420
7
+ data.tar.gz: d066127d841af74c3b735e527d428d405b49ffbbef1cbb4c62e26ca3a41056ce1c907ac64ad03112ca8b808d6112f107f6161c20bc9c62a8b1e62b44b222faec
data/CHANGELOG.md CHANGED
@@ -1,6 +1,30 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased](#) (2023-05-19)
3
+ ## [Unreleased](#) (2023-05-31)
4
+
5
+ <br>
6
+
7
+ ## [v0.2.3](#) (2023-05-31)
8
+
9
+ ### New features:
10
+
11
+ - Support
12
+
13
+ - `gist:list`
14
+ - `gist:create`
15
+
16
+ ### Enhancements:
17
+
18
+ - Use similar output as `gh` for `repo:create` and `gist:create`
19
+
20
+ <br>
21
+
22
+ ## [v0.2.2](#) (2023-05-19)
23
+
24
+ ### Enhancements:
25
+
26
+ - Distinguish `gitee help` from `gitee -h`
27
+ - Use `standard_path` to make corss-platform path
4
28
 
5
29
  <br>
6
30
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Gitee CLI in Ruby
1
+ # Gitee CLI
2
2
 
3
- ![Banner](./images/banner.jpg)
3
+ ![Banner](https://gitee.com/ccmywish/gitee-cli/raw/main/images/banner.jpg)
4
4
 
5
5
  Work with [Gitee](https://gitee.com/) from the command line.
6
6
 
@@ -12,9 +12,13 @@ $ gem install gitee-cli
12
12
 
13
13
  ## Usage
14
14
 
15
- 开发/维护文档: https://rubydoc.info/gems/gitee-cli
15
+ > **Note**
16
+ > 1. 此客户端当前仅在`Windows`上测试运行良好。`Linux`和`macOS`理论上可运行。
17
+ > 2. 命令行接口与GitHub的`gh`不太一致
18
+ > 3. 当前API仅实现了较小一部分(我目前需要的)
19
+ > 4. 如果您想要使`gitee`命令与`gh`命令保持兼容,或者需要提供对多平台的支持,或者发现有API需要补充,请考虑捐赠此项目并发起issue。
16
20
 
17
- <br>
21
+ ---
18
22
 
19
23
  使用`gitee-cli`需要`Gitee Access Token`,我们可以通过以下两种方式实现:
20
24
 
@@ -43,7 +47,7 @@ http://localhost
43
47
 
44
48
  # Logo上传gitee-cli的logo即可
45
49
  # 使用下面命令下载
46
- curl -fsSL https://gitee.com/ccmywish/gitee-cli/raw/main/images/AppLogo.jpg -o ~/Desktop/GiteeCLIAppLogo.jpg
50
+ curl -fsSL https://gitee.com/ccmywish/gitee-cli/raw/main/images/AppLogo.jpg -o GiteeCLIAppLogo.jpg
47
51
  ```
48
52
 
49
53
  注册完成后,我们会得到`Client ID`和`Client Secret`。最后一步,在命令行上运行:
@@ -58,7 +62,7 @@ $ gitee auth:apply[id, secret]
58
62
 
59
63
  ### 方式2(临时)
60
64
 
61
- 如果您只想临时使用,或者不方便将token等关键信息存储在本机,请运行:
65
+ 如果您只想临时使用,或者不方便将`token`等关键信息存储在本机,请运行:
62
66
  ```bash
63
67
  $ gitee auth:temp_apply
64
68
  ```
@@ -72,7 +76,9 @@ $ gitee repo:branches[ccmywish, gitee-cli] GITEE_TOKEN=xxx
72
76
  ## 命令行
73
77
 
74
78
  ```bash
75
- gitee -T [pattern] # 显示可执行命令,可以使用pattern来搜索相关命令
79
+ gitee -h # 快速帮助
80
+
81
+ gitee help [pattern] # 显示可执行命令,可以使用pattern来搜索相关命令
76
82
 
77
83
  gitee auth:apply[id,secret] # Apply for an access token using Gitee app
78
84
  gitee auth:temp_apply # Apply for an access token temporarily
@@ -1,15 +1,17 @@
1
1
  # ---------------------------------------------------------------
2
- # File : Rakefile
2
+ # File : front.rb
3
3
  # Authors : Aoran Zeng <ccmywish@qq.com>
4
4
  # Created on : <2023-04-29>
5
- # Last modified : <2023-05-08>
5
+ # Last modified : <2023-05-31>
6
6
  #
7
- # Rakefile:
7
+ # front:
8
8
  #
9
9
  # This file makes use of Rake and handle results of API calls
10
- #
11
10
  # ---------------------------------------------------------------
12
11
 
12
+ require 'rainbow/refinement'
13
+ using Rainbow
14
+
13
15
  # desc "Print help" # Comment to hide this in help info
14
16
  task :help do end # Just an empty task to be specified by default
15
17
 
@@ -18,9 +20,9 @@ namespace "repo" do
18
20
  desc "Create a repository under your space"
19
21
  task :create, [:repo] do |t, repo:|
20
22
  res = Gitee::Gitee4CLI::API::Repositories.create(repo)
21
- if res.successful?
22
- cont = res.content
23
- puts "Created successfully! Please visit: #{cont['html_url']}"
23
+ if res.success?
24
+ puts "#{'✓'.green} Created repository #{repo.blue} on Gitee"
25
+ puts res.content['html_url']
24
26
  else
25
27
  $stderr.puts "Error: " + res.why_failed
26
28
  end
@@ -30,8 +32,8 @@ namespace "repo" do
30
32
  desc "Delete the '<user>/<repo>' repository"
31
33
  task :delete, [:user, :repo] do |t, user:, repo:|
32
34
  res = Gitee::Gitee4CLI::API::Repositories.delete(user,repo)
33
- if res.successful?
34
- puts "Deleted successfully!"
35
+ if res.success?
36
+ puts "Deleted repository #{user}}/#{repo} successfully!"
35
37
  else
36
38
  $stderr.puts "Error: " + res.why_failed
37
39
  end
@@ -42,7 +44,7 @@ namespace "repo" do
42
44
  task :branches, [:user, :repo] do |t, args|
43
45
  user, repo = args.user, args.repo
44
46
  res = Gitee::Gitee4CLI::API::Repositories.all_branches(user, repo)
45
- if res.successful?
47
+ if res.success?
46
48
  res.content.each do |br|
47
49
  puts br['name']
48
50
  end
@@ -55,7 +57,7 @@ namespace "repo" do
55
57
  desc "View the repository's README"
56
58
  task :view, [:user, :repo] do |t, user:, repo:|
57
59
  res = Gitee::Gitee4CLI::API::Repositories.readme(user, repo)
58
- if res.successful?
60
+ if res.success?
59
61
  require 'base64'
60
62
  require 'tty-markdown'
61
63
  # res.content['content'] is UTF-8
@@ -73,7 +75,7 @@ namespace "repo" do
73
75
  task :list, [:user] do |t, args|
74
76
  user = args.user
75
77
  res = Gitee::Gitee4CLI::API::Repositories.list_for_user(user)
76
- if res.successful?
78
+ if res.success?
77
79
  # res.content is_a? Array => true
78
80
  res.content.each_with_index do |repo, i|
79
81
  print "#{i+1}. ".ljust(3), repo['full_name'].ljust(30)
@@ -114,7 +116,7 @@ namespace "auth" do
114
116
  task :apply, [:id, :secret] do |t, id:, secret:|
115
117
  redirect_uri = "http://localhost"
116
118
  res = Gitee::Gitee4CLI::API::Authorization.apply(id, secret, redirect_uri)
117
- if res.successful?
119
+ if res.success?
118
120
  puts "Success to apply, you can run any command now"
119
121
  else
120
122
  $stderr.puts "Error: " + res.why_failed
@@ -123,4 +125,61 @@ namespace "auth" do
123
125
 
124
126
  end
125
127
 
128
+ namespace "issue" do
129
+ desc "List issues in a repository"
130
+ task :list, [:user, :repo] do |t, args|
131
+ user, repo = args.user, args.repo
132
+ res1 = Gitee::Gitee4CLI::API::Issues.all(user, repo, 'open')
133
+ res2 = Gitee::Gitee4CLI::API::Issues.all(user, repo, 'progressing')
134
+ printer = lambda do |res, state, color|
135
+ if res.success?
136
+ res.content.each do |issue|
137
+ print "[#{state}]".bold.send(color), ' '
138
+ puts issue['title']
139
+ puts issue['url']
140
+ puts
141
+ end
142
+ else
143
+ $stderr.puts "Error: " + res.why_failed
144
+ end
145
+ end
146
+ printer.call res1, '待处理', 'yellow'
147
+ printer.call res2, '进行中', 'green'
148
+ end
149
+ end
150
+
151
+
152
+ namespace "gist" do
153
+
154
+ desc "Create a new gist"
155
+ task :create, [:path, :desc] do |t, args|
156
+ path, desc = args.path, args.desc
157
+ require 'pathname'
158
+ path = Pathname.new path
159
+
160
+ res = Gitee::Gitee4CLI::API::Gists.create(path, desc)
161
+ if res.success?
162
+ puts "#{'✓'.green} Created gist #{path.basename.to_s.blue}"
163
+ puts res.content['html_url']
164
+ else
165
+ $stderr.puts "Error: " + res.why_failed
166
+ end
167
+ end
168
+
169
+
170
+ desc "List your gists"
171
+ task :list do |t|
172
+ res = Gitee::Gitee4CLI::API::Gists.list
173
+ if res.success?
174
+ res.content.each_with_index do |gist, i|
175
+ puts gist['description']
176
+ puts gist['html_url']
177
+ puts
178
+ end
179
+ else
180
+ $stderr.puts "Error: " + res.why_failed
181
+ end
182
+ end
183
+ end
184
+
126
185
  task default: :help
data/lib/gitee/cli.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # File : cli.rb
3
3
  # Authors : Aoran Zeng <ccmywish@qq.com>
4
4
  # Created on : <2023-04-28>
5
- # Last modified : <2023-05-19>
5
+ # Last modified : <2023-05-31>
6
6
  #
7
7
  # cli:
8
8
  #
@@ -10,12 +10,13 @@
10
10
  # ---------------------------------------------------------------
11
11
 
12
12
  require 'rake'
13
+ require 'standard_path'
13
14
 
14
15
  # Add method for Rake application
15
16
  #
16
17
  class Rake::Application
17
18
  def run_gitee_cli(args)
18
- opt = ['--rakefile', File.join(__FILE__, '..', 'cli', 'Rakefile') ]
19
+ opt = ['--rakefile', File.join(__FILE__, '..', 'cli', 'front.rb') ]
19
20
  standard_exception_handling do
20
21
  argv = args.push *opt
21
22
  init "gitee", argv
@@ -31,9 +32,9 @@ module Gitee
31
32
  module CLI
32
33
 
33
34
  # gitee-cli version
34
- VERSION = "0.2.1"
35
+ VERSION = "0.2.3"
35
36
 
36
- DEFAULT_TOKEN_DIR = File.join ENV['AppData'], "Gitee CLI in Ruby"
37
+ DEFAULT_TOKEN_DIR = StandardPath.app_data "Gitee CLI in Ruby"
37
38
  DEFAULT_TOKEN_FILE = File.join DEFAULT_TOKEN_DIR, "sec.json"
38
39
 
39
40
  # Read token from local or environment variable
@@ -41,14 +42,14 @@ module Gitee
41
42
  # @return [Array<String, String>] Arrary of access token and refresh token for Gitee API
42
43
  #
43
44
  # @note The token file is a JSON file, with the content being like:
44
- # {
45
- # "access_token": "xxxxxxxxxxxxxxxxxxxxxx",
46
- # "token_type": "bearer",
47
- # "expires_in": 86400,
48
- # "refresh_token": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
49
- # "scope": "user_info projects",
50
- # "created_at": 1683393781
51
- # }
45
+ # {
46
+ # "access_token": "xxxxxxxxxxxxxxxxxxxxxx",
47
+ # "token_type": "bearer",
48
+ # "expires_in": 86400,
49
+ # "refresh_token": "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
50
+ # "scope": "user_info projects",
51
+ # "created_at": 1683393781
52
+ # }
52
53
  #
53
54
  def self.get_token
54
55
  if tok = ENV['GITEE_TOKEN']
@@ -82,7 +83,7 @@ module Gitee
82
83
  def self.run
83
84
  argv = ARGV.dup
84
85
 
85
- help_opts = ['-h', '-H', '--help', 'help']
86
+ help_opts = ['-h', '-H', '--help']
86
87
  version_opts = ['-v', '-V', '--version']
87
88
 
88
89
  if argv.empty?
@@ -92,6 +93,11 @@ module Gitee
92
93
  argv -= help_opts
93
94
  puts help_banner
94
95
  argv << '-T'
96
+ elsif argv[0] == 'help'
97
+ help_what = argv[1]
98
+ argv.clear
99
+ argv << '-D'
100
+ argv << help_what if not help_what.nil?
95
101
  elsif argv.intersect? version_opts
96
102
  argv -= version_opts
97
103
  puts version_banner
@@ -2,21 +2,26 @@
2
2
  # File : api.rb
3
3
  # Authors : Aoran Zeng <ccmywish@qq.com>
4
4
  # Created on : <2023-04-28>
5
- # Last modified : <2023-05-08>
5
+ # Last modified : <2023-05-31>
6
6
  #
7
7
  # api:
8
8
  #
9
9
  # APIs for Gitee
10
- #
11
10
  # ---------------------------------------------------------------
12
11
 
12
+ module Gitee
13
+ module Gitee4CLI
14
+ URL = "https://gitee.com"
15
+ end
16
+ end
17
+
13
18
  # All APIs reside here
14
19
  #
15
20
  # @!macro [new] param_owner
16
- # @param [String] owner 仓库所属空间地址(企业、组织或个人的地址path)
21
+ # @param owner [String] 仓库所属空间地址(企业、组织或个人的地址path)
17
22
  #
18
23
  # @!macro [new] param_repo
19
- # @param [String] repo 仓库路径(path)
24
+ # @param repo [String] 仓库路径(path)
20
25
  #
21
26
  module Gitee::Gitee4CLI::API
22
27
 
@@ -38,19 +43,19 @@ module Gitee::Gitee4CLI::API
38
43
 
39
44
  # New a Result object with successful info and its content or failed reason
40
45
  #
41
- # @param [Boolean] successful If the API call succeeded
42
- # @param [String] generic Content or failed reason
46
+ # @param success_or_fail [Boolean] If the API call succeeded
47
+ # @param generic [String] Content or failed reason
43
48
  #
44
- def initialize(successful, generic)
45
- @status = successful
46
- if successful
49
+ def initialize(success_or_fail, generic)
50
+ @status = success_or_fail
51
+ if @status
47
52
  @content = generic
48
53
  else
49
54
  @why_failed = generic
50
55
  end
51
56
  end
52
57
 
53
- def successful?
58
+ def success?
54
59
  @status
55
60
  end
56
61
 
@@ -98,7 +103,7 @@ module Gitee::Gitee4CLI::API
98
103
  #
99
104
  # @return [String] New access token
100
105
  #
101
- # @HTTP [POST] https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token={refresh_token}
106
+ # @HTTP [POST] https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token={refresh_token}
102
107
  #
103
108
  def refresh_access_token
104
109
  url = Gitee::Gitee4CLI::URL + "/oauth/token?grant_type=refresh_token"
@@ -135,8 +140,8 @@ module Gitee::Gitee4CLI::API
135
140
  # Make a direct request, not being wrapped
136
141
  #
137
142
  # @return
138
- # 1. [Status] `@status` status
139
- # 2. [String] `@body` response body string
143
+ # 1. `@status` [Status] status
144
+ # 2. `@body` [String] response body string
140
145
  #
141
146
  # @note 这个函数不同于`make_api_request`,后者经过包装,请求路径均相对于API,会读取本地的token。
142
147
  # 而此函数的请求将相对于Gitee根路径,不会读取本地token。
@@ -145,7 +150,7 @@ module Gitee::Gitee4CLI::API
145
150
  require 'http'
146
151
  url = [Gitee::Gitee4CLI::URL, @path].join('/')
147
152
  instance_exec {
148
- res = HTTP.send @action.to_sym, url, :form => @params
153
+ res = HTTP.send @method.to_sym, url, :form => @params
149
154
  @status, @body = res.status, res.to_s
150
155
  }
151
156
  end
@@ -153,8 +158,8 @@ module Gitee::Gitee4CLI::API
153
158
  # Make a request for some specific API
154
159
  #
155
160
  # @return
156
- # 1. [Status] `@status` status
157
- # 2. [String] `@body` response body string
161
+ # 1. `@status` [Status] status
162
+ # 2. `@body` [String] response body string
158
163
  #
159
164
  # @note This is only called by module functions
160
165
  #
@@ -190,7 +195,15 @@ module Gitee::Gitee4CLI::API
190
195
 
191
196
  loop do
192
197
 
193
- res = HTTP.send @action.to_sym, url, :form => @params
198
+ if @json_data
199
+ res = HTTP.send @method.to_sym, url, :json => @json_data
200
+ elsif @form_data
201
+ res = HTTP.send @method.to_sym, url, :form => @form_data
202
+ else
203
+ # Always use ':form' if we directly use a simple @params
204
+ res = HTTP.send @method.to_sym, url, :form => @params
205
+ end
206
+
194
207
  @status, @body = res.status, res.to_s
195
208
 
196
209
  break if @status.code != HttpStatusCode::Unauthorized
@@ -212,11 +225,12 @@ module Gitee::Gitee4CLI::API
212
225
  $stderr.puts <<~ERR
213
226
  Error: The local access token is not outdated (Will expire at #{will_expire_at_for_HUMAN}).
214
227
  But Gitee returned '401 Unauthorized', so we've refreshed access token and re-request.
215
- However Gitee returned the same again. Please check the refresh token."
228
+ However Gitee returned the same again. Please check the refresh token.
216
229
  ERR
217
230
  end
231
+ puts
218
232
  err_json_hash.each do |k, v|
219
- $stderr.puts " #{k}: #{v}"
233
+ $stderr.puts " #{k}: #{v}"
220
234
  end
221
235
  exit
222
236
  end
@@ -231,6 +245,25 @@ module Gitee::Gitee4CLI::API
231
245
 
232
246
  end
233
247
 
248
+
249
+ # Generate(Return) successful or failed response to pass to front
250
+ #
251
+ # @param expected_success_status_code [Integer]
252
+ #
253
+ # @return [Result] result
254
+ #
255
+ def return_response_which_expect(expected_success_status_code)
256
+ body = JSON.parse @body
257
+ if @status.code == expected_success_status_code
258
+ Result[true, body]
259
+ else
260
+ $stderr.print "Response Error Message: "
261
+ # force_debug @body
262
+ $stderr.puts body['message']
263
+ Result[false, @status.to_s]
264
+ end
265
+ end
266
+
234
267
  end
235
268
 
236
269
  # Make it only accessible from inside
@@ -247,15 +280,21 @@ module Gitee::Gitee4CLI::API
247
280
 
248
281
  # Apply for a for a Gitee Application
249
282
  #
250
- # @param [String] Client ID
251
- # @param [String] Client Secret
252
- # @param [String] Redirect URI
283
+ # @param client_id [String] Client ID
284
+ # @param client_secret [String] Client Secret
285
+ # @param redirect_uri [String] Redirect URI
253
286
  #
254
287
  # @return [Result] JSON响应体
255
288
  #
256
- # @HTTP [GET] https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code This is the first step, launched in web browser
289
+ # @HTTP [GET]
290
+ # URL:
291
+ # https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code
292
+ # This is the first step, launched in web browser
257
293
  #
258
- # @HTTP [POST] https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret} This is the second step, posted by ourself
294
+ # @HTTP [POST]
295
+ # URL:
296
+ # https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
297
+ # This is the second step, posted by ourself
259
298
  #
260
299
  def self.apply(client_id, client_secret, redirect_uri)
261
300
 
@@ -299,7 +338,7 @@ module Gitee::Gitee4CLI::API
299
338
  code: grant_auth_code,
300
339
  client_id:, redirect_uri:, client_secret:
301
340
  }
302
- @action = 'post'
341
+ @method = 'post'
303
342
  make_direct_request
304
343
 
305
344
  # @body is the JSON string
@@ -328,12 +367,13 @@ module Gitee::Gitee4CLI::API
328
367
  #
329
368
  # @!macro param_repo
330
369
  # @!macro param_owner
331
- # @param [String] sha 文件的 Blob SHA,可通过[获取仓库具体路径下的内容(tree)]API获取
332
- # @param [Boolean] recursive 赋值为1递归获取目录
333
370
  #
334
- # @return [String] JSON响应体
371
+ # @param sha [String] 文件的 Blob SHA,可通过[获取仓库具体路径下的内容(tree)]API获取
372
+ # @param recursive [Boolean] 赋值为1递归获取目录
373
+ #
374
+ # @return [String] JSON响应体
335
375
  #
336
- # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/git/blobs/{sha}
376
+ # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/git/blobs/{sha}
337
377
  #
338
378
  def self.blob(owner, repo, sha, recursive = true)
339
379
  access_token = get_token
@@ -341,20 +381,20 @@ module Gitee::Gitee4CLI::API
341
381
  @params = {access_token:, recursive:}
342
382
  @path = sprintf "repos/%s/%s/git/blobs/%s", owner, repo, sha
343
383
  make_api_request
384
+ return_response_which_expect HttpStatusCode::Ok
344
385
  end
345
386
 
346
387
  # Get directory Tree
347
388
  #
348
389
  # @!macro param_owner
349
390
  # @!macro param_repo
350
- # @param [String] sha 可以是分支名(如master)、Commit或者目录Tree的SHA值
351
- # @param [Boolean] recursive 赋值为1递归获取目录
352
391
  #
353
- # @return [Result] JSON响应体
392
+ # @param sha [String] 可以是分支名(如master)、Commit或者目录Tree的SHA值
393
+ # @param recursive [Boolean] 赋值为1递归获取目录
354
394
  #
355
- # @HTTP [Get] https://gitee.com/api/v5/repos/{owner}/{repo}/git/trees/{sha}
395
+ # @return [Result] JSON响应体
356
396
  #
357
- # @example [URL] https://gitee.com/api/v5/repos/ccmywish/gitee-cli/git/trees/main?access_token=abc
397
+ # @HTTP [Get] https://gitee.com/api/v5/repos/{owner}/{repo}/git/trees/{sha}
358
398
  #
359
399
  def self.tree(owner, repo, sha, recursive = true)
360
400
  access_token = get_token
@@ -371,18 +411,16 @@ module Gitee::Gitee4CLI::API
371
411
  #
372
412
  # @return [Result] JSON响应体
373
413
  #
374
- # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/git/gitee_metrics
375
- #
376
- # @example [URL] https://gitee.com/api/v5/repos/ccmywish/gitee-cli/git/gitee_metrics?access_token=abc
414
+ # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/git/gitee_metrics
377
415
  #
378
416
  def self.gitee_metrics(owner, repo)
379
417
  access_token = get_token
380
418
  @params = {access_token:}
381
419
  @path = sprintf "repos/%s/%s/git/gitee_metrics", owner, repo
382
- @action = 'get'
420
+ @method = 'get'
383
421
  make_api_request
422
+ return_response_which_expect HttpStatusCode::Ok
384
423
  end
385
-
386
424
  end
387
425
 
388
426
 
@@ -401,40 +439,36 @@ module Gitee::Gitee4CLI::API
401
439
  # @!macro param_owner
402
440
  # @!macro param_repo
403
441
  #
404
- # @return [Result] JSON响应体
442
+ # @return [Result] JSON响应体
405
443
  #
406
- # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/branches
444
+ # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/branches
407
445
  #
408
446
  def self.all_branches(owner, repo)
409
447
  access_token = get_token
410
448
  @params = {access_token:}
411
449
  @path = sprintf "repos/%s/%s/branches", owner, repo
412
- @action = 'get'
450
+ @method = 'get'
413
451
  make_api_request
414
- if @status.code == 200
415
- # 'json' is already required somewhere, maybe from 'http.rb'
416
- Result[true, JSON.parse(@body)]
417
- else
418
- Result[false, @status.to_s]
419
- end
452
+ return_response_which_expect HttpStatusCode::Ok
420
453
  end
421
454
 
422
455
  # @todo
423
456
  # Create a new branch from a owner's repo
424
457
  #
425
- #
426
458
  # @!macro param_owner
427
459
  # @!macro param_repo
428
460
  #
429
- # @return [Result] JSON响应体
461
+ # @return [Result] JSON响应体
430
462
  #
431
- # @HTTP [POST] https://gitee.com/api/v5/repos/{owner}/{repo}/branches
463
+ # @HTTP [POST] https://gitee.com/api/v5/repos/{owner}/{repo}/branches
432
464
  #
433
465
  def self.new_branch(owner, repo)
434
466
  access_token = get_token
435
467
  @params = {access_token:}
436
468
  @path = sprintf "repos/%s/%s/branches", owner, repo
469
+ @method = 'post'
437
470
  make_api_request
471
+ return_response_which_expect HttpStatusCode::Created
438
472
  # TODO post the request
439
473
  end
440
474
 
@@ -443,18 +477,20 @@ module Gitee::Gitee4CLI::API
443
477
  #
444
478
  # @!macro param_owner
445
479
  # @!macro param_repo
446
- # @param [String] branch 分支名称
447
480
  #
448
- # @return [Result] JSON响应体
481
+ # @param branch [String] 分支名称
449
482
  #
450
- # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/branches/{branch}
483
+ # @return [Result] JSON响应体
484
+ #
485
+ # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/branches/{branch}
451
486
  #
452
487
  def self.get_branch(owner, repo, branch)
453
488
  access_token = get_token
454
489
  @params = {access_token:}
455
490
  @path = sprintf "repos/%s/%s/branches/%s", owner, repo, branch
456
- @action = 'get'
491
+ @method = 'get'
457
492
  make_api_request
493
+ return_response_which_expect HttpStatusCode::Ok
458
494
  end
459
495
 
460
496
  # Delete a owner's repo
@@ -464,19 +500,15 @@ module Gitee::Gitee4CLI::API
464
500
  #
465
501
  # @return [Result] JSON响应体
466
502
  #
467
- # @HTTP [DELETE] https://gitee.com/api/v5/repos/{owner}/{repo}
503
+ # @HTTP [DELETE] https://gitee.com/api/v5/repos/{owner}/{repo}
468
504
  #
469
505
  def self.delete(owner, repo)
470
506
  access_token = get_token
471
507
  @params = {access_token:}
472
508
  @path = sprintf "repos/%s/%s", owner, repo
473
- @action = 'delete'
509
+ @method = 'delete'
474
510
  make_api_request
475
- if @status.code == HttpStatusCode::NoContent
476
- Result[true, nil]
477
- else
478
- Result[false, @status.to_s]
479
- end
511
+ return_response_which_expect HttpStatusCode::NoContent
480
512
  end
481
513
 
482
514
  # @todo
@@ -487,13 +519,13 @@ module Gitee::Gitee4CLI::API
487
519
  #
488
520
  # @return [String] JSON响应体
489
521
  #
490
- # @HTTP [PUT] https://gitee.com/api/v5/repos/{owner}/{repo}/clear
522
+ # @HTTP [PUT] https://gitee.com/api/v5/repos/{owner}/{repo}/clear
491
523
  #
492
524
  def self.clear(owner, repo)
493
525
  access_token = get_token
494
526
  @params = {access_token:}
495
527
  @path = sprintf "repos/%s/%s/clear", owner, repo
496
- @action = 'put'
528
+ @method = 'put'
497
529
  make_api_request
498
530
  # TODO: put
499
531
  end
@@ -504,19 +536,15 @@ module Gitee::Gitee4CLI::API
504
536
  #
505
537
  # @return [Result] JSON响应体
506
538
  #
507
- # @HTTP [POST] https://gitee.com/api/v5/user/repos
539
+ # @HTTP [POST] https://gitee.com/api/v5/user/repos
508
540
  #
509
541
  def self.create(repo)
510
542
  access_token = get_token
511
543
  @params = {access_token:, name: repo}
512
544
  @path = "user/repos"
513
- @action = 'post'
545
+ @method = 'post'
514
546
  make_api_request
515
- if @status.code == HttpStatusCode::Created
516
- Result[true, JSON.parse(@body)]
517
- else
518
- Result[false, @status.to_s]
519
- end
547
+ return_response_which_expect HttpStatusCode::Created
520
548
  end
521
549
 
522
550
  # Get the README from a owner's repo
@@ -526,42 +554,107 @@ module Gitee::Gitee4CLI::API
526
554
  #
527
555
  # @return [Result] JSON响应体
528
556
  #
529
- # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/readme
557
+ # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/readme
530
558
  #
531
559
  def self.readme(owner, repo)
532
560
  access_token = get_token
533
561
  @params = {access_token:}
534
562
  @path = sprintf "repos/%s/%s/readme", owner, repo
535
- @action = 'get'
563
+ @method = 'get'
536
564
  make_api_request
537
- if @status.code == HttpStatusCode::Ok
538
- Result[true, JSON.parse(@body)]
539
- else
540
- Result[false, @status.to_s]
541
- end
565
+ return_response_which_expect HttpStatusCode::Ok
542
566
  end
543
567
 
544
568
  # List repos from a user
545
569
  #
546
- # @param [String] username 用户名
570
+ # @param username [String] 用户名
547
571
  #
548
572
  # @return [Result] JSON响应体
549
573
  #
550
- # @HTTP [GET] https://gitee.com/api/v5/users/{username}/repos
574
+ # @HTTP [GET] https://gitee.com/api/v5/users/{username}/repos
551
575
  #
552
576
  def self.list_for_user(username)
553
577
  access_token = get_token
554
578
  @params = {access_token:, per_page: 50} # default per_page set to 50 (max 100)
555
579
  @path = sprintf "users/%s/repos", username
556
- @action = 'get'
580
+ @method = 'get'
557
581
  make_api_request
558
- if @status.code == HttpStatusCode::Ok
559
- Result[true, JSON.parse(@body)]
560
- else
561
- Result[false, @status.to_s]
582
+ return_response_which_expect HttpStatusCode::Ok
583
+ end
584
+ end
585
+
586
+
587
+ # Issues APIs
588
+ #
589
+ # @see https://gitee.com/api/v5/swagger#/getV5ReposOwnerRepoIssues
590
+ #
591
+ module Issues
592
+
593
+ extend CommonUtilities
594
+
595
+ # Get all issues
596
+ #
597
+ # @HTTP [GET] https://gitee.com/api/v5/repos/{owner}/{repo}/issues
598
+ #
599
+ def self.all(owner, repo, state)
600
+ access_token = get_token
601
+ @params = {access_token:, state:}
602
+ @path = sprintf "repos/%s/%s/issues", owner, repo
603
+ @method = 'get'
604
+ make_api_request
605
+ return_response_which_expect HttpStatusCode::Ok
606
+ end
607
+ end
608
+
609
+
610
+ # Gists APIs
611
+ #
612
+ module Gists
613
+ extend CommonUtilities
614
+
615
+ # Create a gist
616
+ #
617
+ # @param path [Pathname] 文件路径,Gitee提供的API同时支持多个文件,但是我们为了方便,只传一个文件
618
+ # @param desc [String] gist的描述
619
+ #
620
+ # @HTTP [POST] https://gitee.com/api/v5/gists
621
+ #
622
+ def self.create(path, description)
623
+ access_token = get_token
624
+
625
+ begin
626
+ file = path.read
627
+ rescue => e
628
+ puts %(Error: #{e.message})
629
+ return
562
630
  end
631
+
632
+ # 如果文件有后缀,Gitee Gist 自动识别文件类型
633
+ files = { path.basename.to_s => {"content" => file }}
634
+
635
+ # 这个只能使用 HTTP.post :json 和这个files[Hash]搭配发送请求,
636
+ # 而用 HTTP.post :form 以及 files.to_json 发送请求有bug
637
+ @json_data = {access_token:, files:, description:}
638
+
639
+ @path = "gists"
640
+ @method = 'post'
641
+ make_api_request
642
+ return_response_which_expect HttpStatusCode::Created
563
643
  end
564
644
 
645
+
646
+ # List your gists
647
+ #
648
+ # @HTTP [GET] https://gitee.com/api/v5/gists
649
+ #
650
+ def self.list
651
+ access_token = get_token
652
+ @params = {access_token:}
653
+ @path = "gists"
654
+ @method = 'get'
655
+ make_api_request
656
+ return_response_which_expect HttpStatusCode::Ok
657
+ end
565
658
  end
566
659
 
567
- end
660
+ end # End of module Gitee::Gitee4CLI::API
data/lib/gitee-cli.rb CHANGED
@@ -1,15 +1,13 @@
1
- #!/usr/bin/env ruby
2
1
  # ---------------------------------------------------------------
3
2
  # File : gitee-cli.rb
4
3
  # Authors : Aoran Zeng <ccmywish@qq.com>
5
4
  # Created on : <2023-04-29>
6
- # Last modified : <2023-04-29>
5
+ # Last modified : <2023-05-31>
7
6
  #
8
7
  # gitee-cli:
9
8
  #
10
9
  # Require two lib files
11
- #
12
10
  # ---------------------------------------------------------------
13
11
 
14
- require 'gitee/cli'
15
- require 'gitee/gitee4cli'
12
+ require_relative 'gitee/cli'
13
+ require_relative 'gitee/gitee4cli/api'
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitee-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aoran Zeng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-19 00:00:00.000000000 Z
11
+ date: 2023-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: '13.0'
19
+ version: 13.0.6
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: '13.0'
26
+ version: 13.0.6
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: http
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: standard_path
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rainbow
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.1'
69
97
  description: 'Work with Gitee from the command line.
70
98
 
71
99
  '
@@ -84,8 +112,7 @@ files:
84
112
  - images/banner.jpg
85
113
  - lib/gitee-cli.rb
86
114
  - lib/gitee/cli.rb
87
- - lib/gitee/cli/Rakefile
88
- - lib/gitee/gitee4cli.rb
115
+ - lib/gitee/cli/front.rb
89
116
  - lib/gitee/gitee4cli/api.rb
90
117
  homepage: https://gitee.com/ccmywish
91
118
  licenses:
@@ -101,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
128
  requirements:
102
129
  - - ">="
103
130
  - !ruby/object:Gem::Version
104
- version: 3.1.0
131
+ version: 3.0.0
105
132
  required_rubygems_version: !ruby/object:Gem::Requirement
106
133
  requirements:
107
134
  - - ">="
@@ -1,21 +0,0 @@
1
- # ---------------------------------------------------------------
2
- # File : gitee4cli.rb
3
- # Authors : Aoran Zeng <ccmywish@qq.com>
4
- # Created on : <2023-04-29>
5
- # Last modified : <2023-04-29>
6
- #
7
- # gitee4cli:
8
- #
9
- # Third party Gitee modules for Ruby
10
- #
11
- # ---------------------------------------------------------------
12
-
13
- module Gitee
14
-
15
- module Gitee4CLI
16
- URL = "https://gitee.com"
17
- end
18
-
19
- end
20
-
21
- require 'gitee/gitee4cli/api'