This instance was upgraded to Heptapod 0.31.0 (final) today.

projects_controller.rb 12.5 KB
Newer Older
1
2
# frozen_string_literal: true

Douwe Maan's avatar
Douwe Maan committed
3
class ProjectsController < Projects::ApplicationController
4
  include API::Helpers::RelatedResourcesHelpers
5
  include IssuableCollections
6
  include ExtractsPath
7
  include PreviewMarkdown
8
  include SendFileUpload
9

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

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

Douwe Maan's avatar
Douwe Maan committed
25
  layout :determine_layout
26

27
  def index
28
    redirect_to(current_user ? root_path : explore_root_path)
29
30
  end

31
  # rubocop: disable CodeReuse/ActiveRecord
gitlabhq's avatar
gitlabhq committed
32
  def new
33
34
35
36
    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
37
  end
38
  # rubocop: enable CodeReuse/ActiveRecord
gitlabhq's avatar
gitlabhq committed
39
40

  def edit
41
    @badge_api_endpoint = expose_url(api_v4_projects_badges_path(id: @project.id))
Douwe Maan's avatar
Douwe Maan committed
42
    render 'edit'
gitlabhq's avatar
gitlabhq committed
43
44
45
  end

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

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

Vinnie Okada's avatar
Vinnie Okada committed
51
      redirect_to(
52
        project_path(@project, custom_import_params),
53
        notice: _("Project '%{project_name}' was successfully created.") % { project_name: @project.name }
Vinnie Okada's avatar
Vinnie Okada committed
54
      )
55
    else
Eric Eastwood's avatar
Eric Eastwood committed
56
      render 'new', locals: { active_tab: active_new_project_tab }
gitlabhq's avatar
gitlabhq committed
57
58
    end
  end
gitlabhq's avatar
gitlabhq committed
59

gitlabhq's avatar
gitlabhq committed
60
  def update
61
    result = ::Projects::UpdateService.new(@project, current_user, project_params).execute
62

63
    # Refresh the repo in case anything changed
64
    @repository = @project.repository
65

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

Vinnie Okada's avatar
Vinnie Okada committed
70
        format.html do
71
          redirect_to(edit_project_path(@project, anchor: 'js-general-project-settings'))
Vinnie Okada's avatar
Vinnie Okada committed
72
        end
gitlabhq's avatar
gitlabhq committed
73
      else
74
        flash.now[:alert] = result[:message]
75

Douwe Maan's avatar
Douwe Maan committed
76
        format.html { render 'edit' }
gitlabhq's avatar
gitlabhq committed
77
      end
78
79

      format.js
gitlabhq's avatar
gitlabhq committed
80
    end
81
  end
82

83
  # rubocop: disable CodeReuse/ActiveRecord
84
  def transfer
85
86
    return access_denied! unless can?(current_user, :change_namespace, @project)

87
88
89
90
91
    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
92
    end
gitlabhq's avatar
gitlabhq committed
93
  end
94
  # rubocop: enable CodeReuse/ActiveRecord
gitlabhq's avatar
gitlabhq committed
95

96
  def remove_fork
97
98
    return access_denied! unless can?(current_user, :remove_fork_project, @project)

Baldinof's avatar
Baldinof committed
99
    if ::Projects::UnlinkForkService.new(@project, current_user).execute
100
      flash[:notice] = _('The fork relationship has been removed.')
101
102
103
    end
  end

104
105
106
107
108
109
110
111
112
113
  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
114
  def show
115
    if @project.import_in_progress?
116
      redirect_to project_import_path(@project, custom_import_params)
117
118
119
      return
    end

120
    if @project.pending_delete?
121
      flash.now[:alert] = _("Project '%{project_name}' queued for deletion.") % { project_name: @project.name }
122
123
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
124
    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
125
      format.html do
126
        @notification_setting = current_user.notification_settings_for(@project) if current_user
127
128
        @project = @project.present(current_user: current_user)

129
        render_landing_page
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
130
      end
131

132
133
      format.atom do
        load_events
134
        render layout: 'xml.atom'
135
      end
136
137
138
    end
  end

gitlabhq's avatar
gitlabhq committed
139
  def destroy
140
    return access_denied! unless can?(current_user, :remove_project, @project)
141

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

Lin Jen-Shin's avatar
Lin Jen-Shin committed
145
    redirect_to dashboard_projects_path, status: :found
146
  rescue Projects::DestroyService::DestroyError => ex
