2021-09-04 01:27:46 +05:30
# frozen_string_literal: true
module Integrations
class Jenkins < BaseCi
2021-09-30 23:02:18 +05:30
include HasWebHook
2022-05-07 20:08:51 +05:30
2022-02-05 19:09:49 +05:30
prepend EnableSslVerification
2021-09-04 01:27:46 +05:30
2022-07-16 23:28:13 +05:30
field :jenkins_url ,
2022-07-23 23:45:48 +05:30
title : - > { s_ ( 'ProjectService|Jenkins server URL' ) } ,
2023-01-13 00:05:48 +05:30
exposes_secrets : true ,
2022-07-16 23:28:13 +05:30
required : true ,
placeholder : 'http://jenkins.example.com' ,
2022-07-23 23:45:48 +05:30
help : - > { s_ ( 'The URL of the Jenkins server.' ) }
2022-07-16 23:28:13 +05:30
field :project_name ,
required : true ,
placeholder : 'my_project_name' ,
2022-07-23 23:45:48 +05:30
help : - > { s_ ( 'The name of the Jenkins project. Copy the name from the end of the URL to the project.' ) }
2022-07-16 23:28:13 +05:30
field :username ,
2022-07-23 23:45:48 +05:30
help : - > { s_ ( 'The username for the Jenkins server.' ) }
2022-07-16 23:28:13 +05:30
field :password ,
type : 'password' ,
2022-07-23 23:45:48 +05:30
help : - > { s_ ( 'The password for the Jenkins server.' ) } ,
non_empty_password_title : - > { s_ ( 'ProjectService|Enter new password.' ) } ,
non_empty_password_help : - > { s_ ( 'ProjectService|Leave blank to use your current password.' ) }
2021-09-04 01:27:46 +05:30
validates :jenkins_url , presence : true , addressable_url : true , if : :activated?
validates :project_name , presence : true , if : :activated?
validates :username , presence : true , if : - > ( service ) { service . activated? && service . password_touched? && service . password . present? }
2023-01-13 00:05:48 +05:30
validates :password , presence : true , if : - > ( service ) { service . activated? && service . username . present? }
2021-09-04 01:27:46 +05:30
2023-01-13 00:05:48 +05:30
attribute :merge_requests_events , default : false
attribute :tag_push_events , default : false
2021-09-04 01:27:46 +05:30
def execute ( data )
return unless supported_events . include? ( data [ :object_kind ] )
2021-09-30 23:02:18 +05:30
execute_web_hook! ( data , " #{ data [ :object_kind ] } _hook " )
2021-09-04 01:27:46 +05:30
end
def test ( data )
begin
result = execute ( data )
2023-01-13 00:05:48 +05:30
return { success : false , result : result . message } if result . payload [ :http_status ] != 200
2022-08-27 11:52:29 +05:30
rescue StandardError = > e
return { success : false , result : e }
2021-09-04 01:27:46 +05:30
end
2023-01-13 00:05:48 +05:30
{ success : true , result : result . message }
2021-09-04 01:27:46 +05:30
end
2021-09-30 23:02:18 +05:30
override :hook_url
2021-09-04 01:27:46 +05:30
def hook_url
url = URI . parse ( jenkins_url )
url . path = File . join ( url . path || '/' , " project/ #{ project_name } " )
url . user = ERB :: Util . url_encode ( username ) unless username . blank?
url . password = ERB :: Util . url_encode ( password ) unless password . blank?
url . to_s
end
2022-10-02 17:18:49 +05:30
def url_variables
{ }
end
2021-09-04 01:27:46 +05:30
def self . supported_events
%w( push merge_request tag_push )
end
def title
'Jenkins'
end
def description
s_ ( 'Run CI/CD pipelines with Jenkins.' )
end
def help
2022-05-07 20:08:51 +05:30
docs_link = ActionController :: Base . helpers . link_to _ ( 'Learn more.' ) , Rails . application . routes . url_helpers . help_page_url ( 'integration/jenkins' ) , target : '_blank' , rel : 'noopener noreferrer'
2021-09-04 01:27:46 +05:30
s_ ( 'Run CI/CD pipelines with Jenkins when you push to a repository, or when a merge request is created, updated, or merged. %{docs_link}' ) . html_safe % { docs_link : docs_link . html_safe }
end
def self . to_param
'jenkins'
end
end
end