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
201a6511
Commit
201a6511
authored
Jan 07, 2019
by
Mark Chao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve various specs
Add test case for not allow author to approve
parent
7f80b6ed
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
61 deletions
+77
-61
ee/app/services/approval_rules/finalize_service.rb
ee/app/services/approval_rules/finalize_service.rb
+14
-9
ee/spec/models/approval_merge_request_rule_spec.rb
ee/spec/models/approval_merge_request_rule_spec.rb
+21
-12
ee/spec/models/concerns/approval_rule_like_spec.rb
ee/spec/models/concerns/approval_rule_like_spec.rb
+21
-20
ee/spec/services/approval_rules/finalize_service_spec.rb
ee/spec/services/approval_rules/finalize_service_spec.rb
+21
-20
No files found.
ee/app/services/approval_rules/finalize_service.rb
View file @
201a6511
...
...
@@ -11,13 +11,15 @@ module ApprovalRules
def
execute
return
unless
merge_request
.
merged?
if
merge_request
.
approval_rules
.
regular
.
exists?
merge_group_members_into_users
else
copy_project_approval_rules
ActiveRecord
::
Base
.
transaction
do
if
merge_request
.
approval_rules
.
regular
.
exists?
merge_group_members_into_users
else
copy_project_approval_rules
end
merge_request
.
approval_rules
.
each
(
&
:sync_approved_approvers
)
end
merge_request
.
approval_rules
.
each
(
&
:sync_approved_approvers
)
end
private
...
...
@@ -30,9 +32,12 @@ module ApprovalRules
def
copy_project_approval_rules
merge_request
.
target_project
.
approval_rules
.
each
do
|
project_rule
|
rule
=
merge_request
.
approval_rules
.
create!
(
project_rule
.
attributes
.
slice
(
'approvals_required'
,
'name'
))
rule
.
users
=
project_rule
.
approvers
rule
.
groups
=
project_rule
.
groups
.
public_or_visible_to_user
(
merge_request
.
author
)
users
=
project_rule
.
approvers
groups
=
project_rule
.
groups
.
public_or_visible_to_user
(
merge_request
.
author
)
merge_request
.
approval_rules
.
create!
(
project_rule
.
attributes
.
slice
(
'approvals_required'
,
'name'
).
merge
(
users:
users
,
groups:
groups
)
)
end
end
end
...
...
ee/spec/models/approval_merge_request_rule_spec.rb
View file @
201a6511
# frozen_string_literal: true
require
'
rails
_helper'
require
'
spec
_helper'
RSpec
.
describe
ApprovalMergeRequestRule
,
type: :model
do
describe
ApprovalMergeRequestRule
do
let
(
:merge_request
)
{
create
(
:merge_request
)
}
subject
{
create
(
:approval_merge_request_rule
,
merge_request:
merge_request
)
}
describe
'#project'
do
it
'returns project of MergeRequest'
do
expect
(
subject
.
project
).
to
be_present
expect
(
subject
.
project
).
to
eq
(
merge_request
.
project
)
end
end
describe
'#approvers'
do
context
'when project setting includes author'
do
before
do
merge_request
.
target_project
.
update
(
merge_requests_author_approval:
true
)
create
(
:group
)
do
|
group
|
group
.
add_guest
(
merge_request
.
author
)
subject
.
groups
<<
group
end
before
do
create
(
:group
)
do
|
group
|
group
.
add_guest
(
merge_request
.
author
)
subject
.
groups
<<
group
end
end
context
'when project merge_requests_author_approval is true'
do
it
'contains author'
do
merge_request
.
project
.
update
(
merge_requests_author_approval:
true
)
expect
(
subject
.
approvers
).
to
contain_exactly
(
merge_request
.
author
)
end
end
context
'when project merge_requests_author_approval is false'
do
it
'contains author'
do
merge_request
.
project
.
update
(
merge_requests_author_approval:
false
)
expect
(
subject
.
approvers
).
to
be_empty
end
end
end
describe
'#sync_approved_approvers'
do
...
...
@@ -46,14 +55,14 @@ RSpec.describe ApprovalMergeRequestRule, type: :model do
it
'does nothing'
do
subject
.
sync_approved_approvers
expect
(
subject
.
approved_approvers
).
to
be_empty
expect
(
subject
.
approved_approvers
.
reload
).
to
be_empty
end
end
context
'when merged'
do
let
(
:merge_request
)
{
create
(
:merged_merge_request
)
}
it
'
updates mapping
'
do
it
'
records approved approvers as approved_approvers association
'
do
subject
.
sync_approved_approvers
expect
(
subject
.
approved_approvers
.
reload
).
to
contain_exactly
(
member1
,
member2
)
...
...
ee/spec/models/concerns/approval_rule_like_spec.rb
View file @
201a6511
# frozen_string_literal: true
require
'
rails
_helper'
require
'
spec
_helper'
RSpec
.
describe
ApprovalRuleLike
,
type: :model
do
let
(
:
memb
er1
)
{
create
(
:user
)
}
let
(
:
memb
er2
)
{
create
(
:user
)
}
let
(
:
memb
er3
)
{
create
(
:user
)
}
describe
ApprovalRuleLike
do
let
(
:
us
er1
)
{
create
(
:user
)
}
let
(
:
us
er2
)
{
create
(
:user
)
}
let
(
:
us
er3
)
{
create
(
:user
)
}
let
(
:group1
)
{
create
(
:group
)
}
let
(
:group2
)
{
create
(
:group
)
}
...
...
@@ -15,16 +15,17 @@ RSpec.describe ApprovalRuleLike, type: :model do
describe
'#add_member'
do
it
'adds as a member of the rule'
do
expect
do
subject
.
add_member
(
member1
)
end
.
to
change
{
subject
.
users
.
count
}.
by
(
1
)
subject
.
add_member
(
user1
)
subject
.
add_member
(
group1
)
end
.
to
change
{
subject
.
users
.
count
}.
by
(
1
).
and
change
{
subject
.
groups
.
count
}.
by
(
1
)
end
it
'does nothing if already a member'
do
subject
.
add_member
(
memb
er1
)
subject
.
add_member
(
us
er1
)
expect
do
subject
.
add_member
(
memb
er1
)
end
.
not_to
change
{
subject
.
users
.
count
}
subject
.
add_member
(
us
er1
)
end
.
not_to
change
{
subject
.
users
.
count
+
subject
.
groups
.
count
}
end
end
...
...
@@ -45,31 +46,31 @@ RSpec.describe ApprovalRuleLike, type: :model do
end
describe
'#approvers'
do
let
(
:group1_
member1
)
{
create
(
:user
)
}
let
(
:group2_
member1
)
{
create
(
:user
)
}
let
(
:group1_
user
)
{
create
(
:user
)
}
let
(
:group2_
user
)
{
create
(
:user
)
}
before
do
subject
.
users
<<
memb
er1
subject
.
users
<<
memb
er2
subject
.
users
<<
us
er1
subject
.
users
<<
us
er2
subject
.
groups
<<
group1
subject
.
groups
<<
group2
group1
.
add_guest
(
group1_
member1
)
group2
.
add_guest
(
group2_
member1
)
group1
.
add_guest
(
group1_
user
)
group2
.
add_guest
(
group2_
user
)
end
it
'contains users as direct members and group members'
do
expect
(
subject
.
approvers
).
to
contain_exactly
(
member1
,
member2
,
group1_member1
,
group2_member1
)
expect
(
subject
.
approvers
).
to
contain_exactly
(
user1
,
user2
,
group1_user
,
group2_user
)
end
context
'when user is both a direct member and a group member'
do
before
do
group1
.
add_guest
(
memb
er1
)
group2
.
add_guest
(
memb
er2
)
group1
.
add_guest
(
us
er1
)
group2
.
add_guest
(
us
er2
)
end
it
'contains only unique users'
do
expect
(
subject
.
approvers
).
to
contain_exactly
(
member1
,
member2
,
group1_member1
,
group2_member1
)
expect
(
subject
.
approvers
).
to
contain_exactly
(
user1
,
user2
,
group1_user
,
group2_user
)
end
end
end
...
...
ee/spec/services/approval_rules/finalize_service_spec.rb
View file @
201a6511
...
...
@@ -7,26 +7,26 @@ describe ApprovalRules::FinalizeService do
let
(
:merge_request
)
{
create
(
:merge_request
,
source_project:
project
,
target_project:
project
)
}
describe
'#execute'
do
let!
(
:
memb
er1
)
{
create
(
:user
)
}
let!
(
:
memb
er2
)
{
create
(
:user
)
}
let!
(
:
memb
er3
)
{
create
(
:user
)
}
let!
(
:
us
er1
)
{
create
(
:user
)
}
let!
(
:
us
er2
)
{
create
(
:user
)
}
let!
(
:
us
er3
)
{
create
(
:user
)
}
let!
(
:group1
)
{
create
(
:group
)
}
let!
(
:group2
)
{
create
(
:group
)
}
let!
(
:group1_
memb
er
)
{
create
(
:user
)
}
let!
(
:group2_
memb
er
)
{
create
(
:user
)
}
let!
(
:approval1
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
memb
er1
)
}
let!
(
:approval2
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
memb
er3
)
}
let!
(
:approval3
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
group1_
memb
er
)
}
let!
(
:approval4
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
group2_
memb
er
)
}
let!
(
:group1_
us
er
)
{
create
(
:user
)
}
let!
(
:group2_
us
er
)
{
create
(
:user
)
}
let!
(
:approval1
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
us
er1
)
}
let!
(
:approval2
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
us
er3
)
}
let!
(
:approval3
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
group1_
us
er
)
}
let!
(
:approval4
)
{
create
(
:approval
,
merge_request:
merge_request
,
user:
group2_
us
er
)
}
let!
(
:project_rule
)
{
create
(
:approval_project_rule
,
project:
project
,
name:
'foo'
,
approvals_required:
12
)
}
subject
{
described_class
.
new
(
merge_request
)
}
before
do
group1
.
add_guest
(
group1_
memb
er
)
group2
.
add_guest
(
group2_
memb
er
)
group1
.
add_guest
(
group1_
us
er
)
group2
.
add_guest
(
group2_
us
er
)
project_rule
.
users
=
[
member1
,
memb
er2
]
project_rule
.
users
=
[
user1
,
us
er2
]
project_rule
.
groups
<<
group1
end
...
...
@@ -38,7 +38,7 @@ describe ApprovalRules::FinalizeService do
end
end
context
'when
project rule is not overwritten
'
do
context
'when
there is no merge request rules
'
do
it_behaves_like
'skipping when unmerged'
context
'when merged'
do
...
...
@@ -57,18 +57,18 @@ describe ApprovalRules::FinalizeService do
expect
(
rule
.
name
).
to
eq
(
'foo'
)
expect
(
rule
.
approvals_required
).
to
eq
(
12
)
expect
(
rule
.
users
).
to
contain_exactly
(
member1
,
member2
,
group1_memb
er
)
expect
(
rule
.
users
).
to
contain_exactly
(
user1
,
user2
,
group1_us
er
)
expect
(
rule
.
groups
).
to
contain_exactly
(
group1
)
expect
(
rule
.
approved_approvers
).
to
contain_exactly
(
member1
,
group1_memb
er
)
expect
(
rule
.
approved_approvers
).
to
contain_exactly
(
user1
,
group1_us
er
)
end
end
end
context
'when
project rule is overwritten
'
do
context
'when
there is a regular merge request rule
'
do
before
do
rule
=
create
(
:approval_merge_request_rule
,
merge_request:
merge_request
,
name:
'bar'
,
approvals_required:
32
)
rule
.
users
=
[
member2
,
memb
er3
]
rule
.
users
=
[
user2
,
us
er3
]
rule
.
groups
<<
group2
end
...
...
@@ -78,7 +78,7 @@ describe ApprovalRules::FinalizeService do
let
(
:merge_request
)
{
create
(
:merged_merge_request
,
source_project:
project
,
target_project:
project
)
}
it
'does not copy project rules, and updates approval mapping with MR rules'
do
expect
(
merge_request
).
not_
to
receive
(
:copy_project_approval_rules
)
allow
(
subject
).
to
receive
(
:copy_project_approval_rules
)
expect
do
subject
.
execute
...
...
@@ -88,10 +88,11 @@ describe ApprovalRules::FinalizeService do
expect
(
rule
.
name
).
to
eq
(
'bar'
)
expect
(
rule
.
approvals_required
).
to
eq
(
32
)
expect
(
rule
.
users
).
to
contain_exactly
(
member2
,
member3
,
group2_memb
er
)
expect
(
rule
.
users
).
to
contain_exactly
(
user2
,
user3
,
group2_us
er
)
expect
(
rule
.
groups
).
to
contain_exactly
(
group2
)
expect
(
rule
.
approved_approvers
).
to
contain_exactly
(
member3
,
group2_member
)
expect
(
rule
.
approved_approvers
).
to
contain_exactly
(
user3
,
group2_user
)
expect
(
subject
).
not_to
have_received
(
:copy_project_approval_rules
)
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