jira_service.rb 11.7 KB
Newer Older
1 2
# frozen_string_literal: true

3
class JiraService < IssueTrackerService
4
  extend ::Gitlab::Utils::Override
5
  include Gitlab::Routing
6 7
  include ApplicationHelper
  include ActionView::Helpers::AssetUrlHelper
8

9 10
  validates :url, public_url: true, presence: true, if: :activated?
  validates :api_url, public_url: true, allow_blank: true
11 12
  validates :username, presence: true, if: :activated?
  validates :password, presence: true, if: :activated?
Drew Blessing's avatar
Drew Blessing committed
13

14
  validates :jira_issue_transition_id,
15
            format: { with: Gitlab::Regex.jira_transition_id_regex, message: s_("JiraService|transition ids can have only numbers which can be split with , or ;") },
16 17
            allow_blank: true

18 19
  # Jira Cloud version is deprecating authentication via username and password.
  # We should use username/password for Jira Server and email/api_token for Jira Cloud,
20
  # for more information check: https://gitlab.com/gitlab-org/gitlab-foss/issues/49936.
21 22

  # TODO: we can probably just delegate as part of
23
  # https://gitlab.com/gitlab-org/gitlab/issues/29404
24
  data_field :username, :password, :url, :api_url, :jira_issue_transition_id
25

Drew Blessing's avatar
Drew Blessing committed
26 27
  before_update :reset_password

28 29
  alias_method :project_url, :url

30
  # When these are false GitLab does not create cross reference
31
  # comments on Jira except when an issue gets transitioned.
32
  def self.supported_events
33 34 35
    %w(commit merge_request)
  end

36 37 38 39
  def self.supported_event_actions
    %w(comment)
  end

40
  # {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1
41
  def self.reference_pattern(only_long: true)