147
    redirect_to edit_project_path(@project), status: 302, alert: ex.message
gitlabhq's avatar
gitlabhq committed
148
  end
149

150
  def new_issuable_address
151
152
153
    return render_404 unless Gitlab::IncomingEmail.supports_issue_creation?

    current_user.reset_incoming_email_token!
154
    render json: { new_address: @project.new_issuable_address(current_user, params[:issuable_type]) }
155
156
  end

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

160
    ::Projects::UpdateService.new(@project, current_user, archived: true).execute
161
162

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

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

170
    ::Projects::UpdateService.new(@project, current_user, archived: false).execute
171
172

    respond_to do |format|
173
      format.html { redirect_to project_path(@project) }
174
175
    end
  end
176
177

  def housekeeping
178
179
180
181
    ::Projects::HousekeepingService.new(@project).execute

    redirect_to(
      project_path(@project),
182
      notice: _("Housekeeping successfully started")
183
184
185
    )
  rescue ::Projects::HousekeepingService::LeaseTaken => ex
    redirect_to(
186
      edit_project_path(@project, anchor: 'js-project-advanced-settings'),
187
188
      alert: ex.to_s
    )
189
  end
190

191
  def export
192
    @project.add_export_job(current_user: current_user)
193
194

    redirect_to(
195
      edit_project_path(@project, anchor: 'js-export-project'),
196
      notice: _("Project export started. A download link will be sent by email.")
197
198
199
    )
  end

James Lopez's avatar
James Lopez committed
200
  def download_export
James Lopez's avatar
James Lopez committed
201
    if @project.export_file_exists?
202
      send_upload(@project.export_file, attachment: @project.export_file.filename)
James Lopez's avatar
James Lopez committed
203
    else
204
      redirect_to(
205
        edit_project_path(@project, anchor: 'js-export-project'),
206
        alert: _("Project export link has expired. Please generate a new export from your project settings.")
207
208
209
210
211
212
      )
    end
  end

  def remove_export
    if @project.remove_exports
213
      flash[:notice] = _("Project export has been deleted.")
214
    else
215
      flash[:alert] = _("Project export could not be deleted.")
216
    end
217

218
    redirect_to(edit_project_path(@project, anchor: 'js-export-project'))
219
220
221
222
223
  end

  def generate_new_export
    if @project.remove_exports
      export
224
225
    else
      redirect_to(
226
        edit_project_path(@project, anchor: 'js-export-project'),
227
        alert: _("Project export could not be deleted.")
228
      )
James Lopez's avatar
James Lopez committed
229
    end
James Lopez's avatar
James Lopez committed
230
231
  end

Ciro Santilli's avatar
Ciro Santilli committed
232
233
  def toggle_star
    current_user.toggle_star(@project)
234
    @project.reload
235
236

    render json: {
237
      star_count: @project.star_count
238
    }
Ciro Santilli's avatar
Ciro Santilli committed
239
240
  end

241
  # rubocop: disable CodeReuse/ActiveRecord
242
  def refs
243
244
245
246
247
    find_refs = params['find']

    find_branches = true
    find_tags = true
    find_commits = true
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
248
249

    unless find_refs.nil?
Douwe Maan's avatar
Douwe Maan committed
250
251
252
      find_branches = find_refs.include?('branches')
      find_tags = find_refs.include?('tags')
      find_commits = find_refs.include?('commits')
253
    end
254

255
256
257
    options = {}

    if find_branches
Douwe Maan's avatar
Douwe Maan committed
258
      branches = BranchesFinder.new(@repository, params).execute.take(100).map(&:name)
259
      options['Branches'] = branches
260
    end
261

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

265
      options['Tags'] = tags
Phil Hughes's avatar
Phil Hughes committed
266
267
    end

268
    # If reference is commit id - we should add it to branch/tag selectbox
269
    ref = Addressable::URI.unescape(params[:ref])
Douwe Maan's avatar
Douwe Maan committed
270
    if find_commits && ref && options.flatten(2).exclude?(ref) && ref =~ /\A[0-9a-zA-Z]{6,52}\z/
271
      options['Commits'] = [ref]
272
273
274
275
    end

    render json: options.to_json
  end
276
  # rubocop: enable CodeReuse/ActiveRecord
277

278
  # Render project landing depending of which features are available
279
  # So if page is not available in the list it renders the next page
280
281
282
  #
  # pages list order: repository readme, wiki home, issues list, customize workflow
  def render_landing_page
