Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

This gem adds Gitlab issue tracker support to Errbit.

Version 1.0 is compatible with Errbit 0.4.
Version 1.0.x is compatible with Errbit >= 0.4.

For (much) older versions of Errbit, try the 0.x gem versions or consider
updating your Errbit installation
Expand Down
35 changes: 28 additions & 7 deletions lib/errbit_gitlab_plugin/issue_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def errors
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])
unless gitlab_project_exists?(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}
Expand All @@ -116,7 +116,7 @@ def errors

def create_issue(title, body, reported_by = nil)
ticket = with_gitlab do |g|
g.create_issue(gitlab_project_id, title, description: body, labels: options[:labels])
g.create_issue(urlized_project_name, title, description: body, labels: options[:labels])
end

format('%s/%s', url, ticket.id)
Expand All @@ -125,13 +125,24 @@ def create_issue(title, body, reported_by = nil)
private

#
# Tries to find a project with the given name in the given Gitlab installation
# and returns its ID (if any)
# Replaces `/` with the corresponding url encoding
#
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)
def urlized_project_name(project_name = options[:path_with_namespace])
project_name.gsub('/', '%2F')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if its appropriate here, but you can use URI#encode if the intention is to encode the whole string. The second argument to encode is an additional set of characters to encode beyond the standard ones. URI.encode(project_name, '/') for example.

end

#
# Tries to find a project with the given name in the given Gitlab installation.
#
# @return [Gitlab::Project, NilClass] the project or +nil+ if no such project exists or
# the gitlab installation raised an error (e.g. if the project name contains invalid characters)
#
def gitlab_project(gitlab_url = options[:endpoint], token = options[:api_token], project = options[:path_with_namespace])
@project ||= with_gitlab(gitlab_url, token) do |g|
g.project(urlized_project_name(project))
end
rescue Gitlab::Error::NotFound, Gitlab::Error::InternalServerError
nil
end

#
Expand Down Expand Up @@ -169,6 +180,16 @@ def gitlab_user_exists?(gitlab_url, private_token)
false
end

#
# Checks whether the a project with the given name exists
# in the given endpoint and that the given user has access to it
#
# @see #gitlab_project
#
def gitlab_project_exists?(*args)
!!gitlab_project(*args)
end

#
# Connects to the gitlab installation at +gitlab_url+
# using the given +private_token+ and executes the given block
Expand Down
2 changes: 1 addition & 1 deletion lib/errbit_gitlab_plugin/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ErrbitGitlabPlugin
VERSION = '1.0.1'
VERSION = '1.0.2'
end