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
Boxiang Sun
gitlab-ce
Commits
fe790c75
Commit
fe790c75
authored
May 25, 2017
by
Felipe Artur
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set head pipeline when creating merge requests
parent
5b9e801e
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
13 deletions
+61
-13
app/services/ci/create_pipeline_service.rb
app/services/ci/create_pipeline_service.rb
+10
-11
app/services/merge_requests/create_service.rb
app/services/merge_requests/create_service.rb
+18
-0
changelogs/unreleased/issue_32225_2.yml
changelogs/unreleased/issue_32225_2.yml
+1
-1
spec/services/ci/create_pipeline_service_spec.rb
spec/services/ci/create_pipeline_service_spec.rb
+1
-1
spec/services/merge_requests/create_service_spec.rb
spec/services/merge_requests/create_service_spec.rb
+31
-0
No files found.
app/services/ci/create_pipeline_service.rb
View file @
fe790c75
...
...
@@ -61,6 +61,16 @@ module Ci
private
def
update_merge_requests_head_pipeline
merge_requests
=
MergeRequest
.
where
(
source_branch:
@pipeline
.
ref
,
source_project:
@pipeline
.
project
)
merge_requests
=
merge_requests
.
select
do
|
mr
|
mr
.
diff_head_sha
==
@pipeline
.
sha
end
MergeRequest
.
where
(
id:
merge_requests
).
update_all
(
head_pipeline_id:
@pipeline
.
id
)
end
def
skip_ci?
return
false
unless
pipeline
.
git_commit_message
pipeline
.
git_commit_message
=~
/\[(ci[ _-]skip|skip[ _-]ci)\]/i
...
...
@@ -118,17 +128,6 @@ module Ci
origin_sha
&&
origin_sha
!=
Gitlab
::
Git
::
BLANK_SHA
end
def
update_merge_requests_head_pipeline
merge_requests
=
MergeRequest
.
where
(
source_branch:
@pipeline
.
ref
,
source_project:
@pipeline
.
project
)
merge_requests_ids
=
merge_requests
.
select
do
|
mr
|
mr
.
diff_head_sha
==
@pipeline
.
sha
end
.
map
(
&
:id
)
MergeRequest
.
where
(
id:
merge_requests_ids
).
update_all
(
head_pipeline_id:
@pipeline
.
id
)
end
def
error
(
message
,
save:
false
)
pipeline
.
errors
.
add
(
:base
,
message
)
pipeline
.
drop
if
save
...
...
app/services/merge_requests/create_service.rb
View file @
fe790c75
...
...
@@ -11,7 +11,9 @@ module MergeRequests
merge_request
=
MergeRequest
.
new
merge_request
.
source_project
=
source_project
merge_request
.
source_branch
=
params
[
:source_branch
]
merge_request
.
merge_params
[
'force_remove_source_branch'
]
=
params
.
delete
(
:force_remove_source_branch
)
merge_request
.
head_pipeline
=
head_pipeline_for
(
merge_request
)
create
(
merge_request
)
end
...
...
@@ -22,5 +24,21 @@ module MergeRequests
todo_service
.
new_merge_request
(
issuable
,
current_user
)
issuable
.
cache_merge_request_closes_issues!
(
current_user
)
end
private
def
head_pipeline_for
(
merge_request
)
return
unless
merge_request
.
source_project
sha
=
merge_request
.
source_branch_head
&
.
id
return
unless
sha
pipelines
=
Ci
::
Pipeline
.
where
(
ref:
merge_request
.
source_branch
,
project_id:
merge_request
.
source_project
.
id
,
sha:
sha
).
order
(
id: :desc
)
pipelines
.
first
end
end
end
changelogs/unreleased/issue_32225_2.yml
View file @
fe790c75
---
title
:
Sanity check for pipeline sha before saving merge request pipeline id
title
:
Handle head pipeline when creating merge requests
merge_request
:
author
:
spec/services/ci/create_pipeline_service_spec.rb
View file @
fe790c75
...
...
@@ -36,7 +36,7 @@ describe Ci::CreatePipelineService, services: true do
expect
(
pipeline
.
builds
.
first
).
to
be_kind_of
(
Ci
::
Build
)
end
context
'
#update_merge_requests_head_pipeline
'
do
context
'
when merge requests already exist for this source branch
'
do
it
'updates head pipeline of each merge request'
do
merge_request_1
=
create
(
:merge_request
,
source_branch:
'master'
,
target_branch:
"branch_1"
,
source_project:
project
)
merge_request_2
=
create
(
:merge_request
,
source_branch:
'master'
,
target_branch:
"branch_2"
,
source_project:
project
)
...
...
spec/services/merge_requests/create_service_spec.rb
View file @
fe790c75
...
...
@@ -75,6 +75,37 @@ describe MergeRequests::CreateService, services: true do
expect
(
Todo
.
where
(
attributes
).
count
).
to
eq
1
end
end
context
'when head pipelines already exist for merge request source branch'
do
let
(
:sha
)
{
project
.
commit
(
opts
[
:source_branch
]).
id
}
let!
(
:pipeline_1
)
{
create
(
:ci_pipeline
,
project:
project
,
ref:
opts
[
:source_branch
],
project_id:
project
.
id
,
sha:
sha
)
}
let!
(
:pipeline_2
)
{
create
(
:ci_pipeline
,
project:
project
,
ref:
opts
[
:source_branch
],
project_id:
project
.
id
,
sha:
sha
)
}
let!
(
:pipeline_3
)
{
create
(
:ci_pipeline
,
project:
project
,
ref:
"other_branch"
,
project_id:
project
.
id
)
}
before
do
project
.
merge_requests
.
where
(
source_branch:
opts
[
:source_branch
],
target_branch:
opts
[
:target_branch
]).
destroy_all
end
it
'sets head pipeline'
do
merge_request
=
service
.
execute
expect
(
merge_request
.
head_pipeline
).
to
eq
(
pipeline_2
)
expect
(
merge_request
).
to
be_persisted
end
context
'when merge request head commit sha does not match pipeline sha'
do
it
'sets the head pipeline correctly'
do
pipeline_2
.
update
(
sha:
1234
)
merge_request
=
service
.
execute
expect
(
merge_request
.
head_pipeline
).
to
eq
(
pipeline_1
)
expect
(
merge_request
).
to
be_persisted
end
end
end
end
it_behaves_like
'new issuable record that supports slash commands'
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