Refactor and expose only Gitlab::UrlBuilder.build(record)

Signed-off-by: Rémy Coutable <remy@rymai.me>
This commit is contained in:
Rémy Coutable 2016-04-13 11:25:42 +02:00
parent bbf49ffca1
commit 02cfbf0db5
No known key found for this signature in database
GPG key ID: 46DF07E5CD9E96AB
9 changed files with 147 additions and 101 deletions

View file

@ -154,7 +154,7 @@ class Commit
id: id, id: id,
message: safe_message, message: safe_message,
timestamp: committed_date.xmlschema, timestamp: committed_date.xmlschema,
url: commit_url, url: Gitlab::UrlBuilder.build(self),
author: { author: {
name: author_name, name: author_name,
email: author_email email: author_email
@ -168,10 +168,6 @@ class Commit
data data
end end
def commit_url
project.present? ? "#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/commit/#{id}" : ""
end
# Discover issues should be closed when this commit is pushed to a project's # Discover issues should be closed when this commit is pushed to a project's
# default branch. # default branch.
def closes_issues(current_user = self.committer) def closes_issues(current_user = self.committer)

View file

@ -3,7 +3,7 @@ module Issues
def hook_data(issue, action) def hook_data(issue, action)
issue_data = issue.to_hook_data(current_user) issue_data = issue.to_hook_data(current_user)
issue_url = Gitlab::UrlBuilder.new(:issue).build(issue.id) issue_url = Gitlab::UrlBuilder.build(issue)
issue_data[:object_attributes].merge!(url: issue_url, action: action) issue_data[:object_attributes].merge!(url: issue_url, action: action)
issue_data issue_data
end end

View file

@ -20,8 +20,7 @@ module MergeRequests
def hook_data(merge_request, action) def hook_data(merge_request, action)
hook_data = merge_request.to_hook_data(current_user) hook_data = merge_request.to_hook_data(current_user)
merge_request_url = Gitlab::UrlBuilder.new(:merge_request).build(merge_request.id) hook_data[:object_attributes][:url] = Gitlab::UrlBuilder.build(merge_request)
hook_data[:object_attributes][:url] = merge_request_url
hook_data[:object_attributes][:action] = action hook_data[:object_attributes][:action] = action
hook_data hook_data
end end

View file

@ -1,5 +1,5 @@
- project = note.project - project = note.project
- note_url = Gitlab::UrlBuilder.new(:note).build(note.id) - note_url = Gitlab::UrlBuilder.build(note)
- noteable_identifier = note.noteable.try(:iid) || note.noteable.id - noteable_identifier = note.noteable.try(:iid) || note.noteable.id
.search-result-row .search-result-row
%h5.note-search-caption.str-truncated %h5.note-search-caption.str-truncated

View file

@ -59,8 +59,7 @@ module Gitlab
repository: project.hook_attrs.slice(:name, :url, :description, :homepage) repository: project.hook_attrs.slice(:name, :url, :description, :homepage)
} }
base_data[:object_attributes][:url] = base_data[:object_attributes][:url] = Gitlab::UrlBuilder.build(note)
Gitlab::UrlBuilder.new(:note).build(note.id)
base_data base_data
end end

View file

@ -4,50 +4,58 @@ module Gitlab
include GitlabRoutingHelper include GitlabRoutingHelper
include ActionView::RecordIdentifier include ActionView::RecordIdentifier
def initialize(type) attr_reader :object
@type = type
def self.build(object)
new(object).url
end end
def build(id) def url
case @type case object
when :issue when Commit
build_issue_url(id) commit_url
when :merge_request when Issue
build_merge_request_url(id) issue_url(object)
when :note when MergeRequest
build_note_url(id) merge_request_url(object)
when Note
note_url
else
raise NotImplementedError.new("No URL builder defined for #{object.class}")
end end
end end
private private
def build_issue_url(id) def initialize(object)
issue = Issue.find(id) @object = object
issue_url(issue)
end end
def build_merge_request_url(id) def commit_url(opts = {})
merge_request = MergeRequest.find(id) return '' if object.project.nil?
merge_request_url(merge_request)
namespace_project_commit_url({
namespace_id: object.project.namespace,
project_id: object.project,
id: object.id
}.merge!(opts))
end end
def build_note_url(id) def note_url
note = Note.find(id) if object.for_commit?
if note.for_commit? commit_url(id: object.commit_id, anchor: dom_id(object))
namespace_project_commit_url(namespace_id: note.project.namespace,
id: note.commit_id, elsif object.for_issue?
project_id: note.project, issue = Issue.find(object.noteable_id)
anchor: dom_id(note)) issue_url(issue, anchor: dom_id(object))
elsif note.for_issue?
issue = Issue.find(note.noteable_id) elsif object.for_merge_request?
issue_url(issue, anchor: dom_id(note)) merge_request = MergeRequest.find(object.noteable_id)
elsif note.for_merge_request? merge_request_url(merge_request, anchor: dom_id(object))
merge_request = MergeRequest.find(note.noteable_id)
merge_request_url(merge_request, anchor: dom_id(note)) elsif object.for_snippet?
elsif note.for_snippet? snippet = Snippet.find(object.noteable_id)
snippet = Snippet.find(note.noteable_id) project_snippet_url(snippet, anchor: dom_id(object))
project_snippet_url(snippet, anchor: dom_id(note))
end end
end end
end end

