1
1
# Controller for viewing a file's blame
2
2
class Projects ::BlobController < Projects ::ApplicationController
3
3
include ExtractsPath
4
- include CreatesMergeRequestForCommit
4
+ include CreatesCommit
5
5
include ActionView ::Helpers ::SanitizeHelper
6
6
7
7
# Raised when given an invalid file path
8
8
class InvalidPathError < StandardError ; end
9
9
10
10
before_action :require_non_empty_project , except : [ :new , :create ]
11
11
before_action :authorize_download_code!
12
- before_action :authorize_push_code ! , only : [ :destroy , :create ]
12
+ before_action :authorize_edit_tree ! , only : [ :new , :create , :edit , :update , :destroy ]
13
13
before_action :assign_blob_vars
14
14
before_action :commit , except : [ :new , :create ]
15
15
before_action :blob , except : [ :new , :create ]
16
16
before_action :from_merge_request , only : [ :edit , :update ]
17
17
before_action :require_branch_head , only : [ :edit , :update ]
18
18
before_action :editor_variables , except : [ :show , :preview , :diff ]
19
- before_action :after_edit_path , only : [ :edit , :update ]
20
19
21
20
def new
22
21
commit unless @repository . empty?
23
22
end
24
23
25
24
def create
26
- create_commit ( Files ::CreateService , success_path : after_create_path ,
25
+ create_commit ( Files ::CreateService , success_notice : "The file has been successfully created." ,
26
+ success_path : namespace_project_blob_path ( @project . namespace , @project , File . join ( @target_branch , @file_path ) ) ,
27
27
failure_view : :new ,
28
28
failure_path : namespace_project_new_blob_path ( @project . namespace , @project , @ref ) )
29
29
end
@@ -36,6 +36,14 @@ def edit
36
36
end
37
37
38
38
def update
39
+ after_edit_path =
40
+ if from_merge_request && @target_branch == @ref
41
+ diffs_namespace_project_merge_request_path ( from_merge_request . target_project . namespace , from_merge_request . target_project , from_merge_request ) +
42
+ "#file-path-#{ hexdigest ( @path ) } "
43
+ else
44
+ namespace_project_blob_path ( @project . namespace , @project , File . join ( @target_branch , @path ) )
45
+ end
46
+
39
47
create_commit ( Files ::UpdateService , success_path : after_edit_path ,
40
48
failure_view : :edit ,
41
49
failure_path : namespace_project_blob_path ( @project . namespace , @project , @id ) )
@@ -50,15 +58,10 @@ def preview
50
58
end
51
59
52
60
def destroy
53
- result = Files ::DeleteService . new ( @project , current_user , @commit_params ) . execute
54
-
55
- if result [ :status ] == :success
56
- flash [ :notice ] = "Your changes have been successfully committed"
57
- redirect_to after_destroy_path
58
- else
59
- flash [ :alert ] = result [ :message ]
60
- render :show
61
- end
61
+ create_commit ( Files ::DeleteService , success_notice : "The file has been successfully deleted." ,
62
+ success_path : namespace_project_tree_path ( @project . namespace , @project , @target_branch ) ,
63
+ failure_view : :show ,
64
+ failure_path : namespace_project_blob_path ( @project . namespace , @project , @id ) )
62
65
end
63
66
64
67
def diff
@@ -108,74 +111,13 @@ def assign_blob_vars
108
111
render_404
109
112
end
110
113
111
- def create_commit ( service , success_path :, failure_view :, failure_path :)
112
- result = service . new ( @project , current_user , @commit_params ) . execute
113
-
114
- if result [ :status ] == :success
115
- flash [ :notice ] = "Your changes have been successfully committed"
116
- respond_to do |format |
117
- format . html { redirect_to success_path }
118
- format . json { render json : { message : "success" , filePath : success_path } }
119
- end
120
- else
121
- flash [ :alert ] = result [ :message ]
122
- respond_to do |format |
123
- format . html { render failure_view }
124
- format . json { render json : { message : "failed" , filePath : failure_path } }
125
- end
126
- end
127
- end
128
-
129
- def after_create_path
130
- @after_create_path ||=
131
- if create_merge_request?
132
- new_merge_request_path
133
- else
134
- namespace_project_blob_path ( @project . namespace , @project , File . join ( @new_branch , @file_path ) )
135
- end
136
- end
137
-
138
- def after_edit_path
139
- @after_edit_path ||=
140
- if create_merge_request?
141
- new_merge_request_path
142
- elsif from_merge_request && @new_branch == @ref
143
- diffs_namespace_project_merge_request_path ( from_merge_request . target_project . namespace , from_merge_request . target_project , from_merge_request ) +
144
- "#file-path-#{ hexdigest ( @path ) } "
145
- else
146
- namespace_project_blob_path ( @project . namespace , @project , File . join ( @new_branch , @path ) )
147
- end
148
- end
149
-
150
- def after_destroy_path
151
- @after_destroy_path ||=
152
- if create_merge_request?
153
- new_merge_request_path
154
- else
155
- namespace_project_tree_path ( @project . namespace , @project , @new_branch )
156
- end
157
- end
158
-
159
114
def from_merge_request
160
115
# If blob edit was initiated from merge request page
161
116
@from_merge_request ||= MergeRequest . find_by ( id : params [ :from_merge_request_id ] )
162
117
end
163
118
164
- def sanitized_new_branch_name
165
- sanitize ( strip_tags ( params [ :new_branch ] ) )
166
- end
167
-
168
119
def editor_variables
169
- @current_branch = @ref
170
-
171
- @new_branch =
172
- if params [ :new_branch ] . present?
173
- sanitized_new_branch_name
174
- elsif ::Gitlab ::GitAccess . new ( current_user , @project ) . can_push_to_branch? ( @ref )
175
- @ref
176
- else
177
- @repository . next_patch_branch
178
- end
120
+ @target_branch = params [ :target_branch ]
179
121
180
122
@file_path =
181
123
if action_name . to_s == 'create'
@@ -194,8 +136,6 @@ def editor_variables
194
136
195
137
@commit_params = {
196
138
file_path : @file_path ,
197
- current_branch : @current_branch ,
198
- target_branch : @new_branch ,
199
139
commit_message : params [ :commit_message ] ,
200
140
file_content : params [ :content ] ,
201
141
file_content_encoding : params [ :encoding ]
0 commit comments