gitea-client 1.4.0 → 1.4.1

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: f693d44ba06b1038fcf43a6b9341f3c7017d5451f699b11bc890a98477df7c80
4
- data.tar.gz: bf791479a13f6efbfd2f93a218554d834bec2f8553c1a5d7bd520ca58dcc0ca1
3
+ metadata.gz: 87ee860a7bcea67ed6b06781b81ae626cac7964ee5985d6090acdc382d03f4fa
4
+ data.tar.gz: 75beb5a00cbc08c30f3ae6ce0679bb2e6dc8dfa1ef75df2ed2caf05dff8ca17c
5
5
  SHA512:
6
- metadata.gz: f39ca2ed50976eefc01908b0714d7234af60fa2a985986bfe93f455cd20a1f4bf58ae945658009fd869ac0584cbed121dd7257b992b2f4049b92d02f3ec18b69
7
- data.tar.gz: 6dfd8596162d3b11e4b5c3540a805bcc84cbcc0c6e2d4a3e4c7181c15bd86db1843a7efc97fc87abf64f10075bb75bb992235f6923d09e6e375ac61360ac14aa
6
+ metadata.gz: 9c56c0a6bb6fa9f3fdd445762966ddb58b239c71d1b4a7326e1abb2bcc6cbc47815c5000f535ffdcc03d40ceaee7a8e0395db55b7dd5253ab3ff2a761f388693
7
+ data.tar.gz: 8d5a1f9e312b3e6357eda0ecb24dbc50cab698e933186038be2247502bc55e177045fc0c761ddd886492a600021b0f0d9b176a4703fbadc43dfefb64f2b83bcd
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- gitea-client (1.4.0)
4
+ gitea-client (1.4.1)
5
5
  rest-client (~> 2.1.0)
6
6
 
7
7
  GEM
@@ -0,0 +1,13 @@
1
+ module Gitea
2
+ module Api
3
+ module Hat
4
+ module Admin
5
+
6
+ def patch_admin_users_by_username(username, opt = {})
7
+ @http.patch("/admin/users/#{username}", opt)
8
+ end
9
+
10
+ end # Admin
11
+ end # Hat
12
+ end # Api
13
+ end # Gitea
@@ -0,0 +1,48 @@
1
+ module Gitea
2
+ module Api
3
+ module Hat
4
+ class Client
5
+
6
+ # 构造Api client,用于操作Api数据
7
+ # @param opts [Hash] 构造client时的参数选项
8
+ # @option opts [string] domain [必填] gitea服务地址
9
+ # @option opts [string] hat_base_url [必填] api相对路径
10
+ # @option opts [string] admin_username [必填] 管理员账号
11
+ # @option opts [string] admin_password [必填] 管理员密码
12
+ # @option opts [string] username [选填] 用户账号
13
+ # @option opts [string] password [选填] 用户密码
14
+ # @option opts [string] gitea_token [选填] 用户token
15
+ def initialize(opts)
16
+ @config = Config.new(opts)
17
+ Gitea::Common::Logging.set_log_file(@config.log_filepath)
18
+ @http = Http.new(@config)
19
+ end
20
+
21
+ def config
22
+ @config
23
+ end
24
+
25
+ def config=(config)
26
+ unless config.is_a?(Gitea::Api::Hat::Config)
27
+ fail Exception, "load config failure!"
28
+ end
29
+ @config = config
30
+ @http = Http.new(config)
31
+ end
32
+
33
+ def token
34
+ @config.token
35
+ end
36
+
37
+ def token=(token)
38
+ @config.token = token
39
+ end
40
+
41
+ include Gitea::Api::Hat::Repository
42
+ include Gitea::Api::Hat::Users
43
+ include Gitea::Api::Hat::Organization
44
+ include Gitea::Api::Hat::Admin
45
+ end
46
+ end # Hat
47
+ end # Api
48
+ end # Gitea
@@ -0,0 +1,36 @@
1
+ module Gitea
2
+ module Api
3
+
4
+ module Hat
5
+ class Config < Common::Struct::Base
6
+ attrs :domain, :hat_base_url, :username, :password, :token, :open_timeout, :read_timeout, :log_filepath
7
+
8
+ def initialize(opts = {})
9
+ super(opts)
10
+
11
+ valid!
12
+ normalize_domain
13
+ end
14
+
15
+ private
16
+ def valid!
17
+ if @domain.nil? || @hat_base_url.nil?
18
+ fail Exception, "@domain, @hat_base_url not permit blank."
19
+ end
20
+ end
21
+
22
+ def normalize_domain
23
+ uri = URI.parse(domain)
24
+ uri = URI.parse(domain)
25
+ uri = URI.parse("http://#{domain}") unless uri.scheme
26
+
27
+ if uri.scheme != 'http' and uri.scheme != 'https'
28
+ fail ClientError, "Only HTTP and HTTPS domain are accepted."
29
+ end
30
+
31
+ @domain = uri
32
+ end
33
+ end
34
+ end # Hat
35
+ end # Api
36
+ end # Gitea
@@ -0,0 +1,132 @@
1
+ require 'base64'
2
+ require 'json'
3
+
4
+ module Gitea
5
+ module Api
6
+ module Hat
7
+ class Http
8
+ DEFAULT_CONTENT_TYPE = 'application/json'
9
+ TOKEN_HEADER = 'Authorization'
10
+ OPEN_TIMEOUT = 10
11
+ READ_TIMEOUT = 120
12
+
13
+
14
+ def initialize(config)
15
+ @config = config
16
+ end
17
+
18
+ include Gitea::Common::Logging
19
+
20
+ def get_request_url(api_url)
21
+ url = @config.domain.dup
22
+ url.query = nil
23
+ url.fragment = nil
24
+ [url.to_s, @config.hat_base_url, api_url].join('')
25
+ end
26
+
27
+ def get(api_url = '', http_options = {}, &block)
28
+ do_request('GET', api_url, http_options, &block)
29
+ end
30
+
31
+ def put(api_url = '', http_options = {}, &block)
32
+ do_request('PUT', api_url, http_options, &block)
33
+ end
34
+
35
+ def patch(api_url = '', http_options = {}, &block)
36
+ do_request('PATCH', api_url, http_options, &block)
37
+ end
38
+
39
+ def post(api_url = '', http_options = {}, &block)
40
+ do_request('POST', api_url, http_options, &block)
41
+ end
42
+
43
+ def delete(api_url = '', http_options = {}, &block)
44
+ do_request('DELETE', api_url, http_options, &block)
45
+ end
46
+
47
+ def head(api_url = '', http_options = {}, &block)
48
+ do_request('HEAD', api_url, http_options, &block)
49
+ end
50
+
51
+ def options(api_url = '', http_options = {}, &block)
52
+ do_request('OPTIONS', api_url, http_options, &block)
53
+ end
54
+
55
+ private
56
+ # Do Http request
57
+ def do_request(verb, api_url, http_options = {}, &block)
58
+
59
+ headers = http_options[:headers] || {}
60
+ headers['user-agent'] = get_user_agent
61
+ headers['date'] = Time.now.httpdate
62
+ headers['content-type'] ||= DEFAULT_CONTENT_TYPE
63
+
64
+ if @config.username and @config.password
65
+ headers[TOKEN_HEADER] = 'Basic ' + Base64::encode64(@config.username + ":" + @config.password)
66
+ end
67
+
68
+ headers[:params] = http_options[:query] || {}
69
+ headers[:params].merge!({access_token: @config.token}) if @config.token
70
+ logger.info("Gitea Hat Client Begin a Request!...")
71
+ logger.info("Send HTTP request, verb: #{verb}, http_options: #{http_options}")
72
+ logger.info("Relative Url: #{api_url}")
73
+ logger.info("Headers: #{headers}")
74
+ request = RestClient::Request.new(
75
+ :method => verb,
76
+ :url => get_request_url(api_url),
77
+ :headers => headers,
78
+ :payload => http_options[:body],
79
+ :open_timeout => @config.open_timeout || OPEN_TIMEOUT,
80
+ :read_timeout => @config.read_timeout || READ_TIMEOUT
81
+ )
82
+
83
+
84
+ response = request.execute do |resp, &blk|
85
+ if resp.code >= 300
86
+ e = Gitea::Api::ServerError.new(resp)
87
+ logger.error(e.to_s)
88
+ raise e
89
+ else
90
+ resp.return!(&blk)
91
+ end
92
+ end
93
+
94
+ # If streaming read_body is used, we need to create the
95
+ # RestClient::Response ourselves
96
+ unless response.is_a?(RestClient::Response)
97
+ if response.code.to_i >= 300
98
+ body = response.body
99
+ if RestClient::version < '2.1.0'
100
+ body = RestClient::Request.decode(response['content-encoding'], response.body)
101
+ end
102
+ response = RestClient::Response.create(body, response, request)
103
+ e = Gitea::Api::ServerError.new(response)
104
+ logger.error(e.to_s)
105
+ raise e
106
+ end
107
+ response = RestClient::Response.create(nil, response, request)
108
+ response.return!
109
+ end
110
+
111
+ begin
112
+ if response.headers.has_key?(:x_total) || response.headers.has_key?(:x_total_count)
113
+ return {data: JSON.parse(response), total_data: response.headers[:x_total_count]}
114
+ else
115
+ return JSON.parse(response)
116
+ end
117
+ rescue => e
118
+ logger.error(e.to_s)
119
+ return {}
120
+ end
121
+
122
+ logger.info("Gitea Hat Client Success End a Request!...")
123
+ end
124
+
125
+ def get_user_agent
126
+ "gitea-client/#{VERSION}"
127
+ end
128
+
129
+ end
130
+ end # Hat
131
+ end # Common
132
+ end # Gitea
@@ -0,0 +1,25 @@
1
+ module Gitea
2
+ module Api
3
+ module Hat
4
+ module Organization
5
+
6
+ def patch_orgs_by_org(org, opt = {})
7
+ @http.patch("/orgs/#{org}", opt)
8
+ end
9
+
10
+ def post_orgs(opt = {})
11
+ @http.post("/orgs", opt)
12
+ end
13
+
14
+ def put_teams_repos_by_id_org(id, org, opt = {})
15
+ @http.put("/teams/#{id}/repos/#{org}", opt)
16
+ end
17
+
18
+ def delete_teams_repos_by_id_org(id, org, opt = {})
19
+ @http.delete("/teams/#{id}/repos/#{org}", opt)
20
+ end
21
+
22
+ end # Organization
23
+ end # Hat
24
+ end # Api
25
+ end # Gitea
@@ -0,0 +1,168 @@
1
+ module Gitea
2
+ module Api
3
+ module Hat
4
+ module Repository
5
+ def get_repos_contents_by_owner_repo(owner, repo, opt = {})
6
+ @http.get("/repos/#{owner}/#{repo}/contents", opt)
7
+ end
8
+
9
+ def get_repos_contents_by_owner_repo_filepath(owner, repo, filepath, opt = {})
10
+ @http.get("/repos/#{owner}/#{repo}/contents/#{filepath}", opt)
11
+ end
12
+
13
+ def get_repos_find_by_owner_repo(owner, repo, opt = {})
14
+ @http.get("/repos/#{owner}/#{repo}/find", opt)
15
+ end
16
+
17
+ def get_repos_git_commits_by_owner_repo_sha(owner, repo, sha, opt = {})
18
+ @http.get("/repos/#{owner}/#{repo}/git/commits/#{sha}", opt)
19
+ end
20
+
21
+ def get_repos_commits_diff_by_owner_repo_sha(owner, repo, sha, opt = {})
22
+ @http.get("/repos/#{owner}/#{repo}/commits/#{sha}/diff", opt)
23
+ end
24
+
25
+ def post_repos_hooks_by_owner_repo(owner, repo, opt = {})
26
+ @http.post("/repos/#{owner}/#{repo}/hooks", opt)
27
+ end
28
+
29
+ def patch_repos_hooks_by_owner_repo_id(owner, repo, id, opt = {})
30
+ @http.patch("/repos/#{owner}/#{repo}/hooks/#{id}", opt)
31
+ end
32
+
33
+ def get_repos_hooks_hooktasks_by_owner_repo_id(owner, repo, id, opt = {})
34
+ @http.get("/repos/#{owner}/#{repo}/hooks/#{id}/hooktasks", opt)
35
+ end
36
+
37
+ def get_repos_releases_latest_by_owner_repo(owner, repo, opt = {})
38
+ @http.get("/repos/#{owner}/#{repo}/releases/latest", opt)
39
+ end
40
+
41
+ def get_repos_releases_by_owner_repo(owner, repo, opt = {})
42
+ @http.get("/repos/#{owner}/#{repo}/releases", opt)
43
+ end
44
+
45
+ def get_repos_releases_by_owner_repo_id(owner, repo, opt = {})
46
+ @http.get("/repos/#{owner}/#{repo}/releases/#{id}", opt)
47
+ end
48
+
49
+ def post_repos_releases_by_owner_repo(owner, repo, opt = {})
50
+ @http.post("/repos/#{owner}/#{repo}/releases", opt)
51
+ end
52
+
53
+ def patch_repos_releases_by_owner_repo_id(owner, repo, id, opt = {})
54
+ @http.patch("/repos/#{owner}/#{repo}/releases/#{id}", opt)
55
+ end
56
+
57
+ def get_repos_branch_name_set_by_owner_repo(owner, repo, opt = {})
58
+ @http.get("/repos/#{owner}/#{repo}/branch_name_set", opt)
59
+ end
60
+
61
+ def get_repos_branches_by_owner_repo(owner, repo, opt={})
62
+ @http.get("/repos/#{owner}/#{repo}/branches", opt)
63
+ end
64
+
65
+ def get_repos_branches_branches_slice_by_owner_repo(owner, repo, opt = {})
66
+ @http.get("/repos/#{owner}/#{repo}/branches/branches_slice", opt)
67
+ end
68
+
69
+ def get_repos_tag_name_set_by_owner_repo(owner, repo, opt = {})
70
+ @http.get("/repos/#{owner}/#{repo}/tag_name_set", opt)
71
+ end
72
+
73
+ def get_repos_tags_by_owner_repo(owner, repo, opt = {})
74
+ @http.get("/repos/#{owner}/#{repo}/tags", opt)
75
+ end
76
+
77
+ def get_repos_branch_tag_count_by_owner_repo(owner, repo, opt = {})
78
+ @http.get("/repos/#{owner}/#{repo}/branch_tag_count", opt)
79
+ end
80
+
81
+ def get_repos_readme_by_owner_repo(owner, repo, opt = {})
82
+ @http.get("/repos/#{owner}/#{repo}/readme", opt)
83
+ end
84
+
85
+ def get_repos_readme_by_owner_repo_filepath(owner, repo, filepath, opt = {})
86
+ @http.get("/repos/#{owner}/#{repo}/readme/#{filepath}", opt)
87
+ end
88
+
89
+ def get_repos_commits_slice_by_owner_repo(owner, repo, filepath, opt = {})
90
+ @http.get("/repos/#{owner}/#{repo}/commits_slice", opt)
91
+ end
92
+
93
+ def get_repos_contributors_by_owner_repo(owner, repo, opt = {})
94
+ @http.get("/repos/#{owner}/#{repo}/contributors", opt)
95
+ end
96
+
97
+ def get_repos_code_stats_by_owner_repo(owner, repo, opt = {})
98
+ @http.get("/repos/#{owner}/#{repo}/code_stats", opt)
99
+ end
100
+
101
+ def get_repos_count_by_owner_repo(owner, repo, opt = {})
102
+ @http.get("/repos/#{owner}/#{repo}/count", opt)
103
+ end
104
+
105
+ def get_repos_file_commits_by_owner_repo_filepath(owner, repo, filepath, opt = {})
106
+ @http.get("/repos/#{owner}/#{repo}/file_commits/#{filepath}", opt)
107
+ end
108
+
109
+ def get_repos_blame_by_owner_repo(owner, repo, opt = {})
110
+ @http.get("/repos/#{owner}/#{repo}/blame", opt)
111
+ end
112
+
113
+ def get_repos_compare_by_owner_repo_baseref_headref(owner, repo, baseRef, headRef, opt = {})
114
+ @http.get("/repos/#{owner}/#{repo}/compare/#{baseRef}...#{headRef}", opt)
115
+ end
116
+
117
+ def post_repos_transfer_by_owner_repo(owner, repo, opt = {})
118
+ @http.post("/repos/#{owner}/#{repo}/transfer", opt)
119
+ end
120
+
121
+ def get_repos_pulls_by_owner_repo_index(owner, repo, index, opt = {})
122
+ @http.get("/repos/#{owner}/#{repo}/pulls/#{index}", opt)
123
+ end
124
+
125
+ def get_repos_pulls_commits_by_owner_repo_index(owner, repo, index, opt = {})
126
+ @http.get("/repos/#{owner}/#{repo}/pulls/#{index}/commits", opt)
127
+ end
128
+
129
+ def get_repos_pulls_files_by_owner_repo_index(owner, repo, index, opt = {})
130
+ @http.get("/repos/#{owner}/#{repo}/pulls/#{index}/files", opt)
131
+ end
132
+
133
+ def get_repos_wikies_by_owner_repo(owner, repo, opt = {})
134
+ @http.get("/repos/#{owner}/#{repo}/wikies", opt)
135
+ end
136
+
137
+ def post_repos_wikies_by_owner_repo(owner, repo, opt = {})
138
+ @http.post("/repos/#{owner}/#{repo}/wikies", opt)
139
+ end
140
+
141
+ def patch_repos_wikies_by_owner_repo_pagename(owner, repo, pageName, opt = {})
142
+ @http.patch("/repos/#{owner}/#{repo}/wikies/#{pageName}", opt)
143
+ end
144
+
145
+ def get_repos_wikies_by_owner_repo_pagename(owner, repo, pageName, opt = {})
146
+ @http.get("/repos/#{owner}/#{repo}/wikies/#{pageName}", opt)
147
+ end
148
+
149
+ def delete_repos_wikies_by_owner_repo_pagename(owner, repo, pageName, opt = {})
150
+ @http.delete("/repos/#{owner}/#{repo}/wikies/#{pageName}", opt)
151
+ end
152
+
153
+ def post_repos_contents_batch_by_owner_repo(owner, repo, opt = {})
154
+ @http.post("/repos/#{owner}/#{repo}/contents/batch", opt)
155
+ end
156
+
157
+ def get_repos_pulls_versions_by_owner_repo_index(owner, repo, index, opt = {})
158
+ @http.get("/repos/#{owner}/#{repo}/pulls/#{index}/versions", opt)
159
+ end
160
+
161
+ def get_repos_pulls_versions_diff_by_owner_repo_index_id(owner, repo, index, versionID, opt = {})
162
+ @http.get("/repos/#{owner}/#{repo}/pulls/#{index}/versions/#{versionID}/diff", opt)
163
+ end
164
+
165
+ end # Repository
166
+ end # Hat
167
+ end # Api
168
+ end # Gitea
@@ -0,0 +1,13 @@
1
+ module Gitea
2
+ module Api
3
+ module Hat
4
+ module Users
5
+
6
+ def get_users_heatmap_by_username(username, opt = {})
7
+ @http.get("/users/#{username}/heatmap", opt)
8
+ end
9
+
10
+ end # Users
11
+ end # Hat
12
+ end # Api
13
+ end # Gitea
data/lib/gitea/api.rb CHANGED
@@ -11,3 +11,10 @@ require_relative 'api/client'
11
11
  require_relative 'api/config'
