gitlab_client 0.1.0.pre.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 11777515445d58c4e72df0578185d0f8bf6b581267387af83b893d9752495284
4
+ data.tar.gz: d9d68f6b4da43f5fd95bb8f6c7d3026f7d2a579363c2d7a7fc8e036192e37c22
5
+ SHA512:
6
+ metadata.gz: d74f85de7642009196a577303d9cb694e90c462c51c71fb455a5203b2c67fcb3347cde0294ec78ef0eee26d60525ffac2fbfa51f4593c84070e1f8edbca0909a
7
+ data.tar.gz: fef3c36b91460e1d3bc0c605733084d0487ce37e6c2e4428f82975370c026bcd8fa72a715feb530f1a828a3358e5bf1d64b9515fbf2e86af63a144cd4c1bdbc7
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ ---
2
+ AllCops:
3
+ TargetRubyVersion: 3.0
4
+ NewCops: enable
5
+ SuggestExtensions: false
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Layout/LineLength:
16
+ Max: 120
17
+
18
+ Style/Documentation:
19
+ Enabled: false
20
+
21
+ Gemspec/RequireMFA:
22
+ Enabled: false
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-09-22
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at aboobackervyd@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in gitlab_client.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "steep"
15
+
16
+ gem "webmock"
17
+
18
+ gem "simplecov-cobertura", "~> 2.1"
19
+
20
+ gem "gem-release", "~> 2.2"
data/Gemfile.lock ADDED
@@ -0,0 +1,127 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gitlab_client (0.1.0.pre.alpha.pre.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (7.1.1)
10
+ base64
11
+ bigdecimal
12
+ concurrent-ruby (~> 1.0, >= 1.0.2)
13
+ connection_pool (>= 2.2.5)
14
+ drb
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ mutex_m
18
+ tzinfo (~> 2.0)
19
+ addressable (2.8.5)
20
+ public_suffix (>= 2.0.2, < 6.0)
21
+ ast (2.4.2)
22
+ base64 (0.1.1)
23
+ bigdecimal (3.1.4)
24
+ concurrent-ruby (1.2.2)
25
+ connection_pool (2.4.1)
26
+ crack (0.4.5)
27
+ rexml
28
+ csv (3.2.7)
29
+ docile (1.4.0)
30
+ drb (2.1.1)
31
+ ruby2_keywords
32
+ ffi (1.16.3)
33
+ fileutils (1.7.1)
34
+ gem-release (2.2.2)
35
+ hashdiff (1.0.1)
36
+ i18n (1.14.1)
37
+ concurrent-ruby (~> 1.0)
38
+ json (2.6.3)
39
+ language_server-protocol (3.17.0.3)
40
+ listen (3.8.0)
41
+ rb-fsevent (~> 0.10, >= 0.10.3)
42
+ rb-inotify (~> 0.9, >= 0.9.10)
43
+ logger (1.5.3)
44
+ minitest (5.20.0)
45
+ mutex_m (0.1.2)
46
+ parallel (1.23.0)
47
+ parser (3.2.2.4)
48
+ ast (~> 2.4.1)
49
+ racc
50
+ public_suffix (5.0.3)
51
+ racc (1.7.1)
52
+ rainbow (3.1.1)
53
+ rake (13.0.6)
54
+ rb-fsevent (0.11.2)
55
+ rb-inotify (0.10.1)
56
+ ffi (~> 1.0)
57
+ rbs (3.2.2)
58
+ regexp_parser (2.8.2)
59
+ rexml (3.2.6)
60
+ rubocop (1.57.1)
61
+ base64 (~> 0.1.1)
62
+ json (~> 2.3)
63
+ language_server-protocol (>= 3.17.0)
64
+ parallel (~> 1.10)
65
+ parser (>= 3.2.2.4)
66
+ rainbow (>= 2.2.2, < 4.0)
67
+ regexp_parser (>= 1.8, < 3.0)
68
+ rexml (>= 3.2.5, < 4.0)
69
+ rubocop-ast (>= 1.28.1, < 2.0)
70
+ ruby-progressbar (~> 1.7)
71
+ unicode-display_width (>= 2.4.0, < 3.0)
72
+ rubocop-ast (1.29.0)
73
+ parser (>= 3.2.1.0)
74
+ ruby-progressbar (1.13.0)
75
+ ruby2_keywords (0.0.5)
76
+ securerandom (0.2.2)
77
+ simplecov (0.22.0)
78
+ docile (~> 1.1)
79
+ simplecov-html (~> 0.11)
80
+ simplecov_json_formatter (~> 0.1)
81
+ simplecov-cobertura (2.1.0)
82
+ rexml
83
+ simplecov (~> 0.19)
84
+ simplecov-html (0.12.3)
85
+ simplecov_json_formatter (0.1.4)
86
+ steep (1.5.3)
87
+ activesupport (>= 5.1)
88
+ concurrent-ruby (>= 1.1.10)
89
+ csv (>= 3.0.9)
90
+ fileutils (>= 1.1.0)
91
+ json (>= 2.1.0)
92
+ language_server-protocol (>= 3.15, < 4.0)
93
+ listen (~> 3.0)
94
+ logger (>= 1.3.0)
95
+ parser (>= 3.1)
96
+ rainbow (>= 2.2.2, < 4.0)
97
+ rbs (>= 3.1.0)
98
+ securerandom (>= 0.1)
99
+ strscan (>= 1.0.0)
100
+ terminal-table (>= 2, < 4)
101
+ strscan (3.0.7)
102
+ terminal-table (3.0.2)
103
+ unicode-display_width (>= 1.1.1, < 3)
104
+ tzinfo (2.0.6)
105
+ concurrent-ruby (~> 1.0)
106
+ unicode-display_width (2.5.0)
107
+ webmock (3.19.1)
108
+ addressable (>= 2.8.0)
109
+ crack (>= 0.3.2)
110
+ hashdiff (>= 0.4.0, < 2.0.0)
111
+
112
+ PLATFORMS
113
+ arm64-darwin-21
114
+ arm64-darwin-22
115
+
116
+ DEPENDENCIES
117
+ gem-release (~> 2.2)
118
+ gitlab_client!
119
+ minitest (~> 5.0)
120
+ rake (~> 13.0)
121
+ rubocop (~> 1.21)
122
+ simplecov-cobertura (~> 2.1)
123
+ steep
124
+ webmock
125
+
126
+ BUNDLED WITH
127
+ 2.4.21
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Aboobacker MK
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # GitlabClient
2
+
3
+ Opiniated and minimal Gitlab client for ruby with zero dependencies
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gitlab_client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gitlab_client
20
+
21
+ ## Usage
22
+
23
+ ### Rest API
24
+
25
+
26
+ ```ruby
27
+ GitlabClient::Rest.new(private_token: '<TOKEN>').call( path: 'api/v4/version')
28
+ ```
29
+
30
+ ```ruby
31
+ client = GitlabClient::Rest.new(private_token: '<TOKEN>')
32
+ response = client.call( path: 'api/v4/projects?owned=true&with_programming_language=ruby')
33
+ puts response.body
34
+ while response.links['next']
35
+ puts response.call(links['next'])
36
+ end
37
+ ```
38
+
39
+ ### Graphql API
40
+
41
+ ```ruby
42
+ query = '{ projects { edges { node { id name description } } } }'
43
+ GitlabClient::Graphql.new(private_token: '<TOKEN>').call(query: query)
44
+ ```
45
+
46
+
47
+ ### Difference between Gitlab gem and GitlabClient gem
48
+
49
+ - Gitlab client gem is thin, it does not include helper methods per endpoint, nor CLI
50
+ - Gitlab client gem also supports Graphql WIP
51
+ - Gitlab client gem have built in mechanism for pagination TODO
52
+ - Gitlab client gem does not have global state, you can connect to more than one Gitlab instances from same code safely
53
+ - Gitlab client gem does not depend on other gems, ie no worry about dependency updates
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tachyons/gitlab_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/tachyons/gitlab_client/blob/master/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
68
+
69
+ ## Code of Conduct
70
+
71
+ Everyone interacting in the GitlabClient project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/tachyons/gitlab_client/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task :steep do
17
+ system "bundle exec steep check"
18
+ end
19
+
20
+ task default: %i[test rubocop steep]
data/Steepfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ D = Steep::Diagnostic
4
+
5
+ target :lib do
6
+ signature "sig"
7
+
8
+ check "lib" # Directory name
9
+
10
+ library "json", "uri", "net-http" # Standard libraries
11
+
12
+ configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
13
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "gitlab_client"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/gitlab_client"
4
+
5
+ client = GitlabClient::Rest.new(access_token: ENV.fetch("PERSONAL_ACCESS_TOKEN", nil))
6
+
7
+ response = client.post(path: "api/v4/projects/", data: {
8
+ name: "new project #{Random.rand(500)}"
9
+ })
10
+
11
+ response = client.put(path: "api/v4/projects/#{response.body["id"]}", data: {
12
+ name: "new project #{Random.rand(500)}"
13
+ })
14
+
15
+ response = client.delete(path: "api/v4/projects/#{response.body["id"]}")
16
+
17
+ puts response.body
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/gitlab_client"
4
+
5
+ client = GitlabClient::Rest.new(access_token: ENV.fetch("PERSONAL_ACCESS_TOKEN", nil))
6
+
7
+ response = client.call(path: "api/v4/projects?pagination=keyset&per_page=50&order_by=id&sort=asc")
8
+
9
+ loop do
10
+ break unless response.success?
11
+
12
+ response.body.each do |project|
13
+ puts project["name"]
14
+ end
15
+ response = response.next!(client)
16
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../lib/gitlab_client"
4
+
5
+ client = GitlabClient::Rest.new(access_token: ENV.fetch("PERSONAL_ACCESS_TOKEN", nil))
6
+
7
+ response = client.call(path: "api/v4/users",
8
+ params: { pagination: "keyset", per_page: 50, order_by: "id",
9
+ sort: "asc" })
10
+
11
+ loop do
12
+ break unless response.success?
13
+
14
+ response.body.each do |project|
15
+ puts project["name"]
16
+ end
17
+ response = response.next!(client)
18
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ require "net/http"
5
+ require "json"
6
+
7
+ module GitlabClient
8
+ # Gitlab client
9
+ class Graphql
10
+ DEFAULT_HEADERS = { "Accept" => "application/json", "Content-Type" => "application/json" }.freeze
11
+ GRAPHQL_ENDPOINT = "api/graphql"
12
+ MAXIMUM_REDIRECTS = 5
13
+ attr_reader :access_token, :refresh_token, :private_token
14
+
15
+ def initialize(access_token: nil, refresh_token: nil, private_token: nil, host: "https://gitlab.com/")
16
+ @host = host
17
+ @access_token = access_token
18
+ @refresh_token = refresh_token
19
+ @private_token = private_token
20
+ end
21
+
22
+ def call(query:, variables: {})
23
+ response = Net::HTTP.post(uri, { query: query, variables: variables }.to_json, headers)
24
+
25
+ response
26
+ .then { |res| handle_error(res) }
27
+ .then { |res| JSON.parse(res.body) }
28
+ end
29
+
30
+ private
31
+
32
+ def handle_error(response)
33
+ case response
34
+ when Net::HTTPClientError
35
+ raise Error, JSON.parse(response.body)
36
+ end
37
+ response
38
+ end
39
+
40
+ def headers
41
+ return DEFAULT_HEADERS.merge({ "PRIVATE-TOKEN" => private_token }) if private_token
42
+ return DEFAULT_HEADERS.merge({ "Authorization" => "Bearer #{access_token}" }) if access_token
43
+
44
+ {}
45
+ end
46
+
47
+ def uri
48
+ URI.join(@host, GRAPHQL_ENDPOINT)
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,160 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ require "net/http"
5
+ require "json"
6
+ require "uri"
7
+
8
+ module GitlabClient
9
+ class Rest
10
+ class Response
11
+ LINK_REGEX = /<([^>]+)>; rel="([^"]+)"/
12
+ attr_accessor :body, :headers, :status_code, :raw_response
13
+
14
+ def initialize(body:, headers:, status_code:, raw_response:)
15
+ @body = body
16
+ @headers = headers
17
+ @status_code = status_code
18
+ @raw_response = raw_response
19
+ end
20
+
21
+ def success?
22
+ (200..299).cover?(@status_code)
23
+ end
24
+
25
+ def links
26
+ return {} unless headers["link"]
27
+
28
+ @links ||= headers["link"].first.split(",").to_h { |l| l.match(LINK_REGEX).captures }.invert
29
+ end
30
+
31
+ def next!(client)
32
+ return nil unless next?
33
+
34
+ client.call(path: next_link)
35
+ end
36
+
37
+ private
38
+
39
+ def next_link
40
+ links["next"]
41
+ end
42
+
43
+ def next?
44
+ !next_link.nil?
45
+ end
46
+ end
47
+ DEFAULT_HEADERS = { "Accept" => "application/json",
48
+ "Content-Type" => "application/json",
49
+ "User-Agent" => "gitlab_client #{GitlabClient::VERSION}" }.freeze
50
+ MAXIMUM_REDIRECTS = 5
51
+ attr_reader :access_token, :refresh_token, :private_token
52
+
53
+ def initialize(access_token: nil, refresh_token: nil, private_token: nil, host: "https://gitlab.com")
54
+ @host = host
55
+ @access_token = access_token
56
+ @refresh_token = refresh_token
57
+ @private_token = private_token
58
+ end
59
+
60
+ # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
61
+ def call(path:, method: "GET", custom_headers: {}, data: {}, params: {})
62
+ raise ArgumentError if path.nil?
63
+
64
+ path = URI(path)
65
+ path.query = URI.encode_www_form(params)
66
+
67
+ req_headers = headers.merge(custom_headers)
68
+ uri = URI.join(@host, path.to_s)
69
+ case method
70
+ when "GET"
71
+ response = Net::HTTP.get_response(uri, req_headers)
72
+ when "POST"
73
+ response = Net::HTTP.post(uri, data.to_json, headers)
74
+ when "PUT"
75
+ response = http.put(uri.to_s, data.to_json, headers)
76
+ when "DELETE"
77
+ response = http.delete(uri.to_s, headers)
78
+ end
79
+
80
+ response
81
+ .then { |raw_response| build_response(raw_response) }
82
+ .then { |res| handle_pagination(res) }
83
+ end
84
+ # rubocop:enable Metrics/MethodLength,Metrics/AbcSize
85
+
86
+ #
87
+ # Sends GET request to GitLab API
88
+ #
89
+ # @param [<String>] path path to send request to
90
+ # @param [<Hash>] custom_headers custom headers to be included
91
+ # @param [<Hash>] params query params to be included
92
+ #
93
+ # @return [<GitLabClient::Rest::Response>]
94
+ #
95
+ def get(path:, custom_headers: {}, params: {})
96
+ call(path: path, custom_headers: custom_headers, method: "GET", data: {}, params: params)
97
+ end
98
+
99
+ # Sends POST request to GitLab API
100
+ #
101
+ # @param [<String>] path path to send request to
102
+ # @param [<Hash>] data data to be included
103
+ # @param [<Hash>] custom_headers custom headers to be included
104
+ # @param [<Hash>] params query params to be included
105
+ # @return [<GitLabClient::Rest::Response>]
106
+
107
+ def post(path:, data:, custom_headers: {}, params: {})
108
+ call(path: path, custom_headers: custom_headers, method: "POST", data: data, params: params)
109
+ end
110
+
111
+ # Sends PUT request to GitLab API
112
+ #
113
+ # @param [<String>] path path to send request to
114
+ # @param [<Hash>] custom_headers custom headers to be included
115
+ # @param [<Hash>] params query params to be included
116
+ # @return [<GitLabClient::Rest::Response>]
117
+
118
+ def put(path:, data:, custom_headers: {}, params: {})
119
+ call(path: path, custom_headers: custom_headers, method: "PUT", data: data, params: params)
120
+ end
121
+
122
+ # Sends DELETE request to GitLab API
123
+ #
124
+ # @param [<String>] path path to send request to
125
+ # @param [<Hash>] custom_headers custom headers to be included
126
+ # @param [<Hash>] params query params to be included
127
+ # @return [<GitLabClient::Rest::Response>]
128
+
129
+ def delete(path:, custom_headers: {}, params: {})
130
+ call(path: path, custom_headers: custom_headers, method: "DELETE", data: {}, params: params)
131
+ end
132
+
133
+ private
134
+
135
+ def http
136
+ host = URI(@host).host || ""
137
+ http = Net::HTTP.new(host, URI(@host).port)
138
+ http.use_ssl = true
139
+ http
140
+ end
141
+
142
+ def build_response(raw_response)
143
+ Response.new(body: JSON.parse(raw_response.body),
144
+ headers: raw_response.to_hash,
145
+ status_code: raw_response.code.to_i,
146
+ raw_response: raw_response)
147
+ end
148
+
149
+ def handle_pagination(response)
150
+ response
151
+ end
152
+
153
+ def headers
154
+ return DEFAULT_HEADERS.merge({ "PRIVATE-TOKEN" => private_token }) if private_token
155
+ return DEFAULT_HEADERS.merge({ "Authorization" => "Bearer #{access_token}" }) if access_token
156
+
157
+ {}
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GitlabClient
4
+ VERSION = "0.1.0.pre.3"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ # typed: true
3
+
4
+ require_relative "gitlab_client/version"
5
+ require_relative "gitlab_client/rest"
6
+ require_relative "gitlab_client/graphql"
7
+
8
+ module GitlabClient
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,23 @@
1
+ module GitlabClient
2
+ class Error < StandardError
3
+ end
4
+
5
+ class Graphql
6
+ DEFAULT_HEADERS: Hash[String, String?]
7
+ GRAPHQL_ENDPOINT: String
8
+ MAXIMUM_REDIRECTS: Integer
9
+ @host: String
10
+
11
+ attr_reader access_token: String?
12
+ attr_reader refresh_token: String?
13
+ attr_reader private_token: String?
14
+ def initialize: (?access_token: String?, ?refresh_token: String?, ?private_token: String?, ?host: String) -> void
15
+ def access_token?: -> bool
16
+ def call: (query: String, ?variables: Hash[String, String?]) -> JSON
17
+
18
+ private
19
+ def handle_error: (Net::HTTPResponse response) -> (Net::HTTPResponse)
20
+ def headers: () -> ::Hash[String, String?]
21
+ def uri: -> URI::Generic
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ module GitlabClient
2
+ class Error < StandardError
3
+ end
4
+
5
+ class Rest
6
+ DEFAULT_HEADERS: Hash[String, String?]
7
+ MAXIMUM_REDIRECTS: Integer
8
+ @host: String
9
+
10
+ attr_reader access_token: String?
11
+ attr_reader refresh_token: String?
12
+ attr_reader private_token: String?
13
+ def initialize: (?access_token: String?, ?refresh_token: String?, ?private_token: String?, ?host: String) -> void
14
+ def access_token?: -> bool
15
+ def headers: -> Hash[String, String?]
16
+ def call: (path: String?, ?method: String, ?custom_headers: Hash[String, String?], ?data: Hash[String, String?], ?params: Hash[String, String]) -> GitlabClient::Rest::Response
17
+
18
+ private
19
+ def http: () -> Net::HTTP
20
+ def build_response: (untyped raw_response) -> GitlabClient::Rest::Response
21
+ def handle_pagination: (untyped response) -> untyped
22
+ def handle_redirect: (Net::HTTP | ::Net::HTTPResponse response, ?Integer limit) -> (Net::HTTP | Net::HTTPSuccess| ::Net::HTTPResponse| nil)
23
+ def handle_error: (Net::HTTP? response) -> Net::HTTP?
24
+
25
+ class Response
26
+ @links: Hash[String, String]
27
+ LINK_REGEX: Regexp
28
+
29
+ attr_accessor body: Hash[String, String]
30
+ attr_accessor headers: Hash[String, untyped]
31
+ attr_accessor status_code: Integer
32
+ attr_accessor raw_response: Net::HTTP
33
+ def initialize: (body: Hash[String, String], headers: Hash[String, untyped], status_code: Integer, raw_response: Net::HTTP) -> void
34
+ def success?: -> bool
35
+ def links: () -> ::Hash[String, String]
36
+
37
+ def next!: (GitlabClient::Rest client) -> (GitlabClient::Rest::Response?)
38
+
39
+ private
40
+
41
+ def next_link: () -> String?
42
+
43
+ def next?: () -> bool
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,6 @@
1
+ module GitlabClient
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.pre.3
5
+ platform: ruby
6
+ authors:
7
+ - Aboobacker MK
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Gitlab API client
14
+ email:
15
+ - aboobackervyd@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rubocop.yml"
21
+ - CHANGELOG.md
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - LICENSE.txt
26
+ - README.md
27
+ - Rakefile
28
+ - Steepfile
29
+ - bin/console
30
+ - bin/setup
31
+ - examples/crud_project.rb
32
+ - examples/project_list.rb
33
+ - examples/users_list.rb
34
+ - lib/gitlab_client.rb
35
+ - lib/gitlab_client/graphql.rb
36
+ - lib/gitlab_client/rest.rb
37
+ - lib/gitlab_client/version.rb
38
+ - sig/gitlab_client.rbs
39
+ - sig/gitlab_client/graphql.rbs
40
+ - sig/gitlab_client/rest.rbs
41
+ homepage: https://gitlab.com/tachyons-gitlab/gitlab_client
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://gitlab.com/tachyons-gitlab/gitlab_client
46
+ source_code_uri: https://gitlab.com/tachyons-gitlab/gitlab_client
47
+ changelog_uri: https://gitlab.com/tachyons-gitlab/gitlab_client
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.1
62
+ requirements: []
63
+ rubygems_version: 3.4.20
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Gitlab Client
67
+ test_files: []