BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
819fc98f
Commit
819fc98f
authored
Jan 03, 2018
by
Tiago Botelho
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Protected branch is now created for default branch on import
parent
1e950e31
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
89 additions
and
20 deletions
+89
-20
project.rb
app/models/project.rb
+22
-0
git_push_service.rb
app/services/git_push_service.rb
+1
-18
create_service.rb
app/services/protected_branches/create_service.rb
+2
-2
3968-protected-branch-is-not-set-for-default-branch-on-import.yml
...tected-branch-is-not-set-for-default-branch-on-import.yml
+5
-0
project_spec.rb
spec/models/project_spec.rb
+42
-0
create_service_spec.rb
spec/services/protected_branches/create_service_spec.rb
+16
-0
repository_import_worker_spec.rb
spec/workers/repository_import_worker_spec.rb
+1
-0
No files found.
app/models/project.rb
View file @
819fc98f
...
...
@@ -1459,6 +1459,7 @@ class Project < ActiveRecord::Base
import_finish
remove_import_jid
update_project_counter_caches
after_create_default_branch
end
def
update_project_counter_caches
...
...
@@ -1472,6 +1473,27 @@ class Project < ActiveRecord::Base
end
end
def
after_create_default_branch
return
unless
default_branch
# Ensure HEAD points to the default branch in case it is not master
change_head
(
default_branch
)
if
current_application_settings
.
default_branch_protection
!=
Gitlab
::
Access
::
PROTECTION_NONE
&&
!
ProtectedBranch
.
protected?
(
self
,
default_branch
)
params
=
{
name:
default_branch
,
push_access_levels_attributes:
[{
access_level:
current_application_settings
.
default_branch_protection
==
Gitlab
::
Access
::
PROTECTION_DEV_CAN_PUSH
?
Gitlab
::
Access
::
DEVELOPER
:
Gitlab
::
Access
::
MASTER
}],
merge_access_levels_attributes:
[{
access_level:
current_application_settings
.
default_branch_protection
==
Gitlab
::
Access
::
PROTECTION_DEV_CAN_MERGE
?
Gitlab
::
Access
::
DEVELOPER
:
Gitlab
::
Access
::
MASTER
}]
}
ProtectedBranches
::
CreateService
.
new
(
self
,
creator
,
params
).
execute
(
skip_authorization:
true
)
end
end
def
remove_import_jid
return
unless
import_jid
...
...
app/services/git_push_service.rb
View file @
819fc98f
...
...
@@ -154,24 +154,7 @@ class GitPushService < BaseService
offset
=
[
@push_commits_count
-
PROCESS_COMMIT_LIMIT
,
0
].
max
@push_commits
=
project
.
repository
.
commits
(
params
[
:newrev
],
offset:
offset
,
limit:
PROCESS_COMMIT_LIMIT
)
# Ensure HEAD points to the default branch in case it is not master
project
.
change_head
(
branch_name
)
# Set protection on the default branch if configured
if
current_application_settings
.
default_branch_protection
!=
PROTECTION_NONE
&&
!
ProtectedBranch
.
protected?
(
@project
,
@project
.
default_branch
)
params
=
{
name:
@project
.
default_branch
,
push_access_levels_attributes:
[{
access_level:
current_application_settings
.
default_branch_protection
==
PROTECTION_DEV_CAN_PUSH
?
Gitlab
::
Access
::
DEVELOPER
:
Gitlab
::
Access
::
MASTER
}],
merge_access_levels_attributes:
[{
access_level:
current_application_settings
.
default_branch_protection
==
PROTECTION_DEV_CAN_MERGE
?
Gitlab
::
Access
::
DEVELOPER
:
Gitlab
::
Access
::
MASTER
}]
}
ProtectedBranches
::
CreateService
.
new
(
@project
,
current_user
,
params
).
execute
end
@project
.
after_create_default_branch
end
def
build_push_data
...
...
app/services/protected_branches/create_service.rb
View file @
819fc98f
...
...
@@ -2,8 +2,8 @@ module ProtectedBranches
class
CreateService
<
BaseService
attr_reader
:protected_branch
def
execute
raise
Gitlab
::
Access
::
AccessDeniedError
unless
can?
(
current_user
,
:admin_project
,
project
)
def
execute
(
skip_authorization:
false
)
raise
Gitlab
::
Access
::
AccessDeniedError
unless
skip_authorization
||
can?
(
current_user
,
:admin_project
,
project
)
project
.
protected_branches
.
create
(
params
)
end
...
...
changelogs/unreleased/3968-protected-branch-is-not-set-for-default-branch-on-import.yml
0 → 100644
View file @
819fc98f
---
title
:
Protected branch is now created for default branch on import
merge_request
:
16198
author
:
type
:
fixed
spec/models/project_spec.rb
View file @
819fc98f
...
...
@@ -3072,9 +3072,51 @@ describe Project do
expect
(
project
).
to
receive
(
:import_finish
)
expect
(
project
).
to
receive
(
:update_project_counter_caches
)
expect
(
project
).
to
receive
(
:remove_import_jid
)
expect
(
project
).
to
receive
(
:after_create_default_branch
)
project
.
after_import
end
context
'branch protection'
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
it
'does not protect when branch protection is disabled'
do
stub_application_setting
(
default_branch_protection:
Gitlab
::
Access
::
PROTECTION_NONE
)
project
.
after_import
expect
(
project
.
protected_branches
).
to
be_empty
end
it
"gives developer access to push when branch protection is set to 'developers can push'"
do
stub_application_setting
(
default_branch_protection:
Gitlab
::
Access
::
PROTECTION_DEV_CAN_PUSH
)
project
.
after_import
expect
(
project
.
protected_branches
).
not_to
be_empty
expect
(
project
.
default_branch
).
to
eq
(
project
.
protected_branches
.
first
.
name
)
expect
(
project
.
protected_branches
.
first
.
push_access_levels
.
map
(
&
:access_level
)).
to
eq
([
Gitlab
::
Access
::
DEVELOPER
])
end
it
"gives developer access to merge when branch protection is set to 'developers can merge'"
do
stub_application_setting
(
default_branch_protection:
Gitlab
::
Access
::
PROTECTION_DEV_CAN_MERGE
)
project
.
after_import
expect
(
project
.
protected_branches
).
not_to
be_empty
expect
(
project
.
default_branch
).
to
eq
(
project
.
protected_branches
.
first
.
name
)
expect
(
project
.
protected_branches
.
first
.
merge_access_levels
.
map
(
&
:access_level
)).
to
eq
([
Gitlab
::
Access
::
DEVELOPER
])
end
it
'protects default branch'
do
project
.
after_import
expect
(
project
.
protected_branches
).
not_to
be_empty
expect
(
project
.
default_branch
).
to
eq
(
project
.
protected_branches
.
first
.
name
)
expect
(
project
.
protected_branches
.
first
.
push_access_levels
.
map
(
&
:access_level
)).
to
eq
([
Gitlab
::
Access
::
MASTER
])
expect
(
project
.
protected_branches
.
first
.
merge_access_levels
.
map
(
&
:access_level
)).
to
eq
([
Gitlab
::
Access
::
MASTER
])
end
end
end
describe
'#update_project_counter_caches'
do
...
...
spec/services/protected_branches/create_service_spec.rb
View file @
819fc98f
...
...
@@ -19,5 +19,21 @@ describe ProtectedBranches::CreateService do
expect
(
project
.
protected_branches
.
last
.
push_access_levels
.
map
(
&
:access_level
)).
to
eq
([
Gitlab
::
Access
::
MASTER
])
expect
(
project
.
protected_branches
.
last
.
merge_access_levels
.
map
(
&
:access_level
)).
to
eq
([
Gitlab
::
Access
::
MASTER
])
end
context
'when user does not have permission'
do
let
(
:user
)
{
create
(
:user
)
}
before
do
project
.
add_developer
(
user
)
end
it
'creates a new protected branch if we skip authorization step'
do
expect
{
service
.
execute
(
skip_authorization:
true
)
}.
to
change
(
ProtectedBranch
,
:count
).
by
(
1
)
end
it
'raises Gitlab::Access:AccessDeniedError'
do
expect
{
service
.
execute
}.
to
raise_error
(
Gitlab
::
Access
::
AccessDeniedError
)
end
end
end
end
spec/workers/repository_import_worker_spec.rb
View file @
819fc98f
...
...
@@ -32,6 +32,7 @@ describe RepositoryImportWorker do
expect_any_instance_of
(
Projects
::
ImportService
).
to
receive
(
:execute
)
.
and_return
({
status: :ok
})
expect_any_instance_of
(
Project
).
to
receive
(
:after_import
).
and_call_original
expect_any_instance_of
(
Repository
).
to
receive
(
:expire_emptiness_caches
)
expect_any_instance_of
(
Project
).
to
receive
(
:import_finish
)
...
...
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