Commit 9841861b authored by Robert Speicher's avatar Robert Speicher

Add Compare support to UrlBuilder

parent ef5c16af
......@@ -24,6 +24,8 @@ module Gitlab
instance.project_job_url(object.project, object, **options)
when Commit
commit_url(object, **options)
when Compare
compare_url(object, **options)
when Group
instance.group_canonical_url(object, **options)
when Issue
......@@ -68,6 +70,12 @@ module Gitlab
instance.commit_url(commit, **options)
end
def compare_url(compare, **options)
return '' unless compare.project
instance.project_compare_url(compare.project, **options.merge(compare.to_param))
end
def note_url(note, **options)
if note.for_commit?
return '' unless note.project
......
# frozen_string_literal: true
FactoryBot.define do
factory :compare do
skip_create # No persistence
start_project { association(:project, :repository) }
target_project { start_project }
start_ref { 'master' }
target_ref { 'feature' }
base_sha { nil }
straight { false }
initialize_with do
CompareService
.new(start_project, start_ref)
.execute(target_project, target_ref, base_sha: base_sha, straight: straight)
end
end
end
......@@ -69,6 +69,27 @@ RSpec.describe Gitlab::UrlBuilder do
end
end
context 'when passing a compare' do
# NOTE: The Compare requires an actual repository, which isn't available
# with the `build_stubbed` strategy used by the table tests above
let_it_be(:compare) { create(:compare) }
let_it_be(:project) { compare.project }
it 'returns the full URL' do
expect(subject.build(compare)).to eq("#{Gitlab.config.gitlab.url}/#{project.full_path}/-/compare/#{compare.base_commit_sha}...#{compare.head_commit_sha}")
end
it 'returns only the path if only_path is given' do
expect(subject.build(compare, only_path: true)).to eq("/#{project.full_path}/-/compare/#{compare.base_commit_sha}...#{compare.head_commit_sha}")
end
it 'returns an empty string for missing project' do
expect(compare).to receive(:project).and_return(nil)
expect(subject.build(compare)).to eq('')
end
end
context 'when passing a commit without a project' do
let(:commit) { build_stubbed(:commit) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment