Read about our upcoming Code of Conduct on this issue

projects_controller.rb 11.7 KB
Newer Older
Douwe Maan's avatar
Douwe Maan committed
1
class ProjectsController < Projects::ApplicationController
2
  include IssuableCollections
3
  include ExtractsPath
4
  include PreviewMarkdown
5
  include SendFileUpload
6

7
  before_action :whitelist_query_limiting, only: [:create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
8
  before_action :authenticate_user!, except: [:index, :show, :activity, :refs]
9
  before_action :redirect_git_extension, only: [:show]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10
11
  before_action :project, except: [:index, :new, :create]
  before_action :repository, except: [:index, :new, :create]
12
  before_action :assign_ref_vars, only: [:show], if: :repo_exists?
winniehell's avatar
winniehell committed
13
  before_action :tree, only: [:show], if: [:repo_exists?, :project_view_files?]
14
  before_action :lfs_blob_ids, only: [:show], if: [:repo_exists?, :project_view_files?]
15
  before_action :project_export_enabled, only: [:export, :download_export, :remove_export, :generate_new_export]
gitlabhq's avatar
gitlabhq committed
16
17

  # Authorize
18
  before_action :authorize_admin_project!, only: [:edit, :update, :housekeeping, :download_export, :export, :remove_export, :generate_new_export]
19
  before_action :event_filter, only: [:show, :activity]
gitlabhq's avatar
gitlabhq committed
20

Douwe Maan's avatar
Douwe Maan committed
21
  layout :determine_layout
22

23
  def index
24
    redirect_to(current_user ? root_path : explore_root_path)
25
26
  end

gitlabhq's avatar
gitlabhq committed
27
  def new
28
29
30
31
    namespace = Namespace.find_by(id: params[:namespace_id]) if params[:namespace_id]
    return access_denied! if namespace && !can?(current_user, :create_projects, namespace)

    @project = Project.new(namespace_id: namespace&.id)
gitlabhq's avatar
gitlabhq committed
32
33
34
  end

  def edit
Douwe Maan's avatar
Douwe Maan committed
35
    render 'edit'
gitlabhq's avatar
gitlabhq committed
36
37
38
  end

  def create
39
    @project = ::Projects::CreateService.new(current_user, project_params).execute
gitlabhq's avatar
gitlabhq committed
40

41
    if @project.saved?
42
43
      cookies[:issue_board_welcome_hidden] = { path: project_path(@project), value: nil, expires: Time.at(0) }

Vinnie Okada's avatar
Vinnie Okada committed
44
      redirect_to(
45
        project_path(@project, custom_import_params),
46
        notice: _("Project '%{project_name}' was successfully created.") % { project_name: @project.name }
Vinnie Okada's avatar
Vinnie Okada committed
47
      )
48
    else
Eric Eastwood's avatar
Eric Eastwood committed
49
      render 'new', locals: { active_tab: active_new_project_tab }
gitlabhq's avatar
gitlabhq committed
50
51
    end
  end
gitlabhq's avatar
gitlabhq committed
52

gitlabhq's avatar
gitlabhq committed
53
  def update
54
    result = ::Projects::UpdateService.new(@project, current_user, project_params).execute
55

56
    # Refresh the repo in case anything changed
57
    @repository = @project.repository
58

gitlabhq's avatar
gitlabhq committed
59
    respond_to do |format|
60
      if result[:status] == :success
61
        flash[:notice] = _("Project '%{project_name}' was successfully updated.") % { project_name: @project.name }
62

Vinnie Okada's avatar
Vinnie Okada committed
63
        format.html do
64
          redirect_to(edit_project_path(@project))
Vinnie Okada's avatar
Vinnie Okada committed
65
        end
gitlabhq's avatar
gitlabhq committed
66
      else
67
        flash.now[:alert] = result[:message]
68

Douwe Maan's avatar
Douwe Maan committed
69
        format.html { render 'edit' }
gitlabhq's avatar
gitlabhq committed
70
      end
71
72

      format.js
gitlabhq's avatar
gitlabhq committed
73
    end
74
  end
75

76
  def transfer
77
78
    return access_denied! unless can?(current_user, :change_namespace, @project)

79
80
81
82
83
    namespace = Namespace.find_by(id: params[:new_namespace_id])
    ::Projects::TransferService.new(project, current_user).execute(namespace)

    if @project.errors[:new_namespace].present?
      flash[:alert] = @project.errors[:new_namespace].first
skv-headless's avatar
skv-headless committed
84
    end
gitlabhq's avatar
gitlabhq committed
85
86
  end

87
  def remove_fork
88
89
    return access_denied! unless can?(current_user, :remove_fork_project, @project)

Baldinof's avatar
Baldinof committed
90
    if ::Projects::UnlinkForkService.new(@project, current_user).execute
91
      flash[:notice] = _('The fork relationship has been removed.')
92
93
94
    end
  end

95
96
97
98
99
100
101
102
103
104
  def activity
    respond_to do |format|
      format.html
      format.json do
        load_events
        pager_json('events/_events', @events.count)
      end
    end
  end

gitlabhq's avatar
gitlabhq committed
105
  def show
106
    if @project.import_in_progress?
107
      redirect_to project_import_path(@project, custom_import_params)
108
109
110
      return
    end

111
    if @project.pending_delete?
112
      flash.now[:alert] = _("Project '%{project_name}' queued for deletion.") % { project_name: @project.name }
113
114
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
115
    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
116
      format.html do
117
        @notification_setting = current_user.notification_settings_for(@project) if current_user
118
119
        @project = @project.present(current_user: current_user)

120
        render_landing_page
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
121
      end
122

123
124
      format.atom do
        load_events
125
        render layout: 'xml.atom'
126
      end
127
128
129
    end
  end

gitlabhq's avatar
gitlabhq committed
130
  def destroy
131
    return access_denied! unless can?(current_user, :remove_project, @project)
132

Stan Hu's avatar
Stan Hu committed
133
    ::Projects::DestroyService.new(@project, current_user, {}).async_execute
134
    flash[:notice] = _("Project '%{project_name}' is in the process of being deleted.") % { project_name: @project.full_name }
gitlabhq's avatar
gitlabhq committed
135

136
    redirect_to dashboard_projects_path, status: 302
137
  rescue Projects::DestroyService::DestroyError => ex
138
    redirect_to edit_project_path(@project), status: 302, alert: ex.message
gitlabhq's avatar
gitlabhq committed
139
  end
140

141
  def new_issuable_address
142
143
144
    return render_404 unless Gitlab::IncomingEmail.supports_issue_creation?

    current_user.reset_incoming_email_token!
145
    render json: { new_address: @project.new_issuable_address(current_user, params[:issuable_type]) }
146
147
  end

148
  def archive
149
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed
150

151
    @project.archive!
152
153

    respond_to do |format|
154
      format.html { redirect_to project_path(@project) }
155
156
157
158
    end
  end

  def unarchive
159
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed
160

161
    @project.unarchive!
162
163

    respond_to do |format|
164
      format.html { redirect_to project_path(@project) }
165
166
    end
  end
167
168

  def housekeeping
169
170
171
172
    ::Projects::HousekeepingService.new(@project).execute

    redirect_to(
      project_path(@project),
173
      notice: _("Housekeeping successfully started")
174
175
176
177
178
179
    )
  rescue ::Projects::HousekeepingService::LeaseTaken => ex
    redirect_to(
      edit_project_path(@project),
      alert: ex.to_s
    )
180
  end
181

182
  def export
183
    @project.add_export_job(current_user: current_user)
184
185

    redirect_to(
James Lopez's avatar
James Lopez committed
186
      edit_project_path(@project),
187
      notice: _("Project export started. A download link will be sent by email.")
188
189
190
    )
  end

James Lopez's avatar
James Lopez committed
191
  def download_export
192
193
194
    if export_project_object_storage?
      send_upload(@project.import_export_upload.export_file)
    elsif export_project_path
James Lopez's avatar
James Lopez committed
195
196
      send_file export_project_path, disposition: 'attachment'
    else
197
198
      redirect_to(
        edit_project_path(@project),
199
        alert: _("Project export link has expired. Please generate a new export from your project settings.")
200
201
202
203
204
205
      )
    end
  end

  def remove_export
    if @project.remove_exports
206
      flash[:notice] = _("Project export has been deleted.")
207
    else
208
      flash[:alert] = _("Project export could not be deleted.")
209
    end
210

211
212
213
214
215
216
    redirect_to(edit_project_path(@project))
  end

  def generate_new_export
    if @project.remove_exports
      export
217
218
219
    else
      redirect_to(
        edit_project_path(@project),
220
        alert: _("Project export could not be deleted.")
221
      )
James Lopez's avatar
James Lopez committed
222
    end
James Lopez's avatar
James Lopez committed
223
224
  end

Ciro Santilli's avatar
Ciro Santilli committed
225
226
  def toggle_star
    current_user.toggle_star(@project)
227
    @project.reload
228
229

    render json: {
230
      star_count: @project.star_count
231
    }
Ciro Santilli's avatar
Ciro Santilli committed
232
233
  end

234
  def refs
235
236
237
238
239
    find_refs = params['find']

    find_branches = true
    find_tags = true
    find_commits = true
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
240
241

    unless find_refs.nil?
Douwe Maan's avatar
Douwe Maan committed
242
243
244
      find_branches = find_refs.include?('branches')
      find_tags = find_refs.include?('tags')
      find_commits = find_refs.include?('commits')
245
    end
246

247
248
249
    options = {}

    if find_branches
Douwe Maan's avatar
Douwe Maan committed
250
      branches = BranchesFinder.new(@repository, params).execute.take(100).map(&:name)
251
      options['Branches'] = branches
252
    end
253

Douwe Maan's avatar
Douwe Maan committed
254
255
    if find_tags && @repository.tag_count.nonzero?
      tags = TagsFinder.new(@repository, params).execute.take(100).map(&:name)
256

257
      options['Tags'] = tags
Phil Hughes's avatar
Phil Hughes committed
258
259
    end

260
    # If reference is commit id - we should add it to branch/tag selectbox
261
    ref = Addressable::URI.unescape(params[:ref])
Douwe Maan's avatar
Douwe Maan committed
262
    if find_commits && ref && options.flatten(2).exclude?(ref) && ref =~ /\A[0-9a-zA-Z]{6,52}\z/
263
      options['Commits'] = [ref]
264
265
266
267
268
    end

    render json: options.to_json
  end

269
270
271
272
273
  # Render project landing depending of which features are available
  # So if page is not availble in the list it renders the next page
  #
  # pages list order: repository readme, wiki home, issues list, customize workflow
  def render_landing_page
274
    if can?(current_user, :download_code, @project)
275
      return render 'projects/no_repo' unless @project.repository_exists?
276

277
278
      render 'projects/empty' if @project.empty_repo?
    else
279
      if can?(current_user, :read_wiki, @project)
280
281
        @project_wiki = @project.wiki
        @wiki_home = @project_wiki.find_page('home', params[:version_id])
282
      elsif @project.feature_available?(:issues, current_user)
283
        @issues = issuables_collection.page(params[:page])
284
285
        @collection_type = 'Issue'
        @issuable_meta_data = issuable_meta_data(@issues, @collection_type)
286
287
288
289
290
291
      end

      render :show
    end
  end

292
293
294
295
  def finder_type
    IssuesFinder
  end

Douwe Maan's avatar
Douwe Maan committed
296
297
298
299
300
301
302
303
  def determine_layout
    if [:new, :create].include?(action_name.to_sym)
      'application'
    elsif [:edit, :update].include?(action_name.to_sym)
      'project_settings'
    else
      'project'
    end
304
  end
305

306
  def load_events
307
308
309
310
311
    projects = Project.where(id: @project.id)

    @events = EventCollection
      .new(projects, offset: params[:offset].to_i, filter: event_filter)
      .to_a
312
313

    Events::RenderService.new(current_user).execute(@events, atom_request: request.format.atom?)
314
315
  end

316
  def project_params
317
    params.require(:project)
318
      .permit(project_params_attributes)
319
  end
Felipe Artur's avatar
Felipe Artur committed
320

321
  def project_params_attributes
322
323
324
325
    [
      :avatar,
      :build_allow_git_fetch,
      :build_coverage_regex,
326
      :build_timeout_human_readable,
327
      :resolve_outdated_diff_discussions,
Felipe Artur's avatar
Felipe Artur committed
328
      :container_registry_enabled,
329
330
331
332
333
334
335
336
337
      :default_branch,
      :description,
      :import_url,
      :issues_tracker,
      :issues_tracker_id,
      :last_activity_at,
      :lfs_enabled,
      :name,
      :namespace_id,
338
      :only_allow_merge_if_all_discussions_are_resolved,
339
      :only_allow_merge_if_pipeline_succeeds,
340
      :printing_merge_request_link_enabled,
341
342
343
344
345
346
      :path,
      :public_builds,
      :request_access_enabled,
      :runners_token,
      :tag_list,
      :visibility_level,
347
      :template_name,
348
      :merge_method,
349
      :initialize_with_readme,
350
351
352
353
354
355
356
357
358
359

      project_feature_attributes: %i[
        builds_access_level
        issues_access_level
        merge_requests_access_level
        repository_access_level
        snippets_access_level
        wiki_access_level
      ]
    ]
360
  end
361

362
363
364
365
  def custom_import_params
    {}
  end

Eric Eastwood's avatar
Eric Eastwood committed
366
367
368
369
  def active_new_project_tab
    project_params[:import_url].present? ? 'import' : 'blank'
  end

370
  def repo_exists?
Jacob Vosmaer's avatar
Jacob Vosmaer committed
371
    project.repository_exists? && !project.empty_repo?
372
373
374
375
376

  rescue Gitlab::Git::Repository::NoRepository
    project.repository.expire_exists_cache

    false
377
378
  end

379
  def project_view_files?
380
381
382
383
384
    if current_user
      current_user.project_view == 'files'
    else
      project_view_files_allowed?
    end
385
386
  end

387
  # Override extract_ref from ExtractsPath, which returns the branch and file path
Douwe Maan's avatar
Douwe Maan committed
388
  # for the blob/tree, which in this case is just the root of the default branch.
389
390
391
392
393
394
  # This way we avoid to access the repository.ref_names.
  def extract_ref(_id)
    [get_id, '']
  end

  # Override get_id from ExtractsPath in this case is just the root of the default branch.
395
396
397
  def get_id
    project.repository.root_ref
  end
398
399
400
401

  def project_view_files_allowed?
    !project.empty_repo? && can?(current_user, :download_code, project)
  end
402
403
404
405
406

  def build_canonical_path(project)
    params[:namespace_id] = project.namespace.to_param
    params[:id] = project.to_param

blackst0ne's avatar
blackst0ne committed
407
    url_for(safe_params)
408
  end
409
410

  def project_export_enabled
411
    render_404 unless Gitlab::CurrentSettings.project_export_enabled?
412
  end
413
414
415
416
417
418
419

  def redirect_git_extension
    # Redirect from
    #   localhost/group/project.git
    # to
    #   localhost/group/project
    #
420
    redirect_to request.original_url.sub(%r{\.git/?\Z}, '') if params[:format] == 'git'
421
  end
422
423
424
425

  def whitelist_query_limiting
    Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42440')
  end
426
427
428
429
430
431
432
433

  def export_project_path
    @export_project_path ||= @project.export_project_path
  end

  def export_project_object_storage?
    @project.export_project_object_exists?
  end
gitlabhq's avatar
gitlabhq committed
434
end