Add dedicated libraries to access CI information and API

Signed-off-by: Balasankar "Balu" C <balasankar@gitlab.com>
This commit is contained in:
Balasankar "Balu" C 2023-06-15 12:24:54 +05:30
parent 553d16b05b
commit aa268bd819
No known key found for this signature in database
GPG Key ID: B77D2E2E23735427
4 changed files with 76 additions and 0 deletions

12
lib/gitlab/api_client.rb Normal file
View File

@ -0,0 +1,12 @@
require_relative 'build/info/ci'
require_relative 'build/info/secrets'
require 'gitlab'
module Gitlab
class APIClient
def initialize(endpoint = Build::Info::CI.api_v4_url, token = Build::Info::Secrets.api_token)
@client = ::Gitlab::Client.new(endpoint: endpoint, private_token: token)
end
end
end

View File

@ -0,0 +1,39 @@
require_relative '../../util'
require_relative '../../api_client'
require_relative '../check'
module Build
class Info
class CI
class << self
def branch_name
Gitlab::Util.get_env('CI_COMMIT_BRANCH')
end
def tag_name
Gitlab::Util.get_env('CI_COMMIT_TAG')
end
def project_id
Gitlab::Util.get_env('CI_PROJECT_ID')
end
def pipeline_id
Gitlab::Util.get_env('CI_PIPELINE_ID')
end
def job_id
Gitlab::Util.get_env('CI_JOB_ID')
end
def job_token
Gitlab::Util.get_env('CI_JOB_TOKEN')
end
def api_v4_url
Gitlab::Util.get_env('CI_API_V4_URL')
end
end
end
end
end

View File

@ -0,0 +1,17 @@
require_relative '../../util'
module Build
class Info
class Secrets
class << self
def api_token(scope: 'reporter')
Gitlab::Util.get_env("GITLAB_API_#{scope.upcase}_TOKEN")
end
def ci_job_token
Build::Info::CI.job_token
end
end
end
end
end

View File

@ -19,6 +19,14 @@ require 'knapsack'
require 'gitlab/util'
require 'rspec-parameterized'
# Because our library directory is also named `gitlab`, it collides with
# upstream 'gitlab' gem's code. So, we set a dummy value to the `VERSION`
# constant so that the `gitlab` library can be required without issues. The
# proper fix is renaming our directory to `omnibus_gitlab` to match convention.
module Gitlab
VERSION = 'dummy-version'.freeze
end
# Load support libraries to provide common convenience methods for our tests
Dir["./spec/support/**/*.rb"].each { |f| require f }