Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
gitlab-ce
Commits
3ade8260
Commit
3ade8260
authored
Jun 14, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add specs for models and services
parent
e8f09f02
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
139 additions
and
28 deletions
+139
-28
app/models/deployment.rb
app/models/deployment.rb
+2
-0
app/models/environment.rb
app/models/environment.rb
+4
-0
app/services/create_deployment_service.rb
app/services/create_deployment_service.rb
+16
-24
db/migrate/20160610204157_add_deployments.rb
db/migrate/20160610204157_add_deployments.rb
+2
-2
db/schema.rb
db/schema.rb
+2
-2
doc/permissions/permissions.md
doc/permissions/permissions.md
+2
-0
spec/factories/deployments.rb
spec/factories/deployments.rb
+12
-0
spec/factories/environments.rb
spec/factories/environments.rb
+7
-0
spec/models/deployment_spec.rb
spec/models/deployment_spec.rb
+17
-0
spec/models/environment_spec.rb
spec/models/environment_spec.rb
+14
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+2
-0
spec/services/create_deployment_service_spec.rb
spec/services/create_deployment_service_spec.rb
+59
-0
No files found.
app/models/deployment.rb
View file @
3ade8260
...
@@ -8,6 +8,8 @@ class Deployment < ActiveRecord::Base
...
@@ -8,6 +8,8 @@ class Deployment < ActiveRecord::Base
validates_presence_of
:sha
validates_presence_of
:sha
validates_presence_of
:ref
validates_presence_of
:ref
validates_associated
:project
validates_associated
:environment
delegate
:name
,
to: :environment
,
prefix:
true
delegate
:name
,
to: :environment
,
prefix:
true
...
...
app/models/environment.rb
View file @
3ade8260
...
@@ -9,6 +9,10 @@ class Environment < ActiveRecord::Base
...
@@ -9,6 +9,10 @@ class Environment < ActiveRecord::Base
format:
{
with:
Gitlab
::
Regex
.
environment_name_regex
,
format:
{
with:
Gitlab
::
Regex
.
environment_name_regex
,
message:
Gitlab
::
Regex
.
environment_name_regex_message
}
message:
Gitlab
::
Regex
.
environment_name_regex_message
}
validates_uniqueness_of
:name
,
scope: :project_id
validates_associated
:project
def
last_deployment
def
last_deployment
deployments
.
last
deployments
.
last
end
end
...
...
app/services/create_deployment_service.rb
View file @
3ade8260
require_relative
'base_service'
require_relative
'base_service'
class
CreateDeploymentService
<
BaseService
class
CreateDeploymentService
<
BaseService
def
execute
(
deployable
)
def
execute
(
deployable
=
nil
)
environment
=
find_environment
(
params
[
:environment
])
environment
=
create_or_find_environment
(
params
[
:environment
])
return
error
(
'no environment'
)
unless
environmnet
deployment
=
create_deployment
(
environment
,
deployable
)
project
.
deployments
.
create
(
if
deployment
.
persisted?
environment:
environment
,
success
(
deployment
)
ref:
params
[
:ref
],
else
tag:
params
[
:tag
],
error
(
deployment
.
errors
)
sha:
params
[
:sha
],
end
user:
current_user
,
deployable:
deployable
,
)
end
end
private
private
def
find_environment
(
environment
)
def
create_or_
find_environment
(
environment
)
project
.
environments
.
find_by
(
name:
environment
)
find_environment
(
environment
)
||
create_environment
(
environment
)
end
end
def
create_deployment
(
environment
,
deployable
)
def
create_environment
(
environment
)
environment
.
deployments
.
create
(
project
.
environments
.
create
(
name:
environment
)
project:
project
,
ref:
build
.
ref
,
tag:
build
.
tag
,
sha:
build
.
sha
,
user:
current_user
,
deployable:
deployable
,
)
end
end
def
success
(
deployment
)
def
find_environment
(
environment
)
out
=
super
()
project
.
environments
.
find_by
(
name:
environment
)
out
[
:deployment
]
=
deployment
out
end
end
end
end
db/migrate/20160610204157_add_deployments.rb
View file @
3ade8260
...
@@ -13,8 +13,8 @@ class AddDeployments < ActiveRecord::Migration
...
@@ -13,8 +13,8 @@ class AddDeployments < ActiveRecord::Migration
t
.
boolean
:tag
t
.
boolean
:tag
t
.
string
:sha
t
.
string
:sha
t
.
integer
:user_id
t
.
integer
:user_id
t
.
integer
:deployable_id
,
null:
false
t
.
integer
:deployable_id
t
.
string
:deployable_type
,
null:
false
t
.
string
:deployable_type
t
.
datetime
:created_at
t
.
datetime
:created_at
t
.
datetime
:updated_at
t
.
datetime
:updated_at
end
end
...
...
db/schema.rb
View file @
3ade8260
...
@@ -390,8 +390,8 @@ ActiveRecord::Schema.define(version: 20160610301627) do
...
@@ -390,8 +390,8 @@ ActiveRecord::Schema.define(version: 20160610301627) do
t
.
boolean
"tag"
t
.
boolean
"tag"
t
.
string
"sha"
t
.
string
"sha"
t
.
integer
"user_id"
t
.
integer
"user_id"
t
.
integer
"deployable_id"
,
null:
false
t
.
integer
"deployable_id"
t
.
string
"deployable_type"
,
null:
false
t
.
string
"deployable_type"
t
.
datetime
"created_at"
t
.
datetime
"created_at"
t
.
datetime
"updated_at"
t
.
datetime
"updated_at"
end
end
...
...
doc/permissions/permissions.md
View file @
3ade8260
...
@@ -28,6 +28,7 @@ documentation](../workflow/add-user/add-user.md).
...
@@ -28,6 +28,7 @@ documentation](../workflow/add-user/add-user.md).
| Manage labels | | ✓ | ✓ | ✓ | ✓ |
| Manage labels | | ✓ | ✓ | ✓ | ✓ |
| See a commit status | | ✓ | ✓ | ✓ | ✓ |
| See a commit status | | ✓ | ✓ | ✓ | ✓ |
| See a container registry | | ✓ | ✓ | ✓ | ✓ |
| See a container registry | | ✓ | ✓ | ✓ | ✓ |
| See a environments | | ✓ | ✓ | ✓ | ✓ |
| Manage merge requests | | | ✓ | ✓ | ✓ |
| Manage merge requests | | | ✓ | ✓ | ✓ |
| Create new merge request | | | ✓ | ✓ | ✓ |
| Create new merge request | | | ✓ | ✓ | ✓ |
| Create new branches | | | ✓ | ✓ | ✓ |
| Create new branches | | | ✓ | ✓ | ✓ |
...
@@ -40,6 +41,7 @@ documentation](../workflow/add-user/add-user.md).
...
@@ -40,6 +41,7 @@ documentation](../workflow/add-user/add-user.md).
| Create or update commit status | | | ✓ | ✓ | ✓ |
| Create or update commit status | | | ✓ | ✓ | ✓ |
| Update a container registry | | | ✓ | ✓ | ✓ |
| Update a container registry | | | ✓ | ✓ | ✓ |
| Remove a container registry image | | | ✓ | ✓ | ✓ |
| Remove a container registry image | | | ✓ | ✓ | ✓ |
| Manage environments | | | ✓ | ✓ | ✓ |
| Create new milestones | | | | ✓ | ✓ |
| Create new milestones | | | | ✓ | ✓ |
| Add new team members | | | | ✓ | ✓ |
| Add new team members | | | | ✓ | ✓ |
| Push to protected branches | | | | ✓ | ✓ |
| Push to protected branches | | | | ✓ | ✓ |
...
...
spec/factories/deployments.rb
0 → 100644
View file @
3ade8260
FactoryGirl
.
define
do
factory
:deployment
,
class:
Deployment
do
sha
'97de212e80737a608d939f648d959671fb0a0142'
ref
'master'
environment
factory: :environment
after
(
:build
)
do
|
deployment
,
evaluator
|
deployment
.
project
=
deployment
.
environment
.
project
end
end
end
spec/factories/environments.rb
0 → 100644
View file @
3ade8260
FactoryGirl
.
define
do
factory
:environment
,
class:
Environment
do
sequence
(
:name
)
{
|
n
|
"environment
#{
n
}
"
}
project
factory: :empty_project
end
end
spec/models/deployment_spec.rb
0 → 100644
View file @
3ade8260
require
'spec_helper'
describe
Deployment
,
models:
true
do
subject
{
build
(
:deployment
)
}
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
belong_to
(
:environment
)
}
it
{
is_expected
.
to
belong_to
(
:user
)
}
it
{
is_expected
.
to
belong_to
(
:deployable
)
}
it
{
is_expected
.
to
delegate_method
(
:name
).
to
(
:environment
).
with_prefix
}
it
{
is_expected
.
to
delegate_method
(
:commit
).
to
(
:project
)
}
it
{
is_expected
.
to
delegate_method
(
:commit_title
).
to
(
:commit
).
as
(
:try
)
}
it
{
is_expected
.
to
validate_presence_of
(
:ref
)
}
it
{
is_expected
.
to
validate_presence_of
(
:sha
)
}
end
spec/models/environment_spec.rb
0 → 100644
View file @
3ade8260
require
'spec_helper'
describe
Environment
,
models:
true
do
let
(
:environment
)
{
create
(
:environment
)
}
it
{
is_expected
.
to
belong_to
(
:project
)
}
it
{
is_expected
.
to
have_many
(
:deployments
)
}
it
{
is_expected
.
to
delegate_method
(
:last_deployment
).
to
(
:deployments
).
as
(
:last
)
}
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
)
}
end
spec/models/project_spec.rb
View file @
3ade8260
...
@@ -28,6 +28,8 @@ describe Project, models: true do
...
@@ -28,6 +28,8 @@ describe Project, models: true do
it
{
is_expected
.
to
have_many
(
:runners
)
}
it
{
is_expected
.
to
have_many
(
:runners
)
}
it
{
is_expected
.
to
have_many
(
:variables
)
}
it
{
is_expected
.
to
have_many
(
:variables
)
}
it
{
is_expected
.
to
have_many
(
:triggers
)
}
it
{
is_expected
.
to
have_many
(
:triggers
)
}
it
{
is_expected
.
to
have_many
(
:environments
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:deployments
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:todos
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_many
(
:todos
).
dependent
(
:destroy
)
}
end
end
...
...
spec/services/create_deployment_service_spec.rb
0 → 100644
View file @
3ade8260
require
'spec_helper'
describe
CreateDeploymentService
,
services:
true
do
let
(
:build
)
{
create
(
:ci_build
)
}
let
(
:project
)
{
build
.
project
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:service
)
{
described_class
.
new
(
project
,
user
,
params
)
}
describe
'#execute'
do
let
(
:params
)
do
{
environment:
'production'
,
ref:
'master'
,
sha:
build
.
sha
,
}
end
subject
{
service
.
execute
}
context
'when no environments exist'
do
it
'does create a new environment'
do
expect
{
subject
}.
to
change
{
Environment
.
count
}.
by
(
1
)
end
it
'does create a deployment'
do
expect
(
subject
).
to
be_persisted
end
end
context
'when environment exist'
do
before
{
create
(
:environment
,
project:
project
,
name:
'production'
)
}
it
'does not create a new environment'
do
expect
{
subject
}.
not_to
change
{
Environment
.
count
}
end
it
'does create a deployment'
do
expect
(
subject
).
to
be_persisted
end
end
context
'for environment with invalid name'
do
let
(
:params
)
do
{
environment:
'name with spaces'
,
ref:
'master'
,
sha:
build
.
sha
,
}
end
it
'does not create a new environment'
do
expect
{
subject
}.
not_to
change
{
Environment
.
count
}
end
it
'does not create a deployment'
do
expect
(
subject
).
not_to
be_persisted
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment