Commit be9aa7f1 authored by Z.J. van de Weg's avatar Z.J. van de Weg

Add an URL field to Environments

This MR adds a string (thus max 255 chars) field to the enviroments
table to expose it later in other features.
parent 242f8377
......@@ -9,6 +9,7 @@ v 8.11.0 (unreleased)
- Add support for using RequestStore within Sidekiq tasks via SIDEKIQ_REQUEST_STORE env variable
- Optimize maximum user access level lookup in loading of notes
- Add "No one can push" as an option for protected branches. !5081
- Environments have an url to link to
- Limit git rev-list output count to one in forced push check
- Clean up unused routes (Josef Strzibny)
- Add green outline to New Branch button. !5447 (winniehell)
......
......@@ -2,8 +2,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController
layout 'project'
before_action :authorize_read_environment!
before_action :authorize_create_environment!, only: [:new, :create]
before_action :authorize_update_environment!, only: [:destroy]
before_action :environment, only: [:show, :destroy]
before_action :authorize_update_environment!, only: [:edit, :destroy]
before_action :environment, only: [:show, :edit, :update, :destroy]
def index
@environments = project.environments
......@@ -17,13 +17,24 @@ class Projects::EnvironmentsController < Projects::ApplicationController
@environment = project.environments.new
end
def edit
end
def create
@environment = project.environments.create(create_params)
@environment = project.environments.create(environment_params)
if @environment.persisted?
redirect_to namespace_project_environment_path(project.namespace, project, @environment)
else
render 'new'
render :new
end
end
def update
if @environment.update(environment_params)
redirect_to namespace_project_environment_path(project.namespace, project, @environment)
else
render :edit
end
end
......@@ -39,8 +50,8 @@ class Projects::EnvironmentsController < Projects::ApplicationController
private
def create_params
params.require(:environment).permit(:name)
def environment_params
params.require(:environment).permit(:name, :external_url)
end
def environment
......
......@@ -10,6 +10,10 @@ class Environment < ActiveRecord::Base
format: { with: Gitlab::Regex.environment_name_regex,
message: Gitlab::Regex.environment_name_regex_message }
validates :external_url,
uniqueness: { scope: :project_id },
length: { maximum: 255 }
def last_deployment
deployments.last
end
......
= form_for @environment, url: namespace_project_environments_path(@project.namespace, @project), html: { class: 'col-lg-9' } do |f|
= form_errors(@environment)
.form-group
= f.label :name, 'Name', class: 'label-light'
= f.text_field :name, required: true, class: 'form-control'
= f.submit 'Create environment', class: 'btn btn-create'
= link_to 'Cancel', namespace_project_environments_path(@project.namespace, @project), class: 'btn btn-cancel'
.row.prepend-top-default.append-bottom-default
.col-lg-3
%h4.prepend-top-0
Environments
%p
Environments allow you to track deployments of your application
= succeed "." do
= link_to "Read more about environments", help_page_path("ci/environments")
= form_for [@project.namespace.becomes(Namespace), @project, @environment], html: { class: 'col-lg-9' } do |f|
= form_errors(@environment)
.form-group
= f.label :name, 'Name', class: 'label-light'
= f.text_field :name, required: true, class: 'form-control'
.form-group
= f.label :external_url, 'External URL', class: 'label-light'
= f.url_field :external_url, class: 'form-control'
.form-actions
= f.submit 'Save', class: 'btn btn-save'
= link_to 'Cancel', namespace_project_environments_path(@project.namespace, @project), class: 'btn btn-cancel'
- page_title "Edit", @environment.name, "Environments"
%h3.page-title
Edit environment
%hr
= render 'form'
- page_title 'New Environment'
.row.prepend-top-default.append-bottom-default
.col-lg-3
%h4.prepend-top-0
New Environment
%p
Environments allow you to track deployments of your application
= succeed "." do
= link_to "Read more about environments", help_page_path("ci/environments")
= render 'form'
%h3.page-title
New environment
%hr
= render 'form'
......@@ -6,10 +6,10 @@
.top-area
.col-md-9
%h3.page-title= @environment.name.capitalize
.col-md-3
.nav-controls
- if can?(current_user, :update_environment, @environment)
= link_to 'Edit', edit_namespace_project_environment_path(@project.namespace, @project, @environment), class: 'btn'
= 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
- if @deployments.blank?
......
......@@ -741,7 +741,7 @@ Rails.application.routes.draw do
end
end
resources :environments, only: [:index, :show, :new, :create, :destroy]
resources :environments, constraints: { id: /\d+/ }
resources :builds, only: [:index, :show], constraints: { id: /\d+/ } do
collection do
......
# See http://doc.gitlab.com/ce/development/migration_style_guide.html
# for more information on how to write migrations for GitLab.
class AddExternalUrlToEnviroments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
add_column(:environments, :external_url, :string)
end
end
......@@ -427,9 +427,10 @@ ActiveRecord::Schema.define(version: 20160722221922) do
create_table "environments", force: :cascade do |t|
t.integer "project_id"
t.string "name", null: false
t.string "name", null: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "external_url"
end
add_index "environments", ["project_id", "name"], name: "index_environments_on_project_id_and_name", using: :btree
......
require 'spec_helper'
describe Projects::EnvironmentsController do
let(:environment) { create(:environment) }
let(:project) { environment.project }
let(:user) { create(:user) }
before do
project.team << [user, :master]
sign_in(user)
end
render_views
describe 'GET show' do
context 'with valid id' do
it 'responds with a status code 200' do
get :show, namespace_id: project.namespace, project_id: project, id: environment.id
expect(response).to be_ok
end
end
context 'with invalid id' do
it 'responds with a status code 404' do
get :show, namespace_id: project.namespace, project_id: project, id: 12345
expect(response).to be_not_found
end
end
end
describe 'GET edit' do
it 'responds with a status code 200' do
get :edit, namespace_id: project.namespace, project_id: project, id: environment.id
expect(response).to be_ok
end
end
describe 'PATCH #update' do
it 'responds with a 302' do
patch :update, namespace_id: project.namespace, project_id:
project, id: environment.id, environment: { external_url: 'https://git.gitlab.com' }
expect(response).to have_http_status(302)
end
end
end
......@@ -3,5 +3,6 @@ FactoryGirl.define do
sequence(:name) { |n| "environment#{n}" }
project factory: :empty_project
sequence(:external_url) { |n| "https://env#{n}.example.gitlab.com" }
end
end
......@@ -140,7 +140,7 @@ feature 'Environments', feature: true do
context 'for valid name' do
before do
fill_in('Name', with: 'production')
click_on 'Create environment'
click_on 'Save'
end
scenario 'does create a new pipeline' do
......@@ -151,7 +151,7 @@ feature 'Environments', feature: true do
context 'for invalid name' do
before do
fill_in('Name', with: 'name with spaces')
click_on 'Create environment'
click_on 'Save'
end
scenario 'does show errors' do
......
......@@ -11,4 +11,14 @@ describe Environment, models: true do
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:name).scoped_to(:project_id) }
it { is_expected.to validate_length_of(:name).is_within(0..255) }
it { is_expected.to validate_length_of(:external_url).is_within(0..255) }
# To circumvent a not null violation of the name column:
# https://github.com/thoughtbot/shoulda-matchers/issues/336
it 'validates uniqueness of :external_url' do
create(:environment)
is_expected.to validate_uniqueness_of(:external_url).scoped_to(:project_id)
end
end
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