Commit 6cdbb27e authored by Kamil Trzcinski's avatar Kamil Trzcinski

Refactor code to use available and stopped statuses and refactor views to use separate renders

parent 7aea16b6
......@@ -10,8 +10,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController
@all_environments = project.environments
@environments =
case @scope
when 'closed' then @all_environments.closed
else @all_environments.opened
when 'stopped' then @all_environments.stopped
else @all_environments.available
end
end
......@@ -45,7 +45,9 @@ class Projects::EnvironmentsController < Projects::ApplicationController
end
def stop
action = @environment.stop_action
new_action = action.active? ? action : action.play(current_user)
redirect_to [project.namespace.become(Namespace), project, new_action]
end
def destroy
......
......@@ -416,7 +416,7 @@ class Projects::MergeRequestsController < Projects::ApplicationController
id: environment.id,
name: environment.name,
url: namespace_project_environment_path(project.namespace, project, environment),
stop_url: (stop_namespace_project_environment_path(project.namespace, project, environment) if environment.closeable?),
stop_url: (stop_namespace_project_environment_path(project.namespace, project, environment) if environment.stoppable?),
external_url: environment.external_url,
external_url_formatted: environment.formatted_external_url,
deployed_at: deployment.try(:created_at),
......
......@@ -106,14 +106,6 @@ module Ci
project.builds_enabled? && commands.present? && manual? && skipped?
end
def close_environment?
options.fetch(:environment, {}).fetch(:close, false)
end
def closes_environment?(name)
environment == name && close_environment?
end
def play(current_user = nil)
# Try to queue a current build
if self.enqueue
......
......@@ -11,6 +11,8 @@ class Deployment < ActiveRecord::Base
delegate :name, to: :environment, prefix: true
store :properties, accessors: [:on_stop]
after_save :create_ref
def commit
......@@ -34,7 +36,7 @@ class Deployment < ActiveRecord::Base
end
def manual_actions
deployable.try(:other_actions)
@manual_actions ||= deployable.try(:other_actions)
end
def includes_commit?(commit)
......@@ -84,17 +86,14 @@ class Deployment < ActiveRecord::Base
take
end
def close_action
def stop_action
return nil unless manual_actions
@close_action ||=
manual_actions.find do |manual_action|
manual_action.try(:closes_environment?, deployable.environment)
end
@stop_action ||= manual_actions.find_by(name: on_stop)
end
def closeable?
close_action.present?
def stoppable?
on_stop.present? && stop_action.present?
end
def formatted_deployment_time
......
......@@ -19,22 +19,22 @@ class Environment < ActiveRecord::Base
allow_nil: true,
addressable_url: true
delegate :closeable?, :close_action, to: :last_deployment, allow_nil: true
delegate :stoppable?, :stop_action, to: :last_deployment, allow_nil: true
scope :opened, -> { where(state: [:opened]) }
scope :closed, -> { where(state: [:closed]) }
scope :available, -> { where(state: [:available]) }
scope :stopped, -> { where(state: [:stopped]) }
state_machine :state, initial: :opened do
event :close do
transition opened: :closed
state_machine :state, initial: :available do
event :start do
transition stopped: :available
end
event :reopen do
transition closed: :opened
event :stop do
transition available: :stopped
end
state :opened
state :closed
state :available
state :stopped
end
def last_deployment
......
......@@ -1292,7 +1292,7 @@ class Project < ActiveRecord::Base
environment_ids.where(ref: ref)
end
environments.opened.where(id: environment_ids).select do |environment|
environments.available.where(id: environment_ids).select do |environment|
environment.includes_commit?(commit)
end
end
......
......@@ -8,12 +8,12 @@ class CreateDeploymentService < BaseService
@deployable = deployable
@environment = prepare_environment
if close?
@environment.close
if stop?
@environment.stop
return
end
@environment.reopen
@environment.start
deploy.tap do |deployment|
deployment.update_merge_request_metrics!
......@@ -61,10 +61,6 @@ class CreateDeploymentService < BaseService
options[:url]
end
def close?
options[:close]
end
def options
params[:options] || {}
end
......@@ -72,4 +68,8 @@ class CreateDeploymentService < BaseService
def variables
params[:variables] || []
end
def stop?
params[:options].fetch(:stop, false)
end
end
- if can?(current_user, :create_deployment, deployment) && deployment.deployable
.pull-right
- external_url = deployment.environment.external_url
- if external_url
= link_to external_url, target: '_blank', class: 'btn external-url' do
= icon('external-link')
- if can?(current_user, :create_deployment, deployment)
- actions = deployment.manual_actions
- if actions.present?
.inline
......@@ -20,14 +13,3 @@
= custom_icon('icon_play')
%span= action.name.humanize
- if local_assigns.fetch(:allow_close, false) && deployment.closeable?
.inline
= link_to [:play, @project.namespace.becomes(Namespace), @project, deployment.close_action], method: :post, class: 'btn close-env-link', rel: 'nofollow', data: { confirm: 'Are you sure you want to close this environment?' } do
= icon('stop', class: 'close-env-icon')
- if local_assigns.fetch(:allow_rollback, false)
= link_to [:retry, @project.namespace.becomes(Namespace), @project, deployment.deployable], method: :post, class: 'btn' do
- if deployment.last?
Re-deploy
- else
Rollback
......@@ -17,4 +17,6 @@
#{time_ago_with_tooltip(deployment.created_at)}
%td.hidden-xs
= render 'projects/deployments/actions', deployment: deployment, allow_rollback: true
.pull-right
= render 'projects/deployments/actions', deployment: deployment
= render 'projects/deployments/rollback', deployment: deployment
- if can?(current_user, :create_deployment, deployment) && deployment.deployable
= link_to [:retry, @project.namespace.becomes(Namespace), @project, deployment.deployable], method: :post, class: 'btn btn-build' do
- if deployment.last?
Re-deploy
- else
Rollback
......@@ -28,4 +28,8 @@
#{time_ago_with_tooltip(last_deployment.created_at)}
%td.hidden-xs
= render 'projects/deployments/actions', deployment: last_deployment, allow_close: environment.opened?
.pull-right
= render 'projects/environments/external_url', environment: environment
= render 'projects/deployments/actions', deployment: last_deployment
= render 'projects/environments/stop', environment: environment
= render 'projects/deployments/rollback', deployment: last_deployment
- if environment.external_url
= link_to environment.external_url, target: '_blank', class: 'btn external-url' do
= icon('external-link')
- if environment.available? && environment.stoppable?
.inline
= link_to stop_namespace_project_environment_path(@project.namespace, @project, environment), method: :post,
class: 'btn close-env-link', rel: 'nofollow', data: { confirm: 'Are you sure you want to close this environment?' } do
= icon('stop', class: 'close-env-icon')
......@@ -8,14 +8,14 @@
%li{class: ('active' if @scope.nil?)}
= link_to project_environments_path(@project) do
Available
%span.badge.js-avaibale-environments-count
= number_with_delimiter(@all_environments.opened.count)
%span.badge.js-available-environments-count
= number_with_delimiter(@all_environments.available.count)
%li{class: ('active' if @scope == 'closed')}
= link_to project_environments_path(@project, scope: :closed) do
%li{class: ('active' if @scope == 'stopped')}
= link_to project_environments_path(@project, scope: :stopped) do
Stopped
%span.badge.js-stopped-environments-count
= number_with_delimiter(@all_environments.closed.count)
= number_with_delimiter(@all_environments.stopped.count)
.nav-controls
- if can?(current_user, :create_environment, @project) && !@all_environments.blank?
......
......@@ -2,18 +2,20 @@
- page_title "Environments"
= render "projects/pipelines/head"
- last_deployment = @environment.last_deployment
%div{ class: container_class }
.top-area
.col-md-9
%h3.page-title= @environment.name.capitalize
.col-md-3
.nav-controls
- if can?(current_user, :read_environmnet, @environment)
= render 'projects/environments/external_url', environment: @environment
- if can?(current_user, :update_environment, @environment)
= link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn'
- if @environment.opened? && last_deployment.try(:close_action)
= link_to 'Close', [:play, @project.namespace.becomes(Namespace), @project, last_deployment.close_action], data: { confirm: 'Are you sure you want to close this environment?' }, class: 'btn btn-danger', method: :post
- if @environment.available? && @environment.stoppable?
= link_to 'Stop', stop_namespace_project_environment_path(@project.namespace, @project, @environment), data: { confirm: 'Are you sure you want to close this environment?' }, class: 'btn btn-danger', method: :post
= link_to 'Destroy', namespace_project_environment_path(@project.namespace, @project, @environment), data: { confirm: 'Are you sure you want to delete this environment?' }, class: 'btn btn-danger', method: :delete
.deployments-container
- if @deployments.blank?
......
......@@ -16,8 +16,8 @@ class Gitlab::Seeder::Pipelines
{ name: 'env:alpha', stage: 'deploy', environment: 'alpha', status: :pending },
{ name: 'env:beta', stage: 'deploy', environment: 'beta', status: :running },
{ name: 'env:gamma', stage: 'deploy', environment: 'gamma', status: :canceled },
{ name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success },
{ name: 'close staging', stage: 'deploy', environment: 'staging', when: 'manual', status: :skipped, options: { environment: { close: true } } },
{ name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success, options: { environment: { on_stop: 'stop staging' } } },
{ name: 'stop staging', stage: 'deploy', environment: 'staging', when: 'manual', status: :skipped },
{ name: 'production', stage: 'deploy', environment: 'production', when: 'manual', status: :skipped },
{ name: 'slack', stage: 'notify', when: 'manual', status: :created },
]
......
......@@ -6,7 +6,7 @@ class AddStateToEnvironment < ActiveRecord::Migration
DOWNTIME = false
def up
add_column_with_default(:environments, :state, :string, default: :opened)
add_column_with_default(:environments, :state, :string, default: :available)
end
def down
......
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddPropertiesToDeployment < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
# Set this constant to true if this migration requires downtime.
DOWNTIME = false
# When a migration requires downtime you **must** uncomment the following
# constant and define a short and easy to understand explanation as to why the
# migration requires downtime.
# DOWNTIME_REASON = ''
# When using the methods "add_concurrent_index" or "add_column_with_default"
# you must disable the use of transactions as these methods can not run in an
# existing transaction. When using "add_concurrent_index" make sure that this
# method is the _only_ method called in the migration, any other changes
# should go in a separate migration. This ensures that upon failure _only_ the
# index creation fails and can be retried or reverted easily.
#
# To disable transactions uncomment the following line and remove these
# comments:
# disable_ddl_transaction!
def change
add_column :deployments, :properties, :text
end
end
......@@ -398,22 +398,13 @@ ActiveRecord::Schema.define(version: 20161007133303) do
add_index "emails", ["user_id"], name: "index_emails_on_user_id", using: :btree
create_table "environments", force: :cascade do |t|
<<<<<<< HEAD
t.integer "project_id"
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "external_url"
t.string "environment_type"
t.string "state", default: "opened", null: false
=======
t.integer "project_id"
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "external_url"
t.string "environment_type"
>>>>>>> origin/master
t.string "state", default: "available", null: false
end
add_index "environments", ["project_id", "name"], name: "index_environments_on_project_id_and_name", using: :btree
......
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