From 06050b345003f800e462d9fc399c98605e19ed4e Mon Sep 17 00:00:00 2001 From: Ghxst <200635707+GHX5T-SOL@users.noreply.github.com> Date: Tue, 26 May 2026 07:29:39 +0200 Subject: [PATCH 1/2] #853: wrap GitLab VCS failures --- objects/vcs/gitlab.rb | 19 +++++++++----- test/test_gitlab.rb | 59 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 6 deletions(-) diff --git a/objects/vcs/gitlab.rb b/objects/vcs/gitlab.rb index bc0249e8..4812d0b5 100644 --- a/objects/vcs/gitlab.rb +++ b/objects/vcs/gitlab.rb @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT require 'gitlab' +require 'net/http' require_relative '../git_repo' require_relative '../clients/gitlab' @@ -10,6 +11,12 @@ # API: https://github.com/NARKOZ/gitlab # class GitlabRepo + ERRORS = [ + Gitlab::Error::Error, + Net::OpenTimeout, + Errno::ECONNREFUSED + ].freeze + attr_reader :repo, :name def initialize(client, json, config = {}) @@ -34,14 +41,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) @@ -81,8 +88,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 GitLab issue #{issue_id}: #{e.message}" end def create_commit_comment(sha, comment) diff --git a/test/test_gitlab.rb b/test/test_gitlab.rb index 313b9690..902d770a 100644 --- a/test/test_gitlab.rb +++ b/test/test_gitlab.rb @@ -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) @@ -15,4 +16,62 @@ 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 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 From e0dce92df1c7ff1cadf50e97afd1b9ba532f2107 Mon Sep 17 00:00:00 2001 From: Ghost Date: Tue, 26 May 2026 07:34:14 +0200 Subject: [PATCH 2/2] Cover GitLab transport failures --- objects/vcs/gitlab.rb | 8 ++++++-- test/test_gitlab.rb | 9 ++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/objects/vcs/gitlab.rb b/objects/vcs/gitlab.rb index 4812d0b5..991da1ea 100644 --- a/objects/vcs/gitlab.rb +++ b/objects/vcs/gitlab.rb @@ -2,6 +2,7 @@ # SPDX-License-Identifier: MIT require 'gitlab' +require 'faraday' require 'net/http' require_relative '../git_repo' require_relative '../clients/gitlab' @@ -13,8 +14,11 @@ class GitlabRepo ERRORS = [ Gitlab::Error::Error, + Faraday::Error, Net::OpenTimeout, - Errno::ECONNREFUSED + Net::ReadTimeout, + Errno::ECONNREFUSED, + SocketError ].freeze attr_reader :repo, :name @@ -89,7 +93,7 @@ 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 *ERRORS => e - raise "Can't comment GitLab issue #{issue_id}: #{e.message}" + raise "Can't comment on GitLab issue #{issue_id}: #{e.message}" end def create_commit_comment(sha, comment) diff --git a/test/test_gitlab.rb b/test/test_gitlab.rb index 902d770a..ab4571b3 100644 --- a/test/test_gitlab.rb +++ b/test/test_gitlab.rb @@ -37,7 +37,14 @@ 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 GitLab issue 42: denied", error.message) + 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