# HG changeset patch # User Balasankar "Balu" C <balasankar@gitlab.com> # Date 1686812094 -19800 # Thu Jun 15 12:24:54 2023 +0530 # Node ID 095406f353980a0d492e2e401ec6018f3c995511 # Parent 937b2fa1422a83d5a0a06b3e4e75e0bb063b8378 Add dedicated libraries to access CI information and API Signed-off-by: Balasankar "Balu" C <balasankar@gitlab.com> diff --git a/lib/gitlab/api_client.rb b/lib/gitlab/api_client.rb new file mode 100644 --- /dev/null +++ b/lib/gitlab/api_client.rb @@ -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 diff --git a/lib/gitlab/build/info/ci.rb b/lib/gitlab/build/info/ci.rb new file mode 100644 --- /dev/null +++ b/lib/gitlab/build/info/ci.rb @@ -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 diff --git a/lib/gitlab/build/info/secrets.rb b/lib/gitlab/build/info/secrets.rb new file mode 100644 --- /dev/null +++ b/lib/gitlab/build/info/secrets.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -19,6 +19,14 @@ 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 }