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
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
8e84acbf
Commit
8e84acbf
authored
Apr 12, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimise CI status accessor
parent
89f0dc71
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
79 additions
and
1 deletion
+79
-1
app/models/ci/commit.rb
app/models/ci/commit.rb
+5
-1
app/models/commit_status.rb
app/models/commit_status.rb
+35
-0
app/models/concerns/ci_status.rb
app/models/concerns/ci_status.rb
+1
-0
db/migrate/20160412174954_add_ci_commit_indexes.rb
db/migrate/20160412174954_add_ci_commit_indexes.rb
+7
-0
db/migrate/20160412175417_update_ci_commit.rb
db/migrate/20160412175417_update_ci_commit.rb
+31
-0
No files found.
app/models/ci/commit.rb
View file @
8e84acbf
...
...
@@ -44,8 +44,12 @@ module Ci
sha
[
0
...
8
]
end
def
self
.
stages
CommitStatus
.
where
(
commit:
all
).
stages
end
def
stages
statuses
.
group
(
:stage
).
order
(
:stage_idx
).
pluck
(
:stage
)
statuses
.
stages
end
def
project_id
...
...
app/models/commit_status.rb
View file @
8e84acbf
...
...
@@ -49,6 +49,7 @@ class CommitStatus < ActiveRecord::Base
scope
:latest
,
->
{
where
(
id:
unscope
(
:select
).
select
(
'max(id)'
).
group
(
:name
,
:commit_id
))
}
scope
:ordered
,
->
{
order
(
:ref
,
:stage_idx
,
:name
)
}
scope
:ignored
,
->
{
where
(
allow_failure:
true
,
status:
[
:failed
,
:canceled
])
}
AVAILABLE_STATUSES
=
[
'pending'
,
'running'
,
'success'
,
'failed'
,
'canceled'
]
...
...
@@ -84,6 +85,40 @@ class CommitStatus < ActiveRecord::Base
delegate
:before_sha
,
:sha
,
:short_sha
,
to: :commit
,
prefix:
false
def
self
.
stages
order_by
=
'max(stage_idx)'
group
(
'stage'
).
order
(
order_by
).
pluck
(
:stage
,
order_by
).
map
(
&
:first
).
compact
end
def
self
.
status_sql
builds
=
all
.
select
(
'count(id)'
).
to_sql
success
=
all
.
success
.
select
(
'count(id)'
).
to_sql
ignored
=
all
.
failed
.
where
(
allow_failure:
true
).
select
(
'count(id)'
).
to_sql
if
all
.
try
(
:ignored
)
ignored
||=
'0'
pending
=
all
.
pending
.
select
(
'count(id)'
).
to_sql
running
=
all
.
running
.
select
(
'count(id)'
).
to_sql
canceled
=
all
.
canceled
.
select
(
'count(id)'
).
to_sql
deduce_status
=
"(CASE
WHEN (
#{
builds
}
)=0 THEN 'skipped'
WHEN (
#{
builds
}
)=(
#{
success
}
)+(
#{
ignored
}
) THEN 'success'
WHEN (
#{
builds
}
)=(
#{
pending
}
) THEN 'pending'
WHEN (
#{
builds
}
)=(
#{
canceled
}
) THEN 'canceled'
WHEN (
#{
running
}
)+(
#{
pending
}
)>0 THEN 'running'
ELSE 'failed'
END)"
deduce_status
end
def
self
.
status
pluck
(
self
.
status_sql
).
first
end
def
self
.
stages_status
Hash
[
group
(
:stage
).
pluck
(
:stage
,
self
.
status_sql
)]
end
def
ignored?
allow_failure?
&&
(
failed?
||
canceled?
)
end
...
...
app/models/concerns/ci_status.rb
View file @
8e84acbf
...
...
@@ -43,6 +43,7 @@ module CiStatus
scope
:pending
,
->
{
where
(
status:
'pending'
)
}
scope
:success
,
->
{
where
(
status:
'success'
)
}
scope
:failed
,
->
{
where
(
status:
'failed'
)
}
scope
:canceled
,
->
{
where
(
status:
'canceled'
)
}
scope
:running_or_pending
,
->
{
where
(
status:
[
:running
,
:pending
])
}
scope
:finished
,
->
{
where
(
status:
[
:success
,
:failed
,
:canceled
])
}
end
...
...
db/migrate/20160412174954_add_ci_commit_indexes.rb
0 → 100644
View file @
8e84acbf
class
AddCiCommitIndexes
<
ActiveRecord
::
Migration
def
change
add_index
:ci_commits
,
[
:gl_project_id
,
:sha
]
add_index
:ci_commits
,
[
:gl_project_id
,
:status
]
add_index
:ci_commits
,
[
:status
]
end
end
db/migrate/20160412175417_update_ci_commit.rb
0 → 100644
View file @
8e84acbf
class
UpdateCiCommit
<
ActiveRecord
::
Migration
def
change
execute
(
"UPDATE ci_commits SET status=
#{
status
}
, ref=
#{
ref
}
, tag=
#{
tag
}
WHERE status IS NULL"
)
end
def
status
builds
=
'(SELECT COUNT(*) FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id)'
success
=
"(SELECT COUNT(*) FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id AND status='success')"
ignored
=
"(SELECT COUNT(*) FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id AND (status='failed' OR status='canceled') AND allow_failure)"
pending
=
"(SELECT COUNT(*) FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id AND status='pending')"
running
=
"(SELECT COUNT(*) FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id AND status='running')"
canceled
=
"(SELECT COUNT(*) FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id AND status='canceled')"
"(CASE
WHEN
#{
builds
}
=0 THEN 'skipped'
WHEN
#{
builds
}
=
#{
success
}
+
#{
ignored
}
THEN 'success'
WHEN
#{
builds
}
=
#{
pending
}
THEN 'pending'
WHEN
#{
builds
}
=
#{
canceled
}
THEN 'canceled'
WHEN
#{
running
}
+
#{
pending
}
>0 THEN 'running'
ELSE 'failed'
END)"
end
def
ref
'(SELECT ref FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id ORDER BY id DESC LIMIT 1)'
end
def
tag
'(SELECT tag FROM ci_builds WHERE ci_builds.commit_id=ci_commits.id ORDER BY id DESC LIMIT 1)'
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