2014-12-08 21:54:09 +00:00
|
|
|
class TeamcityService < CiService
|
|
|
|
prop_accessor :teamcity_url, :build_type, :username, :password
|
|
|
|
|
2015-12-01 23:45:36 +00:00
|
|
|
validates :teamcity_url, presence: true, url: true, if: :activated?
|
2014-12-08 21:54:09 +00:00
|
|
|
validates :build_type, presence: true, if: :activated?
|
2015-02-03 05:15:44 +00:00
|
|
|
validates :username,
|
|
|
|
presence: true,
|
2015-12-15 19:37:23 +00:00
|
|
|
if: ->(service) { service.activated? && service.password }
|
2015-02-03 05:15:44 +00:00
|
|
|
validates :password,
|
|
|
|
presence: true,
|
2015-12-15 19:37:23 +00:00
|
|
|
if: ->(service) { service.activated? && service.username }
|
2014-12-08 21:54:09 +00:00
|
|
|
|
|
|
|
attr_accessor :response
|
|
|
|
|
|
|
|
after_save :compose_service_hook, if: :activated?
|
2015-10-09 10:20:38 +00:00
|
|
|
before_update :reset_password
|
2014-12-08 21:54:09 +00:00
|
|
|
|
|
|
|
def compose_service_hook
|
|
|
|
hook = service_hook || build_service_hook
|
|
|
|
hook.save
|
|
|
|
end
|
|
|
|
|
2015-10-09 10:20:38 +00:00
|
|
|
def reset_password
|
2015-10-15 07:09:01 +00:00
|
|
|
if teamcity_url_changed? && !password_touched?
|
2015-10-09 10:20:38 +00:00
|
|
|
self.password = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-08 21:54:09 +00: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
|
|
|
|
|
|
|
|
def to_param
|
|
|
|
'teamcity'
|
|
|
|
end
|
|
|
|
|
2015-02-28 16:33:18 +00:00
|
|
|
def supported_events
|
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
2014-12-08 21:54:09 +00:00
|
|
|
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)
|
2016-06-01 14:44:39 +00:00
|
|
|
@response = get_path("httpAuth/app/rest/builds/branch:unspecified:any,number:#{sha}")
|
2014-12-08 21:54:09 +00:00
|
|
|
end
|
|
|
|
|
2015-03-05 14:58:04 +00:00
|
|
|
def build_page(sha, ref)
|
2014-12-08 21:54:09 +00:00
|
|
|
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.
|
2016-06-01 14:44:39 +00:00
|
|
|
build_url("viewLog.html?buildTypeId=#{build_type}")
|
2014-12-08 21:54:09 +00:00
|
|
|
else
|
|
|
|
# If actual build link is available, go to build result page.
|
|
|
|
built_id = @response['build']['id']
|
2016-06-01 14:44:39 +00:00
|
|
|
build_url("viewLog.html?buildId=#{built_id}&buildTypeId=#{build_type}")
|
2014-12-08 21:54:09 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-05 14:58:04 +00:00
|
|
|
def commit_status(sha, ref)
|
2014-12-08 21:54:09 +00:00
|
|
|
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
|
|
|
|
|
2015-02-19 05:02:57 +00:00
|
|
|
def execute(data)
|
2015-02-28 16:33:18 +00:00
|
|
|
return unless supported_events.include?(data[:object_kind])
|
2015-02-19 05:02:57 +00:00
|
|
|
|
2014-12-08 21:54:09 +00:00
|
|
|
auth = {
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
}
|
|
|
|
|
2015-03-10 10:51:36 +00:00
|
|
|
branch = Gitlab::Git.ref_name(data[:ref])
|
2014-12-08 21:54:09 +00:00
|
|
|
|
2016-06-01 14:44:39 +00:00
|
|
|
HTTParty.post(
|
|
|
|
build_url('httpAuth/app/rest/buildQueue'),
|
2016-01-05 21:26:31 +00:00
|
|
|
body: "<build branchName=\"#{branch}\">"\
|
|
|
|
"<buildType id=\"#{build_type}\"/>"\
|
|
|
|
'</build>',
|
|
|
|
headers: { 'Content-type' => 'application/xml' },
|
|
|
|
basic_auth: auth
|
|
|
|
)
|
2014-12-08 21:54:09 +00:00
|
|
|
end
|
2016-06-01 14:44:39 +00:00
|
|
|
|
|
|
|
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
|
2014-12-08 21:54:09 +00:00
|
|
|
end
|