42
    @reference_pattern ||= /(?<issue>\b#{Gitlab::Regex.jira_issue_key_regex})/
43 44
  end

45
  def initialize_properties
46 47 48 49 50
    {}
  end

  def data_fields
    jira_tracker_data || self.build_jira_tracker_data
51 52
  end

Drew Blessing's avatar
Drew Blessing committed
53
  def reset_password
54 55 56 57 58 59 60 61 62 63 64 65
    data_fields.password = nil if reset_password?
  end

  def set_default_data
    return unless issues_tracker.present?

    self.title ||= issues_tracker['title']

    return if url

    data_fields.url ||= issues_tracker['url']
    data_fields.api_url ||= issues_tracker['api_url']
Drew Blessing's avatar
Drew Blessing committed
66
  end
67

68
  def options
69
    url = URI.parse(client_url)
70

71
    {
72
      username: username&.strip,
73
      password: password,
74
      site: URI.join(url, '/').to_s, # Intended to find the root
75
      context_path: url.path,
76 77
      auth_type: :basic,
      read_timeout: 120,
78 79
      use_cookies: true,
      additional_cookies: ['OBBasicAuth=fromDialog'],
80
      use_ssl: url.scheme == 'https'
81 82 83 84
    }
  end

  def client
85 86 87 88 89 90
    @client ||= begin
      JIRA::Client.new(options).tap do |client|
        # Replaces JIRA default http client with our implementation
        client.request_client = Gitlab::Jira::HttpClient.new(client.options)
      end
    end
91 92
  end

93
  def help
94
    "You need to configure Jira before enabling this service. For more details
95
    read the
96
    [Jira service documentation](#{help_page_url('user/project/integrations/jira')})."
97 98
  end

99 100
  def default_title
    'Jira'
101 102
  end

103 104
  def default_description
    s_('JiraService|Jira issue tracker')
105 106
  end

107
  def self.to_param
108 109
    'jira'
  end
Drew Blessing's avatar
Drew Blessing committed
110 111

  def fields
112
    [
113
      { type: 'text', name: 'url', title: s_('JiraService|Web URL'), placeholder: 'https://jira.example.com', required: true },
114
      { type: 'text', name: 'api_url', title: s_('JiraService|Jira API URL'), placeholder: s_('JiraService|If different from Web URL') },
115 116 117
      { type: 'text', name: 'username', title: s_('JiraService|Username or Email'), placeholder: s_('JiraService|Use a username for server version and an email for cloud version'), required: true },
      { type: 'password', name: 'password', title: s_('JiraService|Password or API token'), placeholder: s_('JiraService|Use a password for server version and an API token for cloud version'), required: true },
      { type: 'text', name: 'jira_issue_transition_id', title: s_('JiraService|Transition ID(s)'), placeholder: s_('JiraService|Use , or ; to separate multiple transition IDs') }
118 119 120 121 122 123 124 125 126
    ]
  end

  def issues_url
    "#{url}/browse/:id"
  end

  def new_issue_url
    "#{url}/secure/CreateIssue.jspa"
Drew Blessing's avatar
Drew Blessing committed
127 128
  end

129 130
  alias_method :original_url, :url
  def url
131 132 133 134 135 136
    original_url&.delete_suffix('/')
  end

  alias_method :original_api_url, :api_url
  def api_url
    original_api_url&.delete_suffix('/')
137 138
  end

139 140 141 142
  def execute(push)
    # This method is a no-op, because currently JiraService does not
    # support any events.
  end
143

144 145
  def close_issue(entity, external_issue)
    issue = jira_request { client.Issue.find(external_issue.iid) }
146

147
    return if issue.nil? || has_resolution?(issue) || !jira_issue_transition_id.present?
148

Arturo Herrero's avatar
Arturo Herrero committed
149 150 151
    commit_id = case entity
                when Commit then entity.id
                when MergeRequest then entity.diff_head_sha
152 153 154 155
                end

    commit_url = build_entity_url(:commit, commit_id)

156
    # Depending on the Jira project's workflow, a comment during transition
157 158 159
    # 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)
160
    add_issue_solved_comment(issue, commit_id, commit_url) if has_resolution?(issue)
Drew Blessing's avatar
Drew Blessing committed
161 162 163
  end

  def create_cross_reference_note(mentioned, noteable, author)
164
    unless can_cross_reference?(noteable)
165
      return s_("JiraService|Events for %{noteable_model_name} are disabled.") % { noteable_model_name: noteable.model_name.plural.humanize(capitalize: false) }
166 167
    end

168 169
    jira_issue = jira_request { client.Issue.find(mentioned.id) }

170
    return unless jira_issue.present?
171

172 173 174
    noteable_id   = noteable.respond_to?(:iid) ? noteable.iid : noteable.id
    noteable_type = noteable_name(noteable)
    entity_url    = build_entity_url(noteable_type, noteable_id)
Drew Blessing's avatar
Drew Blessing committed
175 176 177 178

    data = {
      user: {
        name: author.name,
179
        url: resource_url(user_path(author))
Drew Blessing's avatar
Drew Blessing committed
180 181
      },
      project: {
182
        name: project.full_path,
183
        url: resource_url(namespace_project_path(project.namespace, project)) # rubocop:disable Cop/ProjectPathHelper
Drew Blessing's avatar
Drew Blessing committed
184 185
      },
      entity: {
186
        name: noteable_type.humanize.downcase,
187 188
        url: entity_url,
        title: noteable.title
Drew Blessing's avatar
Drew Blessing committed
189 190 191
      }
    }

192
    add_comment(data, jira_issue)
Drew Blessing's avatar
Drew Blessing committed
193 194
  end

195 196
  def test(_)
    result = test_settings
197
    success = result.present?
198
    result = @error&.message unless success
199 200

    { success: success, result: result }
201 202
  end

203
  # Jira does not need test data.
204 205 206 207 208
  # We are requesting the project that belongs to the project key.
  def test_data(user = nil, project = nil)
    nil
  end

209 210 211 212 213 214 215 216 217 218
  override :support_close_issue?
  def support_close_issue?
    true
  end

  override :support_cross_reference?
  def support_cross_reference?
    true
  end

219 220
  private

Drew Blessing's avatar
Drew Blessing committed
221
  def test_settings
222
    return unless client_url.present?
223

224
    # Test settings by getting the project
225
    jira_request { client.ServerInfo.all.attrs }
Drew Blessing's avatar
Drew Blessing committed
226 227
  end

228 229 230 231 232 233 234 235
  def can_cross_reference?(noteable)
    case noteable
    when Commit then commit_events
    when MergeRequest then merge_requests_events
    else true
    end
  end

236 237 238
  # jira_issue_transition_id can have multiple values split by , or ;
  # the issue is transitioned at the order given by the user
  # if any transition fails it will log the error message and stop the transition sequence
Drew Blessing's avatar
Drew Blessing committed
239
  def transition_issue(issue)
240
    jira_issue_transition_id.scan(Gitlab::Regex.jira_transition_id_regex).each do |transition_id|
Nick Thomas's avatar
Nick Thomas committed
241 242
      issue.transitions.build.save!(transition: { id: transition_id })
    rescue => error
243 244 245 246 247 248 249 250 251
      log_error(
        "Issue transition failed",
          error: {
            exception_class: error.class.name,
            exception_message: error.message,
            exception_backtrace: Gitlab::BacktraceCleaner.clean_backtrace(error.backtrace)
          },
         client_url: client_url
      )
Nick Thomas's avatar
Nick Thomas committed
252
      return false
253
    end
Drew Blessing's avatar
Drew Blessing committed
254 255 256
  end

  def add_issue_solved_comment(issue, commit_id, commit_url)
257
    link_title   = "Solved by commit #{commit_id}."
258 259 260
    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)
Drew Blessing's avatar
Drew Blessing committed
261 262
  end

263 264 265 266 267
  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]
