File: jenkins.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (91 lines) | stat: -rw-r--r-- 2,675 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module Integrations
  class Jenkins < BaseCi
    include HasWebHook

    prepend EnableSslVerification

    field :jenkins_url,
      title: -> { s_('ProjectService|Jenkins server URL') },
      exposes_secrets: true,
      required: true,
      placeholder: 'http://jenkins.example.com',
      help: -> { s_('URL of the Jenkins server.') }

    field :project_name,
      required: true,
      placeholder: 'my_project_name',
      help: -> { s_('Name of the Jenkins project.') }

    field :username,
      help: -> { s_('Username of the Jenkins server.') }

    field :password,
      type: :password,
      help: -> { s_('Password of 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.') }

    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? }
    validates :password, presence: true, if: ->(service) { service.activated? && service.username.present? }

    attribute :merge_requests_events, default: false
    attribute :tag_push_events, default: false

    def execute(data)
      return unless supported_events.include?(data[:object_kind])

      execute_web_hook!(data, "#{data[:object_kind]}_hook")
    end

    def test(data)
      begin
        result = execute(data)
        return { success: false, result: result.message } if result.payload[:http_status] != 200
      rescue StandardError => e
        return { success: false, result: e }
      end

      { success: true, result: result.message }
    end

    override :hook_url
    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

    def url_variables
      {}
    end

    def self.supported_events
      %w[push merge_request tag_push]
    end

    def self.title
      'Jenkins'
    end

    def self.description
      s_('Run CI/CD pipelines with Jenkins.')
    end

    def self.help
      build_help_page_url(
        'integration/jenkins.md',
        s_("Run CI/CD pipelines with Jenkins when you push to a repository, or when a merge request is created, updated, or merged.")
      )
    end

    def self.to_param
      'jenkins'
    end
  end
end