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

This commit is contained in:
Kamil Trzcinski 2016-10-17 12:45:31 +02:00
parent 7aea16b646
commit 6cdbb27ec3
19 changed files with 111 additions and 94 deletions

View file

@ -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

View file

@ -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),

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,33 +1,15 @@
- 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')
- actions = deployment.manual_actions
- if actions.present?
.inline
.dropdown
%a.dropdown-new.btn.btn-default{type: 'button', 'data-toggle' => 'dropdown'}
= custom_icon('icon_play')
= icon('caret-down')
%ul.dropdown-menu.dropdown-menu-align-right
- actions.each do |action|
%li
= link_to [:play, @project.namespace.becomes(Namespace), @project, action], method: :post, rel: 'nofollow' do
= custom_icon('icon_play')
%span= action.name.humanize
- if can?(current_user, :create_deployment, deployment)
- actions = deployment.manual_actions
- if actions.present?
.inline
.dropdown
%a.dropdown-new.btn.btn-default{type: 'button', 'data-toggle' => 'dropdown'}
= custom_icon('icon_play')
= icon('caret-down')
%ul.dropdown-menu.dropdown-menu-align-right
- actions.each do |action|
%li
= link_to [:play, @project.namespace.becomes(Namespace), @project, action], method: :post, rel: 'nofollow' do
= 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

View file

@ -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

View file

@ -0,0 +1,6 @@
- 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

View file

@ -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

View file

@ -0,0 +1,3 @@
- if environment.external_url
= link_to environment.external_url, target: '_blank', class: 'btn external-url' do
= icon('external-link')

View file

@ -0,0 +1,5 @@
- 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')

View file

@ -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?

View file

@ -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?

View file

@ -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 },
]

View file

@ -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

View file

@ -0,0 +1,29 @@
# 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

View file

@ -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