Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-11-25 18:09:13 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-11-25 18:09:13 +0300
commit36b55e94e16da524ce0dbee0558e0b95300f0063 (patch)
treeb39204e8a006811d7e85ac932ca74f568624e2b6 /scripts/api/get_job_id
parenta9b7ab6f2b734409762e4a42dac660d23c636d97 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'scripts/api/get_job_id')
-rwxr-xr-xscripts/api/get_job_id124
1 files changed, 124 insertions, 0 deletions
diff --git a/scripts/api/get_job_id b/scripts/api/get_job_id
new file mode 100755
index 00000000000..5928af81282
--- /dev/null
+++ b/scripts/api/get_job_id
@@ -0,0 +1,124 @@
+#!/usr/bin/env ruby
+# frozen_string_literal: true
+
+require 'rubygems'
+require 'gitlab'
+require 'optparse'
+require 'cgi'
+
+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
+
+ def initialize(options)
+ @project = options.delete(:project)
+ @pipeline_query = options.delete(:pipeline_query)
+ @job_query = options.delete(:job_query)
+ @pipeline_id = options.delete(:pipeline_id)
+ @job_name = options.delete(:job_name)
+ @api_token = options.delete(:api_token)
+
+ Gitlab.configure do |config|
+ config.endpoint = 'https://gitlab.com/api/v4'
+ config.private_token = api_token if api_token
+ end
+
+ warn "No API token given." unless api_token
+ end
+
+ def execute
+ find_job_with_filtered_pipelines || find_job_in_pipeline
+ end
+
+ private
+
+ attr_reader :project, :pipeline_query, :job_query, :pipeline_id, :job_name, :api_token
+
+ def find_job_with_filtered_pipelines
+ return if pipeline_query.empty?
+
+ Gitlab.get(
+ "/projects/#{CGI.escape(project)}/pipelines",
+ query: pipeline_query_params,
+ unauthenticated: api_token.nil?
+ ).auto_paginate do |pipeline|
+ Gitlab.get(
+ "/projects/#{CGI.escape(project)}/pipelines/#{pipeline.id}/jobs",
+ query: job_query_params,
+ unauthenticated: api_token.nil?
+ ).auto_paginate do |job|
+ return job if job.name == job_name # rubocop:disable Cop/AvoidReturnFromBlocks
+ end
+ end
+
+ raise 'Job not found!'
+ end
+
+ def find_job_in_pipeline
+ return unless pipeline_id
+
+ Gitlab.get(
+ "/projects/#{CGI.escape(project)}/pipelines/#{pipeline_id}/jobs",
+ query: job_query_params,
+ unauthenticated: api_token.nil?
+ ).auto_paginate do |job|
+ return job if job.name == job_name # rubocop:disable Cop/AvoidReturnFromBlocks
+ end
+
+ raise 'Job not found!'
+ end
+
+ def pipeline_query_params
+ @pipeline_query_params ||= { per_page: 100, **pipeline_query }
+ end
+
+ def job_query_params
+ @job_query_params ||= { per_page: 100, **job_query }
+ end
+end
+
+if $0 == __FILE__
+ options = JobFinder::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("-i", "--pipeline-id pipeline_id", String, "A pipeline ID (defaults to $CI_PIPELINE_ID)") do |value|
+ options[:pipeline_id] = value
+ 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('=')])
+ 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('=')])
+ 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!
+
+ job = JobFinder.new(options).execute
+
+ return if job.nil?
+
+ puts job.id
+end