12
12
  require_relative 'api/exception'
13
13
  require_relative 'api/http'
14
+ require_relative 'api/hat/repository'
15
+ require_relative 'api/hat/organization'
16
+ require_relative 'api/hat/users'
17
+ require_relative 'api/hat/admin'
18
+ require_relative 'api/hat/client'
19
+ require_relative 'api/hat/config'
20
+ require_relative 'api/hat/http'
data/lib/gitea/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Gitea
4
- VERSION = "1.4.0"
4
+ VERSION = "1.4.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gitea-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - viletyy
@@ -49,6 +49,13 @@ files:
49
49
  - lib/gitea/api/client.rb
50
50
  - lib/gitea/api/config.rb
51
51
  - lib/gitea/api/exception.rb
52
+ - lib/gitea/api/hat/admin.rb
53
+ - lib/gitea/api/hat/client.rb
54
+ - lib/gitea/api/hat/config.rb
55
+ - lib/gitea/api/hat/http.rb
56
+ - lib/gitea/api/hat/organization.rb
57
+ - lib/gitea/api/hat/repository.rb
58
+ - lib/gitea/api/hat/users.rb
52
59
  - lib/gitea/api/http.rb
53
60
  - lib/gitea/api/issue.rb
54
61
  - lib/gitea/api/miscellaneous.rb