api-clients 0.0.1

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: 27349b1c4e24b1f6365753c40b8c6d4eb45808bd77e56af4e2a4452f170a6463
4
+ data.tar.gz: f3c817aa66b949ffd78d98650eb2f85d59522e0080c6888a70dbf7a036f4727c
5
+ SHA512:
6
+ metadata.gz: 642b69d625d514bc5d61f7ba4537c76798b508911ecf3adab5cab54486b4ef71d6fbcdf481873d2bddcbe170e8e1a355fdd183ffb0475077dcdeeec11d159ab2
7
+ data.tar.gz: 71a573c26f8c602e190174f85b8c6d27c92ace795d0b076b61d178a9cade88dd00c993999ad3596281538a44aae0295832fb15217c42ac985e0be8f288c3b03f
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-07-31
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in api-clients.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
11
+
12
+ gem "httparty", "~> 0.21.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Joaquin
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,35 @@
1
+ # Api::Clients
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/api/clients`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/api-clients.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
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 default: %i[test rubocop]
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/api/clients/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "api-clients"
7
+ spec.version = Api::Clients::VERSION
8
+ spec.authors = ["Joaquin Rivera Padron"]
9
+ spec.email = ["joahking@gmail.com"]
10
+
11
+ spec.summary = "API clients for tools like Cortex, Mend, Sonarqube"
12
+ spec.description = "API clients for tools like Cortex, Mend, Sonarqube to decorate projects with repository data"
13
+ spec.metadata["homepage_uri"] = "https://rubygems.org/gems/api-clients"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = "https://rubygems.org/gems/api-clients"
20
+ spec.metadata["source_code_uri"] = "https://github.com/joahking/api-tools"
21
+ spec.metadata["changelog_uri"] = "https://github.com/joahking/api-tools/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(__dir__) do
26
+ `git ls-files -z`.split("\x0").reject do |f|
27
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "httparty", "0.21.0"
35
+ end
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Public: Add gitlab path as info.x-cortex-tag to Cortex service
4
+
5
+ require 'httparty'
6
+ require 'json'
7
+ require 'yaml'
8
+
9
+ module Api
10
+ module Clients
11
+ class Cortex
12
+ attr_accessor :gitlab_url
13
+
14
+ CORTEX_API = "https://api.cortex.sportradar.ag/api/v1"
15
+ GITLAB_URL = "https://gitlab.sportradar.ag/"
16
+
17
+ API_KEY = ENV["CORTEX_API_KEY"]
18
+
19
+ # Public:
20
+ # - gitlab_url: gitlab project url
21
+ def initialize(gitlab_url:)
22
+ @gitlab_url = gitlab_url
23
+ end
24
+
25
+ # Public: service tag in cortex.yaml
26
+ def service
27
+ @service ||= config["info"]["x-cortex-tag"]
28
+ end
29
+
30
+ # Public: read the service info on cortex
31
+ def service_info
32
+ @service_info ||= get_call(get_service_url)
33
+ end
34
+
35
+ # Public: add gitlab info to cortex service
36
+ def update_service!
37
+ return if service_has_gitlab_info?
38
+ post_call post_service_url, post_body
39
+ end
40
+
41
+ private
42
+
43
+ def service_has_gitlab_info?
44
+ return true if service_gitlab_info == gitlab_path
45
+ false
46
+ end
47
+
48
+ # Private: gitlab info in cortex service
49
+ def service_gitlab_info
50
+ @service_gitlab_info ||= service_info["info"]["x-cortex-git"]["gitlab"]["repository"]
51
+ end
52
+
53
+ def gitlab_path
54
+ @gitlab_path ||= gitlab_url.gsub(GITLAB_URL, '')
55
+ end
56
+
57
+ def config
58
+ @config ||= YAML.load_file('cortex.yaml')
59
+ end
60
+
61
+ def get_service_url
62
+ "#{CORTEX_API}/catalog/#{service}/openapi"
63
+ end
64
+
65
+ def get_call(url)
66
+ res = HTTParty.get(url,
67
+ headers: { 'Authorization' => "Bearer #{API_KEY}" })
68
+
69
+ JSON.parse res.body
70
+ end
71
+
72
+ def post_service_url
73
+ "#{CORTEX_API}/open-api"
74
+ end
75
+
76
+ def post_body
77
+ @post_body ||= begin
78
+ body = config
79
+ body["info"] ||= {}
80
+ body["info"].merge! gitlab_info
81
+ body
82
+ end
83
+ end
84
+
85
+ # Private:
86
+ # - gitlab_path is accepted via API and cortex.yaml, shows ok on UI
87
+ # - gitlab_url is accepted via API and cortex.yaml, BUT does not show on UI
88
+ # - gitlab_id is accepted via cortex.yaml BUT not via API, shows ok on UI
89
+ def gitlab_info
90
+ {
91
+ "x-cortex-git" => {
92
+ "gitlab" => {
93
+ "repository" => gitlab_path
94
+ }
95
+ }
96
+ }
97
+ end
98
+
99
+ def post_call(url, body)
100
+ res = HTTParty.post(url,
101
+ body: body.to_yaml,
102
+ headers: {
103
+ 'Authorization' => "Bearer #{API_KEY}",
104
+ 'Content-Type' => 'application/openapi;charset=UTF-8'
105
+ })
106
+
107
+ JSON.parse res.body
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,123 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Public: Add gitlab url as tags to Mend project
4
+
5
+ require 'httparty'
6
+ require 'json'
7
+
8
+ module Api
9
+ module Clients
10
+ class Mend
11
+ attr_accessor :gitlab_url, :product, :project
12
+
13
+ MEND_API = "https://saas.whitesourcesoftware.com/api/v1.4"
14
+
15
+ ORG_TOKEN = ENV["MEND_ORG_TOKEN"]
16
+ USER_KEY = ENV["MEND_USER_KEY"]
17
+
18
+ # Public:
19
+ # - product: product name
20
+ # - project: project name
21
+ # - gitlab_url: gitlab project url
22
+ def initialize(product:, project:, gitlab_url:)
23
+ @gitlab_url = gitlab_url
24
+ @product = product
25
+ @project = project
26
+ end
27
+
28
+ # Public: tag the project with gitlab url
29
+ def tag_project!
30
+ save_project_tag tag: "gitlab-url", value: gitlab_url
31
+ end
32
+
33
+ # Public: get project tags
34
+ def project_tags
35
+ post_call(get_tags_body)["projectTags"].first["tags"]
36
+ end
37
+
38
+ private
39
+
40
+ # Private: return the product token
41
+ def product_token
42
+ @product_token ||= fetch_products["products"]
43
+ .select { |pr| pr["productName"] == @product }
44
+ .first["productToken"]
45
+ end
46
+
47
+ # Private: return the project token
48
+ def project_token
49
+ @project_token ||= fetch_projects["projects"]
50
+ .select { |pr| pr["projectName"] == @project }
51
+ .first["projectToken"]
52
+ end
53
+
54
+ def fetch_projects
55
+ post_call get_projects_body
56
+ end
57
+
58
+ def get_projects_body
59
+ @get_projects_body ||= {
60
+ "requestType" => "getAllProjects",
61
+ }.merge(product_auth)
62
+ end
63
+
64
+ def fetch_products
65
+ post_call get_products_body
66
+ end
67
+
68
+ def get_products_body
69
+ @get_products_body ||= {
70
+ "requestType" => "getAllProducts"
71
+ }.merge(org_auth)
72
+ end
73
+
74
+ def save_project_tag(tag:, value:)
75
+ post_call save_tag_body(tag: tag, value: value)
76
+ end
77
+
78
+ def save_tag_body(tag:, value:)
79
+ {
80
+ "requestType" => "saveProjectTag",
81
+ "tagKey" => tag,
82
+ "tagValue" => value,
83
+ }.merge(project_auth)
84
+ end
85
+
86
+ def get_tags_body
87
+ @get_tags_body ||= {
88
+ "requestType" => "getProjectTags"
89
+ }.merge(project_auth)
90
+ end
91
+
92
+ # Private: make a post request with body, returns parsed response
93
+ def post_call(body)
94
+ res = HTTParty.post(MEND_API,
95
+ body: body.to_json,
96
+ headers: { 'Content-Type' => 'application/json' })
97
+
98
+ JSON.parse res.body
99
+ end
100
+
101
+ def org_auth
102
+ @org_auth ||= {
103
+ "userKey" => USER_KEY,
104
+ "orgToken" => ORG_TOKEN
105
+ }
106
+ end
107
+
108
+ def product_auth
109
+ @product_auth ||= {
110
+ "userKey" => USER_KEY,
111
+ "productToken" => product_token
112
+ }
113
+ end
114
+
115
+ def project_auth
116
+ @project_auth ||= {
117
+ "userKey" => USER_KEY,
118
+ "projectToken" => project_token
119
+ }
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,73 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Public: Add gitlab url as link to Sonarqube project
4
+
5
+ require 'httparty'
6
+ require 'json'
7
+
8
+ module Api
9
+ module Clients
10
+ class Sonarqube
11
+ attr_accessor :gitlab_url, :project
12
+
13
+ SQ_API = "https://sonarqube.sportradar.ag/api"
14
+ TOKEN = ENV["SQ_TOKEN"]
15
+ USER = ENV["SQ_USER"]
16
+
17
+ # Public:
18
+ # - project: project key
19
+ # - gitlab_url: gitlab project url
20
+ def initialize(project:, gitlab_url:)
21
+ @gitlab_url = gitlab_url
22
+ @project = project
23
+ end
24
+
25
+ # Public: create link of project with gitlab's url
26
+ def link_project!
27
+ return if project_has_link?
28
+ post_call save_link_url
29
+ end
30
+
31
+ # Public: get project links
32
+ def project_links
33
+ get_call(get_links_url)["links"]
34
+ end
35
+
36
+ private
37
+
38
+ def link_name
39
+ @link_name ||= "gitlab-url"
40
+ end
41
+
42
+ # Private: does project has already link gitlab-url?
43
+ def project_has_link?
44
+ project_links.any? { |link| link["name"] == link_name }
45
+ rescue
46
+ false
47
+ end
48
+
49
+ def get_links_url
50
+ @get_links_url ||= "project_links/search?projectKey=#{project}"
51
+ end
52
+
53
+ def save_link_url
54
+ @save_link_url ||= "project_links/create?projectKey=#{project}&name=#{link_name}&url=#{gitlab_url}"
55
+ end
56
+
57
+ def get_call(url)
58
+ res = HTTParty.get "#{SQ_API}/#{url}", basic_auth: auth
59
+ JSON.parse res.body
60
+ end
61
+
62
+ def post_call(url)
63
+ res = HTTParty.post "#{SQ_API}/#{url}", basic_auth: auth
64
+ JSON.parse res.body
65
+ end
66
+
67
+ def auth
68
+ @auth ||= { username: USER, password: TOKEN }
69
+ end
70
+
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Api
4
+ module Clients
5
+ VERSION = "0.0.1"
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "clients/version"
4
+ require_relative "clients/cortex"
5
+ require_relative "clients/mend"
6
+ require_relative "clients/sonarqube"
7
+
8
+ module Api
9
+ module Clients
10
+ class Error < StandardError; end
11
+ # Your code goes here...
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ module Api
2
+ module Clients
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api-clients
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joaquin Rivera Padron
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.21.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.21.0
27
+ description: API clients for tools like Cortex, Mend, Sonarqube to decorate projects
28
+ with repository data
29
+ email:
30
+ - joahking@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rubocop.yml"
36
+ - CHANGELOG.md
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - api-clients.gemspec
42
+ - lib/api/clients.rb
43
+ - lib/api/clients/cortex.rb
44
+ - lib/api/clients/mend.rb
45
+ - lib/api/clients/sonarqube.rb
46
+ - lib/api/clients/version.rb
47
+ - sig/api/clients.rbs
48
+ homepage:
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://rubygems.org/gems/api-clients
53
+ allowed_push_host: https://rubygems.org
54
+ source_code_uri: https://github.com/joahking/api-tools
55
+ changelog_uri: https://github.com/joahking/api-tools/CHANGELOG.md
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.6.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.3.5
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: API clients for tools like Cortex, Mend, Sonarqube
75
+ test_files: []