Commit 49046fe3 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'rs-api-compare-web_url' into 'master'

Expose web_url to Compare API endpoint

See merge request gitlab-org/gitlab!68676
parents 864ab350 aea08697
...@@ -25,6 +25,16 @@ class Compare ...@@ -25,6 +25,16 @@ class Compare
@straight = straight @straight = straight
end end
# Return a Hash of parameters for passing to a URL helper
#
# See `namespace_project_compare_url`
def to_param
{
from: @straight ? start_commit_sha : base_commit_sha,
to: head_commit_sha
}
end
def cache_key def cache_key
[@project, :compare, diff_refs.hash] [@project, :compare, diff_refs.hash]
end end
......
...@@ -200,7 +200,8 @@ Example response: ...@@ -200,7 +200,8 @@ Example response:
"deleted_file": false "deleted_file": false
}], }],
"compare_timeout": false, "compare_timeout": false,
"compare_same_ref": false "compare_same_ref": false,
"web_url": "https://gitlab.example.com/thedude/gitlab-foss/-/compare/ae73cb07c9eeaf35924a10f713b364d32b2dd34f...0b4bc9a49b562e85de7cc9e834518ea6828729b9"
} }
``` ```
......
...@@ -20,6 +20,10 @@ module API ...@@ -20,6 +20,10 @@ module API
end end
expose :same, as: :compare_same_ref expose :same, as: :compare_same_ref
expose :web_url do |compare, _|
Gitlab::UrlBuilder.build(compare)
end
end end
end end
end end
...@@ -24,6 +24,8 @@ module Gitlab ...@@ -24,6 +24,8 @@ module Gitlab
instance.project_job_url(object.project, object, **options) instance.project_job_url(object.project, object, **options)
when Commit when Commit
commit_url(object, **options) commit_url(object, **options)
when Compare
compare_url(object, **options)
when Group when Group
instance.group_canonical_url(object, **options) instance.group_canonical_url(object, **options)
when Issue when Issue
...@@ -68,6 +70,12 @@ module Gitlab ...@@ -68,6 +70,12 @@ module Gitlab
instance.commit_url(commit, **options) instance.commit_url(commit, **options)
end 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) def note_url(note, **options)
if note.for_commit? if note.for_commit?
return '' unless note.project 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 ...@@ -69,6 +69,27 @@ RSpec.describe Gitlab::UrlBuilder do
end end
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 context 'when passing a commit without a project' do
let(:commit) { build_stubbed(:commit) } let(:commit) { build_stubbed(:commit) }
......
...@@ -354,6 +354,7 @@ RSpec.describe API::Repositories do ...@@ -354,6 +354,7 @@ RSpec.describe API::Repositories do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['commits']).to be_present expect(json_response['commits']).to be_present
expect(json_response['diffs']).to be_present expect(json_response['diffs']).to be_present
expect(json_response['web_url']).to be_present
end end
it "compares branches with explicit merge-base mode" do it "compares branches with explicit merge-base mode" do
...@@ -365,6 +366,7 @@ RSpec.describe API::Repositories do ...@@ -365,6 +366,7 @@ RSpec.describe API::Repositories do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['commits']).to be_present expect(json_response['commits']).to be_present
expect(json_response['diffs']).to be_present expect(json_response['diffs']).to be_present
expect(json_response['web_url']).to be_present
end end
it "compares branches with explicit straight mode" do it "compares branches with explicit straight mode" do
...@@ -376,6 +378,7 @@ RSpec.describe API::Repositories do ...@@ -376,6 +378,7 @@ RSpec.describe API::Repositories do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['commits']).to be_present expect(json_response['commits']).to be_present
expect(json_response['diffs']).to be_present expect(json_response['diffs']).to be_present
expect(json_response['web_url']).to be_present
end end
it "compares tags" do it "compares tags" do
...@@ -384,6 +387,7 @@ RSpec.describe API::Repositories do ...@@ -384,6 +387,7 @@ RSpec.describe API::Repositories do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['commits']).to be_present expect(json_response['commits']).to be_present
expect(json_response['diffs']).to be_present expect(json_response['diffs']).to be_present
expect(json_response['web_url']).to be_present
end end
it "compares commits" do it "compares commits" do
...@@ -393,6 +397,7 @@ RSpec.describe API::Repositories do ...@@ -393,6 +397,7 @@ RSpec.describe API::Repositories do
expect(json_response['commits']).to be_empty expect(json_response['commits']).to be_empty
expect(json_response['diffs']).to be_empty expect(json_response['diffs']).to be_empty
expect(json_response['compare_same_ref']).to be_falsey expect(json_response['compare_same_ref']).to be_falsey
expect(json_response['web_url']).to be_present
end end
it "compares commits in reverse order" do it "compares commits in reverse order" do
...@@ -401,6 +406,7 @@ RSpec.describe API::Repositories do ...@@ -401,6 +406,7 @@ RSpec.describe API::Repositories do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(json_response['commits']).to be_present expect(json_response['commits']).to be_present
expect(json_response['diffs']).to be_present expect(json_response['diffs']).to be_present
expect(json_response['web_url']).to be_present
end end
it "compare commits between different projects with non-forked relation" do it "compare commits between different projects with non-forked relation" do
......
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