Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
3f5fe879dd95
Commit
3f5fe879
authored
Oct 02, 2015
by
Kamil Trzcinski
Browse files
Refactor commit and build
parent
32f957b9203c
Changes
32
Hide whitespace changes
Inline
Side-by-side
app/controllers/ci/builds_controller.rb
View file @
3f5fe879
...
...
@@ -18,7 +18,7 @@
if
commit
# Redirect to commit page
redirect_to
ci_project_
ref_
commit_path
(
@project
,
@build
.
commit
.
ref
,
@build
.
commit
.
sha
)
redirect_to
ci_project_commit_path
(
@project
,
@build
.
commit
)
return
end
end
...
...
app/controllers/ci/commits_controller.rb
View file @
3f5fe879
...
...
@@ -13,7 +13,7 @@
end
def
status
commit
=
Ci
::
Project
.
find
(
params
[
:project_id
]).
commits
.
find_by_sha
_and_ref
!
(
params
[
:id
],
params
[
:ref_id
])
commit
=
Ci
::
Project
.
find
(
params
[
:project_id
]).
commits
.
find_by_sha!
(
params
[
:id
],
params
[
:ref_id
])
render
json:
commit
.
to_json
(
only:
[
:id
,
:sha
],
methods:
[
:status
,
:coverage
])
rescue
ActiveRecord
::
RecordNotFound
render
json:
{
status:
"not_found"
}
...
...
@@ -22,7 +22,7 @@
def
cancel
commit
.
builds
.
running_or_pending
.
each
(
&
:cancel
)
redirect_to
ci_project_
ref_
commits_path
(
project
,
commit
.
ref
,
commit
.
sha
)
redirect_to
ci_project_commits_path
(
project
,
commit
.
sha
)
end
private
...
...
@@ -32,7 +32,7 @@
end
def
commit
@commit
||=
Ci
::
Project
.
find
(
params
[
:project_id
]).
commits
.
find_by_sha
_and_ref
!
(
params
[
:id
]
,
params
[
:ref_id
]
)
@commit
||=
Ci
::
Project
.
find
(
params
[
:project_id
]).
commits
.
find_by_sha!
(
params
[
:id
])
end
end
end
app/controllers/ci/projects_controller.rb
View file @
3f5fe879
...
...
@@ -19,7 +19,8 @@
@ref
=
params
[
:ref
]
@commits
=
@project
.
commits
.
reverse_order
@commits
=
@commits
.
where
(
ref:
@ref
)
if
@ref
# TODO: this is broken
# @commits = @commits.where(ref: @ref) if @ref
@commits
=
@commits
.
page
(
params
[
:page
]).
per
(
20
)
end
...
...
app/helpers/ci/commits_helper.rb
View file @
3f5fe879
module
Ci
module
CommitsHelper
def
ci_commit_path
(
commit
)
ci_project_
ref_
commits_path
(
commit
.
project
,
commit
.
ref
,
commit
.
sha
)
ci_project_commits_path
(
commit
.
project
,
commit
)
end
def
commit_link
(
commit
)
...
...
app/helpers/ci/gitlab_helper.rb
View file @
3f5fe879
...
...
@@ -26,7 +26,7 @@
def
yaml_web_editor_link
(
project
)
commits
=
project
.
commits
if
commits
.
any?
&&
commits
.
last
.
push_data
[
:
ci_yaml_file
]
if
commits
.
any?
&&
commits
.
last
.
ci_yaml_file
"
#{
project
.
gitlab_url
}
/edit/master/.gitlab-ci.yml"
else
"
#{
project
.
gitlab_url
}
/new/master"
...
...
app/helpers/ci_status_helper.rb
View file @
3f5fe879
module
CiStatusHelper
def
ci_status_path
(
ci_commit
)
ci_project_
ref_
commits_path
(
ci_commit
.
project
,
ci_commit
.
ref
,
ci_commit
)
ci_project_commits_path
(
ci_commit
.
project
,
ci_commit
)
end
def
ci_status_icon
(
ci_commit
)
...
...
app/models/ci/build.rb
View file @
3f5fe879
...
...
@@ -34,7 +34,8 @@
belongs_to
:trigger_request
,
class_name:
'Ci::TriggerRequest'
serialize
:options
serialize
:push_data
validates
:commit
,
presence:
true
validates
:status
,
presence:
true
validates
:coverage
,
numericality:
true
,
allow_blank:
true
...
...
@@ -37,7 +38,8 @@
validates
:commit
,
presence:
true
validates
:status
,
presence:
true
validates
:coverage
,
numericality:
true
,
allow_blank:
true
validates_presence_of
:ref
scope
:running
,
->
()
{
where
(
status:
"running"
)
}
scope
:pending
,
->
()
{
where
(
status:
"pending"
)
}
...
...
@@ -45,6 +47,9 @@
scope
:failed
,
->
()
{
where
(
status:
"failed"
)
}
scope
:unstarted
,
->
()
{
where
(
runner_id:
nil
)
}
scope
:running_or_pending
,
->
()
{
where
(
status
:[
:running
,
:pending
])
}
scope
:latest
,
->
()
{
group
(
:name
).
order
(
stage_idx: :asc
,
created_at: :desc
)
}
scope
:ignore_failures
,
->
()
{
where
(
allow_failure:
false
)
}
scope
:for_ref
,
->
(
ref
)
{
where
(
ref:
ref
)
}
acts_as_taggable
...
...
@@ -82,6 +87,7 @@
new_build
.
name
=
build
.
name
new_build
.
allow_failure
=
build
.
allow_failure
new_build
.
stage
=
build
.
stage
new_build
.
stage_idx
=
build
.
stage_idx
new_build
.
trigger_request
=
build
.
trigger_request
new_build
.
save
new_build
...
...
@@ -187,6 +193,16 @@
project
.
name
end
def
project_recipients
recipients
=
project
.
email_recipients
.
split
(
' '
)
if
project
.
email_add_pusher?
&&
push_data
[
:user_email
].
present?
recipients
<<
push_data
[
:user_email
]
end
recipients
.
uniq
end
def
repo_url
project
.
repo_url_with_auth
end
...
...
app/models/ci/commit.rb
View file @
3f5fe879
...
...
@@ -23,9 +23,7 @@
has_many
:builds
,
dependent: :destroy
,
class_name:
'Ci::Build'
has_many
:trigger_requests
,
dependent: :destroy
,
class_name:
'Ci::TriggerRequest'
serialize
:push_data
validates_presence_of
:ref
,
:sha
,
:before_sha
,
:push_data
validates_presence_of
:sha
validate
:valid_commit_sha
def
self
.
truncate_sha
(
sha
)
...
...
@@ -69,7 +67,7 @@
end
def
git_author_name
commit_data
[
:
author
][
:
name
]
if
commit_data
&&
commit_data
[
:author
]
commit_data
.
author
_
name
if
commit_data
end
def
git_author_email
...
...
@@ -73,7 +71,7 @@
end
def
git_author_email
commit_data
[
:
author
][
:
email
]
if
commit_data
&&
commit_data
[
:author
]
commit_data
.
author
_
email
if
commit_data
end
def
git_commit_message
...
...
@@ -77,7 +75,7 @@
end
def
git_commit_message
commit_data
[
:
message
]
if
commit_data
&&
commit_data
[
:message
]
commit_data
.
message
if
commit_data
end
def
short_before_sha
...
...
@@ -89,10 +87,8 @@
end
def
commit_data
push_data
[
:commits
].
find
do
|
commit
|
commit
[
:id
]
==
sha
end
@commit
||=
gl_project
.
commit
(
sha
)
rescue
nil
end
...
...
@@ -95,14 +91,8 @@
rescue
nil
end
def
project_recipients
recipients
=
project
.
email_recipients
.
split
(
' '
)
if
project
.
email_add_pusher?
&&
push_data
[
:user_email
].
present?
recipients
<<
push_data
[
:user_email
]
end
recipients
.
uniq
def
stage
builds_without_retry
.
group
(
:stage_idx
).
select
(
:stage
).
last
end
...
...
@@ -107,11 +97,5 @@
end
def
stage
return
unless
config_processor
stages
=
builds_without_retry
.
select
(
&
:active?
).
map
(
&
:stage
)
config_processor
.
stages
.
find
{
|
stage
|
stages
.
include?
stage
}
end
def
create_builds_for_stage
(
stage
,
trigger_request
)
def
create_builds
(
ref
,
tag
,
push_data
,
trigger_request
=
nil
)
return
if
skip_ci?
&&
trigger_request
.
blank?
return
unless
config_processor
...
...
@@ -116,17 +100,5 @@
return
if
skip_ci?
&&
trigger_request
.
blank?
return
unless
config_processor
builds_attrs
=
config_processor
.
builds_for_stage_and_ref
(
stage
,
ref
,
tag
)
builds_attrs
.
map
do
|
build_attrs
|
builds
.
create!
({
name:
build_attrs
[
:name
],
commands:
build_attrs
[
:script
],
tag_list:
build_attrs
[
:tags
],
options:
build_attrs
[
:options
],
allow_failure:
build_attrs
[
:allow_failure
],
stage:
build_attrs
[
:stage
],
trigger_request:
trigger_request
,
})
end
CreateBuildsService
.
new
.
execute
(
self
,
config_processor
,
ref
,
tag
,
push_data
,
trigger_request
)
end
...
...
@@ -131,13 +103,6 @@
end
def
create_next_builds
(
trigger_request
)
return
if
skip_ci?
&&
trigger_request
.
blank?
return
unless
config_processor
stages
=
builds
.
where
(
trigger_request:
trigger_request
).
group_by
(
&
:stage
)
config_processor
.
stages
.
any?
do
|
stage
|
!
stages
.
include?
(
stage
)
&&
create_builds_for_stage
(
stage
,
trigger_request
).
present?
end
def
refs
builds
.
group
(
:ref
).
pluck
(
:ref
)
end
...
...
@@ -142,12 +107,7 @@
end
def
create_builds
(
trigger_request
=
nil
)
return
if
skip_ci?
&&
trigger_request
.
blank?
return
unless
config_processor
config_processor
.
stages
.
any?
do
|
stage
|
create_builds_for_stage
(
stage
,
trigger_request
).
present?
end
def
last_ref
builds
.
latest
.
first
.
try
(
:ref
)
end
def
builds_without_retry
...
...
@@ -151,22 +111,7 @@
end
def
builds_without_retry
@builds_without_retry
||=
begin
grouped_builds
=
builds
.
group_by
(
&
:name
)
grouped_builds
.
map
do
|
name
,
builds
|
builds
.
sort_by
(
&
:id
).
last
end
end
end
def
builds_without_retry_sorted
return
builds_without_retry
unless
config_processor
stages
=
config_processor
.
stages
builds_without_retry
.
sort_by
do
|
build
|
[
stages
.
index
(
build
.
stage
)
||
-
1
,
build
.
name
||
""
]
end
builds
.
latest
end
def
retried_builds
...
...
@@ -180,12 +125,15 @@
return
'failed'
elsif
builds
.
none?
return
'skipped'
elsif
success?
'success'
elsif
pending?
'pending'
elsif
running?
'running'
elsif
canceled?
'canceled'
end
statuses
=
builds_without_retry
.
ignore_failures
.
pluck
(
:status
)
if
statuses
.
all?
{
|
status
|
status
==
'success'
}
return
'success'
elsif
statuses
.
all?
{
|
status
|
status
==
'pending'
}
return
'pending'
elsif
statuses
.
include?
(
'running'
)
||
statuses
.
include?
(
'pending'
)
return
'running'
elsif
statuses
.
all?
{
|
status
|
status
==
'canceled'
}
return
'canceled'
else
...
...
@@ -191,6 +139,6 @@
else
'failed'
return
'failed'
end
end
def
pending?
...
...
@@ -193,10 +141,8 @@
end
end
def
pending?
builds_without_retry
.
all?
do
|
build
|
build
.
pending?
end
status
==
'pending'
end
def
running?
...
...
@@ -200,9 +146,7 @@
end
def
running?
builds_without_retry
.
any?
do
|
build
|
build
.
running?
||
build
.
pending?
end
status
==
'running'
end
def
success?
...
...
@@ -206,9 +150,7 @@
end
def
success?
builds_without_retry
.
all?
do
|
build
|
build
.
success?
||
build
.
ignored?
end
status
==
'success'
end
def
failed?
...
...
@@ -216,12 +158,10 @@
end
def
canceled?
builds_without_retry
.
all?
do
|
build
|
build
.
canceled?
end
status
==
'canceled'
end
def
duration
@duration
||=
builds_without_retry
.
select
(
&
:duration
).
sum
(
&
:duration
).
to_i
end
...
...
@@ -222,9 +162,13 @@
end
def
duration
@duration
||=
builds_without_retry
.
select
(
&
:duration
).
sum
(
&
:duration
).
to_i
end
def
duration_for_ref
(
ref
)
builds_without_retry
.
for_ref
(
ref
).
select
(
&
:duration
).
sum
(
&
:duration
).
to_i
end
def
finished_at
@finished_at
||=
builds
.
order
(
'finished_at DESC'
).
first
.
try
(
:finished_at
)
end
...
...
@@ -238,8 +182,8 @@
end
end
def
matrix
?
builds_without_retry
.
size
>
1
def
matrix
_for_ref?
(
ref
)
builds_without_retry
.
for_ref
(
ref
).
pluck
(
:id
).
size
>
1
end
def
config_processor
...
...
@@ -243,7 +187,7 @@
end
def
config_processor
@config_processor
||=
Ci
::
GitlabCiYamlProcessor
.
new
(
push_data
[
:
ci_yaml_file
]
)
@config_processor
||=
Ci
::
GitlabCiYamlProcessor
.
new
(
ci_yaml_file
)
rescue
Ci
::
GitlabCiYamlProcessor
::
ValidationError
=>
e
save_yaml_error
(
e
.
message
)
nil
...
...
@@ -253,5 +197,11 @@
nil
end
def
ci_yaml_file
gl_project
.
repository
.
blob_at
(
sha
,
'.gitlab-ci.yml'
)
rescue
nil
end
def
skip_ci?
return
false
if
builds
.
any?
...
...
@@ -256,7 +206,6 @@
def
skip_ci?
return
false
if
builds
.
any?
commits
=
push_data
[
:commits
]
commits
.
present?
&&
commits
.
last
[
:message
]
=~
/(\[ci skip\])/
git_commit_message
=~
/(\[ci skip\])/
if
git_commit_message
end
def
update_committed!
...
...
app/models/ci/project_status.rb
View file @
3f5fe879
...
...
@@ -28,18 +28,6 @@
status
end
# only check for toggling build status within same ref.
def
last_commit_changed_status?
ref
=
last_commit
.
ref
last_commits
=
commits
.
where
(
ref:
ref
).
last
(
2
)
if
last_commits
.
size
<
2
false
else
last_commits
[
0
].
status
!=
last_commits
[
1
].
status
end
end
def
last_commit_for_ref
(
ref
)
commits
.
where
(
ref:
ref
).
last
end
...
...
app/models/project.rb
View file @
3f5fe879
...
...
@@ -744,7 +744,11 @@
end
def
ci_commit
(
sha
)
gitlab_ci_project
.
commits
.
find_by
(
sha:
sha
)
if
gitlab_ci?
ci_commits
.
find_by
(
sha:
sha
)
end
def
ensure_ci_commit
(
sha
)
ci_commit
(
sha
)
||
ci_commits
.
create
(
sha:
sha
)
end
def
ensure_gitlab_ci_project
...
...
app/models/project_services/ci/hip_chat_message.rb
View file @
3f5fe879
...
...
@@ -13,7 +13,7 @@
lines
.
push
(
"<a href=
\"
#{
ci_project_url
(
project
)
}
\"
>
#{
project
.
name
}
</a> - "
)
if
commit
.
matrix?
lines
.
push
(
"<a href=
\"
#{
ci_project_
ref_
commits_url
(
project
,
commit
.
ref
,
commit
.
sha
)
}
\"
>Commit #
#{
commit
.
id
}
</a></br>"
)
lines
.
push
(
"<a href=
\"
#{
ci_project_commits_url
(
project
,
commit
.
sha
)
}
\"
>Commit #
#{
commit
.
id
}
</a></br>"
)
else
first_build
=
commit
.
builds_without_retry
.
first
lines
.
push
(
"<a href=
\"
#{
ci_project_build_url
(
project
,
first_build
)
}
\"
>Build '
#{
first_build
.
name
}
' #
#{
first_build
.
id
}
</a></br>"
)
...
...
app/models/project_services/ci/mail_service.rb
View file @
3f5fe879
...
...
@@ -61,7 +61,7 @@
end
def
execute
(
build
)
build
.
commit
.
project_recipients
.
each
do
|
recipient
|
build
.
project_recipients
.
each
do
|
recipient
|
case
build
.
status
.
to_sym
when
:success
mailer
.
build_success_email
(
build
.
id
,
recipient
)
...
...
app/models/project_services/ci/slack_message.rb
View file @
3f5fe879
...
...
@@ -48,7 +48,7 @@
def
attachment_message
out
=
"<
#{
ci_project_url
(
project
)
}
|
#{
project_name
}
>: "
if
commit
.
matrix?
out
<<
"Commit <
#{
ci_project_
ref_
commits_url
(
project
,
commit
.
ref
,
commit
.
sha
)
}
|
\#
#{
commit
.
id
}
> "
out
<<
"Commit <
#{
ci_project_commits_url
(
project
,
commit
.
sha
)
}
|
\#
#{
commit
.
id
}
> "
else
build
=
commit
.
builds_without_retry
.
first
out
<<
"Build <
#{
ci_project_build_url
(
project
,
build
)
}
|
\#
#{
build
.
id
}
> "
...
...
app/models/project_services/gitlab_ci_service.rb
View file @
3f5fe879
...
...
@@ -63,7 +63,7 @@
end
def
get_ci_commit
(
sha
,
ref
)
Ci
::
Project
.
find
(
project
.
gitlab_ci_project
).
commits
.
find_by_sha
_and_ref!
(
sha
,
ref
)
Ci
::
Project
.
find
(
project
.
gitlab_ci_project
).
commits
.
find_by_sha
!
(
sha
)
end
def
commit_status
(
sha
,
ref
)
...
...
@@ -80,7 +80,7 @@
def
build_page
(
sha
,
ref
)
if
project
.
gitlab_ci_project
.
present?
ci_project_
ref_
commits_url
(
project
.
gitlab_ci_project
,
ref
,
sha
)
ci_project_commits_url
(
project
.
gitlab_ci_project
,
sha
)
end
end
...
...
app/services/ci/create_builds_service.rb
0 → 100644
View file @
3f5fe879
module
Ci
class
CreateBuildsService
def
execute
(
commit
,
ref
,
tag
,
push_data
,
config_processor
,
trigger_request
)
config_processor
.
stages
.
any?
do
|
stage
|
builds_attrs
=
config_processor
.
builds_for_stage_and_ref
(
stage
,
ref
,
tag
)
builds_attrs
.
map
do
|
build_attrs
|
# don't create the same build twice
unless
commit
.
builds
.
find_by_name_and_trigger_request
(
name:
build_attrs
[
:name
],
ref:
ref
,
tag:
tag
,
trigger_request:
trigger_request
)
commit
.
builds
.
create!
({
name:
build_attrs
[
:name
],
commands:
build_attrs
[
:script
],
tag_list:
build_attrs
[
:tags
],
options:
build_attrs
[
:options
],
allow_failure:
build_attrs
[
:allow_failure
],
stage:
build_attrs
[
:stage
],
stage_idx:
build_attrs
[
:stage_idx
],
trigger_request:
trigger_request
,
ref:
ref
,
tag:
tag
,
push_data:
push_data
,
})
end
end
end
end
end
end
app/services/ci/create_commit_service.rb
View file @
3f5fe879
...
...
@@ -16,5 +16,16 @@
return
false
end
commit
=
project
.
commits
.
find_by_sha_and_ref
(
sha
,
ref
)
tag
=
origin_ref
.
start_with?
(
'refs/tags/'
)
push_data
=
{
before:
before_sha
,
after:
sha
,
ref:
ref
,
user_name:
params
[
:user_name
],
user_email:
params
[
:user_email
],
repository:
params
[
:repository
],
commits:
params
[
:commits
],
total_commits_count:
params
[
:total_commits_count
],
ci_yaml_file:
params
[
:ci_yaml_file
]
}
...
...
@@ -20,25 +31,3 @@
# Create commit if not exists yet
unless
commit
data
=
{
ref:
ref
,
sha:
sha
,
tag:
origin_ref
.
start_with?
(
'refs/tags/'
),
before_sha:
before_sha
,
push_data:
{
before:
before_sha
,
after:
sha
,
ref:
ref
,
user_name:
params
[
:user_name
],
user_email:
params
[
:user_email
],
repository:
params
[
:repository
],
commits:
params
[
:commits
],
total_commits_count:
params
[
:total_commits_count
],
ci_yaml_file:
params
[
:ci_yaml_file
]
}
}
commit
=
project
.
commits
.
create
(
data
)
end
commit
=
project
.
gl_project
.
ensure_ci_commit
(
sha
)
commit
.
update_committed!
...
...
@@ -44,5 +33,5 @@
commit
.
update_committed!
commit
.
create_builds
unless
commit
.
builds
.
any?
commit
.
create_builds
(
ref
,
tag
,
push_data
)
commit
end
...
...
app/services/ci/create_trigger_request_service.rb
View file @
3f5fe879
module
Ci
class
CreateTriggerRequestService
def
execute
(
project
,
trigger
,
ref
,
variables
=
nil
)
commit
=
project
.
commits
.
where
(
ref:
ref
).
last
commit
=
project
.
gl_project
.
commit
(
ref
)
return
unless
commit
...
...
@@ -5,3 +5,4 @@
return
unless
commit
ci_commit
=
project
.
gl_project
.
ensure_ci_commit
(
commit
.
sha
)
trigger_request
=
trigger
.
trigger_requests
.
create!
(
...
...
@@ -7,5 +8,4 @@
trigger_request
=
trigger
.
trigger_requests
.
create!
(
commit:
commit
,
variables:
variables
)
...
...
@@ -9,7 +9,7 @@
variables:
variables
)
if
commit
.
create_builds
(
trigger_request
)
if
ci_
commit
.
create_builds
(
ref
,
tag
,
nil
,
trigger_request
)
trigger_request
end
end
...
...
app/views/ci/builds/_build.html.haml
View file @
3f5fe879
...
...
@@ -6,6 +6,10 @@
=
link_to
ci_project_build_path
(
build
.
project
,
build
)
do
%strong
Build ##{build.id}
-
if
defined?
(
ref
)
%td
=
build
.
ref
%td
=
build
.
stage
...
...
app/views/ci/builds/show.html.haml
View file @
3f5fe879
#up-build-trace
-
if
@commit
.
matrix
?
-
if
@commit
.
matrix
_for_ref?
(
@build
.
ref
)
%ul
.center-top-menu
...
...
@@ -3,5 +3,5 @@
%ul
.center-top-menu
-
@commit
.
builds_without_retry
_sorted
.
each
do
|
build
|