2016-01-14 09:20:23 -05:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: services
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# build_events :boolean default(FALSE), not null
|
|
|
|
#
|
|
|
|
|
2015-01-20 19:46:27 -05:00
|
|
|
class JiraService < IssueTrackerService
|
2016-09-29 17:11:32 -04:00
|
|
|
include Gitlab::Routing.url_helpers
|
2015-01-20 19:46:27 -05:00
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
validates :url, url: true, presence: true, if: :activated?
|
|
|
|
validates :project_key, presence: true, if: :activated?
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-01-14 09:20:23 -05:00
|
|
|
prop_accessor :username, :password, :url, :project_key,
|
|
|
|
: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-10-06 18:05:27 -04:00
|
|
|
# {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1
|
|
|
|
def reference_pattern
|
|
|
|
@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'],
|
|
|
|
url: issues_tracker['url']
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
def reset_password
|
|
|
|
# don't reset the password if a new one is provided
|
2016-01-14 09:20:23 -05:00
|
|
|
if url_changed? && !password_touched?
|
2015-12-17 17:08:14 -05:00
|
|
|
self.password = nil
|
|
|
|
end
|
|
|
|
end
|
2015-01-20 19:46:27 -05:00
|
|
|
|
2016-01-14 09:20:23 -05:00
|
|
|
def options
|
|
|
|
url = URI.parse(self.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
|
|
|
|
|
|
|
|
def jira_project
|
|
|
|
@jira_project ||= client.Project.find(project_key)
|
|
|
|
end
|
|
|
|
|
2015-02-12 20:06:55 -05:00
|
|
|
def help
|
2016-01-14 09:20:23 -05:00
|
|
|
'See the ' \
|
2015-03-27 15:57:53 -04:00
|
|
|
'[integration doc](http://doc.gitlab.com/ce/integration/external-issue-tracker.html) '\
|
|
|
|
'for details.'
|
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
|
|
|
|
|
|
|
|
def to_param
|
|
|
|
'jira'
|
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
|
|
|
|
def fields
|
2016-01-14 09:20:23 -05:00
|
|
|
[
|
|
|
|
{ type: 'text', name: 'url', title: 'URL', placeholder: 'https://jira.example.com' },
|
2016-09-29 17:11:32 -04:00
|
|
|
{ type: 'text', name: 'project_key', placeholder: 'Project Key' },
|
2015-12-17 17:08:14 -05:00
|
|
|
{ type: 'text', name: 'username', placeholder: '' },
|
|
|
|
{ type: 'password', name: 'password', placeholder: '' },
|
|
|
|
{ type: 'text', name: 'jira_issue_transition_id', placeholder: '2' }
|
2016-01-14 09:20:23 -05:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
# URLs to redirect from Gitlab issues pages to jira issue tracker
|
2016-01-14 09:20:23 -05:00
|
|
|
def project_url
|
|
|
|
"#{url}/issues/?jql=project=#{project_key}"
|
|
|
|
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
|
|
|
|
|
|
|
|
def execute(push, issue = nil)
|
|
|
|
if issue.nil?
|
|
|
|
# No specific issue, that means
|
|
|
|
# we just want to test settings
|
|
|
|
test_settings
|
|
|
|
else
|
|
|
|
close_issue(push, issue)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_cross_reference_note(mentioned, noteable, author)
|
2016-01-14 09:20:23 -05:00
|
|
|
issue_key = mentioned.id
|
2015-12-17 17:08:14 -05:00
|
|
|
project = self.project
|
|
|
|
noteable_name = noteable.class.name.underscore.downcase
|
|
|
|
noteable_id = if noteable.is_a?(Commit)
|
|
|
|
noteable.id
|
|
|
|
else
|
|
|
|
noteable.iid
|
|
|
|
end
|
|
|
|
|
|
|
|
entity_url = build_entity_url(noteable_name.to_sym, noteable_id)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
user: {
|
|
|
|
name: author.name,
|
|
|
|
url: resource_url(user_path(author)),
|
|
|
|
},
|
|
|
|
project: {
|
|
|
|
name: project.path_with_namespace,
|
|
|
|
url: resource_url(namespace_project_path(project.namespace, project))
|
|
|
|
},
|
|
|
|
entity: {
|
|
|
|
name: noteable_name.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-01-14 09:20:23 -05:00
|
|
|
add_comment(data, issue_key)
|
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
|
|
|
|
|
|
|
|
def can_test?
|
|
|
|
username.present? && password.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
2016-09-29 17:11:32 -04:00
|
|
|
return unless url.present?
|
2016-01-14 09:20:23 -05:00
|
|
|
# Test settings by getting the project
|
|
|
|
jira_project
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-01-14 09:20:23 -05:00
|
|
|
rescue Errno::ECONNREFUSED, JIRA::HTTPError => e
|
2016-09-29 17:11:32 -04:00
|
|
|
Rails.logger.info "#{self.class.name} ERROR: #{e.message}. API URL: #{url}."
|
2015-12-17 17:08:14 -05:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def close_issue(entity, issue)
|
|
|
|
commit_id = if entity.is_a?(Commit)
|
|
|
|
entity.id
|
|
|
|
elsif entity.is_a?(MergeRequest)
|
2016-09-29 17:11:32 -04:00
|
|
|
entity.diff_head_sha
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
2016-09-29 17:11:32 -04:00
|
|
|
|
2015-12-17 17:08:14 -05:00
|
|
|
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. Split the operation in to two calls so the
|
|
|
|
# comment always works.
|
|
|
|
transition_issue(issue)
|
|
|
|
add_issue_solved_comment(issue, commit_id, commit_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
def transition_issue(issue)
|
2016-01-14 09:20:23 -05:00
|
|
|
issue = client.Issue.find(issue.iid)
|
|
|
|
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-01-14 09:20:23 -05:00
|
|
|
comment = "Issue solved with [#{commit_id}|#{commit_url}]."
|
|
|
|
send_message(issue.iid, comment)
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-01-14 09:20:23 -05:00
|
|
|
def add_comment(data, issue_key)
|
2015-12-17 17:08:14 -05:00
|
|
|
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-09-29 17:11:32 -04:00
|
|
|
message = "[#{user_name}|#{user_url}] mentioned this issue in [a #{entity_name} of #{project_name}|#{entity_url}]:\n'#{entity_title}'"
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
unless comment_exists?(issue_key, message)
|
2016-01-14 09:20:23 -05:00
|
|
|
send_message(issue_key, message)
|
2016-09-29 17:11:32 -04:00
|
|
|
end
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
def comment_exists?(issue_key, message)
|
|
|
|
comments = client.Issue.find(issue_key).comments
|
|
|
|
comments.map { |comment| comment.body.include?(message) }.any?
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
def send_message(issue_key, message)
|
|
|
|
return unless url.present?
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
issue = client.Issue.find(issue_key)
|
2015-12-17 17:08:14 -05:00
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
if issue.comments.build.save!(body: message)
|
|
|
|
result_message = "#{self.class.name} SUCCESS: Successfully posted to #{url}."
|
2015-12-17 17:08:14 -05:00
|
|
|
end
|
|
|
|
|
2016-09-29 17:11:32 -04:00
|
|
|
Rails.logger.info(result_message)
|
|
|
|
result_message
|
|
|
|
rescue URI::InvalidURIError, Errno::ECONNREFUSED, JIRA::HTTPError => e
|
|
|
|
Rails.logger.info "#{self.class.name} Send message ERROR: #{url} - #{e.message}"
|
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
|
|
|
|
|
|
|
|
def build_entity_url(entity_name, entity_id)
|
|
|
|
resource_url(
|
|
|
|
polymorphic_url(
|
|
|
|
[
|
|
|
|
self.project.namespace.becomes(Namespace),
|
|
|
|
self.project,
|
|
|
|
entity_name
|
|
|
|
],
|
|
|
|
id: entity_id,
|
|
|
|
routing_type: :path
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2015-01-20 19:46:27 -05:00
|
|
|
end
|