Skip to content
projects_controller.rb 9.11 KiB
Newer Older
class ProjectsController < Projects::ApplicationController
  include ExtractsPath

  before_action :authenticate_user!, except: [:show, :activity, :refs]
  before_action :project, except: [:new, :create]
  before_action :repository, except: [:new, :create]
  before_action :assign_ref_vars, only: [:show], if: :repo_exists?
  before_action :tree, only: [:show], if: [:repo_exists?, :project_view_files?]
gitlabhq's avatar
gitlabhq committed

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

  layout :determine_layout
    redirect_to(current_user ? root_path : explore_root_path)
gitlabhq's avatar
gitlabhq committed
  def new
    @project = Project.new
  end

  def edit
    render 'edit'
gitlabhq's avatar
gitlabhq committed
  end

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

Vinnie Okada's avatar
Vinnie Okada committed
      redirect_to(
        project_path(@project),
        notice: "Project '#{@project.name}' was successfully created."
Vinnie Okada's avatar
Vinnie Okada committed
      )
gitlabhq's avatar
gitlabhq committed
    end
  end
gitlabhq's avatar
gitlabhq committed

gitlabhq's avatar
gitlabhq committed
  def update
    status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
    # Refresh the repo in case anything changed
    @repository = project.repository

gitlabhq's avatar
gitlabhq committed
    respond_to do |format|
        flash[:notice] = "Project '#{@project.name}' was successfully updated."
Vinnie Okada's avatar
Vinnie Okada committed
        format.html do
          redirect_to(
            edit_project_path(@project),
            notice: "Project '#{@project.name}' was successfully updated."
Vinnie Okada's avatar
Vinnie Okada committed
          )
        end
gitlabhq's avatar
gitlabhq committed
      else
        format.html { render 'edit' }
gitlabhq's avatar
gitlabhq committed
      end
gitlabhq's avatar
gitlabhq committed
    end
    return access_denied! unless can?(current_user, :change_namespace, @project)

    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
    end
gitlabhq's avatar
gitlabhq committed
  end

    return access_denied! unless can?(current_user, :remove_fork_project, @project)

    if ::Projects::UnlinkForkService.new(@project, current_user).execute
Douwe Maan's avatar
Douwe Maan committed
      flash[:notice] = 'The fork relationship has been removed.'
  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
  def show
    if @project.import_in_progress?
Vinnie Okada's avatar
Vinnie Okada committed
      redirect_to namespace_project_import_path(@project.namespace, @project)
    if @project.pending_delete?
      flash[:alert] = "Project #{@project.name} queued for deletion."
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
      format.html do
        @notification_setting = current_user.notification_settings_for(@project) if current_user
            render 'projects/empty'
            render :show
          render 'projects/no_repo'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
      end
      format.atom do
        load_events
        render layout: false
      end
gitlabhq's avatar
gitlabhq committed
  def destroy
    return access_denied! unless can?(current_user, :remove_project, @project)
Stan Hu's avatar
Stan Hu committed
    ::Projects::DestroyService.new(@project, current_user, {}).async_execute
    flash[:alert] = "Project '#{@project.name}' will be deleted."
gitlabhq's avatar
gitlabhq committed

    redirect_to dashboard_projects_path
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
gitlabhq's avatar
gitlabhq committed
  end
  def autocomplete_sources
    noteable =
      case params[:type]
      when 'Issue'
        IssuesFinder.new(current_user, project_id: @project.id).
          execute.find_by(iid: params[:type_id])
      when 'MergeRequest'
        MergeRequestsFinder.new(current_user, project_id: @project.id).
          execute.find_by(iid: params[:type_id])
      when 'Commit'
        @project.commit(params[:type_id])
      else
        nil
      end

    autocomplete = ::Projects::AutocompleteService.new(@project, current_user)
    participants = ::Projects::ParticipantsService.new(@project, current_user).execute(noteable)
    @suggestions = {
      emojis: Gitlab::AwardEmoji.urls,
      issues: autocomplete.issues,
      milestones: autocomplete.milestones,
      mergerequests: autocomplete.merge_requests,
      labels: autocomplete.labels,
      commands: autocomplete.commands(noteable, params[:type])
    }

    respond_to do |format|
      format.json { render json: @suggestions }
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed

    @project.archive!
      format.html { redirect_to project_path(@project) }
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed

    @project.unarchive!
      format.html { redirect_to project_path(@project) }
    ::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
    )
    @project.add_export_job(current_user: current_user)
James Lopez's avatar
James Lopez committed
      edit_project_path(@project),
      notice: "Project export started. A download link will be sent by email."
James Lopez's avatar
James Lopez committed
  def download_export
James Lopez's avatar
James Lopez committed
    if export_project_path
      send_file export_project_path, disposition: 'attachment'
    else
      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
      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
    else
      redirect_to(
        edit_project_path(@project),
        alert: "Project export could not be deleted."
      )
James Lopez's avatar
James Lopez committed
    end
Ciro Santilli's avatar
Ciro Santilli committed
  def toggle_star
    current_user.toggle_star(@project)
    @project.reload
Ciro Santilli's avatar
Ciro Santilli committed
  end

    ext = Gitlab::ReferenceExtractor.new(@project, current_user)
    ext.analyze(text, author: current_user)

    render json: {
      body:       view_context.markdown(text),
      references: {
        users: ext.users.map(&:username)
      }
    }
  def refs
    options = {
      'Branches' => @repository.branch_names,
    unless @repository.tag_count.zero?
      options['Tags'] = VersionSorter.rsort(@repository.tag_names)
    # If reference is commit id - we should add it to branch/tag selectbox
    ref = Addressable::URI.unescape(params[:ref])
    if ref && options.flatten(2).exclude?(ref) && ref =~ /\A[0-9a-zA-Z]{6,52}\z/
      options['Commits'] = [ref]
    end

    render json: options.to_json
  end

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

    project_feature_attributes =
      {
        project_feature_attributes:
          [
            :issues_access_level, :builds_access_level,
            :wiki_access_level, :merge_requests_access_level, :snippets_access_level
          ]
      }

      :name, :path, :description, :issues_tracker, :tag_list, :runners_token,
      :container_registry_enabled,
      :issues_tracker_id, :default_branch,
      :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar,
      :build_allow_git_fetch, :build_timeout_in_minutes, :build_coverage_regex,
      :public_builds, :only_allow_merge_if_build_succeeds, :request_access_enabled,
      :lfs_enabled, project_feature_attributes
    project.repository_exists? && !project.empty_repo? && project.repo

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

    false
  def project_view_files?
    current_user && current_user.project_view == 'files'
  end

  # Override extract_ref from ExtractsPath, which returns the branch and file path
Douwe Maan's avatar
Douwe Maan committed
  # for the blob/tree, which in this case is just the root of the default branch.
  # 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.
  def get_id
    project.repository.root_ref
  end
gitlabhq's avatar
gitlabhq committed
end