Class: JiraService

Inherits:
IssueTrackerService show all
Extended by:
Gitlab::Utils::Override
Includes:
ActionView::Helpers::AssetUrlHelper, ApplicationHelper, Gitlab::Routing, Gitlab::Utils::StrongMemoize
Defined in:
app/models/project_services/jira_service.rb

Constant Summary collapse

PROJECTS_PER_PAGE =
50
DEPLOYMENT_TYPES =

TODO: use jira_service.deployment_type enum when gitlab.com/gitlab-org/gitlab/-/merge_requests/37003 is merged

{
  server: 'SERVER',
  cloud: 'CLOUD'
}.freeze

Constants inherited from Service

Service::DEV_SERVICE_NAMES, Service::SERVICE_NAMES

Instance Attribute Summary

Attributes included from Importable

#imported, #importing

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::Override

extended, extensions, included, method_added, override, prepended, queue_verification, verify!

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Methods included from ApplicationHelper

#active_when, #add_page_startup_api_call, #admin_section?, #asset_to_string, #autocomplete_data_sources, #body_data, #body_data_page, #client_class_list, #client_js_flags, #collapsed_sidebar?, #conditional_link_to, #contact_sales_url, #current_action?, #current_controller?, #edited_time_ago_with_tooltip, #external_storage_url_or_path, #extra_config, #gitlab_config, #hexdigest, #last_commit, #linkedin_url, #locale_path, #outdated_browser?, #page_class, #page_filter_path, #page_startup_api_calls, #partial_exists?, #path_to_key, #project_data, #promo_host, #promo_url, #read_only_message, #registry_config, #render_if_exists, #show_callout?, #show_last_push_widget?, #simple_sanitize, #static_objects_external_storage_enabled?, #stylesheet_link_tag_defer, #support_url, #system_message_class, #template_exists?, #time_ago_with_tooltip, #truncate_first_line, #twitter_url

Methods included from StartupCssHelper

#use_startup_css?

Methods included from Gitlab::Routing

add_helpers, includes_helpers, redirect_legacy_paths, url_helpers

Methods inherited from IssueTrackerService

#default?, #handle_properties, #issue_path, #issue_tracker_path, #issue_url, #legacy_properties_data, #new_issue_path, #supports_data_fields?

Methods inherited from Service

#activated?, #api_field_names, #async_execute, available_services_names, available_services_types, boolean_accessor, build_from_integration, #can_test?, #category, #configurable_event_actions, #configurable_events, default_integration, dev_services_names, #editable?, #event_channel_names, #event_field, event_names, #event_names, find_or_create_templates, find_or_initialize_all, find_or_initialize_integration, #global_fields, instance_exists_for?, #issue_tracker?, #json_fields, #operating?, prop_accessor, #reset_updated_properties, services_names, services_types, #show_active_box?, #supported_events, #supports_data_fields?, #to_data_fields_hash, #to_param, #to_service_hash, #updated_properties

Methods included from ProjectServicesLoggable

#build_message, #log_error, #log_info, #logger

Methods inherited from ApplicationRecord

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.reference_pattern(only_long: true) ⇒ Object

PROJECT-KEY-NUMBER Examples: JIRA-1, PROJECT-1


56
57
58
# File 'app/models/project_services/jira_service.rb', line 56

