diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-08-19 09:08:42 +0000 |
commit | b76ae638462ab0f673e5915986070518dd3f9ad3 (patch) | |
tree | bdab0533383b52873be0ec0eb4d3c66598ff8b91 /scripts/api | |
parent | 434373eabe7b4be9593d18a585fb763f1e5f1a6f (diff) | |
download | gitlab-ce-b76ae638462ab0f673e5915986070518dd3f9ad3.tar.gz |
Add latest changes from gitlab-org/gitlab@14-2-stable-eev14.2.0-rc42
Diffstat (limited to 'scripts/api')
-rwxr-xr-x | scripts/api/cancel_pipeline.rb | 29 | ||||
-rw-r--r-- | scripts/api/default_options.rb | 11 | ||||
-rwxr-xr-x | scripts/api/download_job_artifact.rb | 18 | ||||
-rwxr-xr-x | scripts/api/get_job_id.rb | 45 | ||||
-rwxr-xr-x | scripts/api/play_job.rb | 63 |
5 files changed, 57 insertions, 109 deletions
diff --git a/scripts/api/cancel_pipeline.rb b/scripts/api/cancel_pipeline.rb index 0965877a69a..2de50dcee80 100755 --- a/scripts/api/cancel_pipeline.rb +++ b/scripts/api/cancel_pipeline.rb @@ -1,39 +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'], - api_token: ENV['GITLAB_BOT_MULTI_PROJECT_PIPELINE_POLLING_TOKEN'] - }.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| @@ -44,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 8e2207c6fa7..23202ad3912 100755 --- a/scripts/api/download_job_artifact.rb +++ b/scripts/api/download_job_artifact.rb @@ -1,30 +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'], - api_token: ENV['GITLAB_BOT_MULTI_PROJECT_PIPELINE_POLLING_TOKEN'] - }.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)) @@ -36,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 @@ -65,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| @@ -84,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 dd0b7fbada0..166c9198951 100755 --- a/scripts/api/get_job_id.rb +++ b/scripts/api/get_job_id.rb @@ -1,18 +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: {}, - api_token: ENV['GITLAB_BOT_MULTI_PROJECT_PIPELINE_POLLING_TOKEN'] - }.freeze + DEFAULT_OPTIONS = API::DEFAULT_OPTIONS.merge( + pipeline_query: {}.freeze, + job_query: {}.freeze + ).freeze def initialize(options) @project = options.delete(:project) @@ -27,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 @@ -39,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 @@ -56,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 @@ -68,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 @@ -76,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 @@ -107,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| @@ -126,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 408dfdf1ef0..00000000000 --- a/scripts/api/play_job.rb +++ /dev/null @@ -1,63 +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'], - api_token: ENV['GITLAB_BOT_MULTI_PROJECT_PIPELINE_POLLING_TOKEN'] - }.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 |