Commit f774939b authored by Shinya Maeda's avatar Shinya Maeda Committed by Michael Kozono

Abstract with release presenter

This commit abstracts FE specific code with release presenter
parent 35011d7e
# frozen_string_literal: true
class Release < ApplicationRecord
include Presentable
include CacheMarkdownField
include Gitlab::Utils::StrongMemoize
......
# frozen_string_literal: true
class ReleasePresenter < Gitlab::View::Presenter::Delegated
include ActionView::Helpers::UrlHelper
presents :release
delegate :project, :tag, to: :release
def commit_path
return unless release.commit && can_download_code?
project_commit_path(project, release.commit.id)
end
def tag_path
return unless can_download_code?
project_tag_path(project, release.tag)
end
def merge_requests_url
return unless release_mr_issue_urls_available?
project_merge_requests_url(project, params_for_issues_and_mrs)
end
def issues_url
return unless release_mr_issue_urls_available?
project_issues_url(project, params_for_issues_and_mrs)
end
private
def can_download_code?
can?(current_user, :download_code, project)
end
def params_for_issues_and_mrs
{ scope: 'all', state: 'opened', release_tag: release.tag }
end
def release_mr_issue_urls_available?
::Feature.enabled?(:release_mr_issue_urls, project)
end
end
......@@ -1291,6 +1291,8 @@ module API
end
class Release < Grape::Entity
include ::API::Helpers::Presentable
expose :name
expose :tag, as: :tag_name, if: ->(_, _) { can_download_code? }
expose :description
......@@ -1303,8 +1305,8 @@ module API
expose :commit, using: Entities::Commit, if: ->(_, _) { can_download_code? }
expose :upcoming_release?, as: :upcoming_release
expose :milestones, using: Entities::Milestone, if: -> (release, _) { release.milestones.present? }
expose :commit_path, if: ->(_, _) { can_download_code? }
expose :tag_path, if: ->(_, _) { can_download_code? }
expose :commit_path, expose_nil: false
expose :tag_path, expose_nil: false
expose :assets do
expose :assets_count, as: :count do |release, _|
assets_to_exclude = can_download_code? ? [] : [:sources]
......@@ -1316,8 +1318,8 @@ module API
end
end
expose :_links do
expose :merge_requests_url, if: -> (_) { release_mr_issue_urls_available? }
expose :issues_url, if: -> (_) { release_mr_issue_urls_available? }
expose :merge_requests_url, expose_nil: false
expose :issues_url, expose_nil: false
end
private
......@@ -1325,36 +1327,6 @@ module API
def can_download_code?
Ability.allowed?(options[:current_user], :download_code, object.project)
end
def commit_path
return unless object.commit
Gitlab::Routing.url_helpers.project_commit_path(project, object.commit.id)
end
def tag_path
Gitlab::Routing.url_helpers.project_tag_path(project, object.tag)
end
def merge_requests_url
Gitlab::Routing.url_helpers.project_merge_requests_url(project, params_for_issues_and_mrs)
end
def issues_url
Gitlab::Routing.url_helpers.project_issues_url(project, params_for_issues_and_mrs)
end
def params_for_issues_and_mrs
{ scope: 'all', state: 'opened', release_tag: object.tag }
end
def release_mr_issue_urls_available?
::Feature.enabled?(:release_mr_issue_urls, project)
end
def project
@project ||= object.project
end
end
class Tag < Grape::Entity
......
# frozen_string_literal: true
require 'spec_helper'
describe ReleasePresenter do
include Gitlab::Routing.url_helpers
let_it_be(:project) { create(:project, :repository) }
let(:developer) { create(:user) }
let(:guest) { create(:user) }
let(:user) { developer }
let(:release) { create(:release, project: project) }
let(:presenter) { described_class.new(release, current_user: user) }
before do
project.add_developer(developer)
project.add_guest(guest)
end
describe '#commit_path' do
subject { presenter.commit_path }
it 'returns commit path' do
is_expected.to eq(project_commit_path(project, release.commit.id))
end
context 'when commit is not found' do
let(:release) { create(:release, project: project, sha: 'not-found') }
it { is_expected.to be_nil }
end
context 'when user is guest' do
let(:user) { guest }
it { is_expected.to be_nil }
end
end
describe '#tag_path' do
subject { presenter.tag_path }
it 'returns tag path' do
is_expected.to eq(project_tag_path(project, release.tag))
end
context 'when user is guest' do
let(:user) { guest }
it { is_expected.to be_nil }
end
end
describe '#merge_requests_url' do
subject { presenter.merge_requests_url }
it 'returns merge requests url' do
is_expected.to match /#{project_merge_requests_url(project)}/
end
context 'when release_mr_issue_urls feature flag is disabled' do
before do
stub_feature_flags(release_mr_issue_urls: false)
end
it { is_expected.to be_nil }
end
end
describe '#issues_url' do
subject { presenter.issues_url }
it 'returns merge requests url' do
is_expected.to match /#{project_issues_url(project)}/
end
context 'when release_mr_issue_urls feature flag is disabled' do
before do
stub_feature_flags(release_mr_issue_urls: false)
end
it { is_expected.to be_nil }
end
end
end
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