PageRenderTime 23ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/project_services/jira_service.rb

https://gitlab.com/griest/gitlab-ce
Ruby | 320 lines | 242 code | 61 blank | 17 comment | 24 complexity | 4d702908db28589d9aff90e8df55091f MD5 | raw file
  1. class JiraService < IssueTrackerService
  2. include Gitlab::Routing
  3. validates :url, url: true, presence: true, if: :activated?
  4. validates :api_url, url: true, allow_blank: true
  5. validates :username, presence: true, if: :activated?
  6. validates :password, presence: true, if: :activated?
  7. prop_accessor :username, :password, :url, :api_url, :jira_issue_transition_id, :title, :description
  8. before_update :reset_password
  9. # This is confusing, but JiraService does not really support these events.
  10. # The values here are required to display correct options in the service
  11. # configuration screen.
  12. def self.supported_events
  13. %w(commit merge_request)
  14. end
  15. # {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1
  16. def self.reference_pattern(only_long: true)
  17. @reference_pattern ||= /(?<issue>\b([A-Z][A-Z0-9_]+-)\d+)/
  18. end
  19. def initialize_properties
  20. super do
  21. self.properties = {
  22. title: issues_tracker['title'],
  23. url: issues_tracker['url'],
  24. api_url: issues_tracker['api_url']
  25. }
  26. end
  27. end
  28. def reset_password
  29. self.password = nil if reset_password?
  30. end
  31. def options
  32. url = URI.parse(client_url)
  33. {
  34. username: self.username,
  35. password: self.password,
  36. site: URI.join(url, '/').to_s,
  37. context_path: url.path.chomp('/'),
  38. auth_type: :basic,
  39. read_timeout: 120,
  40. use_cookies: true,
  41. additional_cookies: ['OBBasicAuth=fromDialog'],
  42. use_ssl: url.scheme == 'https'
  43. }
  44. end
  45. def client
  46. @client ||= JIRA::Client.new(options)
  47. end
  48. def help
  49. "You need to configure JIRA before enabling this service. For more details
  50. read the
  51. [JIRA service documentation](#{help_page_url('user/project/integrations/jira')})."
  52. end
  53. def title
  54. if self.properties && self.properties['title'].present?
  55. self.properties['title']
  56. else
  57. 'JIRA'
  58. end
  59. end
  60. def description
  61. if self.properties && self.properties['description'].present?
  62. self.properties['description']
  63. else
  64. 'Jira issue tracker'
  65. end
  66. end
  67. def self.to_param
  68. 'jira'
  69. end
  70. def fields
  71. [
  72. { type: 'text', name: 'url', title: 'Web URL', placeholder: 'https://jira.example.com', required: true },
  73. { type: 'text', name: 'api_url', title: 'JIRA API URL', placeholder: 'If different from Web URL' },
  74. { type: 'text', name: 'username', placeholder: '', required: true },
  75. { type: 'password', name: 'password', placeholder: '', required: true },
  76. { type: 'text', name: 'jira_issue_transition_id', title: 'Transition ID', placeholder: '' }
  77. ]
  78. end
  79. def issues_url
  80. "#{url}/browse/:id"
  81. end
  82. def new_issue_url
  83. "#{url}/secure/CreateIssue.jspa"
  84. end
  85. def execute(push)
  86. # This method is a no-op, because currently JiraService does not
  87. # support any events.
  88. end
  89. def close_issue(entity, external_issue)
  90. issue = jira_request { client.Issue.find(external_issue.iid) }
  91. return if issue.nil? || has_resolution?(issue) || !jira_issue_transition_id.present?
  92. commit_id = if entity.is_a?(Commit)
  93. entity.id
  94. elsif entity.is_a?(MergeRequest)
  95. entity.diff_head_sha
  96. end
  97. commit_url = build_entity_url(:commit, commit_id)
  98. # Depending on the JIRA project's workflow, a comment during transition
  99. # may or may not be allowed. Refresh the issue after transition and check
  100. # if it is closed, so we don't have one comment for every commit.
  101. issue = jira_request { client.Issue.find(issue.key) } if transition_issue(issue)
  102. add_issue_solved_comment(issue, commit_id, commit_url) if has_resolution?(issue)
  103. end
  104. def create_cross_reference_note(mentioned, noteable, author)
  105. unless can_cross_reference?(noteable)
  106. return "Events for #{noteable.model_name.plural.humanize(capitalize: false)} are disabled."
  107. end
  108. jira_issue = jira_request { client.Issue.find(mentioned.id) }
  109. return unless jira_issue.present?
  110. noteable_id = noteable.respond_to?(:iid) ? noteable.iid : noteable.id
  111. noteable_type = noteable_name(noteable)
  112. entity_url = build_entity_url(noteable_type, noteable_id)
  113. data = {
  114. user: {
  115. name: author.name,
  116. url: resource_url(user_path(author))
  117. },
  118. project: {
  119. name: project.full_path,
  120. url: resource_url(namespace_project_path(project.namespace, project)) # rubocop:disable Cop/ProjectPathHelper
  121. },
  122. entity: {
  123. name: noteable_type.humanize.downcase,
  124. url: entity_url,
  125. title: noteable.title
  126. }
  127. }
  128. add_comment(data, jira_issue)
  129. end
  130. # reason why service cannot be tested
  131. def disabled_title
  132. "Please fill in Password and Username."
  133. end
  134. def test(_)
  135. result = test_settings
  136. success = result.present?
  137. result = @error if @error && !success
  138. { success: success, result: result }
  139. end
  140. # JIRA does not need test data.
  141. # We are requesting the project that belongs to the project key.
  142. def test_data(user = nil, project = nil)
  143. nil
  144. end
  145. def test_settings
  146. return unless client_url.present?
  147. # Test settings by getting the project
  148. jira_request { client.ServerInfo.all.attrs }
  149. end
  150. private
  151. def can_cross_reference?(noteable)
  152. case noteable
  153. when Commit then commit_events
  154. when MergeRequest then merge_requests_events
  155. else true
  156. end
  157. end
  158. def transition_issue(issue)
  159. issue.transitions.build.save(transition: { id: jira_issue_transition_id })
  160. end
  161. def add_issue_solved_comment(issue, commit_id, commit_url)
  162. link_title = "GitLab: Solved by commit #{commit_id}."
  163. comment = "Issue solved with [#{commit_id}|#{commit_url}]."
  164. link_props = build_remote_link_props(url: commit_url, title: link_title, resolved: true)
  165. send_message(issue, comment, link_props)
  166. end
  167. def add_comment(data, issue)
  168. user_name = data[:user][:name]
  169. user_url = data[:user][:url]
  170. entity_name = data[:entity][:name]
  171. entity_url = data[:entity][:url]
  172. entity_title = data[:entity][:title]
  173. project_name = data[:project][:name]
  174. message = "[#{user_name}|#{user_url}] mentioned this issue in [a #{entity_name} of #{project_name}|#{entity_url}]:\n'#{entity_title.chomp}'"
  175. link_title = "GitLab: Mentioned on #{entity_name} - #{entity_title}"
  176. link_props = build_remote_link_props(url: entity_url, title: link_title)
  177. unless comment_exists?(issue, message)
  178. send_message(issue, message, link_props)
  179. end
  180. end
  181. def has_resolution?(issue)
  182. issue.respond_to?(:resolution) && issue.resolution.present?
  183. end
  184. def comment_exists?(issue, message)
  185. comments = jira_request { issue.comments }
  186. comments.present? && comments.any? { |comment| comment.body.include?(message) }
  187. end
  188. def send_message(issue, message, remote_link_props)
  189. return unless client_url.present?
  190. jira_request do
  191. remote_link = find_remote_link(issue, remote_link_props[:object][:url])
  192. if remote_link
  193. remote_link.save!(remote_link_props)
  194. elsif issue.comments.build.save!(body: message)
  195. new_remote_link = issue.remotelink.build
  196. new_remote_link.save!(remote_link_props)
  197. end
  198. result_message = "#{self.class.name} SUCCESS: Successfully posted to #{client_url}."
  199. Rails.logger.info(result_message)
  200. result_message
  201. end
  202. end
  203. def find_remote_link(issue, url)
  204. links = jira_request { issue.remotelink.all }
  205. links.find { |link| link.object["url"] == url }
  206. end
  207. def build_remote_link_props(url:, title:, resolved: false)
  208. status = {
  209. resolved: resolved
  210. }
  211. {
  212. GlobalID: 'GitLab',
  213. object: {
  214. url: url,
  215. title: title,
  216. status: status,
  217. icon: { title: 'GitLab', url16x16: 'https://gitlab.com/favicon.ico' }
  218. }
  219. }
  220. end
  221. def resource_url(resource)
  222. "#{Settings.gitlab.base_url.chomp("/")}#{resource}"
  223. end
  224. def build_entity_url(noteable_type, entity_id)
  225. polymorphic_url(
  226. [
  227. self.project.namespace.becomes(Namespace),
  228. self.project,
  229. noteable_type.to_sym
  230. ],
  231. id: entity_id,
  232. host: Settings.gitlab.base_url
  233. )
  234. end
  235. def noteable_name(noteable)
  236. name = noteable.model_name.singular
  237. # ProjectSnippet inherits from Snippet class so it causes
  238. # routing error building the URL.
  239. name == "project_snippet" ? "snippet" : name
  240. end
  241. # Handle errors when doing JIRA API calls
  242. def jira_request
  243. yield
  244. rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => e
  245. @error = e.message
  246. Rails.logger.info "#{self.class.name} Send message ERROR: #{client_url} - #{@error}"
  247. nil
  248. end
  249. def client_url
  250. api_url.present? ? api_url : url
  251. end
  252. def reset_password?
  253. # don't reset the password if a new one is provided
  254. return false if password_touched?
  255. return true if api_url_changed?
  256. return false if api_url.present?
  257. url_changed?
  258. end
  259. end