def self.reference_pattern(only_long: true)
  @reference_pattern ||= /(?<issue>\b#{Gitlab::Regex.jira_issue_key_regex})/
end

.supported_event_actionsObject


51
52
53
# File 'app/models/project_services/jira_service.rb', line 51

def self.supported_event_actions
  %w(comment)
end

.supported_eventsObject

When these are false GitLab does not create cross reference comments on Jira except when an issue gets transitioned.


47
48
49
# File 'app/models/project_services/jira_service.rb', line 47

def self.supported_events
  %w(commit merge_request)
end

.to_paramObject


120
121
122
# File 'app/models/project_services/jira_service.rb', line 120

def self.to_param
  'jira'
end

Instance Method Details

#api_urlObject


148
149
150
# File 'app/models/project_services/jira_service.rb', line 148

def api_url
  original_api_url&.delete_suffix('/')
end

#clientObject


97
98
99
100
101
102
103
104
# File 'app/models/project_services/jira_service.rb', line 97

def client
  @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
end

#close_issue(entity, external_issue) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/project_services/jira_service.rb', line 157

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

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

  commit_id = case entity
              when Commit then entity.id
              when MergeRequest then 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 has_resolution?(issue)
end

#create_cross_reference_note(mentioned, noteable, author) ⇒ Object


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/project_services/jira_service.rb', line 176

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

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

  return unless jira_issue.present?

  noteable_id   = noteable.respond_to?(:iid) ? noteable.iid : noteable.id
  noteable_type = noteable_name(noteable)
  entity_url    = build_entity_url(noteable_type, noteable_id)
  entity_meta   = build_entity_meta(noteable)

  data = {
    user: {
      name: author.name,
      url: resource_url(user_path(author))
    },
    project: {
      name: project.full_path,
      url: resource_url(project_path(project))
    },
    entity: {
      id: entity_meta[:id],
      name: noteable_type.humanize.downcase,
      url: entity_url,
      title: noteable.title,
      description: entity_meta[:description],
      branch: entity_meta[:branch]
    }
  }

  add_comment(data, jira_issue)
end

#data_fieldsObject


64
65
66
# File 'app/models/project_services/jira_service.rb', line 64

def data_fields
  jira_tracker_data || self.build_jira_tracker_data
end

#descriptionObject


116
117
118
# File 'app/models/project_services/jira_service.rb', line 116

def description
  s_('JiraService|Jira issue tracker')
end

#execute(push) ⇒ Object


152
153
154
155
# File 'app/models/project_services/jira_service.rb', line 152

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

#fieldsObject


124
125
126
127
128
129
130
131
132
# File 'app/models/project_services/jira_service.rb', line 124

def fields
  [
    { type: 'text', name: 'url', title: s_('JiraService|Web URL'), placeholder: 'https://jira.example.com', required: true },
    { type: 'text', name: 'api_url', title: s_('JiraService|Jira API URL'), placeholder: s_('JiraService|If different from Web URL') },
    { 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') }
  ]
end

#helpObject


106
107
108
109
110
# File 'app/models/project_services/jira_service.rb', line 106

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

#initialize_propertiesObject


60
61
62
# File 'app/models/project_services/jira_service.rb', line 60

def initialize_properties
  {}
end

#issues_urlObject


134
135
136
# File 'app/models/project_services/jira_service.rb', line 134

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

#new_issue_urlObject


138
139
140
# File 'app/models/project_services/jira_service.rb', line 138

def new_issue_url
  "#{url}/secure/CreateIssue!default.jspa"
end

#optionsObject


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/project_services/jira_service.rb', line 81

def options
  url = URI.parse(client_url)

  {
    username: username&.strip,
    password: password,
    site: URI.join(url, '/').to_s, # Intended to find the root
    context_path: url.path,
    auth_type: :basic,
    read_timeout: 120,
    use_cookies: true,
    additional_cookies: ['OBBasicAuth=fromDialog'],
    use_ssl: url.scheme == 'https'
  }
end

#original_api_urlObject


147
# File 'app/models/project_services/jira_service.rb', line 147

alias_method :original_api_url, :api_url

#original_urlObject


142
# File 'app/models/project_services/jira_service.rb', line 142

alias_method :original_url, :url

#project_urlObject


43
# File 'app/models/project_services/jira_service.rb', line 43

alias_method :project_url, :url

#reset_passwordObject


68
69
70
# File 'app/models/project_services/jira_service.rb', line 68

def reset_password
  data_fields.password = nil if reset_password?
end

#set_default_dataObject


72
73
74
75
76
77
78
79
# File 'app/models/project_services/jira_service.rb', line 72

def set_default_data
  return unless issues_tracker.present?

  return if url

  data_fields.url ||= issues_tracker['url']
  data_fields.api_url ||= issues_tracker['api_url']
end

#support_close_issue?Boolean

Returns:

  • (Boolean)

225
226
227
# File 'app/models/project_services/jira_service.rb', line 225

def support_close_issue?
  true
end

#support_cross_reference?Boolean

Returns:

  • (Boolean)

230
231
232
# File 'app/models/project_services/jira_service.rb', line 230

def support_cross_reference?
  true
end

#test(_) ⇒ Object


216
217
218
219
220
221
222
# File 'app/models/project_services/jira_service.rb', line 216

def test(_)
  result = server_info
  success = result.present?
  result = @error&.message unless success

  { success: success, result: result }
end

#titleObject


112
113
114
# File 'app/models/project_services/jira_service.rb', line 112

def title
  'Jira'
end

#urlObject


143
144
145
# File 'app/models/project_services/jira_service.rb', line 143

def url
  original_url&.delete_suffix('/')
end

#valid_connection?Boolean

Returns:

  • (Boolean)

212
213
214
# File 'app/models/project_services/jira_service.rb', line 212

def valid_connection?
  test(nil)[:success]
end