Commit d20f263a authored by Doug Stull's avatar Doug Stull

Expose items for suggesting a pipeline

- this is an incrimental MR that will expose a few objects
  needed for suggesting a pipeline in the front end
parent ae5d83de
...@@ -2299,6 +2299,10 @@ class Project < ApplicationRecord ...@@ -2299,6 +2299,10 @@ class Project < ApplicationRecord
false false
end end
def uses_default_ci_config?
ci_config_path.blank? || ci_config_path == Gitlab::FileDetector::PATTERNS[:gitlab_ci]
end
private private
def closest_namespace_setting(name) def closest_namespace_setting(name)
......
...@@ -50,6 +50,19 @@ class MergeRequestWidgetEntity < Grape::Entity ...@@ -50,6 +50,19 @@ class MergeRequestWidgetEntity < Grape::Entity
ci_environments_status_project_merge_request_path(merge_request.project, merge_request) ci_environments_status_project_merge_request_path(merge_request.project, merge_request)
end end
expose :merge_request_add_ci_config_path, if: ->(mr, _) { can_add_ci_config_path?(mr) } do |merge_request|
project_new_blob_path(
merge_request.source_project,
merge_request.source_branch,
file_name: '.gitlab-ci.yml',
commit_message: s_("CommitMessage|Add %{file_name}") % { file_name: Gitlab::FileDetector::PATTERNS[:gitlab_ci] }
)
end
expose :human_access do |merge_request|
merge_request.project.team.human_max_access(current_user&.id)
end
# Rendering and redacting Markdown can be expensive. These links are # Rendering and redacting Markdown can be expensive. These links are
# just nice to have in the merge request widget, so only # just nice to have in the merge request widget, so only
# include them if they are explicitly requested on first load. # include them if they are explicitly requested on first load.
...@@ -75,6 +88,13 @@ class MergeRequestWidgetEntity < Grape::Entity ...@@ -75,6 +88,13 @@ class MergeRequestWidgetEntity < Grape::Entity
@presenters ||= {} @presenters ||= {}
@presenters[merge_request] ||= MergeRequestPresenter.new(merge_request, current_user: current_user) # rubocop: disable CodeReuse/Presenter @presenters[merge_request] ||= MergeRequestPresenter.new(merge_request, current_user: current_user) # rubocop: disable CodeReuse/Presenter
end end
def can_add_ci_config_path?(merge_request)
merge_request.source_project&.uses_default_ci_config? &&
merge_request.all_pipelines.none? &&
merge_request.commits_count.positive? &&
can?(current_user, :push_code, merge_request.source_project)
end
end end
MergeRequestWidgetEntity.prepend_if_ee('EE::MergeRequestWidgetEntity') MergeRequestWidgetEntity.prepend_if_ee('EE::MergeRequestWidgetEntity')
...@@ -2095,6 +2095,28 @@ describe Project do ...@@ -2095,6 +2095,28 @@ describe Project do
end end
end end
describe '#uses_default_ci_config?' do
let(:project) { build(:project)}
it 'has a custom ci config path' do
project.ci_config_path = 'something_custom'
expect(project.uses_default_ci_config?).to be_falsey
end
it 'has a blank ci config path' do
project.ci_config_path = ''
expect(project.uses_default_ci_config?).to be_truthy
end
it 'does not have a custom ci config path' do
project.ci_config_path = nil
expect(project.uses_default_ci_config?).to be_truthy
end
end
describe '#latest_successful_build_for_ref' do describe '#latest_successful_build_for_ref' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:pipeline) { create_pipeline(project) } let(:pipeline) { create_pipeline(project) }
......
...@@ -53,6 +53,94 @@ describe MergeRequestWidgetEntity do ...@@ -53,6 +53,94 @@ describe MergeRequestWidgetEntity do
.to eq("/#{resource.project.full_path}/-/merge_requests/#{resource.iid}.diff") .to eq("/#{resource.project.full_path}/-/merge_requests/#{resource.iid}.diff")
end end
describe 'merge_request_add_ci_config_path' do
before do
project.add_role(user, role)
end
context 'when there are pipelines' do
let(:role) { :developer }
before do
create(:ci_empty_pipeline, project: project, sha: resource.all_commit_shas.first, ref: resource.source_branch)
end
it 'no ci config path' do
expect(subject[:merge_request_add_ci_config_path]).to be_nil
end
end
context 'when there are no pipelines' do
context 'when user has permissions' do
let(:role) { :developer }
it 'has add ci config path' do
expect(subject[:merge_request_add_ci_config_path])
.to eq("/#{resource.project.full_path}/-/new/#{resource.source_branch}?commit_message=Add+.gitlab-ci.yml&file_name=.gitlab-ci.yml")
end
context 'when source project is missing' do
before do
resource.source_project = nil
end
it 'returns a blank ci config path' do
expect(subject[:merge_request_add_ci_config_path]).to be_nil
end
end
context 'when there are no commits' do
before do
allow(resource).to receive(:commits_count).and_return(0)
end
it 'returns a blank ci config path' do
expect(subject[:merge_request_add_ci_config_path]).to be_nil
end
end
context 'when ci_config_path is customized' do
it 'has no path if ci_config_path is not set to our default setting' do
project.ci_config_path = 'not_default'
expect(subject[:merge_request_add_ci_config_path]).to be_nil
end
it 'has a path if ci_config_path unset' do
expect(subject[:merge_request_add_ci_config_path]).not_to be_nil
end
it 'has a path if ci_config_path is an empty string' do
project.ci_config_path = ''
expect(subject[:merge_request_add_ci_config_path]).not_to be_nil
end
it 'has a path if ci_config_path is set to our default file' do
project.ci_config_path = Gitlab::FileDetector::PATTERNS[:gitlab_ci]
expect(subject[:merge_request_add_ci_config_path]).not_to be_nil
end
end
end
context 'when user does not have permissions' do
let(:role) { :reporter }
it 'has add ci config path' do
expect(subject[:merge_request_add_ci_config_path]).to be_nil
end
end
end
end
it 'has human access' do
project.add_maintainer(user)
expect(subject[:human_access])
.to eq('Maintainer')
end
describe 'when source project is deleted' do describe 'when source project is deleted' do
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
let(:forked_project) { fork_project(project) } let(:forked_project) { fork_project(project) }
......
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