From dd9228083347300ad556980ebef955e9ac4ff38a Mon Sep 17 00:00:00 2001 From: Rolf Schmidt Date: Thu, 4 Nov 2021 10:10:56 +0000 Subject: [PATCH] Fixes #3830 - Zammad ignores relative GitLab URLs. --- lib/gitlab/http_client.rb | 13 +++++++++++++ lib/gitlab/linked_issue.rb | 4 ++++ spec/integration/gitlab_spec.rb | 13 +++++++++++++ 3 files changed, 30 insertions(+) diff --git a/lib/gitlab/http_client.rb b/lib/gitlab/http_client.rb index 76338e204..f8493dbdc 100644 --- a/lib/gitlab/http_client.rb +++ b/lib/gitlab/http_client.rb @@ -14,6 +14,19 @@ class GitLab @endpoint = endpoint end + # returns path of the subfolder of the endpoint if exists + def endpoint_path + path = URI.parse(endpoint).path + return if path.blank? + return if path == '/api/graphql' + + if path.start_with?('/') + path = path[1..] + end + + path.sub('api/graphql', '') + end + def perform(payload) response = UserAgent.post( endpoint, diff --git a/lib/gitlab/linked_issue.rb b/lib/gitlab/linked_issue.rb index ae9ff8d9e..e05829e0e 100644 --- a/lib/gitlab/linked_issue.rb +++ b/lib/gitlab/linked_issue.rb @@ -106,6 +106,10 @@ class GitLab fullpath = $2 id = $3 + if client.endpoint_path.present? + fullpath.sub!(client.endpoint_path, '') + end + if client.endpoint.exclude?(host) raise Exceptions::UnprocessableEntity, "Issue link doesn't match configured GitLab endpoint '#{client.endpoint}'" end diff --git a/spec/integration/gitlab_spec.rb b/spec/integration/gitlab_spec.rb index 3810c78b3..d17464d3b 100644 --- a/spec/integration/gitlab_spec.rb +++ b/spec/integration/gitlab_spec.rb @@ -77,4 +77,17 @@ RSpec.describe GitLab, type: :integration, required_envs: %w[GITLAB_ENDPOINT GIT end end end + + describe '#variables' do + describe 'Zammad ignores relative GitLab URLs #3830' do + let(:endpoint) { ENV['GITLAB_ENDPOINT'].sub('api/graphql', 'subfolder/api/graphql') } + let(:instance) { described_class.new(endpoint, ENV['GITLAB_APITOKEN']) } + let(:issue_url) { "https://#{URI.parse(ENV['GITLAB_ISSUE_LINK']).host}/subfolder/group/project/-/issues/1" } + let(:linked_issue) { GitLab::LinkedIssue.new(instance.client) } + + it 'does remove the subfolder from the fullpath to get the issue correctly' do + expect(linked_issue.send(:variables, issue_url)[:fullpath]).to eq('group/project') + end + end + end end