diff --git a/README.md b/README.md index 3479a93..b2d9d6c 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/errbit_gitlab_plugin/issue_tracker.rb b/lib/errbit_gitlab_plugin/issue_tracker.rb index 0ba4483..7202a33 100644 --- a/lib/errbit_gitlab_plugin/issue_tracker.rb +++ b/lib/errbit_gitlab_plugin/issue_tracker.rb @@ -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} @@ -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) @@ -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') + 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 # @@ -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 diff --git a/lib/errbit_gitlab_plugin/version.rb b/lib/errbit_gitlab_plugin/version.rb index af8a3a1..4bcfaa7 100644 --- a/lib/errbit_gitlab_plugin/version.rb +++ b/lib/errbit_gitlab_plugin/version.rb @@ -1,3 +1,3 @@ module ErrbitGitlabPlugin - VERSION = '1.0.1' + VERSION = '1.0.2' end