283
    if can?(current_user, :download_code, @project)
284
      return render 'projects/no_repo' unless @project.repository_exists?
285

286
287
      render 'projects/empty' if @project.empty_repo?
    else
288
      if can?(current_user, :read_wiki, @project)
289
290
        @project_wiki = @project.wiki
        @wiki_home = @project_wiki.find_page('home', params[:version_id])
291
      elsif @project.feature_available?(:issues, current_user)
292
        @issues = issuables_collection.page(params[:page])
293
294
        @collection_type = 'Issue'
        @issuable_meta_data = issuable_meta_data(@issues, @collection_type)
295
296
297
298
299
300
      end

      render :show
    end
  end

301
302
303
304
  def finder_type
    IssuesFinder
  end

Douwe Maan's avatar
Douwe Maan committed
305
306
307
308
309
310
311
312
  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
313
  end
314

315
  # rubocop: disable CodeReuse/ActiveRecord
316
  def load_events
317
318
319
320
321
    projects = Project.where(id: @project.id)

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

    Events::RenderService.new(current_user).execute(@events, atom_request: request.format.atom?)
324
  end
325
  # rubocop: enable CodeReuse/ActiveRecord
326

327
  def project_params
328
    params.require(:project)
329
      .permit(project_params_attributes)
330
  end
Felipe Artur's avatar
Felipe Artur committed
331

332
  def project_params_attributes
333
334
335
336
    [
      :avatar,
      :build_allow_git_fetch,
      :build_coverage_regex,
337
      :build_timeout_human_readable,
338
      :resolve_outdated_diff_discussions,
Felipe Artur's avatar
Felipe Artur committed
339
      :container_registry_enabled,
340
341
342
343
344
345
346
347
348
      :default_branch,
      :description,
      :import_url,
      :issues_tracker,
      :issues_tracker_id,
      :last_activity_at,
      :lfs_enabled,
      :name,
      :namespace_id,
349
      :only_allow_merge_if_all_discussions_are_resolved,
350
      :only_allow_merge_if_pipeline_succeeds,
351
      :printing_merge_request_link_enabled,
352
353
354
355
356
357
      :path,
      :public_builds,
      :request_access_enabled,
      :runners_token,
      :tag_list,
      :visibility_level,
358
      :template_name,
359
      :merge_method,
360
      :initialize_with_readme,
361
362
363
364
365
366
367
368

      project_feature_attributes: %i[
        builds_access_level
        issues_access_level
        merge_requests_access_level
        repository_access_level
        snippets_access_level
        wiki_access_level
369
        pages_access_level
370
371
      ]
    ]
372
  end
373

374
375
376
377
  def custom_import_params
    {}
  end

Eric Eastwood's avatar
Eric Eastwood committed
378
379
380
381
  def active_new_project_tab
    project_params[:import_url].present? ? 'import' : 'blank'
  end

382
  def repo_exists?
Jacob Vosmaer's avatar
Jacob Vosmaer committed
383
    project.repository_exists? && !project.empty_repo?
384
385
386
387
388

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

    false
389
390
  end

391
  def project_view_files?
392
393
394
395
396
    if current_user
      current_user.project_view == 'files'
    else
      project_view_files_allowed?
    end
397
398
  end

399
  # Override extract_ref from ExtractsPath, which returns the branch and file path
Douwe Maan's avatar
Douwe Maan committed
400
  # for the blob/tree, which in this case is just the root of the default branch.
401
402
403
404
405
406
  # 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.
407
408
409
  def get_id
    project.repository.root_ref
  end
410
411
412
413

  def project_view_files_allowed?
    !project.empty_repo? && can?(current_user, :download_code, project)
  end
414
415
416
417
418

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

blackst0ne's avatar
blackst0ne committed
419
    url_for(safe_params)
420
  end
421
422

  def project_export_enabled
423
    render_404 unless Gitlab::CurrentSettings.project_export_enabled?
424
  end
425
426
427
428
429
430
431

  def redirect_git_extension
    # Redirect from
    #   localhost/group/project.git
    # to
    #   localhost/group/project
    #
432
    redirect_to request.original_url.sub(%r{\.git/?\Z}, '') if params[:format] == 'git'
433
  end
434
435
436
437

  def whitelist_query_limiting
    Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42440')
  end
438
439
440
441

  def present_project
    @project = @project.present(current_user: current_user)
  end
gitlabhq's avatar
gitlabhq committed
442
end