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
Léo-Paul Géneau
gitlab-ce
Commits
a4978030
Commit
a4978030
authored
Oct 17, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve spec for pipeline metrics worker
parent
9c6c5c79
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
75 additions
and
23 deletions
+75
-23
app/workers/pipeline_metrics_worker.rb
app/workers/pipeline_metrics_worker.rb
+20
-5
spec/models/ci/pipeline_spec.rb
spec/models/ci/pipeline_spec.rb
+9
-18
spec/workers/pipeline_metrics_worker_spec.rb
spec/workers/pipeline_metrics_worker_spec.rb
+46
-0
No files found.
app/workers/pipeline_metrics_worker.rb
View file @
a4978030
...
@@ -5,11 +5,26 @@ class PipelineMetricsWorker
...
@@ -5,11 +5,26 @@ class PipelineMetricsWorker
def
perform
(
pipeline_id
)
def
perform
(
pipeline_id
)
Ci
::
Pipeline
.
find_by
(
id:
pipeline_id
).
try
do
|
pipeline
|
Ci
::
Pipeline
.
find_by
(
id:
pipeline_id
).
try
do
|
pipeline
|
merge_requests
=
pipeline
.
merge_requests
.
map
(
&
:id
)
update_metrics_for_active_pipeline
(
pipeline
)
if
pipeline
.
active?
update_metrics_for_succeeded_pipeline
(
pipeline
)
if
pipeline
.
success?
end
end
private
def
update_metrics_for_active_pipeline
(
pipeline
)
metrics
(
pipeline
).
update_all
(
latest_build_started_at:
pipeline
.
started_at
,
latest_build_finished_at:
nil
)
end
def
update_metrics_for_succeeded_pipeline
(
pipeline
)
metrics
(
pipeline
).
update_all
(
latest_build_started_at:
pipeline
.
started_at
,
latest_build_finished_at:
pipeline
.
finished_at
)
end
metrics
=
MergeRequest
::
Metrics
.
where
(
merge_request_id:
merge_requests
)
def
metrics
(
pipeline
)
metrics
.
update_all
(
latest_build_started_at:
pipeline
.
started_at
)
if
pipeline
.
active?
MergeRequest
::
Metrics
.
where
(
merge_request_id:
merge_requests
(
pipeline
))
metrics
.
update_all
(
latest_build_finished_at:
pipeline
.
finished_at
)
if
pipeline
.
success?
end
end
def
merge_requests
(
pipeline
)
pipeline
.
merge_requests
.
map
(
&
:id
)
end
end
end
end
spec/models/ci/pipeline_spec.rb
View file @
a4978030
...
@@ -187,33 +187,24 @@ describe Ci::Pipeline, models: true do
...
@@ -187,33 +187,24 @@ describe Ci::Pipeline, models: true do
end
end
end
end
describe
"merge request metrics"
do
describe
'merge request metrics'
do
let
(
:project
)
{
FactoryGirl
.
create
:project
}
let
(
:project
)
{
FactoryGirl
.
create
:project
}
let
(
:pipeline
)
{
FactoryGirl
.
create
(
:ci_empty_pipeline
,
status:
'created'
,
project:
project
,
ref:
'master'
,
sha:
project
.
repository
.
commit
(
'master'
).
id
)
}
let
(
:pipeline
)
{
FactoryGirl
.
create
(
:ci_empty_pipeline
,
status:
'created'
,
project:
project
,
ref:
'master'
,
sha:
project
.
repository
.
commit
(
'master'
).
id
)
}
let!
(
:merge_request
)
{
create
(
:merge_request
,
source_project:
project
,
source_branch:
pipeline
.
ref
)
}
let!
(
:merge_request
)
{
create
(
:merge_request
,
source_project:
project
,
source_branch:
pipeline
.
ref
)
}
context
'when transitioning to running'
do
before
do
it
'records the build start time'
do
expect
(
PipelineMetricsWorker
).
to
receive
(
:perform_async
).
with
(
pipeline
.
id
)
time
=
Time
.
now
Timecop
.
freeze
(
time
)
{
build
.
run
}
expect
(
merge_request
.
reload
.
metrics
.
latest_build_started_at
).
to
be_within
(
1
.
second
).
of
(
time
)
end
end
it
'clears the build end time'
do
context
'when transitioning to running'
do
build
.
run
it
'schedules metrics workers'
do
pipeline
.
run
expect
(
merge_request
.
reload
.
metrics
.
latest_build_finished_at
).
to
be_nil
end
end
end
end
context
'when transitioning to success'
do
context
'when transitioning to success'
do
it
'records the build end time'
do
it
'schedules metrics workers'
do
build
.
run
pipeline
.
succeed
time
=
Time
.
now
Timecop
.
freeze
(
time
)
{
build
.
success
}
expect
(
merge_request
.
reload
.
metrics
.
latest_build_finished_at
).
to
be_within
(
1
.
second
).
of
(
time
)
end
end
end
end
end
end
...
...
spec/workers/pipeline_metrics_worker_spec.rb
0 → 100644
View file @
a4978030
require
'spec_helper'
describe
PipelineMetricsWorker
do
let
(
:project
)
{
create
(
:project
)
}
let!
(
:merge_request
)
{
create
(
:merge_request
,
source_project:
project
,
source_branch:
pipeline
.
ref
)
}
let
(
:pipeline
)
do
create
(
:ci_empty_pipeline
,
status:
status
,
project:
project
,
ref:
'master'
,
sha:
project
.
repository
.
commit
(
'master'
).
id
,
started_at:
1
.
hour
.
ago
,
finished_at:
Time
.
now
)
end
describe
'#perform'
do
subject
{
described_class
.
new
.
perform
(
pipeline
.
id
)
}
context
'when pipeline is running'
do
let
(
:status
)
{
'running'
}
it
'records the build start time'
do
subject
expect
(
merge_request
.
reload
.
metrics
.
latest_build_started_at
).
to
eq
(
pipeline
.
started_at
)
end
it
'clears the build end time'
do
subject
expect
(
merge_request
.
reload
.
metrics
.
latest_build_finished_at
).
to
be_nil
end
end
context
'when pipeline succeeded'
do
let
(
:status
)
{
'success'
}
it
'records the build end time'
do
subject
expect
(
merge_request
.
reload
.
metrics
.
latest_build_finished_at
).
to
eq
(
pipeline
.
finished_at
)
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