universal-git-client 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8b4c37347ad2d272fb542744b0f095e9901b45d5a422eb0ef13eba7144377a2a
4
+ data.tar.gz: a6e42628553bbe0674131e8d90f270e2e91d52aa8048dd7a374abd2d368df3a4
5
+ SHA512:
6
+ metadata.gz: 45d3c7fde797319a54f5b05e79b9993ce17ebb3305ee6145f898c70e375edd9952b302e7efacdecf27befa97ce2712e8af1a2cc0eaf596efab43cc2e2d698d01
7
+ data.tar.gz: 461a1270d635a7a15a96a3912313d8377220c1e58cc4879d4afd2ef48a4a22cf6232eea81e511ac06aecb2de8a3d1dbb447bbe755384f1fab66c88e8eab53d44
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License
2
+
3
+ Copyright (c) SmartBear
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
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # README
2
+
3
+ ## Steps to release a new version of universal-git-client gem
4
+
5
+ 1- Push the new code on Github repository
6
+
7
+ 2- Execute the rake task `rake version:bump:[type]` with type: <major|minor|patch>
8
+
9
+ 3- Execute the rake task `rake gemspec:release` It will regenerate, validate gemspec then commit and push to git
10
+
11
+ 4- Go to the universal-git-client repository on Github, from the "Code" tab, click on "N releases" (N: releases count), click on "Draft a new release" and fill the form:
12
+
13
+ - Tag version: version number, for example v0.4.0
14
+ - Release title: version number, for example v0.4.0
15
+ - Write: release notes
16
+
17
+ To validate, click on "Publish release" and it's done :)
18
+
19
+ ## Contributing to universal-git-client
20
+
21
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
22
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
23
+ * Fork the project.
24
+ * Start a feature/bugfix branch.
25
+ * Commit and push until you are happy with your contribution.
26
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
27
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
28
+
29
+ ## Usage
30
+ * To start the comand line interpreter : `pry -I ./lib -r universal-git-client`
31
+
32
+ * To create a client object : `client = UniversalGitClient.factory.build(provider, base_url, access_token)`
33
+ The _base_url_ parameter must be the url to the API of your provider.
34
+
35
+ * _Example_: ```client = UniversalGitClient.factory.build(:bitbucket_server, base_url: "http://bitbucket.ngrok.io/rest/api/1.0", access_token: "123456789")```
36
+
37
+
38
+ ## Copyright
39
+
40
+ Copyright (c) 2019 SmartBear - HipTest
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.2.4
@@ -0,0 +1,18 @@
1
+ require 'universal-git-client/configuration'
2
+ require 'universal-git-client/client_factory'
3
+
4
+ module UniversalGitClient
5
+ class << self
6
+ def configure
7
+ yield Configuration.config
8
+ end
9
+
10
+ def config
11
+ Configuration.config
12
+ end
13
+
14
+ def factory
15
+ ClientFactory
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,72 @@
1
+ require 'universal-git-client/normalizers/github'
2
+ require 'universal-git-client/normalizers/gitlab'
3
+ require 'universal-git-client/normalizers/bitbucket'
4
+ require 'universal-git-client/normalizers/bitbucket_server'
5
+
6
+
7
+ module UniversalGitClient
8
+ class Client
9
+ attr_reader :provider, :http_client
10
+
11
+ def initialize(provider, http_client)
12
+ @provider = provider
13
+ @http_client = http_client
14
+ end
15
+
16
+ def user
17
+ response = http_client.user
18
+ normalize(response, :User)
19
+ end
20
+
21
+ def organizations(*args)
22
+ response = http_client.organizations(*args)
23
+ normalize(response, :Organization)
24
+ end
25
+
26
+ def user_repos(*args)
27
+ response = http_client.user_repos(*args)
28
+ normalize(response, :Repository)
29
+ end
30
+
31
+ def orga_repos(*args)
32
+ response = http_client.orga_repos(*args)
33
+ normalize(response, :Repository)
34
+ end
35
+
36
+ def repository(*args)
37
+ response = http_client.repository(*args)
38
+ normalize(response, :Repository)
39
+ end
40
+
41
+ def branches(*args)
42
+ response = http_client.branches(*args)
43
+ normalize(response, :Branch)
44
+ end
45
+
46
+ def branch(*args)
47
+ response = http_client.branch(*args)
48
+ normalize(response, :Branch)
49
+ end
50
+
51
+ def download_repo_archive(*args)
52
+ http_client.download_repo_archive(*args)
53
+ end
54
+
55
+ def setup_repo_webhook(*args)
56
+ response = http_client.setup_repo_webhook(*args)
57
+ normalize(response, :Webhook)
58
+ end
59
+
60
+ def delete_repo_webhook(*args)
61
+ http_client.delete_repo_webhook(*args)
62
+ end
63
+
64
+ private
65
+
66
+ def normalize(response, resource)
67
+ normalizer = UniversalGitClient.const_get("Normalizers::#{http_client.class.name.demodulize}")
68
+ normalized = normalizer.new(response, resource).normalize
69
+ normalized.serializable_hash
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ require 'universal-git-client/client'
2
+ require 'universal-git-client/http/github'
3
+ require 'universal-git-client/http/gitlab'
4
+ require 'universal-git-client/http/bitbucket'
5
+ require 'universal-git-client/http/bitbucket_server'
6
+
7
+ module UniversalGitClient
8
+ class ClientFactory
9
+ class << self
10
+ def build(provider, *args)
11
+ case provider
12
+ when :github
13
+ Client.new('GitHub', UniversalGitClient::Http::Github.new(*args))
14
+ when :gitlab
15
+ Client.new('GitLab', UniversalGitClient::Http::Gitlab.new(*args))
16
+ when :bitbucket
17
+ Client.new('Bitbucket', UniversalGitClient::Http::Bitbucket.new(*args))
18
+ when :bitbucket_server
19
+ Client.new('Bitbucket Server', UniversalGitClient::Http::BitbucketServer.new(*args))
20
+ else
21
+ raise ArgumentError, 'Unknown provider'
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require 'dry-configurable'
2
+
3
+ module UniversalGitClient
4
+ class Configuration
5
+ extend Dry::Configurable
6
+
7
+ setting :logger
8
+ setting :log_level, :debug
9
+ setting :log_format, :apache
10
+ setting :user_agent, 'UniversalGitClient-Ruby'
11
+ setting :default_pagination, 30
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module UniversalGitClient
2
+ module Errors
3
+ class HttpError < StandardError
4
+ attr_reader :response
5
+
6
+ def initialize(msg, response)
7
+ @response = response
8
+ super(msg)
9
+ end
10
+ end
11
+
12
+ class ClientError < HttpError; end
13
+ class ServerError < HttpError; end
14
+
15
+ class BadRequest < ClientError; end
16
+ class Unauthorized < ClientError; end
17
+ class Forbidden < ClientError; end
18
+ class NotFound < ClientError; end
19
+ class MethodNotAllowed < ClientError; end
20
+ class NotAcceptable < ClientError; end
21
+ class Conflict < ClientError; end
22
+ class UnsupportedMediaType < ClientError; end
23
+ class UnprocessableEntity < ClientError; end
24
+
25
+ class InternalServerError < ServerError; end
26
+ class BadGateway < ServerError; end
27
+ class ServiceUnavailable < ServerError; end
28
+ end
29
+ end
@@ -0,0 +1,69 @@
1
+ require 'universal-git-client/http/response_validation'
2
+ require 'httparty'
3
+ require 'down'
4
+ require 'deep_merge'
5
+
6
+ module UniversalGitClient
7
+ module Http
8
+ class Base
9
+ include HTTParty
10
+ include ResponseValidation
11
+
12
+ def initialize(base_url:, download_url: nil, access_token: nil, private_token: nil)
13
+ if access_token && private_token
14
+ raise ArgumentError, 'access_token and private_token params are mutually exclusive'
15
+ end
16
+
17
+ @base_url = base_url.chomp('/')
18
+ @download_url = download_url
19
+ @access_token = access_token
20
+ @private_token = private_token
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :base_url, :download_url, :access_token, :private_token
26
+
27
+ def user_agent
28
+ UniversalGitClient.config.user_agent
29
+ end
30
+
31
+ def default_elements_per_page
32
+ UniversalGitClient.config.default_pagination
33
+ end
34
+
35
+ def logger
36
+ UniversalGitClient.config.logger
37
+ end
38
+
39
+ def log_level
40
+ UniversalGitClient.config.log_level
41
+ end
42
+
43
+ def log_format
44
+ UniversalGitClient.config.log_format
45
+ end
46
+
47
+ def default_options
48
+ {
49
+ logger: logger,
50
+ log_level: log_level,
51
+ log_format: log_format,
52
+ base_uri: base_url,
53
+ headers: {
54
+ 'Accept' => 'application/json',
55
+ 'Content-Type' => 'application/json',
56
+ 'Authorization' => (access_token ? "Bearer #{access_token}" : nil),
57
+ 'Private-Token' => private_token,
58
+ 'User-Agent' => user_agent,
59
+ }.compact,
60
+ }
61
+ end
62
+
63
+ def down_default_options
64
+ options = default_options[:headers].reject { |k, _v| %w[Accept Content-Type].include?(k) }
65
+ options.reject { |k, _v| [:base_uri, :logger, :log_level, :log_format].include?(k) }
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,118 @@
1
+ require 'universal-git-client/http/base'
2
+ require 'uri'
3
+
4
+ module UniversalGitClient
5
+ module Http
6
+ class Bitbucket < Base
7
+ def user
8
+ with_response_validation! do
9
+ self.class.get('/user', default_options)
10
+ end
11
+ end
12
+
13
+ def organizations(page: 1, per_page: nil)
14
+ with_response_validation! do
15
+ self.class.get(
16
+ '/teams',
17
+ default_options.merge(
18
+ query: {
19
+ role: 'member',
20
+ page: page,
21
+ pagelen: per_page || default_elements_per_page,
22
+ },
23
+ )
24
+ )
25
+ end
26
+ end
27
+
28
+ def user_repos(page: 1, per_page: nil)
29
+ current_user_uuid = URI.encode_www_form_component(self.user['uuid'])
30
+ with_response_validation! do
31
+ self.class.get(
32
+ "/repositories/#{current_user_uuid}",
33
+ default_options.merge(
34
+ query: {
35
+ page: page,
36
+ pagelen: per_page || default_elements_per_page,
37
+ },
38
+ )
39
+ )
40
+ end
41
+ end
42
+
43
+ def orga_repos(organization:, page: 1, per_page: nil)
44
+ with_response_validation! do
45
+ self.class.get(
46
+ "/repositories/#{organization}",
47
+ default_options.merge(
48
+ query: {
49
+ page: page,
50
+ pagelen: per_page || default_elements_per_page,
51
+ },
52
+ )
53
+ )
54
+ end
55
+ end
56
+
57
+ def repository(owner:, repo:)
58
+ with_response_validation! do
59
+ self.class.get("/repositories/#{owner}/#{repo}", default_options)
60
+ end
61
+ end
62
+
63
+ def branches(owner:, repo:, page: 1, per_page: nil)
64
+ with_response_validation! do
65
+ self.class.get(
66
+ "/repositories/#{owner}/#{repo}/refs/branches",
67
+ default_options.merge(
68
+ query: {
69
+ page: page,
70
+ pagelen: per_page || default_elements_per_page,
71
+ },
72
+ )
73
+ )
74
+ end
75
+ end
76
+
77
+ def branch(owner:, repo:, branch:)
78
+ with_response_validation! do
79
+ self.class.get(
80
+ "/repositories/#{owner}/#{repo}/refs/branches/#{branch}",
81
+ default_options
82
+ )
83
+ end
84
+ end
85
+
86
+ def download_repo_archive(owner:, repo:, branch: nil)
87
+ Down.download("#{download_url}/#{owner}/#{repo}/get/#{branch}.zip", down_default_options)
88
+ end
89
+
90
+ def setup_repo_webhook(owner:, repo:, webhook_url:, webhook_secret: nil)
91
+ with_response_validation! do
92
+ self.class.post(
93
+ "/repositories/#{owner}/#{repo}/hooks",
94
+ default_options.merge(
95
+ body: {
96
+ description: 'webhook',
97
+ url: webhook_secret ? "#{webhook_url}?secret=#{webhook_secret}" : webhook_url,
98
+ active: true,
99
+ events: [
100
+ 'repo:push',
101
+ ],
102
+ }.to_json,
103
+ )
104
+ )
105
+ end
106
+ end
107
+
108
+ def delete_repo_webhook(owner:, repo:, webhook_id:)
109
+ with_response_validation! do
110
+ self.class.delete(
111
+ "/repositories/#{owner}/#{repo}/hooks/#{webhook_id}",
112
+ default_options
113
+ )
114
+ end
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,134 @@
1
+ require 'universal-git-client/http/base'
2
+ require 'universal-git-client/http/helpers/dummy_response'
3
+
4
+ module UniversalGitClient
5
+ module Http
6
+ class BitbucketServer < Base
7
+ def user
8
+ Helpers::DummyResponse.response(options: default_options)
9
+ end
10
+
11
+ def organizations(page: 1, per_page: nil)
12
+ options = default_options.merge(
13
+ query: {
14
+ start: limit(per_page) * (page - 1),
15
+ limit: limit(per_page)
16
+ }
17
+ )
18
+ body = {
19
+ size: 0,
20
+ start: 0,
21
+ limit: limit(per_page),
22
+ values: []
23
+ }
24
+ Helpers::DummyResponse.response(body: body, options: options)
25
+ end
26
+
27
+ def user_repos(page: 1, per_page: nil)
28
+ with_response_validation! do
29
+ self.class.get(
30
+ '/repos',
31
+ default_options.merge(
32
+ query: {
33
+ state: 'AVAILABLE',
34
+ start: limit(per_page) * (page - 1),
35
+ limit: limit(per_page)
36
+ }
37
+ )
38
+ )
39
+ end
40
+ end
41
+
42
+ def orga_repos(organization:, page: 1, per_page: nil)
43
+ options = default_options.merge(
44
+ query: {
45
+ start: limit(per_page) * (page - 1),
46
+ limit: limit(per_page)
47
+ }
48
+ )
49
+ body = {
50
+ size: 0,
51
+ start: 0,
52
+ limit: limit(per_page),
53
+ values: []
54
+ }
55
+ Helpers::DummyResponse.response(body: body, options: options)
56
+ end
57
+
58
+ def repository(owner:, repo:)
59
+ with_response_validation! do
60
+ self.class.get("/projects/#{owner}/repos/#{repo}", default_options)
61
+ end
62
+ end
63
+
64
+ def branches(owner:, repo:, page: 1, per_page: nil)
65
+ with_response_validation! do
66
+ self.class.get(
67
+ "/projects/#{owner}/repos/#{repo}/branches",
68
+ default_options.merge(
69
+ query: {
70
+ start: limit(per_page) * (page - 1),
71
+ limit: limit(per_page)
72
+ }
73
+ )
74
+ )
75
+ end
76
+ end
77
+
78
+ def branch(owner:, repo:, branch:)
79
+ with_response_validation! do
80
+ response = self.class.get(
81
+ "/projects/#{owner}/repos/#{repo}/branches",
82
+ default_options.merge(
83
+ query: {
84
+ filterText: branch,
85
+ limit: 1
86
+ }
87
+ )
88
+ )
89
+ Helpers::DummyResponse.response(request: response.request, body: response.parsed_response['values'].first)
90
+ end
91
+ end
92
+
93
+ def download_repo_archive(owner:, repo:, branch: nil)
94
+ Down.download("#{base_url}/projects/#{owner}/repos/#{repo}/archive?at=#{branch}&format=zip", down_default_options)
95
+ end
96
+
97
+ def setup_repo_webhook(owner:, repo:, webhook_url:, webhook_secret: nil)
98
+ with_response_validation! do
99
+ self.class.post(
100
+ "/projects/#{owner}/repos/#{repo}/webhooks",
101
+ default_options.merge(
102
+ body: {
103
+ name: 'webhook',
104
+ url: webhook_url,
105
+ active: true,
106
+ events: [
107
+ 'repo:refs_changed'
108
+ ],
109
+ configuration: {
110
+ secret: webhook_secret
111
+ }
112
+ }.to_json
113
+ )
114
+ )
115
+ end
116
+ end
117
+
118
+ def delete_repo_webhook(owner:, repo:, webhook_id:)
119
+ with_response_validation! do
120
+ self.class.delete(
121
+ "/projects/#{owner}/repos/#{repo}/webhooks/#{webhook_id}",
122
+ default_options
123
+ )
124
+ end
125
+ end
126
+
127
+ private
128
+
129
+ def limit(per_page)
130
+ per_page || default_elements_per_page
131
+ end
132
+ end
133
+ end
134
+ end