From 80f5d0d15f8d7ced767651978fb016072003f376 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 16 Aug 2021 06:09:08 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- scripts/api/cancel_pipeline.rb | 30 ++++++++--------- scripts/api/default_options.rb | 11 +++++++ scripts/api/download_job_artifact.rb | 19 +++++------ scripts/api/get_job_id.rb | 46 +++++++++++++------------- scripts/api/play_job.rb | 64 ------------------------------------ scripts/rspec_helpers.sh | 10 +++--- 6 files changed, 62 insertions(+), 118 deletions(-) create mode 100644 scripts/api/default_options.rb delete mode 100755 scripts/api/play_job.rb (limited to 'scripts') diff --git a/scripts/api/cancel_pipeline.rb b/scripts/api/cancel_pipeline.rb index f3aea90f54f..2de50dcee80 100755 --- a/scripts/api/cancel_pipeline.rb +++ b/scripts/api/cancel_pipeline.rb @@ -1,40 +1,32 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'rubygems' require 'gitlab' require 'optparse' -require_relative 'get_job_id' +require_relative 'default_options' class CancelPipeline - DEFAULT_OPTIONS = { - project: ENV['CI_PROJECT_ID'], - pipeline_id: ENV['CI_PIPELINE_ID'], - # Default to "CI scripts API usage" at https://gitlab.com/gitlab-org/gitlab/-/settings/access_tokens - api_token: ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] - }.freeze - def initialize(options) @project = options.delete(:project) @pipeline_id = options.delete(:pipeline_id) - Gitlab.configure do |config| - config.endpoint = 'https://gitlab.com/api/v4' - config.private_token = options.delete(:api_token) - end + @client = Gitlab.client( + endpoint: options.delete(:endpoint) || API::DEFAULT_OPTIONS[:endpoint], + private_token: options.delete(:api_token) + ) end def execute - Gitlab.cancel_pipeline(project, pipeline_id) + client.cancel_pipeline(project, pipeline_id) end private - attr_reader :project, :pipeline_id + attr_reader :project, :pipeline_id, :client end if $0 == __FILE__ - options = CancelPipeline::DEFAULT_OPTIONS.dup + options = API::DEFAULT_OPTIONS.dup OptionParser.new do |opts| opts.on("-p", "--project PROJECT", String, "Project where to find the job (defaults to $CI_PROJECT_ID)") do |value| @@ -45,10 +37,14 @@ if $0 == __FILE__ options[:pipeline_id] = value end - opts.on("-t", "--api-token API_TOKEN", String, "A value API token with the `read_api` scope") do |value| + opts.on("-t", "--api-token API_TOKEN", String, "A value API token with the `api` scope") do |value| options[:api_token] = value end + opts.on("-E", "--endpoint ENDPOINT", String, "The API endpoint for the API token. (defaults to $CI_API_V4_URL and fallback to https://gitlab.com/api/v4)") do |value| + options[:endpoint] = value + end + opts.on("-h", "--help", "Prints this help") do puts opts exit diff --git a/scripts/api/default_options.rb b/scripts/api/default_options.rb new file mode 100644 index 00000000000..70fb9683733 --- /dev/null +++ b/scripts/api/default_options.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module API + DEFAULT_OPTIONS = { + project: ENV['CI_PROJECT_ID'], + pipeline_id: ENV['CI_PIPELINE_ID'], + # Default to "CI scripts API usage" at https://gitlab.com/gitlab-org/gitlab/-/settings/access_tokens + api_token: ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'], + endpoint: ENV['CI_API_V4_URL'] || 'https://gitlab.com/api/v4' + }.freeze +end diff --git a/scripts/api/download_job_artifact.rb b/scripts/api/download_job_artifact.rb index d725de3f1b6..23202ad3912 100755 --- a/scripts/api/download_job_artifact.rb +++ b/scripts/api/download_job_artifact.rb @@ -1,31 +1,26 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'rubygems' require 'optparse' require 'fileutils' require 'uri' require 'cgi' require 'net/http' +require_relative 'default_options' class ArtifactFinder - DEFAULT_OPTIONS = { - project: ENV['CI_PROJECT_ID'], - # Default to "CI scripts API usage" at https://gitlab.com/gitlab-org/gitlab/-/settings/access_tokens - api_token: ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] - }.freeze - def initialize(options) @project = options.delete(:project) @job_id = options.delete(:job_id) @api_token = options.delete(:api_token) + @endpoint = options.delete(:endpoint) || API::DEFAULT_OPTIONS[:endpoint] @artifact_path = options.delete(:artifact_path) warn "No API token given." unless api_token end def execute - url = "https://gitlab.com/api/v4/projects/#{CGI.escape(project)}/jobs/#{job_id}/artifacts" + url = "#{endpoint}/projects/#{CGI.escape(project)}/jobs/#{job_id}/artifacts" if artifact_path FileUtils.mkdir_p(File.dirname(artifact_path)) @@ -37,7 +32,7 @@ class ArtifactFinder private - attr_reader :project, :job_id, :api_token, :artifact_path + attr_reader :project, :job_id, :api_token, :endpoint, :artifact_path def fetch(uri_str, limit = 10) raise 'Too many HTTP redirects' if limit == 0 @@ -66,7 +61,7 @@ class ArtifactFinder end if $0 == __FILE__ - options = ArtifactFinder::DEFAULT_OPTIONS.dup + options = API::DEFAULT_OPTIONS.dup OptionParser.new do |opts| opts.on("-p", "--project PROJECT", String, "Project where to find the job (defaults to $CI_PROJECT_ID)") do |value| @@ -85,6 +80,10 @@ if $0 == __FILE__ options[:api_token] = value end + opts.on("-E", "--endpoint ENDPOINT", String, "The API endpoint for the API token. (defaults to $CI_API_V4_URL and fallback to https://gitlab.com/api/v4)") do |value| + options[:endpoint] = value + end + opts.on("-h", "--help", "Prints this help") do puts opts exit diff --git a/scripts/api/get_job_id.rb b/scripts/api/get_job_id.rb index f6f1f326225..166c9198951 100755 --- a/scripts/api/get_job_id.rb +++ b/scripts/api/get_job_id.rb @@ -1,19 +1,15 @@ #!/usr/bin/env ruby # frozen_string_literal: true -require 'rubygems' require 'gitlab' require 'optparse' +require_relative 'default_options' class JobFinder - DEFAULT_OPTIONS = { - project: ENV['CI_PROJECT_ID'], - pipeline_id: ENV['CI_PIPELINE_ID'], - pipeline_query: {}, - job_query: {}, - # Default to "CI scripts API usage" at https://gitlab.com/gitlab-org/gitlab/-/settings/access_tokens - api_token: ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] - }.freeze + DEFAULT_OPTIONS = API::DEFAULT_OPTIONS.merge( + pipeline_query: {}.freeze, + job_query: {}.freeze + ).freeze def initialize(options) @project = options.delete(:project) @@ -28,10 +24,10 @@ class JobFinder warn "No API token given." if api_token.empty? - Gitlab.configure do |config| - config.endpoint = 'https://gitlab.com/api/v4' - config.private_token = api_token - end + @client = Gitlab.client( + endpoint: options.delete(:endpoint) || DEFAULT_OPTIONS[:endpoint], + private_token: api_token + ) end def execute @@ -40,13 +36,13 @@ class JobFinder private - attr_reader :project, :pipeline_query, :job_query, :pipeline_id, :job_name, :artifact_path + attr_reader :project, :pipeline_query, :job_query, :pipeline_id, :job_name, :artifact_path, :client def find_job_with_artifact return if artifact_path.nil? - Gitlab.pipelines(project, pipeline_query_params).auto_paginate do |pipeline| - Gitlab.pipeline_jobs(project, pipeline.id, job_query_params).auto_paginate do |job| + client.pipelines(project, pipeline_query_params).auto_paginate do |pipeline| + client.pipeline_jobs(project, pipeline.id, job_query_params).auto_paginate do |job| return job if found_job_with_artifact?(job) # rubocop:disable Cop/AvoidReturnFromBlocks end end @@ -57,8 +53,8 @@ class JobFinder def find_job_with_filtered_pipelines return if pipeline_query.empty? - Gitlab.pipelines(project, pipeline_query_params).auto_paginate do |pipeline| - Gitlab.pipeline_jobs(project, pipeline.id, job_query_params).auto_paginate do |job| + client.pipelines(project, pipeline_query_params).auto_paginate do |pipeline| + client.pipeline_jobs(project, pipeline.id, job_query_params).auto_paginate do |job| return job if found_job_by_name?(job) # rubocop:disable Cop/AvoidReturnFromBlocks end end @@ -69,7 +65,7 @@ class JobFinder def find_job_in_pipeline return unless pipeline_id - Gitlab.pipeline_jobs(project, pipeline_id, job_query_params).auto_paginate do |job| + client.pipeline_jobs(project, pipeline_id, job_query_params).auto_paginate do |job| return job if found_job_by_name?(job) # rubocop:disable Cop/AvoidReturnFromBlocks end @@ -77,7 +73,7 @@ class JobFinder end def found_job_with_artifact?(job) - artifact_url = "https://gitlab.com/api/v4/projects/#{CGI.escape(project)}/jobs/#{job.id}/artifacts/#{artifact_path}" + artifact_url = "#{client.endpoint}/projects/#{CGI.escape(project)}/jobs/#{job.id}/artifacts/#{artifact_path}" response = HTTParty.head(artifact_url) # rubocop:disable Gitlab/HTTParty response.success? end @@ -108,11 +104,13 @@ if $0 == __FILE__ end opts.on("-q", "--pipeline-query pipeline_query", String, "Query to pass to the Pipeline API request") do |value| - options[:pipeline_query].merge!(Hash[*value.split('=')]) + options[:pipeline_query] = + options[:pipeline_query].merge(Hash[*value.split('=')]) end opts.on("-Q", "--job-query job_query", String, "Query to pass to the Job API request") do |value| - options[:job_query].merge!(Hash[*value.split('=')]) + options[:job_query] = + options[:job_query].merge(Hash[*value.split('=')]) end opts.on("-j", "--job-name job_name", String, "A job name that needs to exist in the found pipeline") do |value| @@ -127,6 +125,10 @@ if $0 == __FILE__ options[:api_token] = value end + opts.on("-E", "--endpoint ENDPOINT", String, "The API endpoint for the API token. (defaults to $CI_API_V4_URL and fallback to https://gitlab.com/api/v4)") do |value| + options[:endpoint] = value + end + opts.on("-h", "--help", "Prints this help") do puts opts exit diff --git a/scripts/api/play_job.rb b/scripts/api/play_job.rb deleted file mode 100755 index 2c5cc75619d..00000000000 --- a/scripts/api/play_job.rb +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -require 'rubygems' -require 'gitlab' -require 'optparse' -require_relative 'get_job_id' - -class PlayJob - DEFAULT_OPTIONS = { - project: ENV['CI_PROJECT_ID'], - pipeline_id: ENV['CI_PIPELINE_ID'], - # Default to "CI scripts API usage" at https://gitlab.com/gitlab-org/gitlab/-/settings/access_tokens - api_token: ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] - }.freeze - - def initialize(options) - @options = options - - Gitlab.configure do |config| - config.endpoint = 'https://gitlab.com/api/v4' - config.private_token = options.fetch(:api_token) - end - end - - def execute - job = JobFinder.new(options.slice(:project, :api_token, :pipeline_id, :job_name).merge(scope: 'manual')).execute - - Gitlab.job_play(project, job.id) - end - - private - - attr_reader :options - - def project - options[:project] - end -end - -if $0 == __FILE__ - options = PlayJob::DEFAULT_OPTIONS.dup - - OptionParser.new do |opts| - opts.on("-p", "--project PROJECT", String, "Project where to find the job (defaults to $CI_PROJECT_ID)") do |value| - options[:project] = value - end - - opts.on("-j", "--job-name JOB_NAME", String, "A job name that needs to exist in the found pipeline") do |value| - options[:job_name] = value - end - - opts.on("-t", "--api-token API_TOKEN", String, "A value API token with the `read_api` scope") do |value| - options[:api_token] = value - end - - opts.on("-h", "--help", "Prints this help") do - puts opts - exit - end - end.parse! - - PlayJob.new(options).execute -end diff --git a/scripts/rspec_helpers.sh b/scripts/rspec_helpers.sh index a788c3417c2..0714ecfce80 100644 --- a/scripts/rspec_helpers.sh +++ b/scripts/rspec_helpers.sh @@ -10,14 +10,14 @@ function retrieve_tests_metadata() { local test_metadata_job_id # Ruby - test_metadata_job_id=$(scripts/api/get_job_id.rb --project "${project_path}" -q "status=success" -q "ref=${artifact_branch}" -q "username=gitlab-bot" -Q "scope=success" --job-name "update-tests-metadata") + test_metadata_job_id=$(scripts/api/get_job_id.rb --endpoint "https://gitlab.com/api/v4" --project "${project_path}" -q "status=success" -q "ref=${artifact_branch}" -q "username=gitlab-bot" -Q "scope=success" --job-name "update-tests-metadata") if [[ ! -f "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" ]]; then - scripts/api/download_job_artifact.rb --project "${project_path}" --job-id "${test_metadata_job_id}" --artifact-path "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" || echo "{}" > "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" + scripts/api/download_job_artifact.rb --endpoint "https://gitlab.com/api/v4" --project "${project_path}" --job-id "${test_metadata_job_id}" --artifact-path "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" || echo "{}" > "${KNAPSACK_RSPEC_SUITE_REPORT_PATH}" fi if [[ ! -f "${FLAKY_RSPEC_SUITE_REPORT_PATH}" ]]; then - scripts/api/download_job_artifact.rb --project "${project_path}" --job-id "${test_metadata_job_id}" --artifact-path "${FLAKY_RSPEC_SUITE_REPORT_PATH}" || echo "{}" > "${FLAKY_RSPEC_SUITE_REPORT_PATH}" + scripts/api/download_job_artifact.rb --endpoint "https://gitlab.com/api/v4" --project "${project_path}" --job-id "${test_metadata_job_id}" --artifact-path "${FLAKY_RSPEC_SUITE_REPORT_PATH}" || echo "{}" > "${FLAKY_RSPEC_SUITE_REPORT_PATH}" fi } @@ -48,10 +48,10 @@ function retrieve_tests_mapping() { local artifact_branch="master" local test_metadata_with_mapping_job_id - test_metadata_with_mapping_job_id=$(scripts/api/get_job_id.rb --project "${project_path}" -q "status=success" -q "ref=${artifact_branch}" -q "username=gitlab-bot" -Q "scope=success" --job-name "update-tests-metadata" --artifact-path "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz") + test_metadata_with_mapping_job_id=$(scripts/api/get_job_id.rb --endpoint "https://gitlab.com/api/v4" --project "${project_path}" -q "status=success" -q "ref=${artifact_branch}" -q "username=gitlab-bot" -Q "scope=success" --job-name "update-tests-metadata" --artifact-path "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz") if [[ ! -f "${RSPEC_PACKED_TESTS_MAPPING_PATH}" ]]; then - (scripts/api/download_job_artifact.rb --project "${project_path}" --job-id "${test_metadata_with_mapping_job_id}" --artifact-path "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz" && gzip -d "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz") || echo "{}" > "${RSPEC_PACKED_TESTS_MAPPING_PATH}" + (scripts/api/download_job_artifact.rb --endpoint "https://gitlab.com/api/v4" --project "${project_path}" --job-id "${test_metadata_with_mapping_job_id}" --artifact-path "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz" && gzip -d "${RSPEC_PACKED_TESTS_MAPPING_PATH}.gz") || echo "{}" > "${RSPEC_PACKED_TESTS_MAPPING_PATH}" fi scripts/unpack-test-mapping "${RSPEC_PACKED_TESTS_MAPPING_PATH}" "${RSPEC_TESTS_MAPPING_PATH}" -- cgit v1.2.3