12
spec/factories/commits.rb Normal file
View file

@ -0,0 +1,12 @@
require_relative '../support/repo_helpers'
FactoryGirl.define do
factory :commit do
git_commit RepoHelpers.sample_commit
project factory: :empty_project
initialize_with do
new(git_commit, project)
end
end
end

View file

@ -4,13 +4,12 @@ describe 'Gitlab::NoteDataBuilder', lib: true do
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:data) { Gitlab::NoteDataBuilder.build(note, user) } let(:data) { Gitlab::NoteDataBuilder.build(note, user) }
let(:note_url) { Gitlab::UrlBuilder.new(:note).build(note.id) }
let(:fixed_time) { Time.at(1425600000) } # Avoid time precision errors let(:fixed_time) { Time.at(1425600000) } # Avoid time precision errors
before(:each) do before(:each) do
expect(data).to have_key(:object_attributes) expect(data).to have_key(:object_attributes)
expect(data[:object_attributes]).to have_key(:url) expect(data[:object_attributes]).to have_key(:url)
expect(data[:object_attributes][:url]).to eq(note_url) expect(data[:object_attributes][:url]).to eq(Gitlab::UrlBuilder.build(note))
expect(data[:object_kind]).to eq('note') expect(data[:object_kind]).to eq('note')
expect(data[:user]).to eq(user.hook_attrs) expect(data[:user]).to eq(user.hook_attrs)
end end

View file

