2015-02-02 18:24:40 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: services
|
|
|
|
#
|
2015-03-04 22:14:00 +00:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# type :string(255)
|
|
|
|
# title :string(255)
|
|
|
|
# project_id :integer
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# active :boolean default(FALSE), not null
|
|
|
|
# properties :text
|
|
|
|
# template :boolean default(FALSE)
|
2015-02-19 05:02:57 +00:00
|
|
|
# push_events :boolean default(TRUE)
|
|
|
|
# issues_events :boolean default(TRUE)
|
|
|
|
# merge_requests_events :boolean default(TRUE)
|
|
|
|
# tag_push_events :boolean default(TRUE)
|
2015-03-05 18:38:23 +00:00
|
|
|
# note_events :boolean default(TRUE), not null
|
2015-02-02 18:24:40 +00:00
|
|
|
#
|
|
|
|
|
2015-01-21 00:46:27 +00:00
|
|
|
class IssueTrackerService < Service
|
|
|
|
|
2015-01-27 06:08:27 +00:00
|
|
|
validates :project_url, :issues_url, :new_issue_url, presence: true, if: :activated?
|
|
|
|
|
2015-01-23 18:28:38 +00:00
|
|
|
def category
|
|
|
|
:issue_tracker
|
|
|
|
end
|
|
|
|
|
2015-01-28 17:28:17 +00:00
|
|
|
def default?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2015-01-28 21:19:32 +00:00
|
|
|
def issue_url(iid)
|
|
|
|
self.issues_url.gsub(':id', iid.to_s)
|
|
|
|
end
|
|
|
|
|
2015-01-27 00:24:11 +00:00
|
|
|
def fields
|
|
|
|
[
|
|
|
|
{ type: 'text', name: 'description', placeholder: description },
|
|
|
|
{ type: 'text', name: 'project_url', placeholder: 'Project url' },
|
2015-02-03 04:36:54 +00:00
|
|
|
{ type: 'text', name: 'issues_url', placeholder: 'Issue url' },
|
|
|
|
{ type: 'text', name: 'new_issue_url', placeholder: 'New Issue url' }
|
2015-01-27 00:24:11 +00:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_properties
|
|
|
|
if properties.nil?
|
|
|
|
if enabled_in_gitlab_config
|
|
|
|
self.properties = {
|
|
|
|
title: issues_tracker['title'],
|
2015-03-10 13:06:15 +00:00
|
|
|
project_url: add_issues_tracker_id(issues_tracker['project_url']),
|
|
|
|
issues_url: add_issues_tracker_id(issues_tracker['issues_url']),
|
|
|
|
new_issue_url: add_issues_tracker_id(issues_tracker['new_issue_url'])
|
2015-01-27 00:24:11 +00:00
|
|
|
}
|
2015-01-27 06:08:27 +00:00
|
|
|
else
|
|
|
|
self.properties = {}
|
2015-01-27 00:24:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-28 16:33:18 +00:00
|
|
|
def supported_events
|
|
|
|
%w(push)
|
|
|
|
end
|
|
|
|
|
2015-02-12 21:02:58 +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
|
|
|
|
2015-02-12 21:02:58 +00:00
|
|
|
message = "#{self.type} was unable to reach #{self.project_url}. Check the url and try again."
|
|
|
|
result = false
|
|
|
|
|
|
|
|
begin
|
|
|
|
url = URI.parse(self.project_url)
|
|
|
|
|
|
|
|
if url.host && url.port
|
2015-02-12 21:21:47 +00:00
|
|
|
http = Net::HTTP.start(url.host, url.port, { open_timeout: 5, read_timeout: 5 })
|
2015-02-12 21:02:58 +00:00
|
|
|
response = http.head("/")
|
|
|
|
|
|
|
|
if response
|
|
|
|
message = "#{self.type} received response #{response.code} when attempting to connect to #{self.project_url}"
|
|
|
|
result = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue Timeout::Error, SocketError, Errno::ECONNRESET, Errno::ECONNREFUSED => error
|
|
|
|
message = "#{self.type} had an error when trying to connect to #{self.project_url}: #{error.message}"
|
|
|
|
end
|
|
|
|
Rails.logger.info(message)
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2015-01-27 00:24:11 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def enabled_in_gitlab_config
|
|
|
|
Gitlab.config.issues_tracker &&
|
|
|
|
Gitlab.config.issues_tracker.values.any? &&
|
|
|
|
issues_tracker
|
|
|
|
end
|
|
|
|
|
|
|
|
def issues_tracker
|
|
|
|
Gitlab.config.issues_tracker[to_param]
|
|
|
|
end
|
|
|
|
|
2015-03-10 13:06:15 +00:00
|
|
|
def add_issues_tracker_id(url)
|
2015-02-12 01:34:41 +00:00
|
|
|
if self.project
|
|
|
|
id = self.project.issues_tracker_id
|
2015-01-27 00:24:11 +00:00
|
|
|
|
2015-02-12 01:34:41 +00:00
|
|
|
if id
|
2015-03-10 13:06:15 +00:00
|
|
|
url = url.gsub(":issues_tracker_id", id)
|
2015-02-12 01:34:41 +00:00
|
|
|
end
|
2015-01-27 00:24:11 +00:00
|
|
|
end
|
2015-02-12 01:34:41 +00:00
|
|
|
|
2015-03-10 13:06:15 +00:00
|
|
|
url
|
2015-01-27 00:24:11 +00:00
|
|
|
end
|
2015-01-21 00:46:27 +00:00
|
|
|
end
|