268
    entity_title = data[:entity][:title]
Drew Blessing's avatar
Drew Blessing committed
269 270
    project_name = data[:project][:name]

271
    message      = "[#{user_name}|#{user_url}] mentioned this issue in [a #{entity_name} of #{project_name}|#{entity_url}]:\n'#{entity_title.chomp}'"
272
    link_title   = "#{entity_name.capitalize} - #{entity_title}"
273
    link_props   = build_remote_link_props(url: entity_url, title: link_title)
Drew Blessing's avatar
Drew Blessing committed
274

275 276
    unless comment_exists?(issue, message)
      send_message(issue, message, link_props)
277
    end
Drew Blessing's avatar
Drew Blessing committed
278 279
  end

280 281 282 283
  def has_resolution?(issue)
    issue.respond_to?(:resolution) && issue.resolution.present?
  end

284 285 286 287
  def comment_exists?(issue, message)
    comments = jira_request { issue.comments }

    comments.present? && comments.any? { |comment| comment.body.include?(message) }
Drew Blessing's avatar
Drew Blessing committed
288 289
  end

290
  def send_message(issue, message, remote_link_props)
291
    return unless client_url.present?
Drew Blessing's avatar
Drew Blessing committed
292

293
    jira_request do
294 295 296 297 298
      remote_link = find_remote_link(issue, remote_link_props[:object][:url])

      create_issue_comment(issue, message) unless remote_link
      remote_link ||= issue.remotelink.build
      remote_link.save!(remote_link_props)
Drew Blessing's avatar
Drew Blessing committed
299

300
      log_info("Successfully posted", client_url: client_url)
301
      "SUCCESS: Successfully posted to #{client_url}."
Drew Blessing's avatar
Drew Blessing committed
302
    end
303
  end
Drew Blessing's avatar
Drew Blessing committed
304

305 306 307 308 309 310
  def create_issue_comment(issue, message)
    return unless comment_on_event_enabled

    issue.comments.build.save!(body: message)
  end

311 312
  def find_remote_link(issue, url)
    links = jira_request { issue.remotelink.all }
313
    return unless links
314 315 316 317

    links.find { |link| link.object["url"] == url }
  end

318 319 320 321 322 323 324
  def build_remote_link_props(url:, title:, resolved: false)
    status = {
      resolved: resolved
    }

    {
      GlobalID: 'GitLab',
325
      relationship: 'mentioned on',
326 327 328 329
      object: {
        url: url,
        title: title,
        status: status,
330
        icon: {
331
          title: 'GitLab', url16x16: asset_url(Gitlab::Favicon.main, host: gitlab_config.base_url)
332
        }
333 334
      }
    }
Drew Blessing's avatar
Drew Blessing committed
335 336 337
  end

  def resource_url(resource)
338
    "#{Settings.gitlab.base_url.chomp("/")}#{resource}"
Drew Blessing's avatar
Drew Blessing committed
339 340
  end

341
  def build_entity_url(noteable_type, entity_id)
Jarka Kadlecova's avatar
Jarka Kadlecova committed
342 343 344 345
    polymorphic_url(
      [
        self.project.namespace.becomes(Namespace),
        self.project,
346
        noteable_type.to_sym
Jarka Kadlecova's avatar
Jarka Kadlecova committed
347 348 349
      ],
      id:   entity_id,
      host: Settings.gitlab.base_url
Drew Blessing's avatar
Drew Blessing committed
350 351
    )
  end
352

353 354 355 356 357 358 359 360
  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

361
  # Handle errors when doing Jira API calls
362 363
  def jira_request
    yield
364 365 366 367 368 369 370 371
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError, OpenSSL::SSL::SSLError => error
    @error = error
    log_error(
      "Error sending message",
      client_url: client_url,
      error: {
        exception_class: error.class.name,
        exception_message: error.message,
372
        exception_backtrace: Gitlab::BacktraceCleaner.clean_backtrace(error.backtrace)
373 374
      }
    )
375 376
    nil
  end
377 378

  def client_url
379
    api_url.presence || url
380 381 382 383 384 385 386 387 388 389
  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
390 391 392 393

  def self.event_description(event)
    case event
    when "merge_request", "merge_request_events"
394
      s_("JiraService|Jira comments will be created when an issue gets referenced in a merge request.")
395
    when "commit", "commit_events"
396
      s_("JiraService|Jira comments will be created when an issue gets referenced in a commit.")
397 398
    end
  end
399
end