BigW Consortium Gitlab

teamcity_service.rb 3.49 KB
Newer Older
1 2 3
class TeamcityService < CiService
  prop_accessor :teamcity_url, :build_type, :username, :password

4
  validates :teamcity_url, presence: true, url: true, if: :activated?
5
  validates :build_type, presence: true, if: :activated?
6 7
  validates :username,
    presence: true,
8
    if: ->(service) { service.activated? && service.password }
9 10
  validates :password,
    presence: true,
11
    if: ->(service) { service.activated? && service.username }
12 13 14 15

  attr_accessor :response

  after_save :compose_service_hook, if: :activated?
16
  before_update :reset_password
17 18 19 20 21 22

  def compose_service_hook
    hook = service_hook || build_service_hook
    hook.save
  end

23
  def reset_password
24
    if teamcity_url_changed? && !password_touched?
25 26 27 28
      self.password = nil
    end
  end

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  def title
    'JetBrains TeamCity CI'
  end

  def description
    'A continuous integration and build server'
  end

  def help
    'The build configuration in Teamcity must use the build format '\
    'number %build.vcs.number% '\
    'you will also want to configure monitoring of all branches so merge '\
    'requests build, that setting is in the vsc root advanced settings.'
  end

  def to_param
    'teamcity'
  end

48 49 50 51
  def supported_events
    %w(push)
  end

52 53 54 55 56 57 58 59 60 61 62 63 64
  def fields
    [
      { type: 'text', name: 'teamcity_url',
        placeholder: 'TeamCity root URL like https://teamcity.example.com' },
      { type: 'text', name: 'build_type',
        placeholder: 'Build configuration ID' },
      { type: 'text', name: 'username',
        placeholder: 'A user with permissions to trigger a manual build' },
      { type: 'password', name: 'password' },
    ]
  end

  def build_info(sha)
65
    @response = get_path("httpAuth/app/rest/builds/branch:unspecified:any,number:#{sha}")
66 67
  end

Valery Sizov committed
68
  def build_page(sha, ref)
69 70 71 72 73
    build_info(sha) if @response.nil? || !@response.code

    if @response.code != 200
      # If actual build link can't be determined,
      # send user to build summary page.
74
      build_url("viewLog.html?buildTypeId=#{build_type}")
75 76 77
    else
      # If actual build link is available, go to build result page.
      built_id = @response['build']['id']
78
      build_url("viewLog.html?buildId=#{built_id}&buildTypeId=#{build_type}")
79 80 81
    end
  end

Valery Sizov committed
82
  def commit_status(sha, ref)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    build_info(sha) if @response.nil? || !@response.code
    return :error unless @response.code == 200 || @response.code == 404

    status = if @response.code == 404
               'Pending'
             else
               @response['build']['status']
             end

    if status.include?('SUCCESS')
      'success'
    elsif status.include?('FAILURE')
      'failed'
    elsif status.include?('Pending')
      'pending'
    else
      :error
    end
  end

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

106 107 108 109 110
    auth = {
      username: username,
      password: password,
    }

111
    branch = Gitlab::Git.ref_name(data[:ref])
112

113 114
    HTTParty.post(
      build_url('httpAuth/app/rest/buildQueue'),
115 116 117 118 119 120
      body: "<build branchName=\"#{branch}\">"\
            "<buildType id=\"#{build_type}\"/>"\
            '</build>',
      headers: { 'Content-type' => 'application/xml' },
      basic_auth: auth
    )
121
  end
122 123 124 125 126 127 128 129 130 131 132 133 134 135

  private

  def build_url(path)
    URI.join("#{teamcity_url}/", path).to_s
  end

  def get_path(path)
    HTTParty.get(build_url(path), verify: false,
                                  basic_auth: {
                                    username: username,
                                    password: password
                                  })
  end
136
end