Read about our upcoming Code of Conduct on this issue

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

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

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

Douwe Maan's avatar
Douwe Maan committed
16
  layout :determine_layout
17

18
  def index
19
    redirect_to(current_user ? root_path : explore_root_path)
20
21
  end

gitlabhq's avatar
gitlabhq committed
22
  def new
23
24
25
26
    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
27
28
29
  end

  def edit
Douwe Maan's avatar
Douwe Maan committed
30
    render 'edit'
gitlabhq's avatar
gitlabhq committed
31
32
33
  end

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

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

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

gitlabhq's avatar
gitlabhq committed
48
  def update
49
    result = ::Projects::UpdateService.new(@project, current_user, project_params).execute
50

51
    # Refresh the repo in case anything changed
52
    @repository = @project.repository
53

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

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

Douwe Maan's avatar
Douwe Maan committed
64
        format.html { render 'edit' }
gitlabhq's avatar
gitlabhq committed
65
      end
66
67

      format.js
gitlabhq's avatar
gitlabhq committed
68
    end
69
  end
70

71
  def transfer
72
73
    return access_denied! unless can?(current_user, :change_namespace, @project)

74
75
76
77
78
    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
79
    end
gitlabhq's avatar
gitlabhq committed
80
81
  end

82
  def remove_fork
83
84
    return access_denied! unless can?(current_user, :remove_fork_project, @project)

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

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

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

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

116
117
      format.atom do
        load_events
118
        render layout: 'xml.atom'
119
      end
120
121
122
    end
  end

gitlabhq's avatar
gitlabhq committed
123
  def destroy
124
    return access_denied! unless can?(current_user, :remove_project, @project)
125

Stan Hu's avatar
Stan Hu committed
126
    ::Projects::DestroyService.new(@project, current_user, {}).async_execute
127
    flash[:alert] = _("Project '%{project_name}' will be deleted.") % { project_name: @project.name_with_namespace }
gitlabhq's avatar
gitlabhq committed
128

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

134
135
136
137
138
139
140
  def new_issue_address
    return render_404 unless Gitlab::IncomingEmail.supports_issue_creation?

    current_user.reset_incoming_email_token!
    render json: { new_issue_address: @project.new_issue_address(current_user) }
  end

141
  def archive
142
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed
143

144
    @project.archive!
145
146

    respond_to do |format|
147
      format.html { redirect_to project_path(@project) }
148
149
150
151
    end
  end

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

154
    @project.unarchive!
155
156

    respond_to do |format|
157
      format.html { redirect_to project_path(@project) }
158
159
    end
  end
160
161

  def housekeeping
162
163
164
165
    ::Projects::HousekeepingService.new(@project).execute

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

175
  def export
176
    @project.add_export_job(current_user: current_user)
177
178

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

James Lopez's avatar
James Lopez committed
184
  def download_export
185
186
    export_project_path = @project.export_project_path

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

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

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

Ciro Santilli's avatar
Ciro Santilli committed
217
218
  def toggle_star
    current_user.toggle_star(@project)
219
    @project.reload
220
221

    render json: {
222
      star_count: @project.star_count
223
    }
Ciro Santilli's avatar
Ciro Santilli committed
224
225
  end

226
  def refs
227
228
229
230
231
    find_refs = params['find']

    find_branches = true
    find_tags = true
    find_commits = true
Luke "Jared" Bennett's avatar
Luke "Jared" Bennett committed
232
233

    unless find_refs.nil?
Douwe Maan's avatar
Douwe Maan committed
234
235
236
      find_branches = find_refs.include?('branches')
      find_tags = find_refs.include?('tags')
      find_commits = find_refs.include?('commits')
237
    end
238

239
240
241
    options = {}

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

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

Douwe Maan's avatar
Douwe Maan committed
249
      options[s_('RefSwitcher|Tags')] = tags
Phil Hughes's avatar
Phil Hughes committed
250
251
    end

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

    render json: options.to_json
  end

261
  def preview_markdown
262
263
264
265
266
267
268
269
270
    result = PreviewMarkdownService.new(@project, current_user, params).execute

    render json: {
      body: view_context.markdown(result[:text]),
      references: {
        users: result[:users],
        commands: view_context.markdown(result[:commands])
      }
    }
271
272
  end

273
274
  private

275
276
277
278
279
  # 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
280
    if can?(current_user, :download_code, @project)
281
282
283
284
      return render 'projects/no_repo' unless @project.repository_exists?
      render 'projects/empty' if @project.empty_repo?
    else
      if @project.wiki_enabled?
285
286
        @project_wiki = @project.wiki
        @wiki_home = @project_wiki.find_page('home', params[:version_id])
287
      elsif @project.feature_available?(:issues, current_user)
288
289
290
        @issues = issues_collection.page(params[:page])
        @collection_type = 'Issue'
        @issuable_meta_data = issuable_meta_data(@issues, @collection_type)
291
292
293
294
295
296
      end

      render :show
    end
  end

Douwe Maan's avatar
Douwe Maan committed
297
298
299
300
301
302
303
304
  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
305
  end
306

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

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

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

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

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

359
  def repo_exists?
360
361
362
363
364
365
    project.repository_exists? && !project.empty_repo? && project.repo

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

    false
366
367
  end

368
  def project_view_files?
369
370
371
372
373
    if current_user
      current_user.project_view == 'files'
    else
      project_view_files_allowed?
    end
374
375
  end

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

  def project_view_files_allowed?
    !project.empty_repo? && can?(current_user, :download_code, project)
  end
391
392
393
394
395
396
397

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

    url_for(params)
  end
398
399
400
401

  def project_export_enabled
    render_404 unless current_application_settings.project_export_enabled?
  end
gitlabhq's avatar
gitlabhq committed
402
end