2015-01-20 19:46:27 -05:00
|
|
|
class JiraService < IssueTrackerService
|
2017-07-07 11:43:37 -04:00
|
|
|
include Gitlab::Routing
|
2015-01-20 19:46:27 -05:00
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
validates :url, url: true, presence: true, if: :activated?
|
2017-05-23 05:14:43 -04:00
|
|
|
validates :api_url, url: true, allow_blank: true
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2017-07-25 11:25:41 -04:00
|
|
|
prop_accessor :username, :password, :url, :api_url, :jira_issue_transition_id, :title, :description
|
2016-04-21 11:13:14 -04:00
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
before_update :reset_password
|
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
# This is confusing, but JiraService does not really support these events.
|
|
|
|
# The values here are required to display correct options in the service
|
|
|
|
# configuration screen.
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.supported_events
|
2016-11-14 16:30:01 -05:00
|
|
|
%w(commit merge_request)
|
|
|
|
end
|
|
|
|
|
2016-10-06 18:05:27 -04:00
|
|
|
# {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1
|
2017-07-10 03:38:42 -04:00
|
|
|
def self.reference_pattern(only_long: true)
|
2016-10-06 18:05:27 -04:00
|
|
|
@reference_pattern ||= %r{(?<issue>\b([A-Z][A-Z0-9_]+-)\d+)}
|
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
def initialize_properties
|
|
|
|
super do
|
|
|
|
self.properties = {
|
|
|
|
title: issues_tracker['title'],
|
2017-05-23 05:14:43 -04:00
|
|
|
url: issues_tracker['url'],
|
|
|
|
api_url: issues_tracker['api_url']
|
2016-09-29 17:11:32 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
def reset_password
|
2017-05-23 05:14:43 -04:00
|
|
|
self.password = nil if reset_password?
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
2015-01-20 19:46:27 -05:00
|
|
|
|
2016-01-14 09:20:23 -05:00
|
|
|
def options
|
2017-05-23 05:14:43 -04:00
|
|
|
url = URI.parse(client_url)
|
2016-09-29 17:11:32 -04:00
|
|
|
|
2016-01-14 09:20:23 -05:00
|
|
|
{
|
2016-09-29 17:11:32 -04:00
|
|
|
username: self.username,
|
|
|
|
password: self.password,
|
|
|
|
site: URI.join(url, '/').to_s,
|
|
|
|
context_path: url.path,
|
|
|
|
auth_type: :basic,
|
|
|
|
read_timeout: 120,
|
|
|
|
use_ssl: url.scheme == 'https'
|
2016-01-14 09:20:23 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def client
|
2016-09-29 17:11:32 -04:00
|
|
|
@client ||= JIRA::Client.new(options)
|
2016-01-14 09:20:23 -05:00
|
|
|
end
|
|
|
|
|
2015-02-12 20:06:55 -05:00
|
|
|
def help
|
2017-01-11 06:54:04 -05:00
|
|
|
"You need to configure JIRA before enabling this service. For more details
|
2016-11-22 08:12:09 -05:00
|
|
|
read the
|
2017-03-31 02:08:36 -04:00
|
|
|
[JIRA service documentation](#{help_page_url('user/project/integrations/jira')})."
|
2015-02-12 20:06:55 -05:00
|
|
|
end
|
|
|
|
|
2015-01-20 19:46:27 -05:00
|
|
|
def title
|
|
|
|
if self.properties && self.properties['title'].present?
|
|
|
|
self.properties['title']
|
|
|
|
else
|
|
|
|
'JIRA'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
|
|
|
if self.properties && self.properties['description'].present?
|
|
|
|
self.properties['description']
|
|
|
|
else
|
|
|
|
'Jira issue tracker'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-27 07:44:24 -05:00
|
|
|
def self.to_param
|
2015-01-20 19:46:27 -05:00
|
|
|
'jira'
|
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
|
|
|
|
def fields
|
2016-01-14 09:20:23 -05:00
|
|
|
[
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'text', name: 'url', title: 'Web URL', placeholder: 'https://jira.example.com', required: true },
|
2017-05-23 05:14:43 -04:00
|
|
|
{ type: 'text', name: 'api_url', title: 'JIRA API URL', placeholder: 'If different from Web URL' },
|
2017-05-22 06:07:12 -04:00
|
|
|
{ type: 'text', name: 'username', placeholder: '', required: true },
|
|
|
|
{ type: 'password', name: 'password', placeholder: '', required: true },
|
2017-07-25 11:25:41 -04:00
|
|
|
{ type: 'text', name: 'jira_issue_transition_id', title: 'Transition ID', placeholder: '' }
|
2016-01-14 09:20:23 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def issues_url
|
|
|
|
"#{url}/browse/:id"
|
|
|
|
end
|
|
|
|
|
|
|
|
def new_issue_url
|
|
|
|
"#{url}/secure/CreateIssue.jspa"
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
def execute(push)
|
|
|
|
# This method is a no-op, because currently JiraService does not
|
|
|
|
# support any events.
|
|
|
|
end
|
2016-11-09 16:55:21 -05:00
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
def close_issue(entity, external_issue)
|
|
|
|
issue = jira_request { client.Issue.find(external_issue.iid) }
|
2016-11-09 16:55:21 -05:00
|
|
|
|
2017-08-01 15:19:54 -04:00
|
|
|
return if issue.nil? || has_resolution?(issue) || !jira_issue_transition_id.present?
|
2016-12-01 03:45:06 -05:00
|
|
|
|
|
|
|
commit_id = if entity.is_a?(Commit)
|
|
|
|
entity.id
|
|
|
|
elsif entity.is_a?(MergeRequest)
|
|
|
|
entity.diff_head_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
commit_url = build_entity_url(:commit, commit_id)
|
|
|
|
|
|
|
|
# Depending on the JIRA project's workflow, a comment during transition
|
|
|
|
# may or may not be allowed. Refresh the issue after transition and check
|
|
|
|
# if it is closed, so we don't have one comment for every commit.
|
|
|
|
issue = jira_request { client.Issue.find(issue.key) } if transition_issue(issue)
|
2017-08-01 15:19:54 -04:00
|
|
|
add_issue_solved_comment(issue, commit_id, commit_url) if has_resolution?(issue)
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_cross_reference_note(mentioned, noteable, author)
|
2016-11-14 16:30:01 -05:00
|
|
|
unless can_cross_reference?(noteable)
|
|
|
|
return "Events for #{noteable.model_name.plural.humanize(capitalize: false)} are disabled."
|
|
|
|
end
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
jira_issue = jira_request { client.Issue.find(mentioned.id) }
|
|
|
|
|
2016-11-14 16:30:01 -05:00
|
|
|
return unless jira_issue.present?
|
2016-11-09 16:55:21 -05:00
|
|
|
|
2016-11-17 15:46:31 -05:00
|
|
|
noteable_id = noteable.respond_to?(:iid) ? noteable.iid : noteable.id
|
|
|
|
noteable_type = noteable_name(noteable)
|
|
|
|
entity_url = build_entity_url(noteable_type, noteable_id)
|
2015-12-17 17:08:14 -05:00
|
|
|
|
|
|
|
data = {
|
|
|
|
user: {
|
|
|
|
name: author.name,
|
2017-05-03 07:22:03 -04:00
|
|
|
url: resource_url(user_path(author))
|
2015-12-17 17:08:14 -05:00
|
|
|
},
|
|
|
|
project: {
|
2017-07-20 05:34:09 -04:00
|
|
|
name: project.full_path,
|
2017-06-29 13:06:35 -04:00
|
|
|
url: resource_url(namespace_project_path(project.namespace, project)) # rubocop:disable Cop/ProjectPathHelper
|
2015-12-17 17:08:14 -05:00
|
|
|
},
|
|
|
|
entity: {
|
2016-11-17 15:46:31 -05:00
|
|
|
name: noteable_type.humanize.downcase,
|
2016-09-29 17:11:32 -04:00
|
|
|
url: entity_url,
|
|
|
|
title: noteable.title
|
2015-12-17 17:08:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
add_comment(data, jira_issue)
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-10-27 17:20:47 -04:00
|
|
|
# reason why service cannot be tested
|
|
|
|
def disabled_title
|
|
|
|
"Please fill in Password and Username."
|
|
|
|
end
|
|
|
|
|
2016-12-01 03:45:06 -05:00
|
|
|
def test(_)
|
|
|
|
result = test_settings
|
2017-07-26 03:57:56 -04:00
|
|
|
success = result.present?
|
|
|
|
result = @error if @error && !success
|
|
|
|
|
|
|
|
{ success: success, result: result }
|
2016-12-01 03:45:06 -05:00
|
|
|
end
|
|
|
|
|
2016-10-27 17:20:47 -04:00
|
|
|
# JIRA does not need test data.
|
|
|
|
# We are requesting the project that belongs to the project key.
|
|
|
|
def test_data(user = nil, project = nil)
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
def test_settings
|
2017-05-23 05:14:43 -04:00
|
|
|
return unless client_url.present?
|
2016-01-14 09:20:23 -05:00
|
|
|
# Test settings by getting the project
|
2017-07-25 11:25:41 -04:00
|
|
|
jira_request { client.ServerInfo.all.attrs }
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-14 16:30:01 -05:00
|
|
|
def can_cross_reference?(noteable)
|
|
|
|
case noteable
|
|
|
|
when Commit then commit_events
|
|
|
|
when MergeRequest then merge_requests_events
|
|
|
|
else true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
def transition_issue(issue)
|
2016-01-14 09:20:23 -05:00
|
|
|
issue.transitions.build.save(transition: { id: jira_issue_transition_id })
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def add_issue_solved_comment(issue, commit_id, commit_url)
|
2016-11-09 16:55:21 -05:00
|
|
|
link_title = "GitLab: Solved by commit #{commit_id}."
|
|
|
|
comment = "Issue solved with [#{commit_id}|#{commit_url}]."
|
|
|
|
link_props = build_remote_link_props(url: commit_url, title: link_title, resolved: true)
|
|
|
|
send_message(issue, comment, link_props)
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
def add_comment(data, issue)
|
|
|
|
user_name = data[:user][:name]
|
|
|
|
user_url = data[:user][:url]
|
|
|
|
entity_name = data[:entity][:name]
|
|
|
|
entity_url = data[:entity][:url]
|
2016-09-29 17:11:32 -04:00
|
|
|
entity_title = data[:entity][:title]
|
2015-12-17 17:08:14 -05:00
|
|
|
project_name = data[:project][:name]
|
|
|
|
|
2016-11-04 15:36:37 -04:00
|
|
|
message = "[#{user_name}|#{user_url}] mentioned this issue in [a #{entity_name} of #{project_name}|#{entity_url}]:\n'#{entity_title.chomp}'"
|
2016-11-09 16:55:21 -05:00
|
|
|
link_title = "GitLab: Mentioned on #{entity_name} - #{entity_title}"
|
|
|
|
link_props = build_remote_link_props(url: entity_url, title: link_title)
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
unless comment_exists?(issue, message)
|
|
|
|
send_message(issue, message, link_props)
|
2016-09-29 17:11:32 -04:00
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2017-08-01 15:19:54 -04:00
|
|
|
def has_resolution?(issue)
|
|
|
|
issue.respond_to?(:resolution) && issue.resolution.present?
|
|
|
|
end
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
def comment_exists?(issue, message)
|
|
|
|
comments = jira_request { issue.comments }
|
|
|
|
|
|
|
|
comments.present? && comments.any? { |comment| comment.body.include?(message) }
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
def send_message(issue, message, remote_link_props)
|
2017-05-23 05:14:43 -04:00
|
|
|
return unless client_url.present?
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
jira_request do
|
2017-05-18 02:07:48 -04:00
|
|
|
remote_link = find_remote_link(issue, remote_link_props[:object][:url])
|
|
|
|
if remote_link
|
2016-11-09 16:55:21 -05:00
|
|
|
remote_link.save!(remote_link_props)
|
2017-05-18 02:07:48 -04:00
|
|
|
elsif issue.comments.build.save!(body: message)
|
|
|
|
new_remote_link = issue.remotelink.build
|
|
|
|
new_remote_link.save!(remote_link_props)
|
2016-11-09 16:55:21 -05:00
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2017-05-18 02:07:48 -04:00
|
|
|
result_message = "#{self.class.name} SUCCESS: Successfully posted to #{client_url}."
|
2016-11-09 16:55:21 -05:00
|
|
|
Rails.logger.info(result_message)
|
|
|
|
result_message
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
2016-11-09 16:55:21 -05:00
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2017-05-18 02:07:48 -04:00
|
|
|
def find_remote_link(issue, url)
|
|
|
|
links = jira_request { issue.remotelink.all }
|
|
|
|
|
|
|
|
links.find { |link| link.object["url"] == url }
|
|
|
|
end
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
def build_remote_link_props(url:, title:, resolved: false)
|
|
|
|
status = {
|
|
|
|
resolved: resolved
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
GlobalID: 'GitLab',
|
|
|
|
object: {
|
|
|
|
url: url,
|
|
|
|
title: title,
|
|
|
|
status: status,
|
|
|
|
icon: { title: 'GitLab', url16x16: 'https://gitlab.com/favicon.ico' }
|
|
|
|
}
|
|
|
|
}
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def resource_url(resource)
|
2016-09-01 01:50:27 -04:00
|
|
|
"#{Settings.gitlab.base_url.chomp("/")}#{resource}"
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-11-17 15:46:31 -05:00
|
|
|
def build_entity_url(noteable_type, entity_id)
|
2016-11-05 09:59:08 -04:00
|
|
|
polymorphic_url(
|
|
|
|
[
|
|
|
|
self.project.namespace.becomes(Namespace),
|
|
|
|
self.project,
|
2016-11-17 15:46:31 -05:00
|
|
|
noteable_type.to_sym
|
2016-11-05 09:59:08 -04:00
|
|
|
],
|
|
|
|
id: entity_id,
|
|
|
|
host: Settings.gitlab.base_url
|
2015-12-17 17:08:14 -05:00
|
|
|
)
|
|
|
|
end
|
2016-11-09 16:55:21 -05:00
|
|
|
|
2016-11-17 15:46:31 -05:00
|
|
|
def noteable_name(noteable)
|
|
|
|
name = noteable.model_name.singular
|
|
|
|
|
|
|
|
# ProjectSnippet inherits from Snippet class so it causes
|
|
|
|
# routing error building the URL.
|
|
|
|
name == "project_snippet" ? "snippet" : name
|
|
|
|
end
|
|
|
|
|
2016-11-09 16:55:21 -05:00
|
|
|
# Handle errors when doing JIRA API calls
|
|
|
|
def jira_request
|
|
|
|
yield
|
|
|
|
|
2017-05-18 02:20:16 -04:00
|
|
|
rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => e
|
2017-07-26 03:57:56 -04:00
|
|
|
@error = e.message
|
|
|
|
Rails.logger.info "#{self.class.name} Send message ERROR: #{client_url} - #{@error}"
|
2016-11-09 16:55:21 -05:00
|
|
|
nil
|
|
|
|
end
|
2017-05-23 05:14:43 -04:00
|
|
|
|
|
|
|
def client_url
|
|
|
|
api_url.present? ? api_url : url
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_password?
|
|
|
|
# don't reset the password if a new one is provided
|
|
|
|
return false if password_touched?
|
|
|
|
return true if api_url_changed?
|
|
|
|
return false if api_url.present?
|
|
|
|
|
|
|
|
url_changed?
|
|
|
|
end
|
2015-01-20 19:46:27 -05:00
|
|
|
end
|