2020-11-25 10:09:13 -05:00
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'gitlab'
require 'optparse'
2021-08-16 02:09:08 -04:00
require_relative 'default_options'
2020-11-25 10:09:13 -05:00
class CancelPipeline
def initialize ( options )
@project = options . delete ( :project )
@pipeline_id = options . delete ( :pipeline_id )
2021-08-16 02:09:08 -04:00
@client = Gitlab . client (
endpoint : options . delete ( :endpoint ) || API :: DEFAULT_OPTIONS [ :endpoint ] ,
private_token : options . delete ( :api_token )
)
2020-11-25 10:09:13 -05:00
end
def execute
2021-08-16 02:09:08 -04:00
client . cancel_pipeline ( project , pipeline_id )
2020-11-25 10:09:13 -05:00
end
private
2021-08-16 02:09:08 -04:00
attr_reader :project , :pipeline_id , :client
2020-11-25 10:09:13 -05:00
end
2022-10-14 17:09:20 -04:00
if $PROGRAM_NAME == __FILE__
2021-08-16 02:09:08 -04:00
options = API :: DEFAULT_OPTIONS . dup
2020-11-25 10:09:13 -05:00
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
2021-08-16 02:09:08 -04:00
opts . on ( " -t " , " --api-token API_TOKEN " , String , " A value API token with the `api` scope " ) do | value |
2020-11-25 10:09:13 -05:00
options [ :api_token ] = value
end
2021-08-16 02:09:08 -04:00
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
2020-11-25 10:09:13 -05:00
opts . on ( " -h " , " --help " , " Prints this help " ) do
puts opts
exit
end
end . parse!
CancelPipeline . new ( options ) . execute
end