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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
1c5b172a
Commit
1c5b172a
authored
Apr 16, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Write specs for this feature
parent
dc0d7f1a
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
188 additions
and
19 deletions
+188
-19
app/models/ci/commit.rb
app/models/ci/commit.rb
+4
-5
app/models/commit_status.rb
app/models/commit_status.rb
+11
-6
app/models/concerns/statuseable.rb
app/models/concerns/statuseable.rb
+1
-1
lib/api/commit_statuses.rb
lib/api/commit_statuses.rb
+13
-6
spec/models/ci/commit_spec.rb
spec/models/ci/commit_spec.rb
+71
-0
spec/models/commit_status_spec.rb
spec/models/commit_status_spec.rb
+74
-0
spec/models/project_spec.rb
spec/models/project_spec.rb
+14
-1
No files found.
app/models/ci/commit.rb
View file @
1c5b172a
...
@@ -26,7 +26,10 @@ module Ci
...
@@ -26,7 +26,10 @@ module Ci
has_many
:builds
,
class_name:
'Ci::Build'
has_many
:builds
,
class_name:
'Ci::Build'
has_many
:trigger_requests
,
dependent: :destroy
,
class_name:
'Ci::TriggerRequest'
has_many
:trigger_requests
,
dependent: :destroy
,
class_name:
'Ci::TriggerRequest'
delegate
:stages
,
to: :statuses
validates_presence_of
:sha
validates_presence_of
:sha
validates_presence_of
:status
validate
:valid_commit_sha
validate
:valid_commit_sha
# Invalidate object and save if when touched
# Invalidate object and save if when touched
...
@@ -40,10 +43,6 @@ module Ci
...
@@ -40,10 +43,6 @@ module Ci
CommitStatus
.
where
(
commit:
all
).
stages
CommitStatus
.
where
(
commit:
all
).
stages
end
end
def
stages
statuses
.
stages
end
def
project_id
def
project_id
project
.
id
project
.
id
end
end
...
@@ -82,7 +81,7 @@ module Ci
...
@@ -82,7 +81,7 @@ module Ci
def
retryable?
def
retryable?
builds
.
latest
.
any?
do
|
build
|
builds
.
latest
.
any?
do
|
build
|
build
.
failed?
||
build
.
retryable?
build
.
failed?
&&
build
.
retryable?
end
end
end
end
...
...
app/models/commit_status.rb
View file @
1c5b172a
...
@@ -93,7 +93,10 @@ class CommitStatus < ActiveRecord::Base
...
@@ -93,7 +93,10 @@ class CommitStatus < ActiveRecord::Base
end
end
def
self
.
stages_status
def
self
.
stages_status
Hash
[
group
(
:stage
).
pluck
(
:stage
,
self
.
status_sql
)]
all
.
stages
.
inject
({})
do
|
h
,
stage
|
h
[
stage
]
=
all
.
where
(
stage:
stage
).
status
h
end
end
end
def
ignored?
def
ignored?
...
@@ -101,11 +104,13 @@ class CommitStatus < ActiveRecord::Base
...
@@ -101,11 +104,13 @@ class CommitStatus < ActiveRecord::Base
end
end
def
duration
def
duration
duration
=
if
started_at
&&
finished_at
if
started_at
&&
finished_at
finished_at
-
started_at
finished_at
-
started_at
elsif
started_at
elsif
started_at
Time
.
now
-
started_at
Time
.
now
-
started_at
end
end
duration
.
to_i
end
end
def
stuck?
def
stuck?
...
...
app/models/concerns/statuseable.rb
View file @
1c5b172a
...
@@ -33,7 +33,7 @@ module Statuseable
...
@@ -33,7 +33,7 @@ module Statuseable
def
duration
def
duration
duration_array
=
all
.
map
(
&
:duration
).
compact
duration_array
=
all
.
map
(
&
:duration
).
compact
duration_array
.
reduce
(:
+
)
.
to_i
duration_array
.
reduce
(:
+
)
end
end
def
started_at
def
started_at
...
...
lib/api/commit_statuses.rb
View file @
1c5b172a
...
@@ -50,11 +50,18 @@ module API
...
@@ -50,11 +50,18 @@ module API
commit
=
@project
.
commit
(
params
[
:sha
])
commit
=
@project
.
commit
(
params
[
:sha
])
not_found!
'Commit'
unless
commit
not_found!
'Commit'
unless
commit
ref
=
params
[
:ref
]
||
# Since the CommitStatus is attached to Ci::Commit (in the future Pipeline)
begin
# We need to always have the pipeline object
# To have a valid pipeline object that can be attached to specific MR
# Other CI service needs to send `ref`
# If we don't receive it, we will attach the CommitStatus to
# the first found branch on that commit
ref
=
params
[
:ref
]
unless
ref
branches
=
@project
.
repository
.
branch_names_contains
(
commit
.
sha
)
branches
=
@project
.
repository
.
branch_names_contains
(
commit
.
sha
)
not_found!
'Reference
for commit'
if
branches
.
none?
not_found!
'References
for commit'
if
branches
.
none?
branches
.
first
ref
=
branches
.
first
end
end
ci_commit
=
@project
.
ensure_ci_commit
(
commit
.
sha
,
ref
)
ci_commit
=
@project
.
ensure_ci_commit
(
commit
.
sha
,
ref
)
...
...
spec/models/ci/commit_spec.rb
View file @
1c5b172a
...
@@ -27,6 +27,8 @@ describe Ci::Commit, models: true do
...
@@ -27,6 +27,8 @@ describe Ci::Commit, models: true do
it
{
is_expected
.
to
have_many
(
:trigger_requests
)
}
it
{
is_expected
.
to
have_many
(
:trigger_requests
)
}
it
{
is_expected
.
to
have_many
(
:builds
)
}
it
{
is_expected
.
to
have_many
(
:builds
)
}
it
{
is_expected
.
to
validate_presence_of
:sha
}
it
{
is_expected
.
to
validate_presence_of
:sha
}
it
{
is_expected
.
to
validate_presence_of
:status
}
it
{
is_expected
.
to
delegate_method
(
:stages
).
to
(
:statuses
)
}
it
{
is_expected
.
to
respond_to
:git_author_name
}
it
{
is_expected
.
to
respond_to
:git_author_name
}
it
{
is_expected
.
to
respond_to
:git_author_email
}
it
{
is_expected
.
to
respond_to
:git_author_email
}
...
@@ -297,4 +299,73 @@ describe Ci::Commit, models: true do
...
@@ -297,4 +299,73 @@ describe Ci::Commit, models: true do
expect
(
commit
.
coverage
).
to
be_nil
expect
(
commit
.
coverage
).
to
be_nil
end
end
end
end
describe
'#retryable?'
do
subject
{
commit
.
retryable?
}
context
'no failed builds'
do
before
do
FactoryGirl
.
create
:ci_build
,
name:
"rspec"
,
commit:
commit
,
status:
'success'
end
it
'be not retryable'
do
is_expected
.
to
be_falsey
end
end
context
'with failed builds'
do
before
do
FactoryGirl
.
create
:ci_build
,
name:
"rspec"
,
commit:
commit
,
status:
'running'
FactoryGirl
.
create
:ci_build
,
name:
"rubocop"
,
commit:
commit
,
status:
'failed'
end
it
'be retryable'
do
is_expected
.
to
be_truthy
end
end
end
describe
'#stages'
do
let
(
:commit2
)
{
FactoryGirl
.
create
:ci_commit
,
project:
project
}
subject
{
CommitStatus
.
where
(
commit:
[
commit
,
commit2
]).
stages
}
before
do
FactoryGirl
.
create
:ci_build
,
commit:
commit2
,
stage:
'test'
,
stage_idx:
1
FactoryGirl
.
create
:ci_build
,
commit:
commit
,
stage:
'build'
,
stage_idx:
0
end
it
'return all stages'
do
is_expected
.
to
eq
(
%w(build test)
)
end
end
describe
'#update_state'
do
it
'execute update_state after touching object'
do
expect
(
commit
).
to
receive
(
:update_state
).
and_return
(
true
)
commit
.
touch
end
context
'dependent objects'
do
let
(
:commit_status
)
{
build
:commit_status
,
commit:
commit
}
it
'execute update_state after saving dependent object'
do
expect
(
commit
).
to
receive
(
:update_state
).
and_return
(
true
)
commit_status
.
save
end
end
context
'update state'
do
let
(
:build
)
{
FactoryGirl
.
create
:ci_build
,
:success
,
commit:
commit
,
started_at:
Time
.
now
-
120
,
finished_at:
Time
.
now
-
60
}
before
do
build
end
[
:status
,
:started_at
,
:finished_at
,
:duration
].
each
do
|
param
|
it
"update
#{
param
}
"
do
expect
(
commit
.
send
(
param
)).
to
eq
(
build
.
send
(
param
))
end
end
end
end
end
end
spec/models/commit_status_spec.rb
View file @
1c5b172a
...
@@ -182,4 +182,78 @@ describe CommitStatus, models: true do
...
@@ -182,4 +182,78 @@ describe CommitStatus, models: true do
is_expected
.
to
eq
([
@commit1
,
@commit2
])
is_expected
.
to
eq
([
@commit1
,
@commit2
])
end
end
end
end
describe
'#before_sha'
do
subject
{
commit_status
.
before_sha
}
context
'when no before_sha is set for ci::commit'
do
before
{
commit
.
before_sha
=
nil
}
it
'return blank sha'
do
is_expected
.
to
eq
(
Gitlab
::
Git
::
BLANK_SHA
)
end
end
context
'for before_sha set for ci::commit'
do
let
(
:value
)
{
'1234'
}
before
{
commit
.
before_sha
=
value
}
it
'return the set value'
do
is_expected
.
to
eq
(
value
)
end
end
end
describe
'#stages'
do
before
do
FactoryGirl
.
create
:commit_status
,
commit:
commit
,
stage:
'build'
,
stage_idx:
0
,
status:
'success'
FactoryGirl
.
create
:commit_status
,
commit:
commit
,
stage:
'build'
,
stage_idx:
0
,
status:
'failed'
FactoryGirl
.
create
:commit_status
,
commit:
commit
,
stage:
'deploy'
,
stage_idx:
2
,
status:
'running'
FactoryGirl
.
create
:commit_status
,
commit:
commit
,
stage:
'test'
,
stage_idx:
1
,
status:
'success'
end
context
'stages list'
do
subject
{
CommitStatus
.
where
(
commit:
commit
).
stages
}
it
'return ordered list of stages'
do
is_expected
.
to
eq
(
%w(build test deploy)
)
end
end
context
'stages with statuses'
do
subject
{
CommitStatus
.
where
(
commit:
commit
).
stages_status
}
it
'return list of stages with statuses'
do
is_expected
.
to
eq
({
'build'
=>
'failed'
,
'test'
=>
'success'
,
'deploy'
=>
'running'
})
end
end
end
describe
'#branch?'
do
subject
{
commit_status
.
branch?
}
context
'is not a tag'
do
before
do
commit_status
.
tag
=
false
end
it
'return true when tag is set to false'
do
is_expected
.
to
be_truthy
end
end
context
'is not a tag'
do
before
do
commit_status
.
tag
=
true
end
it
'return false when tag is set to true'
do
is_expected
.
to
be_falsey
end
end
end
end
end
spec/models/project_spec.rb
View file @
1c5b172a
...
@@ -443,7 +443,20 @@ describe Project, models: true do
...
@@ -443,7 +443,20 @@ describe Project, models: true do
let
(
:project
)
{
create
:project
}
let
(
:project
)
{
create
:project
}
let
(
:commit
)
{
create
:ci_commit
,
project:
project
,
ref:
'master'
}
let
(
:commit
)
{
create
:ci_commit
,
project:
project
,
ref:
'master'
}
it
{
expect
(
project
.
ci_commit
(
commit
.
sha
,
'master'
)).
to
eq
(
commit
)
}
subject
{
project
.
ci_commit
(
commit
.
sha
,
'master'
)
}
it
{
is_expected
.
to
eq
(
commit
)
}
context
'return latest'
do
let
(
:commit2
)
{
create
:ci_commit
,
project:
project
,
ref:
'master'
}
before
do
commit
commit2
end
it
{
is_expected
.
to
eq
(
commit2
)
}
end
end
end
describe
:builds_enabled
do
describe
:builds_enabled
do
...
...
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