projects_controller.rb 12.6 KB
Newer Older
1
class ProjectsController < Projects::ApplicationController
2
  include API::Helpers::RelatedResourcesHelpers
3
  include IssuableCollections
4
  include ExtractsPath
5
  include PreviewMarkdown
6
  include SendFileUpload
7
  prepend EE::ProjectsController
8

9
  before_action :whitelist_query_limiting, only: [:create]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
10
  before_action :authenticate_user!, except: [:index, :show, :activity, :refs]
11
  before_action :redirect_git_extension, only: [:show]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12 13
  before_action :project, except: [:index, :new, :create]
  before_action :repository, except: [:index, :new, :create]
14
  before_action :assign_ref_vars, only: [:show], if: :repo_exists?
15
  before_action :assign_tree_vars, only: [:show], if: [:repo_exists?, :project_view_files?]
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]
gitlabhq's avatar
gitlabhq committed
19 20

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

24
  layout :determine_layout
Cyril's avatar
Cyril committed
25

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

103 104 105 106 107 108 109
  def activity
    respond_to do |format|
      format.html
      format.json do
        load_events
        pager_json('events/_events', @events.count)
      end
skv-headless's avatar
skv-headless committed
110
    end
gitlabhq's avatar
gitlabhq committed
111 112 113
  end

  def show
114
    if @project.import_in_progress?
115
      redirect_to project_import_path(@project, custom_import_params)
116 117 118
      return
    end

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

176
  def housekeeping
177
    ::Projects::HousekeepingService.new(@project).execute
178

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

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

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

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

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

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

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

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

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

240
  # rubocop: disable CodeReuse/ActiveRecord
241
  def refs
242
    find_refs = params['find']
243

244 245 246
    find_branches = true
    find_tags = true
    find_commits = true
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
247 248

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

254 255 256
    options = {}

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

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

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

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

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

277 278 279 280 281
  # 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
282
    if can?(current_user, :download_code, @project)
283
      return render 'projects/no_repo' unless @project.repository_exists?
284

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

      render :show
    end
  end

300 301 302 303
  def finder_type
    IssuesFinder
  end

304 305 306 307 308 309 310 311
  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
312
  end
313

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

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

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

326
  def project_params
327
    params.require(:project)
328
      .permit(project_params_attributes)
329
  end
330

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

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

372 373 374 375
  def custom_import_params
    {}
  end

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

380
  def repo_exists?
381
    project.repository_exists? && !project.empty_repo?
382 383 384 385 386

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

    false
387 388
  end

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

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

  # ExtractsPath will set @id = project.path on the show route, but it has to be the
  # branch name for the tree view to work correctly.
  def assign_tree_vars
    @id = get_id
    tree
Nick Thomas's avatar
Nick Thomas committed
414 415
  end

416 417
  def project_view_files_allowed?
    !project.empty_repo? && can?(current_user, :download_code, project)
418
  end
419 420 421 422 423

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

424
    url_for(safe_params)
425
  end
426 427

  def project_export_enabled
428
    render_404 unless Gitlab::CurrentSettings.project_export_enabled?
429
  end
430 431 432 433 434 435 436

  def redirect_git_extension
    # Redirect from
    #   localhost/group/project.git
    # to
    #   localhost/group/project
    #
437
    redirect_to request.original_url.sub(%r{\.git/?\Z}, '') if params[:format] == 'git'
438
  end
439 440 441 442

  def whitelist_query_limiting
    Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42440')
  end
gitlabhq's avatar
gitlabhq committed
443
end