Read about our upcoming Code of Conduct on this issue

projects_controller.rb 11 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

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

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

Douwe Maan's avatar
Douwe Maan committed
19
  layout :determine_layout
20

21
  def index
22
    redirect_to(current_user ? root_path : explore_root_path)
23
24
  end

gitlabhq's avatar
gitlabhq committed
25
  def new
26
27
28
29
    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
30
31
32
  end

  def edit
Douwe Maan's avatar
Douwe Maan committed
33
    render 'edit'
gitlabhq's avatar
gitlabhq committed
34
35
36
  end

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

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

Vinnie Okada's avatar
Vinnie Okada committed
42
      redirect_to(
43
        project_path(@project),
44
        notice: _("Project '%{project_name}' was successfully created.") % { project_name: @project.name }
Vinnie Okada's avatar
Vinnie Okada committed
45
      )
46
47
    else
      render 'new'
gitlabhq's avatar
gitlabhq committed
48
49
    end
  end
gitlabhq's avatar
gitlabhq committed
50

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

54
    # Refresh the repo in case anything changed
55
    @repository = @project.repository
56

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

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

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

      format.js
gitlabhq's avatar
gitlabhq committed
71
    end
72
  end
73

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

77
78
79
80
81
    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
82
    end
gitlabhq's avatar
gitlabhq committed
83
84
  end

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

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

93
94
95
96
97
98
99
100
101
102
  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
103
  def show
104
    if @project.import_in_progress?
105
      redirect_to project_import_path(@project)
106
107
108
      return
    end

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
113
    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
114
      format.html do
115
        @notification_setting = current_user.notification_settings_for(@project) if current_user
116
        render_landing_page
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
117
      end
118

119
120
      format.atom do
        load_events
121
        render layout: 'xml.atom'
122
      end
123
124
125
    end
  end

gitlabhq's avatar
gitlabhq committed
126
  def destroy
127
    return access_denied! unless can?(current_user, :remove_project, @project)
128

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

132
    redirect_to dashboard_projects_path, status: 302
133
  rescue Projects::DestroyService::DestroyError => ex
134
    redirect_to edit_project_path(@project), status: 302, alert: ex.message
gitlabhq's avatar
gitlabhq committed
135
  end
136

137
  def new_issuable_address
138
139
140
    return render_404 unless Gitlab::IncomingEmail.supports_issue_creation?

    current_user.reset_incoming_email_token!
141
    render json: { new_address: @project.new_issuable_address(current_user, params[:issuable_type]) }
142
143
  end

144
  def archive
145
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed
146

147
    @project.archive!
148
149

    respond_to do |format|
150
      format.html { redirect_to project_path(@project) }
151
152
153
154
    end
  end

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

157
    @project.unarchive!
158
159

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

  def housekeeping
165
166
167
168
    ::Projects::HousekeepingService.new(@project).execute

    redirect_to(
      project_path(@project),
169
      notice: _("Housekeeping successfully started")
170
171
172
173
174
175
    )
  rescue ::Projects::HousekeepingService::LeaseTaken => ex
    redirect_to(
      edit_project_path(@project),
      alert: ex.to_s
    )
176
  end
177

178
  def export
179
    @project.add_export_job(current_user: current_user)
180
181

    redirect_to(
James Lopez's avatar
James Lopez committed
182
      edit_project_path(@project),
183
      notice: _("Project export started. A download link will be sent by email.")
184
185
186
    )
  end

James Lopez's avatar
James Lopez committed
187
  def download_export
188
189
    export_project_path = @project.export_project_path

James Lopez's avatar
James Lopez committed
190
191
192
    if export_project_path
      send_file export_project_path, disposition: 'attachment'
    else
193
194
      redirect_to(
        edit_project_path(@project),
195
        alert: _("Project export link has expired. Please generate a new export from your project settings.")
196
197
198
199
200
201
      )
    end
  end

  def remove_export
    if @project.remove_exports
202
      flash[:notice] = _("Project export has been deleted.")
203
    else
204
      flash[:alert] = _("Project export could not be deleted.")
205
    end
206

207
208
209
210
211
212
    redirect_to(edit_project_path(@project))
  end

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

Ciro Santilli's avatar
Ciro Santilli committed
221
222
  def toggle_star
    current_user.toggle_star(@project)
223
    @project.reload
224
225

    render json: {
226
      star_count: @project.star_count
227
    }
Ciro Santilli's avatar
Ciro Santilli committed
228
229
  end

230
  def refs
231
232
233
234
235
    find_refs = params['find']

    find_branches = true
    find_tags = true
    find_commits = true
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
236
237

    unless find_refs.nil?
Douwe Maan's avatar
Douwe Maan committed
238
239
240
      find_branches = find_refs.include?('branches')
      find_tags = find_refs.include?('tags')
      find_commits = find_refs.include?('commits')
241
    end
242

243
244
245
    options = {}

    if find_branches
Douwe Maan's avatar
Douwe Maan committed
246
247
      branches = BranchesFinder.new(@repository, params).execute.take(100).map(&:name)
      options[s_('RefSwitcher|Branches')] = branches
248
    end
249

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

Douwe Maan's avatar
Douwe Maan committed
253
      options[s_('RefSwitcher|Tags')] = tags
Phil Hughes's avatar
Phil Hughes committed
254
255
    end

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

    render json: options.to_json
  end

265
266
  private

267
268
269
270
271
  # 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
272
    if can?(current_user, :download_code, @project)
273
      return render 'projects/no_repo' unless @project.repository_exists?
274

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

      render :show
    end
  end

Douwe Maan's avatar
Douwe Maan committed
291
292
293
294
295
296
297
298
  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
299
  end
300

301
  def load_events
302
303
304
305
306
    projects = Project.where(id: @project.id)

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

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

311
  def project_params
312
    params.require(:project)
313
      .permit(project_params_attributes)
314
  end
Felipe Artur's avatar
Felipe Artur committed
315

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

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

356
  def repo_exists?
Jacob Vosmaer's avatar
Jacob Vosmaer committed
357
    project.repository_exists? && !project.empty_repo?
358
359
360
361
362

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

    false
363
364
  end

365
  def project_view_files?
366
367
368
369
370
    if current_user
      current_user.project_view == 'files'
    else
      project_view_files_allowed?
    end
371
372
  end

373
  # Override extract_ref from ExtractsPath, which returns the branch and file path
Douwe Maan's avatar
Douwe Maan committed
374
  # for the blob/tree, which in this case is just the root of the default branch.
375
376
377
378
379
380
  # 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.
381
382
383
  def get_id
    project.repository.root_ref
  end
384
385
386
387

  def project_view_files_allowed?
    !project.empty_repo? && can?(current_user, :download_code, project)
  end
388
389
390
391
392
393
394

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

    url_for(params)
  end
395
396

  def project_export_enabled
397
    render_404 unless Gitlab::CurrentSettings.project_export_enabled?
398
  end
399
400
401
402
403
404
405

  def redirect_git_extension
    # Redirect from
    #   localhost/group/project.git
    # to
    #   localhost/group/project
    #
406
    redirect_to request.original_url.sub(%r{\.git/?\Z}, '') if params[:format] == 'git'
407
  end
gitlabhq's avatar
gitlabhq committed
408
end