Class: JiraService

Inherits:
IssueTrackerService show all
Includes:
HTTParty
Defined in:
app/models/project_services/jira_service.rb

Overview

Schema Information

Table name: services

id                    :integer          not null, primary key
type                  :string
title                 :string
project_id            :integer
created_at            :datetime         not null
updated_at            :datetime         not null
active                :boolean          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
category              :string           default("common"), not null
default               :boolean          default(FALSE)
wiki_page_events      :boolean          default(TRUE)

Constant Summary collapse

DEFAULT_API_VERSION =
2

Instance Method Summary collapse

Methods inherited from IssueTrackerService

#default?, #initialize_properties, #issue_path, #issue_url, #new_issue_path, #project_path, #supported_events

Methods inherited from Service

#activated?, #async_execute, available_services_names, boolean_accessor, #can_test?, #category, create_from_template, #initialize_properties, #issue_tracker?, prop_accessor, #reset_updated_properties, #supported_events, #template?, #test, #updated_properties

Instance Method Details

#create_cross_reference_note(mentioned, noteable, author) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/project_services/jira_service.rb', line 93

def create_cross_reference_note(mentioned, noteable, author)
  issue_name = mentioned.id
  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,
      url: entity_url,
      title: noteable.title
    }
  }

  add_comment(data, issue_name)
end

#descriptionObject


62
63
64
65
66
67
68
# File 'app/models/project_services/jira_service.rb', line 62

def description
  if self.properties && self.properties['description'].present?
    self.properties['description']
  else
    'Jira issue tracker'
  end
end

#execute(push, issue = nil) ⇒ Object


83
84
85
86
87
88
89
90
91
# File 'app/models/project_services/jira_service.rb', line 83

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

#fieldsObject


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

def fields
  super.push(
    { type: 'text', name: 'api_url', placeholder: 'https://jira.example.com/rest/api/2' },
    { type: 'text', name: 'username', placeholder: '' },
    { type: 'password', name: 'password', placeholder: '' },
    { type: 'text', name: 'jira_issue_transition_id', placeholder: '2' }
  )
end

#helpObject


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

def help
  'Setting `project_url`, `issues_url` and `new_issue_url` will '\
  'allow a user to easily navigate to the Jira issue tracker. See the '\
  '[integration doc](http://doc.gitlab.com/ce/integration/external-issue-tracker.html) '\
  'for details.'
end

#reset_passwordObject


40
41
42
43
44
45
# File 'app/models/project_services/jira_service.rb', line 40

def reset_password
  # don't reset the password if a new one is provided
  if api_url_changed? && !password_touched?
    self.password = nil
  end
end

#test_settingsObject


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/models/project_services/jira_service.rb', line 124

def test_settings
  return unless api_url.present?
  result = JiraService.get(
    jira_api_test_url,
    headers: {
      'Content-Type' => 'application/json',
      'Authorization' => "Basic #{auth}"
    }
  )

  case result.code
  when 201, 200
    Rails.logger.info("#{self.class.name} SUCCESS #{result.code}: Successfully connected to #{api_url}.")
    true
  else
    Rails.logger.info("#{self.class.name} ERROR #{result.code}: #{result.parsed_response}")
    false
  end
rescue Errno::ECONNREFUSED => e
  Rails.logger.info "#{self.class.name} ERROR: #{e.message}. API URL: #{api_url}."
  false
end

#titleObject


54
55
56
57
58
59
60
# File 'app/models/project_services/jira_service.rb', line 54

def title
  if self.properties && self.properties['title'].present?
    self.properties['title']
  else
    'JIRA'
  end
end

#to_paramObject


70
71
72
# File 'app/models/project_services/jira_service.rb', line 70

def to_param
  'jira'
end