class ErrbitGitlabPlugin::IssueTracker

Constants

FIELDS
LABEL
NOTE

Public Class Methods

fields() click to toggle source

Form fields that will be presented to the administrator when setting up or editing the errbit app. The values we collect will be available for use later when we have an instance of this class.

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 39
def self.fields
  FIELDS
end
icons() click to toggle source

Icons to be displayed for this issue tracker

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 46
def self.icons
  @icons ||= {
      create:   ['image/png', ErrbitGitlabPlugin.read_static_file('gitlab_create.png')],
      goto:     ['image/png', ErrbitGitlabPlugin.read_static_file('gitlab_goto.png')],
      inactive: ['image/png', ErrbitGitlabPlugin.read_static_file('gitlab_inactive.png')]
  }
end
label() click to toggle source
# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 26
def self.label
  LABEL
end
note() click to toggle source
# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 30
def self.note
  NOTE
end

Public Instance Methods

comments_allowed?() click to toggle source
# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 73
def comments_allowed?
  true
end
configured?() click to toggle source
# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 69
def configured?
  self.class.fields.keys.all? { |field_name| options[field_name].present? }
end
create_issue(title, body, reported_by = nil) click to toggle source
# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 113
def create_issue(title, body, reported_by = nil)
  ticket = with_gitlab do |g|
    g.create_issue(gitlab_project_id, title, description: body, labels: 'errbit')
  end

  format('%s/%s', url, ticket.id)
end
errors() click to toggle source

Called to validate user input. Just return a hash of errors if there are any

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 78
def errors
  errs = []

  # Make sure that every field is filled out
  self.class.fields.except(:project_id).each_with_object({}) do |(field_name, field_options), h|
    if options[field_name].blank?
      errs << "#{field_options[:label]} must be present"
    end
  end

  # We can only perform the other tests if the necessary values are at least present
  return {:base => errs.to_sentence} unless errs.size.zero?

  # Check if the given endpoint actually exists
  unless gitlab_endpoint_exists?(options[:endpoint])
    errs << 'No Gitlab installation was found under the given URL'
    return {:base => errs.to_sentence}
  end

  # Check if a user by the given token exists
  unless gitlab_user_exists?(options[:endpoint], options[:api_token])
    errs << 'No user with the given API token was found'
    return {:base => errs.to_sentence}
  end

  # Check if there is a project with the given name on the server
  unless gitlab_project_id(options[:endpoint], options[:api_token], options[:path_with_namespace])
    errs << "A project named '#{options[:path_with_namespace]}' could not be found on the server.
             Please make sure to enter it exactly as it appears in your address bar in Gitlab (case sensitive)"
    return {:base => errs.to_sentence}
  end

  {}
end
render_body_args() click to toggle source

Used to pass an own template to errbit’s issue rendering. The rendered template is then passed to any create_issue call.

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 58
def render_body_args
  ['errbit_gitlab_plugin/issue', :formats => [:md]]
end
url() click to toggle source

@return [String] the URL to the given project’s issues section

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 65
def url
  format '%s/%s/issues', options[:endpoint], options[:path_with_namespace]
end

Private Instance Methods

gitlab_endpoint(gitlab_url) click to toggle source

@return [String] a formatted APIv3 URL for the given gitlab_url

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 136
def gitlab_endpoint(gitlab_url)
  format '%s/api/v3', gitlab_url
end
gitlab_endpoint_exists?(gitlab_url) click to toggle source

Checks whether there is a gitlab installation at the given gitlab_url

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 144
def gitlab_endpoint_exists?(gitlab_url)
  with_gitlab(gitlab_url, 'Iamsecret') do |g|
    g.user
  end
rescue Gitlab::Error::Unauthorized
  true
rescue Exception
  false
end
gitlab_project_id(gitlab_url = options[:endpoint], token = options[:api_token], project = options[:path_with_namespace]) click to toggle source

Tries to find a project with the given name in the given Gitlab installation and returns its ID (if any)

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 127
def gitlab_project_id(gitlab_url = options[:endpoint], token = options[:api_token], project = options[:path_with_namespace])
  @project_id ||= with_gitlab(gitlab_url, token) do |g|
    g.projects.auto_paginate.detect { |p| p.path_with_namespace == project }.try(:id)
  end
end
gitlab_user_exists?(gitlab_url, private_token) click to toggle source

Checks whether a user with the given token exists in the gitlab installation located at gitlab_url

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 158
def gitlab_user_exists?(gitlab_url, private_token)
  with_gitlab(gitlab_url, private_token) do |g|
    g.user
  end

  true
rescue Gitlab::Error::Unauthorized
  false
end
with_gitlab(gitlab_url = options[:endpoint], private_token = options[:api_token]) { |client(endpoint: gitlab_endpoint(gitlab_url), private_token: private_token, user_agent: 'Errbit User Agent')| ... } click to toggle source

Connects to the gitlab installation at gitlab_url using the given private_token and executes the given block

# File lib/errbit_gitlab_plugin/issue_tracker.rb, line 172
def with_gitlab(gitlab_url = options[:endpoint], private_token = options[:api_token])
  yield Gitlab.client(endpoint:      gitlab_endpoint(gitlab_url),
                      private_token: private_token,
                      user_agent:    'Errbit User Agent')
end