projects_controller.rb 11.2 KB
Newer Older
1
class ProjectsController < Projects::ApplicationController
2
  include IssuableCollections
3
  include ExtractsPath
4
  prepend EE::ProjectsController
5

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

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

18
  layout :determine_layout
Cyril's avatar
Cyril committed
19

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

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

  def edit
32
    render 'edit'
gitlabhq's avatar
gitlabhq committed
33 34 35
  end

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

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

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

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

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

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

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

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

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

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

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

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

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

92 93 94 95 96 97 98
  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
99
    end
gitlabhq's avatar
gitlabhq committed
100 101 102
  end

  def show
103 104
    # If we're importing while we do have a repository, we're simply updating the mirror.
    if @project.import_in_progress? && !@project.updating_mirror?
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[:alert] = _("Project '%{project_name}' will be 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 138 139 140 141 142 143
  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

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 163
    end
  end

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

167 168
    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 206 207 208 209 210 211
    end
    redirect_to(edit_project_path(@project))
  end

  def generate_new_export
    if @project.remove_exports
      export
212 213 214
    else
      redirect_to(
        edit_project_path(@project),
215
        alert: _("Project export could not be deleted.")
216
      )
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 refs
230
    find_refs = params['find']
231

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

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

242 243 244
    options = {}

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

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

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

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

    render json: options.to_json
  end

264
  def preview_markdown
265 266 267 268 269 270 271 272 273
    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])
      }
    }
274 275
  end

276 277
  private

278 279 280 281 282
  # 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
283
    if can?(current_user, :download_code, @project)
284 285 286 287
      return render 'projects/no_repo' unless @project.repository_exists?
      render 'projects/empty' if @project.empty_repo?
    else
      if @project.wiki_enabled?
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 292 293
        @issues = issues_collection.page(params[:page])
        @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 304 305 306 307
  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
308
  end
309

310
  def load_events
311 312 313 314 315
    projects = Project.where(id: @project.id)

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

318
  def project_params
319
    params.require(:project)
320
      .permit(project_params_attributes)
321
  end
322

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

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

363
  def repo_exists?
364 365 366 367 368 369
    project.repository_exists? && !project.empty_repo? && project.repo

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

    false
370 371
  end

372
  def project_view_files?
373 374 375 376 377
    if current_user
      current_user.project_view == 'files'
    else
      project_view_files_allowed?
    end
378 379
  end

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

  # 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
397 398
  end

399 400
  def project_view_files_allowed?
    !project.empty_repo? && can?(current_user, :download_code, project)
401
  end
402 403 404 405 406 407 408

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

    url_for(params)
  end
409 410 411 412

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