Commit 88c21102 authored by Etienne Baqué's avatar Etienne Baqué

Introduced ReviewAppSetup entity

Used new serializer in Environments controller.
Created review app snippet.
Refactored project logic into a new module.
parent 21aabf98
# frozen_string_literal: true
module ReviewAppSetup
include Gitlab::Utils::StrongMemoize
include ChecksCollaboration
def can_setup_review_app?
strong_memoize(:can_setup_review_app?) do
cicd_missing? || (can_cluster_be_created? && cluster_missing?)
end
end
def cicd_missing?
current_user && can_current_user_push_code? && project.repository.gitlab_ci_yml.blank? && !project.auto_devops_enabled?
end
def can_cluster_be_created?
current_user && can?(current_user, :create_cluster, project)
end
def can_current_user_push_code?
strong_memoize(:can_current_user_push_code) do
if project.empty_repo?
can?(current_user, :push_code, project)
else
can_current_user_push_to_branch?(project.default_branch)
end
end
end
def cluster_missing?
strong_memoize(:cluster_missing?) do
project.clusters.blank?
end
end
def can_current_user_push_to_branch?(branch)
user_access(project).can_push_to_branch?(branch)
end
end
...@@ -32,6 +32,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -32,6 +32,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
render json: { render json: {
environments: serialize_environments(request, response, params[:nested]), environments: serialize_environments(request, response, params[:nested]),
review_app: serialize_review_app,
available_count: project.environments.available.count, available_count: project.environments.available.count,
stopped_count: project.environments.stopped.count stopped_count: project.environments.stopped.count
} }
...@@ -243,6 +244,10 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -243,6 +244,10 @@ class Projects::EnvironmentsController < Projects::ApplicationController
.represent(@environments) .represent(@environments)
end end
def serialize_review_app
ReviewAppSetupSerializer.new(current_user: @current_user).represent(@project)
end
def authorize_stop_environment! def authorize_stop_environment!
access_denied! unless can?(current_user, :stop_environment, environment) access_denied! unless can?(current_user, :stop_environment, environment)
end end
......
...@@ -8,7 +8,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -8,7 +8,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
include TreeHelper include TreeHelper
include IconsHelper include IconsHelper
include ChecksCollaboration include ChecksCollaboration
include Gitlab::Utils::StrongMemoize include ReviewAppSetup
presents :project presents :project
...@@ -124,20 +124,6 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -124,20 +124,6 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
license&.nickname || license&.name || 'LICENSE' license&.nickname || license&.name || 'LICENSE'
end end
def can_current_user_push_code?
strong_memoize(:can_current_user_push_code) do
if empty_repo?
can?(current_user, :push_code, project)
else
can_current_user_push_to_branch?(default_branch)
end
end
end
def can_current_user_push_to_branch?(branch)
user_access(project).can_push_to_branch?(branch)
end
def can_current_user_push_to_default_branch? def can_current_user_push_to_default_branch?
can_current_user_push_to_branch?(default_branch) can_current_user_push_to_branch?(default_branch)
end end
...@@ -293,7 +279,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -293,7 +279,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end end
def gitlab_ci_anchor_data def gitlab_ci_anchor_data
if can_cicd_be_set_up? if cicd_missing?
AnchorData.new(false, AnchorData.new(false,
statistic_icon + _('Set up CI/CD'), statistic_icon + _('Set up CI/CD'),
add_ci_yml_path) add_ci_yml_path)
...@@ -305,18 +291,6 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated ...@@ -305,18 +291,6 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end end
end end
def can_setup_review_app?
can_cicd_be_set_up? || can_cluster_be_created?
end
def can_cicd_be_set_up?
current_user && can_current_user_push_code? && repository.gitlab_ci_yml.blank? && !auto_devops_enabled?
end
def can_cluster_be_created?
current_user && can?(current_user, :create_cluster, project)
end
def topics_to_show def topics_to_show
project.tag_list.take(MAX_TOPICS_TO_SHOW) # rubocop: disable CodeReuse/ActiveRecord project.tag_list.take(MAX_TOPICS_TO_SHOW) # rubocop: disable CodeReuse/ActiveRecord
end end
......
# frozen_string_literal: true
class ReviewAppSetupEntity < Grape::Entity
include RequestAwareEntity
include ReviewAppSetup
expose :can_setup_review_app?
expose :cluster_missing?, if: -> (_, _) { can_setup_review_app? } do |project|
cluster_missing?
end
expose :review_snippet, if: -> (_, _) { can_setup_review_app? } do |_|
YAML.safe_load(File.read(Rails.root.join('lib', 'gitlab', 'ci', 'snippets', 'review_app_default.yml'))).inspect
end
private
def current_user
request.current_user
end
def project
object
end
end
# frozen_string_literal: true
class ReviewAppSetupSerializer < BaseSerializer
entity ReviewAppSetupEntity
end
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#environments-list-view{ data: { environments_data: environments_list_data, #environments-list-view{ data: { environments_data: environments_list_data,
"can-read-environment" => can?(current_user, :read_environment, @project).to_s, "can-read-environment" => can?(current_user, :read_environment, @project).to_s,
"can-create-environment" => can?(current_user, :create_environment, @project).to_s, "can-create-environment" => can?(current_user, :create_environment, @project).to_s,
"can-setup-review-app" => @project.can_setup_review_app?,
"new-environment-path" => new_project_environment_path(@project), "new-environment-path" => new_project_environment_path(@project),
"help-page-path" => help_page_path("ci/environments"), "help-page-path" => help_page_path("ci/environments"),
"deploy-boards-help-path" => help_page_path("user/project/deploy_boards", anchor: "enabling-deploy-boards") } } "deploy-boards-help-path" => help_page_path("user/project/deploy_boards", anchor: "enabling-deploy-boards") } }
deploy_review:
stage: deploy
script:
- echo "Deploy a review app"
environment:
name: review/$CI_COMMIT_REF_NAME
url: https://$CI_ENVIRONMENT_SLUG.example.com
only:
- branches
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