BigW Consortium Gitlab

jira_service.rb 8.09 KB
Newer Older
1
class JiraService < IssueTrackerService
2
  include Gitlab::Routing.url_helpers
3

4 5
  validates :url, url: true, presence: true, if: :activated?
  validates :project_key, presence: true, if: :activated?
Drew Blessing committed
6

7 8
  prop_accessor :username, :password, :url, :project_key,
                :jira_issue_transition_id, :title, :description
9

Drew Blessing committed
10 11
  before_update :reset_password

12 13 14
  # 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.
15
  def self.supported_events
16 17 18
    %w(commit merge_request)
  end

19 20 21 22 23
  # {PROJECT-KEY}-{NUMBER} Examples: JIRA-1, PROJECT-1
  def reference_pattern
    @reference_pattern ||= %r{(?<issue>\b([A-Z][A-Z0-9_]+-)\d+)}
  end

24 25 26 27 28 29 30 31 32
  def initialize_properties
    super do
      self.properties = {
        title: issues_tracker['title'],
        url: issues_tracker['url']
      }
    end
  end

Drew Blessing committed
33 34
  def reset_password
    # don't reset the password if a new one is provided
35
    if url_changed? && !password_touched?
Drew Blessing committed
36 37 38
      self.password = nil
    end
  end
39

40 41
  def options
    url = URI.parse(self.url)
42

43
    {
44 45 46 47 48 49 50
      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'
51 52 53 54
    }
  end

  def client
55
    @client ||= JIRA::Client.new(options)
56 57 58
  end

  def jira_project
59
    @jira_project ||= jira_request { client.Project.find(project_key) }
60 61
  end

62
  def help
63
    "You need to configure JIRA before enabling this service. For more details
64
    read the
65
    [JIRA service documentation](#{help_page_url('user/project/integrations/jira')})."
66 67
  end

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  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

84
  def self.to_param
85 86
    'jira'
  end
Drew Blessing committed
87 88

  def fields
89 90
    [
      { type: 'text', name: 'url', title: 'URL', placeholder: 'https://jira.example.com' },
91
      { type: 'text', name: 'project_key', placeholder: 'Project Key' },
Drew Blessing committed
92 93
      { type: 'text', name: 'username', placeholder: '' },
      { type: 'password', name: 'password', placeholder: '' },
94
      { type: 'text', name: 'jira_issue_transition_id', placeholder: '' }
95 96 97
    ]
  end

98
  # URLs to redirect from Gitlab issues pages to jira issue tracker
99 100 101 102 103 104 105 106 107 108
  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"
Drew Blessing committed
109 110
  end

111 112 113 114
  def execute(push)
    # This method is a no-op, because currently JiraService does not
    # support any events.
  end
115

116 117
  def close_issue(entity, external_issue)
    issue = jira_request { client.Issue.find(external_issue.iid) }
118

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    return if issue.nil? || issue.resolution.present? || !jira_issue_transition_id.present?

    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)
    add_issue_solved_comment(issue, commit_id, commit_url) if issue.resolution
Drew Blessing committed
134 135 136
  end

  def create_cross_reference_note(mentioned, noteable, author)
137 138 139 140
    unless can_cross_reference?(noteable)
      return "Events for #{noteable.model_name.plural.humanize(capitalize: false)} are disabled."
    end

141 142
    jira_issue = jira_request { client.Issue.find(mentioned.id) }

143
    return unless jira_issue.present?
144

145 146 147
    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 committed
148 149 150 151

    data = {
      user: {
        name: author.name,
152
        url: resource_url(user_path(author))
Drew Blessing committed
153 154
      },
      project: {
155 156
        name: self.project.path_with_namespace,
        url: resource_url(namespace_project_path(project.namespace, self.project))
Drew Blessing committed
157 158
      },
      entity: {
159
        name: noteable_type.humanize.downcase,
160 161
        url: entity_url,
        title: noteable.title
Drew Blessing committed
162 163 164
      }
    }

165
    add_comment(data, jira_issue)
Drew Blessing committed
166 167
  end

168 169 170 171 172
  # reason why service cannot be tested
  def disabled_title
    "Please fill in Password and Username."
  end

173 174 175 176 177
  def test(_)
    result = test_settings
    { success: result.present?, result: result }
  end

178 179 180 181 182 183 184 185 186 187
  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

Drew Blessing committed
188
  def test_settings
189
    return unless url.present?
190
    # Test settings by getting the project
191
    jira_request { jira_project.present? }
Drew Blessing committed
192 193 194 195
  end

  private

196 197 198 199 200 201 202 203
  def can_cross_reference?(noteable)
    case noteable
    when Commit then commit_events
    when MergeRequest then merge_requests_events
    else true
    end
  end

Drew Blessing committed
204
  def transition_issue(issue)
205
    issue.transitions.build.save(transition: { id: jira_issue_transition_id })
Drew Blessing committed
206 207 208
  end

  def add_issue_solved_comment(issue, commit_id, commit_url)
209 210 211 212
    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)
Drew Blessing committed
213 214
  end

215 216 217 218 219
  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]
220
    entity_title = data[:entity][:title]
Drew Blessing committed
221 222
    project_name = data[:project][:name]

223
    message      = "[#{user_name}|#{user_url}] mentioned this issue in [a #{entity_name} of #{project_name}|#{entity_url}]:\n'#{entity_title.chomp}'"
224 225
    link_title   = "GitLab: Mentioned on #{entity_name} - #{entity_title}"
    link_props   = build_remote_link_props(url: entity_url, title: link_title)
Drew Blessing committed
226

227 228
    unless comment_exists?(issue, message)
      send_message(issue, message, link_props)
229
    end
Drew Blessing committed
230 231
  end

232 233 234 235
  def comment_exists?(issue, message)
    comments = jira_request { issue.comments }

    comments.present? && comments.any? { |comment| comment.body.include?(message) }
Drew Blessing committed
236 237
  end

238
  def send_message(issue, message, remote_link_props)
239
    return unless url.present?
Drew Blessing committed
240

241 242 243 244 245 246
    jira_request do
      if issue.comments.build.save!(body: message)
        remote_link = issue.remotelink.build
        remote_link.save!(remote_link_props)
        result_message = "#{self.class.name} SUCCESS: Successfully posted to #{url}."
      end
Drew Blessing committed
247

248 249
      Rails.logger.info(result_message)
      result_message
Drew Blessing committed
250
    end
251
  end
Drew Blessing committed
252

253 254 255 256 257 258 259 260 261 262 263 264 265 266
  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' }
      }
    }
Drew Blessing committed
267 268 269
  end

  def resource_url(resource)
270
    "#{Settings.gitlab.base_url.chomp("/")}#{resource}"
Drew Blessing committed
271 272
  end

273
  def build_entity_url(noteable_type, entity_id)
274 275 276 277
    polymorphic_url(
      [
        self.project.namespace.becomes(Namespace),
        self.project,
278
        noteable_type.to_sym
279 280 281
      ],
      id:   entity_id,
      host: Settings.gitlab.base_url
Drew Blessing committed
282 283
    )
  end
284

285 286 287 288 289 290 291 292
  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

293 294 295 296 297 298 299 300
  # Handle errors when doing JIRA API calls
  def jira_request
    yield

  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, URI::InvalidURIError, JIRA::HTTPError => e
    Rails.logger.info "#{self.class.name} Send message ERROR: #{url} - #{e.message}"
    nil
  end
301
end