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
23 changes: 17 additions & 6 deletions objects/vcs/gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# SPDX-License-Identifier: MIT

require 'gitlab'
require 'faraday'
require 'net/http'
require_relative '../git_repo'
require_relative '../clients/gitlab'

Expand All @@ -10,6 +12,15 @@
# API: https://github.com/NARKOZ/gitlab
#
class GitlabRepo
ERRORS = [
Gitlab::Error::Error,
Faraday::Error,
Net::OpenTimeout,
Net::ReadTimeout,
Errno::ECONNREFUSED,
SocketError
].freeze

attr_reader :repo, :name

def initialize(client, json, config = {})
Expand All @@ -34,14 +45,14 @@ def issue(issue_id)
title: title
}
}
rescue Gitlab::Error::NotFound => e
raise "The issue most probably is not found, can' comment: #{e.message}"
rescue *ERRORS => e
raise "Can't read GitLab issue #{issue_id}: #{e.message}"
end

def close_issue(issue_id)
@client.close_issue(@repo.name, issue_id)
rescue Gitlab::Error::NotFound => e
raise "The issue most probably is not found, can't close: #{e.message}"
rescue *ERRORS => e
raise "Can't close GitLab issue #{issue_id}: #{e.message}"
end

def create_issue(data)
Expand Down Expand Up @@ -81,8 +92,8 @@ def add_labels_to_an_issue(issue_id, labels)

def add_comment(issue_id, comment)
@client.create_issue_note(@repo.name, issue_id, comment)
rescue Gitlab::Error::NotFound => e
raise "The issue most probably is not found, can't comment: #{e.message}"
rescue *ERRORS => e
raise "Can't comment on GitLab issue #{issue_id}: #{e.message}"
end

def create_commit_comment(sha, comment)
Expand Down
66 changes: 66 additions & 0 deletions test/test_gitlab.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

require_relative 'test__helper'
require_relative '../objects/clients/gitlab'
require_relative '../objects/vcs/gitlab'

# Github test.
# Author:: Yegor Bugayenko (yegor256@gmail.com)
Expand All @@ -15,4 +16,69 @@ def test_configures_everything_right
gitlab.user('0pdd')['username']
end
end

def test_wraps_issue_read_errors
error = assert_raises(RuntimeError) do
gitlab_repo(client_raising(:issue, Gitlab::Error::Forbidden)).issue(42)
end
assert_includes(error.message, "Can't read GitLab issue 42:")
assert_includes(error.message, 'denied')
end

def test_wraps_issue_close_errors
error = assert_raises(RuntimeError) do
gitlab_repo(client_raising(:close_issue, Gitlab::Error::TooManyRequests)).close_issue(42)
end
assert_includes(error.message, "Can't close GitLab issue 42:")
assert_includes(error.message, 'denied')
end

def test_wraps_comment_errors
error = assert_raises(RuntimeError) do
gitlab_repo(client_raising(:create_issue_note, Net::OpenTimeout)).add_comment(42, 'hello')
end
assert_equal("Can't comment on GitLab issue 42: denied", error.message)
end

def test_wraps_comment_transport_errors
error = assert_raises(RuntimeError) do
gitlab_repo(client_raising(:create_issue_note, Faraday::ConnectionFailed)).add_comment(42, 'hello')
end
assert_equal("Can't comment on GitLab issue 42: denied", error.message)
end

private

def gitlab_repo(client)
GitlabRepo.new(
client,
{
'ref' => 'refs/heads/master',
'checkout_sha' => 'abcdef',
'project' => {
'url' => 'git@gitlab.com:yegor/0pdd.git',
'path_with_namespace' => 'yegor/0pdd',
'default_branch' => 'master'
}
}
)
end

def client_raising(method, error)
response = gitlab_response
Object.new.tap do |client|
client.define_singleton_method(method) do |_repo, *_args|
raise error.new(response) if error < Gitlab::Error::ResponseError # rubocop:disable Style/RaiseArgs
raise error, 'denied'
end
end
end

def gitlab_response
OpenStruct.new(
code: 403,
parsed_response: OpenStruct.new(message: 'denied'),
request: OpenStruct.new(base_uri: 'https://gitlab.com', path: '/api/v4/projects/1')
)
end
end
Loading