Class: ErrbitGitlabPlugin::IssueTracker

Inherits:
ErrbitPlugin::IssueTracker
  • Object
show all
Defined in:
lib/errbit_gitlab_plugin/issue_tracker.rb

Constant Summary collapse

LABEL =
'gitlab'
NOTE =
"Creating issues may take some time as the actual project ID has to be looked up using the Gitlab API. <br/>
If you are using gitlab.com as installation, please make sure to use 'https://', otherwise, their API
will not accept some of the our commands."
FIELDS =
{
    endpoint:            {
        label:       'Gitlab URL',
        placeholder: 'The URL to your gitlab installation or the public gitlab server, e.g. https://www.gitlab.com'
    },
    api_token:           {
        label:       'API Token',
        placeholder: "Your account's API token (see Profile -> Account)"
    },
    path_with_namespace: {
        label:       'Project name',
        placeholder: 'E.g. your_username/your_project'
    }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fieldsObject

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.



39
40
41
# File 'lib/errbit_gitlab_plugin/issue_tracker.rb', line 39

def self.fields
  FIELDS
end

.iconsObject

Icons to be displayed for this issue tracker



46
47
48
49
50
51
52
# 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

.labelObject



26
27
28
# File 'lib/errbit_gitlab_plugin/issue_tracker.rb', line 26

def self.label
  LABEL
end

.noteObject



30
31
32
# File 'lib/errbit_gitlab_plugin/issue_tracker.rb', line 30

def self.note
  NOTE
end

Instance Method Details

#comments_allowed?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/errbit_gitlab_plugin/issue_tracker.rb', line 73

def comments_allowed?
  true
end

#configured?Boolean

Returns:

  • (Boolean)


69
70
71
# 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) ⇒ Object



113
114
115
116
117
118
119
# 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

#errorsObject

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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_argsObject

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



58
59
60
# File 'lib/errbit_gitlab_plugin/issue_tracker.rb', line 58

def render_body_args
  ['errbit_gitlab_plugin/issue', :formats => [:md]]
end

#urlString

Returns the URL to the given project’s issues section.

Returns:

  • (String)

    the URL to the given project’s issues section



65
66
67
# File 'lib/errbit_gitlab_plugin/issue_tracker.rb', line 65

def url
  format '%s/%s/issues', options[:endpoint], options[:path_with_namespace]
end