2014-12-08 16:54:09 -05:00
|
|
|
class TeamcityService < CiService
|
2017-01-12 17:31:02 -05:00
|
|
|
include ReactiveService
|
|
|
|
|
2014-12-08 16:54:09 -05:00
|
|
|
prop_accessor :teamcity_url, :build_type, :username, :password
|
|
|
|
|
2015-12-01 18:45:36 -05:00
|
|
|
validates :teamcity_url, presence: true, url: true, if: :activated?
|
2014-12-08 16:54:09 -05:00
|
|
|
validates :build_type, presence: true, if: :activated?
|
2015-02-03 00:15:44 -05:00
|
|
|
validates :username,
|
|
|
|
presence: true,
|
2015-12-15 14:37:23 -05:00
|
|
|
if: ->(service) { service.activated? && service.password }
|
2015-02-03 00:15:44 -05:00
|
|
|
validates :password,
|
|
|
|
presence: true,
|
2015-12-15 14:37:23 -05:00
|
|
|
if: ->(service) { service.activated? && service.username }
|
2014-12-08 16:54:09 -05:00
|
|
|
|
|
|
|
attr_accessor :response
|
|
|
|
|
|
|
|
after_save :compose_service_hook, if: :activated?
|
2015-10-09 06:20:38 -04:00
|
|
|
before_update :reset_password
|
2014-12-08 16:54:09 -05:00
|
|
|
|
|
|
|
def compose_service_hook
|
|
|
|
hook = service_hook || build_service_hook
|
|
|
|
hook.save
|
|
|
|
end
|
|
|
|
|
2015-10-09 06:20:38 -04:00
|
|
|
def reset_password
|
2015-10-15 03:09:01 -04:00
|
|
|
if teamcity_url_changed? && !password_touched?
|
2015-10-09 06:20:38 -04:00
|
|
|
self.password = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-08 16:54:09 -05:00
|
|
|
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
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2014-12-08 16:54:09 -05:00
|
|
|
'teamcity'
|
|
|
|
end
|
|
|
|
|
|
|
|
def fields
|
|
|
|
[
|
|
|
|
{ type: 'text', name: 'teamcity_url',
|
2017-05-22 06:07:12 -04:00
|
|
|
placeholder: 'TeamCity root URL like https://teamcity.example.com', required: true },
|
2014-12-08 16:54:09 -05:00
|
|
|
{ type: 'text', name: 'build_type',
|
2017-05-22 06:07:12 -04:00
|
|
|
placeholder: 'Build configuration ID', required: true },
|
2014-12-08 16:54:09 -05:00
|
|
|
{ type: 'text', name: 'username',
|
|
|
|
placeholder: 'A user with permissions to trigger a manual build' },
|
2017-05-03 07:22:03 -04:00
|
|
|
{ type: 'password', name: 'password' }
|
2014-12-08 16:54:09 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2015-03-05 09:58:04 -05:00
|
|
|
def build_page(sha, ref)
|
2017-01-12 17:31:02 -05:00
|
|
|
with_reactive_cache(sha, ref) {|cached| cached[:build_page] }
|
2014-12-08 16:54:09 -05:00
|
|
|
end
|
|
|
|
|
2015-03-05 09:58:04 -05:00
|
|
|
def commit_status(sha, ref)
|
2017-01-12 17:31:02 -05:00
|
|
|
with_reactive_cache(sha, ref) {|cached| cached[:commit_status] }
|
|
|
|
end
|
2014-12-08 16:54:09 -05:00
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def calculate_reactive_cache(sha, ref)
|
|
|
|
response = get_path("httpAuth/app/rest/builds/branch:unspecified:any,number:#{sha}")
|
2014-12-08 16:54:09 -05:00
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
{ build_page: read_build_page(response), commit_status: read_commit_status(response) }
|
2014-12-08 16:54:09 -05:00
|
|
|
end
|
|
|
|
|
2015-02-19 00:02:57 -05:00
|
|
|
def execute(data)
|
2015-02-28 11:33:18 -05:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2015-02-19 00:02:57 -05:00
|
|
|
|
2014-12-08 16:54:09 -05:00
|
|
|
auth = {
|
|
|
|
username: username,
|
2017-05-03 07:22:03 -04:00
|
|
|
password: password
|
2014-12-08 16:54:09 -05:00
|
|
|
}
|
|
|
|
|
2015-03-10 06:51:36 -04:00
|
|
|
branch = Gitlab::Git.ref_name(data[:ref])
|
2014-12-08 16:54:09 -05:00
|
|
|
|
2016-06-01 10:44:39 -04:00
|
|
|
HTTParty.post(
|
|
|
|
build_url('httpAuth/app/rest/buildQueue'),
|
2016-01-05 16:26:31 -05:00
|
|
|
body: "<build branchName=\"#{branch}\">"\
|
|
|
|
"<buildType id=\"#{build_type}\"/>"\
|
|
|
|
'</build>',
|
|
|
|
headers: { 'Content-type' => 'application/xml' },
|
|
|
|
basic_auth: auth
|
|
|
|
)
|
2014-12-08 16:54:09 -05:00
|
|
|
end
|
2016-06-01 10:44:39 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-01-12 17:31:02 -05:00
|
|
|
def read_build_page(response)
|
|
|
|
if response.code != 200
|
|
|
|
# If actual build link can't be determined,
|
|
|
|
# send user to build summary page.
|
|
|
|
build_url("viewLog.html?buildTypeId=#{build_type}")
|
|
|
|
else
|
|
|
|
# If actual build link is available, go to build result page.
|
|
|
|
built_id = response['build']['id']
|
|
|
|
build_url("viewLog.html?buildId=#{built_id}&buildTypeId=#{build_type}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_commit_status(response)
|
|
|
|
return :error unless response.code == 200 || response.code == 404
|
|
|
|
|
|
|
|
status = if response.code == 404
|
|
|
|
'Pending'
|
|
|
|
else
|
|
|
|
response['build']['status']
|
|
|
|
end
|
|
|
|
|
|
|
|
return :error unless status.present?
|
|
|
|
|
|
|
|
if status.include?('SUCCESS')
|
|
|
|
'success'
|
|
|
|
elsif status.include?('FAILURE')
|
|
|
|
'failed'
|
|
|
|
elsif status.include?('Pending')
|
|
|
|
'pending'
|
|
|
|
else
|
|
|
|
:error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-01 10:44:39 -04:00
|
|
|
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
|
2014-12-08 16:54:09 -05:00
|
|
|
end
|