Read about our upcoming Code of Conduct on this issue

projects_controller.rb 9.35 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?]
gitlabhq's avatar
gitlabhq committed
10
11

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

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

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

gitlabhq's avatar
gitlabhq committed
21
22
23
24
25
  def new
    @project = Project.new
  end

  def edit
Douwe Maan's avatar
Douwe Maan committed
26
    render 'edit'
gitlabhq's avatar
gitlabhq committed
27
28
29
  end

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

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

Vinnie Okada's avatar
Vinnie Okada committed
35
      redirect_to(
36
        project_path(@project),
Phil Hughes's avatar
Phil Hughes committed
37
        notice: "Project '#{@project.name}' was successfully created."
Vinnie Okada's avatar
Vinnie Okada committed
38
      )
39
40
    else
      render 'new'
gitlabhq's avatar
gitlabhq committed
41
42
    end
  end
gitlabhq's avatar
gitlabhq committed
43

gitlabhq's avatar
gitlabhq committed
44
  def update
45
    result = ::Projects::UpdateService.new(@project, current_user, project_params).execute
46

47
    # Refresh the repo in case anything changed
48
    @repository = @project.repository
49

gitlabhq's avatar
gitlabhq committed
50
    respond_to do |format|
51
      if result[:status] == :success
52
        flash[:notice] = "Project '#{@project.name}' was successfully updated."
Vinnie Okada's avatar
Vinnie Okada committed
53
        format.html do
54
          redirect_to(edit_project_path(@project))
Vinnie Okada's avatar
Vinnie Okada committed
55
        end
gitlabhq's avatar
gitlabhq committed
56
      else
Douwe Maan's avatar
Douwe Maan committed
57
        format.html { render 'edit' }
gitlabhq's avatar
gitlabhq committed
58
      end
59
60

      format.js
gitlabhq's avatar
gitlabhq committed
61
    end
62
  end
63

64
  def transfer
65
66
    return access_denied! unless can?(current_user, :change_namespace, @project)

67
68
69
70
71
    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
72
    end
gitlabhq's avatar
gitlabhq committed
73
74
  end

75
  def remove_fork
76
77
    return access_denied! unless can?(current_user, :remove_fork_project, @project)

Baldinof's avatar
Baldinof committed
78
    if ::Projects::UnlinkForkService.new(@project, current_user).execute
Douwe Maan's avatar
Douwe Maan committed
79
      flash[:notice] = 'The fork relationship has been removed.'
80
81
82
    end
  end

83
84
85
86
87
88
89
90
91
92
  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
93
  def show
94
    if @project.import_in_progress?
Vinnie Okada's avatar
Vinnie Okada committed
95
      redirect_to namespace_project_import_path(@project.namespace, @project)
96
97
98
      return
    end

99
    if @project.pending_delete?
100
      flash[:alert] = "Project #{@project.name} queued for deletion."
101
102
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
103
    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
104
      format.html do
105
        @notification_setting = current_user.notification_settings_for(@project) if current_user
106
        render_landing_page
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
107
      end
108

109
110
111
112
      format.atom do
        load_events
        render layout: false
      end
113
114
115
    end
  end

gitlabhq's avatar
gitlabhq committed
116
  def destroy
117
    return access_denied! unless can?(current_user, :remove_project, @project)
118

Stan Hu's avatar
Stan Hu committed
119
    ::Projects::DestroyService.new(@project, current_user, {}).async_execute
120
    flash[:alert] = "Project '#{@project.name}' will be deleted."
gitlabhq's avatar
gitlabhq committed
121

122
    redirect_to dashboard_projects_path
123
124
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
gitlabhq's avatar
gitlabhq committed
125
  end
126

127
128
129
130
131
132
133
  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

134
  def archive
135
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed
136

137
    @project.archive!
138
139

    respond_to do |format|
140
      format.html { redirect_to project_path(@project) }
141
142
143
144
    end
  end

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

147
    @project.unarchive!
148
149

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

Kamil Trzcinski's avatar
Kamil Trzcinski committed
154
155
156
157
158
159
160
161
162
163
  def remove_pages
    return access_denied! unless can?(current_user, :remove_pages, @project)

    @project.remove_pages

    respond_to do |format|
      format.html { redirect_to project_path(@project) }
    end
  end

164
  def housekeeping
165
166
167
168
169
170
171
172
173
174
175
    ::Projects::HousekeepingService.new(@project).execute

    redirect_to(
      project_path(@project),
      notice: "Housekeeping successfully started"
    )
  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
195
196
197
198
199
200
201
      redirect_to(
        edit_project_path(@project),
        alert: "Project export link has expired. Please generate a new export from your project settings."
      )
    end
  end

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

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

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

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

229
  def preview_markdown
230
    text = params[:text]
231

232
233
    ext = Gitlab::ReferenceExtractor.new(@project, current_user)
    ext.analyze(text, author: current_user)
234
235
236
237
238
239
240

    render json: {
      body:       view_context.markdown(text),
      references: {
        users: ext.users.map(&:username)
      }
    }
241
242
  end

243
244
  def refs
    options = {
245
      'Branches' => @repository.branch_names,
246
247
    }

248
249
    unless @repository.tag_count.zero?
      options['Tags'] = VersionSorter.rsort(@repository.tag_names)
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
254
    ref = Addressable::URI.unescape(params[:ref])
    if 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
262
  private

263
264
265
266
267
268
269
270
271
272
  # 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
    if @project.feature_available?(:repository, current_user)
      return render 'projects/no_repo' unless @project.repository_exists?
      render 'projects/empty' if @project.empty_repo?
    else
      if @project.wiki_enabled?
273
274
        @project_wiki = @project.wiki
        @wiki_home = @project_wiki.find_page('home', params[:version_id])
275
276
277
278
279
280
281
282
283
      elsif @project.feature_available?(:issues, current_user)
        @issues = issues_collection
        @issues = @issues.page(params[:page])
      end

      render :show
    end
  end

Douwe Maan's avatar
Douwe Maan committed
284
285
286
287
288
289
290
291
  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
292
  end
293

294
295
296
297
298
299
300
  def load_events
    @events = @project.events.recent
    @events = event_filter.apply_filter(@events).with_associations
    limit = (params[:limit] || 20).to_i
    @events = @events.limit(limit).offset(params[:offset] || 0)
  end

301
  def project_params
302
303
304
    params.require(:project)
      .permit(project_params_ce)
  end
Felipe Artur's avatar
Felipe Artur committed
305

306
307
308
309
310
311
  def project_params_ce
    [
      :avatar,
      :build_allow_git_fetch,
      :build_coverage_regex,
      :build_timeout_in_minutes,
Felipe Artur's avatar
Felipe Artur committed
312
      :container_registry_enabled,
313
314
315
316
317
318
319
320
321
      :default_branch,
      :description,
      :import_url,
      :issues_tracker,
      :issues_tracker_id,
      :last_activity_at,
      :lfs_enabled,
      :name,
      :namespace_id,
322
      :only_allow_merge_if_all_discussions_are_resolved,
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
      :only_allow_merge_if_build_succeeds,
      :path,
      :public_builds,
      :request_access_enabled,
      :runners_token,
      :tag_list,
      :visibility_level,

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

342
  def repo_exists?
343
344
345
346
347
348
    project.repository_exists? && !project.empty_repo? && project.repo

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

    false
349
350
  end

351
352
353
354
  def project_view_files?
    current_user && current_user.project_view == 'files'
  end

355
  # Override extract_ref from ExtractsPath, which returns the branch and file path
Douwe Maan's avatar
Douwe Maan committed
356
  # for the blob/tree, which in this case is just the root of the default branch.
357
358
359
360
361
362
  # 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.
363
364
365
  def get_id
    project.repository.root_ref
  end
gitlabhq's avatar
gitlabhq committed
366
end