Commit 6292305b authored by Albert Salim's avatar Albert Salim

Merge branch '337779-prefer-ci-api-endpoint' into 'master'

Prefer CI_API_V4_URL for scripts/api and introduce --endpoint to change it

See merge request gitlab-org/gitlab!67978
parents 5ee883bb e8540f5b
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require 'rubygems'
require 'gitlab' require 'gitlab'
require 'optparse' require 'optparse'
require_relative 'get_job_id' require_relative 'default_options'
class CancelPipeline 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) def initialize(options)
@project = options.delete(:project) @project = options.delete(:project)
@pipeline_id = options.delete(:pipeline_id) @pipeline_id = options.delete(:pipeline_id)
Gitlab.configure do |config| @client = Gitlab.client(
config.endpoint = 'https://gitlab.com/api/v4' endpoint: options.delete(:endpoint) || API::DEFAULT_OPTIONS[:endpoint],
config.private_token = options.delete(:api_token) private_token: options.delete(:api_token)
end )
end end
def execute def execute
Gitlab.cancel_pipeline(project, pipeline_id) client.cancel_pipeline(project, pipeline_id)
end end
private private
attr_reader :project, :pipeline_id attr_reader :project, :pipeline_id, :client
end end
if $0 == __FILE__ if $0 == __FILE__
options = CancelPipeline::DEFAULT_OPTIONS.dup options = API::DEFAULT_OPTIONS.dup
OptionParser.new do |opts| OptionParser.new do |opts|
opts.on("-p", "--project PROJECT", String, "Project where to find the job (defaults to $CI_PROJECT_ID)") do |value| 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__ ...@@ -45,10 +37,14 @@ if $0 == __FILE__
options[:pipeline_id] = value options[:pipeline_id] = value
end 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 options[:api_token] = value
end 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 opts.on("-h", "--help", "Prints this help") do
puts opts puts opts
exit exit
......
# 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
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require 'rubygems'
require 'optparse' require 'optparse'
require 'fileutils' require 'fileutils'
require 'uri' require 'uri'
require 'cgi' require 'cgi'
require 'net/http' require 'net/http'
require_relative 'default_options'
class ArtifactFinder 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) def initialize(options)
@project = options.delete(:project) @project = options.delete(:project)
@job_id = options.delete(:job_id) @job_id = options.delete(:job_id)
@api_token = options.delete(:api_token) @api_token = options.delete(:api_token)
@endpoint = options.delete(:endpoint) || API::DEFAULT_OPTIONS[:endpoint]
@artifact_path = options.delete(:artifact_path) @artifact_path = options.delete(:artifact_path)
warn "No API token given." unless api_token warn "No API token given." unless api_token
end end
def execute 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 if artifact_path
FileUtils.mkdir_p(File.dirname(artifact_path)) FileUtils.mkdir_p(File.dirname(artifact_path))
...@@ -37,7 +32,7 @@ class ArtifactFinder ...@@ -37,7 +32,7 @@ class ArtifactFinder
private 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) def fetch(uri_str, limit = 10)
raise 'Too many HTTP redirects' if limit == 0 raise 'Too many HTTP redirects' if limit == 0
...@@ -66,7 +61,7 @@ class ArtifactFinder ...@@ -66,7 +61,7 @@ class ArtifactFinder
end end
if $0 == __FILE__ if $0 == __FILE__
options = ArtifactFinder::DEFAULT_OPTIONS.dup options = API::DEFAULT_OPTIONS.dup
OptionParser.new do |opts| OptionParser.new do |opts|
opts.on("-p", "--project PROJECT", String, "Project where to find the job (defaults to $CI_PROJECT_ID)") do |value| 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__ ...@@ -85,6 +80,10 @@ if $0 == __FILE__
options[:api_token] = value options[:api_token] = value
end 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 opts.on("-h", "--help", "Prints this help") do
puts opts puts opts
exit exit
......
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen_string_literal: true # frozen_string_literal: true
require 'rubygems'
require 'gitlab' require 'gitlab'
require 'optparse' require 'optparse'
require_relative 'default_options'
class JobFinder class JobFinder
DEFAULT_OPTIONS = { DEFAULT_OPTIONS = API::DEFAULT_OPTIONS.merge(
project: ENV['CI_PROJECT_ID'], pipeline_query: {}.freeze,
pipeline_id: ENV['CI_PIPELINE_ID'], job_query: {}.freeze
pipeline_query: {}, ).freeze
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
def initialize(options) def initialize(options)
@project = options.delete(:project) @project = options.delete(:project)
...@@ -28,10 +24,10 @@ class JobFinder ...@@ -28,10 +24,10 @@ class JobFinder
warn "No API token given." if api_token.empty? warn "No API token given." if api_token.empty?
Gitlab.configure do |config| @client = Gitlab.client(
config.endpoint = 'https://gitlab.com/api/v4' endpoint: options.delete(:endpoint) || DEFAULT_OPTIONS[:endpoint],
config.private_token = api_token private_token: api_token
end )
end end
def execute def execute
...@@ -40,13 +36,13 @@ class JobFinder ...@@ -40,13 +36,13 @@ class JobFinder
private 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 def find_job_with_artifact
return if artifact_path.nil? return if artifact_path.nil?
Gitlab.pipelines(project, pipeline_query_params).auto_paginate do |pipeline| client.pipelines(project, pipeline_query_params).auto_paginate do |pipeline|
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_with_artifact?(job) # rubocop:disable Cop/AvoidReturnFromBlocks return job if found_job_with_artifact?(job) # rubocop:disable Cop/AvoidReturnFromBlocks
end end
end end
...@@ -57,8 +53,8 @@ class JobFinder ...@@ -57,8 +53,8 @@ class JobFinder
def find_job_with_filtered_pipelines def find_job_with_filtered_pipelines
return if pipeline_query.empty? return if pipeline_query.empty?
Gitlab.pipelines(project, pipeline_query_params).auto_paginate do |pipeline| client.pipelines(project, pipeline_query_params).auto_paginate do |pipeline|
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 return job if found_job_by_name?(job) # rubocop:disable Cop/AvoidReturnFromBlocks
end end
end end
...@@ -69,7 +65,7 @@ class JobFinder ...@@ -69,7 +65,7 @@ class JobFinder
def find_job_in_pipeline def find_job_in_pipeline
return unless pipeline_id 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 return job if found_job_by_name?(job) # rubocop:disable Cop/AvoidReturnFromBlocks
end end
...@@ -77,7 +73,7 @@ class JobFinder ...@@ -77,7 +73,7 @@ class JobFinder
end end
def found_job_with_artifact?(job) 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 = HTTParty.head(artifact_url) # rubocop:disable Gitlab/HTTParty
response.success? response.success?
end end
...@@ -108,11 +104,13 @@ if $0 == __FILE__ ...@@ -108,11 +104,13 @@ if $0 == __FILE__
end end
opts.on("-q", "--pipeline-query pipeline_query", String, "Query to pass to the Pipeline API request") do |value| 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 end
opts.on("-Q", "--job-query job_query", String, "Query to pass to the Job API request") do |value| 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 end
opts.on("-j", "--job-name job_name", String, "A job name that needs to exist in the found pipeline") do |value| 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__ ...@@ -127,6 +125,10 @@ if $0 == __FILE__
options[:api_token] = value options[:api_token] = value
end 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 opts.on("-h", "--help", "Prints this help") do
puts opts puts opts
exit exit
......
#!/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
...@@ -10,14 +10,14 @@ function retrieve_tests_metadata() { ...@@ -10,14 +10,14 @@ function retrieve_tests_metadata() {
local test_metadata_job_id local test_metadata_job_id
# Ruby # 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 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 fi
if [[ ! -f "${FLAKY_RSPEC_SUITE_REPORT_PATH}" ]]; then 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 fi
} }
...@@ -48,10 +48,10 @@ function retrieve_tests_mapping() { ...@@ -48,10 +48,10 @@ function retrieve_tests_mapping() {
local artifact_branch="master" local artifact_branch="master"
local test_metadata_with_mapping_job_id 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 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 fi
scripts/unpack-test-mapping "${RSPEC_PACKED_TESTS_MAPPING_PATH}" "${RSPEC_TESTS_MAPPING_PATH}" scripts/unpack-test-mapping "${RSPEC_PACKED_TESTS_MAPPING_PATH}" "${RSPEC_TESTS_MAPPING_PATH}"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment