gitlab-org--gitlab-foss/app/controllers/projects/environments_controller.rb

82 lines
2.0 KiB
Ruby
Raw Normal View History

2016-04-29 13:14:38 +00:00
class Projects::EnvironmentsController < Projects::ApplicationController
layout 'project'
2016-06-10 21:36:54 +00:00
before_action :authorize_read_environment!
2016-10-17 20:01:56 +00:00
before_action :authorize_create_environment!, only: [:new, :create]
2016-10-17 19:06:10 +00:00
before_action :authorize_create_deployment!, only: [:stop]
before_action :authorize_update_environment!, only: [:edit, :update]
before_action :environment, only: [:show, :edit, :update, :stop]
2016-04-29 13:14:38 +00:00
def index
2016-10-04 13:11:04 +00:00
@scope = params[:scope]
2016-10-24 15:10:58 +00:00
@environments = project.environments
respond_to do |format|
format.html
format.json do
2016-10-19 16:03:48 +00:00
render json: serialize_as_json(@environments)
end
end
2016-04-29 13:14:38 +00:00
end
def show
2016-06-15 10:07:06 +00:00
@deployments = environment.deployments.order(id: :desc).page(params[:page])
2016-06-10 22:15:53 +00:00
end
def new
@environment = project.environments.new
end
def edit
end
2016-06-10 22:15:53 +00:00
def create
@environment = project.environments.create(environment_params)
2016-06-14 16:34:48 +00:00
if @environment.persisted?
redirect_to namespace_project_environment_path(project.namespace, project, @environment)
else
render :new
end
end
def update
if @environment.update(environment_params)
redirect_to namespace_project_environment_path(project.namespace, project, @environment)
else
render :edit
2016-06-10 22:15:53 +00:00
end
end
2016-10-17 09:33:24 +00:00
def stop
2016-10-17 14:13:19 +00:00
return render_404 unless @environment.stoppable?
2016-10-18 10:02:50 +00:00
new_action = @environment.stop!(current_user)
2016-10-17 14:13:19 +00:00
redirect_to polymorphic_path([project.namespace.becomes(Namespace), project, new_action])
2016-04-29 13:14:38 +00:00
end
2016-06-10 21:36:54 +00:00
private
def environment_params
params.require(:environment).permit(:name, :external_url)
2016-06-10 22:15:53 +00:00
end
2016-06-10 21:36:54 +00:00
def environment
2016-06-15 10:07:06 +00:00
@environment ||= project.environments.find(params[:id])
2016-04-29 13:14:38 +00:00
end
2016-10-19 16:03:48 +00:00
def serialize_as_json(resource)
resource.as_json(
include: {
2016-10-20 09:06:03 +00:00
last_deployment: {
include: {
2016-10-24 11:42:01 +00:00
user: { only: [:id, :name, :username], methods: [:avatar_url] },
deployable: { only: [:id, :name, :ref, :tag] }
2016-10-20 19:37:54 +00:00
},
2016-10-24 11:42:01 +00:00
methods: [:short_sha, :commit_title, :commit]
2016-10-21 14:40:16 +00:00
},
2016-10-24 11:42:01 +00:00
project: { methods: [:namespace] }
2016-10-19 16:03:48 +00:00
}
)
end
2016-04-29 13:14:38 +00:00
end