@ -1,77 +1,110 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::UrlBuilder, lib: true do describe Gitlab::UrlBuilder, lib: true do
describe 'When asking for an issue' do describe '.build' do
it 'returns the issue url' do context 'when passing a Commit' do
issue = create(:issue) it 'returns a proper URL' do
url = Gitlab::UrlBuilder.new(:issue).build(issue.id) commit = build_stubbed(:commit)
expect(url).to eq "#{Settings.gitlab['url']}/#{issue.project.path_with_namespace}/issues/#{issue.iid}"
url = described_class.build(commit)
expect(url).to eq "#{Settings.gitlab['url']}/#{commit.project.path_with_namespace}/commit/#{commit.id}"
end
end end
end
describe 'When asking for an merge request' do context 'when passing an Issue' do
it 'returns the merge request url' do it 'returns a proper URL' do
merge_request = create(:merge_request) issue = build_stubbed(:issue, iid: 42)
url = Gitlab::UrlBuilder.new(:merge_request).build(merge_request.id)
expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.path_with_namespace}/merge_requests/#{merge_request.iid}" url = described_class.build(issue)
expect(url).to eq "#{Settings.gitlab['url']}/#{issue.project.path_with_namespace}/issues/#{issue.iid}"
end
end end
end
describe 'When asking for a note on commit' do context 'when passing a MergeRequest' do
let(:note) { create(:note_on_commit) } it 'returns a proper URL' do
let(:url) { Gitlab::UrlBuilder.new(:note).build(note.id) } merge_request = build_stubbed(:merge_request, iid: 42)
it 'returns the note url' do url = described_class.build(merge_request)
expect(url).to eq "#{Settings.gitlab['url']}/#{note.project.path_with_namespace}/commit/#{note.commit_id}#note_#{note.id}"
expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.path_with_namespace}/merge_requests/#{merge_request.iid}"
end
end end
end
describe 'When asking for a note on commit diff' do context 'when passing a Note' do
let(:note) { create(:note_on_commit_diff) } context 'on a Commit' do
let(:url) { Gitlab::UrlBuilder.new(:note).build(note.id) } it 'returns a proper URL' do
note = build_stubbed(:note_on_commit)
it 'returns the note url' do url = described_class.build(note)
expect(url).to eq "#{Settings.gitlab['url']}/#{note.project.path_with_namespace}/commit/#{note.commit_id}#note_#{note.id}"
end
end
describe 'When asking for a note on issue' do expect(url).to eq "#{Settings.gitlab['url']}/#{note.project.path_with_namespace}/commit/#{note.commit_id}#note_#{note.id}"
let(:issue) { create(:issue) } end
let(:note) { create(:note_on_issue, noteable_id: issue.id) } end
let(:url) { Gitlab::UrlBuilder.new(:note).build(note.id) }
it 'returns the note url' do context 'on a CommitDiff' do
expect(url).to eq "#{Settings.gitlab['url']}/#{issue.project.path_with_namespace}/issues/#{issue.iid}#note_#{note.id}" it 'returns a proper URL' do
end note = build_stubbed(:note_on_commit_diff)
end
describe 'When asking for a note on merge request' do url = described_class.build(note)
let(:merge_request) { create(:merge_request) }
let(:note) { create(:note_on_merge_request, noteable_id: merge_request.id) }
let(:url) { Gitlab::UrlBuilder.new(:note).build(note.id) }
it 'returns the note url' do expect(url).to eq "#{Settings.gitlab['url']}/#{note.project.path_with_namespace}/commit/#{note.commit_id}#note_#{note.id}"
expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.path_with_namespace}/merge_requests/#{merge_request.iid}#note_#{note.id}" end
end end
end
describe 'When asking for a note on merge request diff' do context 'on an Issue' do
let(:merge_request) { create(:merge_request) } it 'returns a proper URL' do
let(:note) { create(:note_on_merge_request_diff, noteable_id: merge_request.id) } issue = create(:issue, iid: 42)
let(:url) { Gitlab::UrlBuilder.new(:note).build(note.id) } note = build_stubbed(:note_on_issue, noteable: issue)
it 'returns the note url' do url = described_class.build(note)
expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.path_with_namespace}/merge_requests/#{merge_request.iid}#note_#{note.id}"
end
end
describe 'When asking for a note on project snippet' do expect(url).to eq "#{Settings.gitlab['url']}/#{issue.project.path_with_namespace}/issues/#{issue.iid}#note_#{note.id}"
let(:snippet) { create(:project_snippet) } end
let(:note) { create(:note_on_project_snippet, noteable_id: snippet.id) } end
let(:url) { Gitlab::UrlBuilder.new(:note).build(note.id) }
it 'returns the note url' do context 'on a MergeRequest' do
expect(url).to eq "#{Settings.gitlab['url']}/#{snippet.project.path_with_namespace}/snippets/#{note.noteable_id}#note_#{note.id}" it 'returns a proper URL' do
merge_request = create(:merge_request, iid: 42)
note = build_stubbed(:note_on_merge_request, noteable: merge_request)
url = described_class.build(note)
expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.path_with_namespace}/merge_requests/#{merge_request.iid}#note_#{note.id}"
end
end
context 'on a MergeRequestDiff' do
it 'returns a proper URL' do
merge_request = create(:merge_request, iid: 42)
note = build_stubbed(:note_on_merge_request_diff, noteable: merge_request)
url = described_class.build(note)
expect(url).to eq "#{Settings.gitlab['url']}/#{merge_request.project.path_with_namespace}/merge_requests/#{merge_request.iid}#note_#{note.id}"
end
end
context 'on a ProjectSnippet' do
it 'returns a proper URL' do
project_snippet = create(:project_snippet)
note = build_stubbed(:note_on_project_snippet, noteable: project_snippet)
url = described_class.build(note)
expect(url).to eq "#{Settings.gitlab['url']}/#{project_snippet.project.path_with_namespace}/snippets/#{note.noteable_id}#note_#{note.id}"
end
end
context 'on another object' do
it 'returns a proper URL' do
project = build_stubbed(:project)
expect { described_class.build(project) }.
to raise_error(NotImplementedError, 'No URL builder defined for Project')
end
end
end end
end end
end end