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
Tatuya Kamada
gitlab-ce
Commits
69086634
Commit
69086634
authored
Feb 15, 2016
by
Rubén Dávila
Committed by
Robert Speicher
Feb 19, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor RevertService.
parent
d958d027
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
30 deletions
+24
-30
app/services/commits/revert_service.rb
app/services/commits/revert_service.rb
+24
-30
No files found.
app/services/commits/revert_service.rb
View file @
69086634
module
Commits
module
Commits
class
RevertService
<
::
BaseService
class
RevertService
<
::
BaseService
class
ValidationError
<
StandardError
;
end
class
ValidationError
<
StandardError
;
end
class
ReversionError
<
StandardError
;
end
def
execute
def
execute
@source_project
=
params
[
:source_project
]
||
@project
@source_project
=
params
[
:source_project
]
||
@project
...
@@ -8,61 +9,54 @@ module Commits
...
@@ -8,61 +9,54 @@ module Commits
@commit
=
params
[
:commit
]
@commit
=
params
[
:commit
]
@create_merge_request
=
params
[
:create_merge_request
].
present?
@create_merge_request
=
params
[
:create_merge_request
].
present?
# Check push permissions to branch
validate
and
commit
validate
rescue
Repository
::
CommitError
,
Gitlab
::
Git
::
Repository
::
InvalidBlobName
,
GitHooksService
::
PreReceiveError
,
ValidationError
,
ReversionError
=>
ex
if
commit
success
else
custom_error
end
rescue
Repository
::
CommitError
,
Gitlab
::
Git
::
Repository
::
InvalidBlobName
,
GitHooksService
::
PreReceiveError
,
ValidationError
=>
ex
error
(
ex
.
message
)
error
(
ex
.
message
)
end
end
def
commit
def
commit
revert_into
=
@create_merge_request
?
@commit
.
revert_branch_name
:
@target_branch
if
@create_merge_request
if
@create_merge_request
# Temporary branch exists and contains the revert commit
# Temporary branch exists and contains the revert commit
return
true
if
repository
.
find_branch
(
@commit
.
revert_branch_name
)
return
success
if
repository
.
find_branch
(
revert_into
)
return
false
unless
create_target_branch
repository
.
revert
(
current_user
,
@commit
,
@commit
.
revert_branch_name
)
create_target_branch
else
repository
.
revert
(
current_user
,
@commit
,
@target_branch
)
end
end
end
private
def
custom_error
unless
repository
.
revert
(
current_user
,
@commit
,
revert_into
)
if
@branch_error_msg
error_msg
=
"Sorry, we cannot revert this
#{
params
[
:revert_type_title
]
}
automatically.
error
(
"There was an error creating the source branch:
#{
@branch_error_msg
}
"
)
It may have already been reverted, or a more recent commit may have updated some of its content."
else
raise_error
(
ReversionError
,
error_msg
)
error
(
"Sorry, we cannot revert this
#{
params
[
:revert_type_title
]
}
automatically.
It may have already been reverted, or a more recent commit may
have updated some of its content."
)
end
end
success
end
end
private
def
create_target_branch
def
create_target_branch
result
=
CreateBranchService
.
new
(
@project
,
current_user
)
result
=
CreateBranchService
.
new
(
@project
,
current_user
)
.
execute
(
@commit
.
revert_branch_name
,
@target_branch
,
source_project:
@source_project
)
.
execute
(
@commit
.
revert_branch_name
,
@target_branch
,
source_project:
@source_project
)
@branch_error_msg
=
result
[
:message
]
if
result
[
:status
]
==
:error
raise_error
(
ReversionError
,
"There was an error creating the source branch:
#{
result
[
:message
]
}
"
)
result
[
:status
]
!=
:error
end
end
end
def
raise_error
(
message
)
def
raise_error
(
klass
,
message
)
raise
ValidationError
.
new
(
message
)
raise
klass
.
new
(
message
)
end
end
def
validate
def
validate
allowed
=
::
Gitlab
::
GitAccess
.
new
(
current_user
,
project
).
can_push_to_branch?
(
@target_branch
)
allowed
=
::
Gitlab
::
GitAccess
.
new
(
current_user
,
project
).
can_push_to_branch?
(
@target_branch
)
unless
allowed
unless
allowed
raise_error
(
"You are not allowed to push into this branch"
)
raise_error
(
ValidationError
,
"You are not allowed to push into this branch"
)
end
end
true
end
end
end
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