gitlab-org--gitlab-foss/app/models/project_services/issue_tracker_service.rb

121 lines
3 KiB
Ruby
Raw Normal View History

2015-02-02 13:24:40 -05:00
# == Schema Information
#
# Table name: services
#
2015-03-04 17:14:00 -05: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)
# push_events :boolean default(TRUE)
# issues_events :boolean default(TRUE)
# merge_requests_events :boolean default(TRUE)
# tag_push_events :boolean default(TRUE)
# note_events :boolean default(TRUE), not null
2015-02-02 13:24:40 -05:00
#
2015-01-20 19:46:27 -05:00
class IssueTrackerService < Service
validates :project_url, :issues_url, :new_issue_url, presence: true, if: :activated?
def category
:issue_tracker
end
def default?
false
end
2015-01-28 16:19:32 -05:00
def issue_url(iid)
self.issues_url.gsub(':id', iid.to_s)
end
def project_path
project_url
end
def new_issue_path
new_issue_url
end
def issue_path(iid)
issue_url(iid)
end
2015-01-26 19:24:11 -05:00
def fields
[
{ type: 'text', name: 'description', placeholder: description },
{ type: 'text', name: 'project_url', placeholder: 'Project url' },
{ type: 'text', name: 'issues_url', placeholder: 'Issue url' },
{ type: 'text', name: 'new_issue_url', placeholder: 'New Issue url' }
2015-01-26 19:24:11 -05:00
]
end
def initialize_properties
if properties.nil?
if enabled_in_gitlab_config
self.properties = {
title: issues_tracker['title'],
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-26 19:24:11 -05:00
}
else
self.properties = {}
2015-01-26 19:24:11 -05:00
end
end
end
def supported_events
%w(push)
end
def execute(data)
return unless supported_events.include?(data[:object_kind])
message = "#{self.type} was unable to reach #{self.project_url}. Check the url and try again."
result = false
begin
response = HTTParty.head(self.project_url, verify: true)
if response
message = "#{self.type} received response #{response.code} when attempting to connect to #{self.project_url}"
result = true
end
rescue HTTParty::Error, 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-26 19:24:11 -05: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
def add_issues_tracker_id(url)
2015-02-11 20:34:41 -05:00
if self.project
id = self.project.issues_tracker_id
2015-01-26 19:24:11 -05:00
2015-02-11 20:34:41 -05:00
if id
url = url.gsub(":issues_tracker_id", id)
2015-02-11 20:34:41 -05:00
end
2015-01-26 19:24:11 -05:00
end
2015-02-11 20:34:41 -05:00
url
2015-01-26 19:24:11 -05:00
end
2015-01-20 19:46:27 -05:00
end