projects_controller.rb 9.25 KB
Newer Older
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?
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

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

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 154

  def housekeeping
155 156 157 158 159 160 161 162 163 164 165
    ::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
    )
166
  end
167

168
  def export
169
    @project.add_export_job(current_user: current_user)
170 171

    redirect_to(
172
      edit_project_path(@project),
173
      notice: "Project export started. A download link will be sent by email."
174 175 176
    )
  end

177
  def download_export
178 179
    export_project_path = @project.export_project_path

James Lopez's avatar
James Lopez committed
180 181 182
    if export_project_path
      send_file export_project_path, disposition: 'attachment'
    else
183 184 185 186 187 188 189 190 191
      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
192 193 194 195 196 197 198 199 200 201
      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
202 203 204 205 206
    else
      redirect_to(
        edit_project_path(@project),
        alert: "Project export could not be deleted."
      )
James Lopez's avatar
James Lopez committed
207
    end
208 209
  end

Ciro Santilli's avatar
Ciro Santilli committed
210 211
  def toggle_star
    current_user.toggle_star(@project)
212
    @project.reload
213 214

    render json: {
215
      star_count: @project.star_count
216
    }
Ciro Santilli's avatar
Ciro Santilli committed
217 218
  end

219
  def preview_markdown
220
    text = params[:text]
221

222 223
    ext = Gitlab::ReferenceExtractor.new(@project, current_user)
    ext.analyze(text, author: current_user)
224 225 226 227 228 229 230

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

233
  def refs
234
    branches = BranchesFinder.new(@repository, params).execute.map(&:name)
235

236
    options = {
237
      'Branches' => branches.take(100),
238 239
    }

240
    unless @repository.tag_count.zero?
241 242
      tags = TagsFinder.new(@repository, params).execute.map(&:name)

243
      options['Tags'] = tags.take(100)
244 245
    end

246
    # If reference is commit id - we should add it to branch/tag selectbox
247 248
    ref = Addressable::URI.unescape(params[:ref])
    if ref && options.flatten(2).exclude?(ref) && ref =~ /\A[0-9a-zA-Z]{6,52}\z/
249
      options['Commits'] = [ref]
250 251 252 253 254
    end

    render json: options.to_json
  end

255 256
  private

257 258 259 260 261 262 263 264 265 266
  # 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?
267 268
        @project_wiki = @project.wiki
        @wiki_home = @project_wiki.find_page('home', params[:version_id])
269 270 271 272 273 274 275 276 277
      elsif @project.feature_available?(:issues, current_user)
        @issues = issues_collection
        @issues = @issues.page(params[:page])
      end

      render :show
    end
  end

278 279 280 281 282 283 284 285
  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
286
  end
287

288 289 290 291 292 293 294
  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

295
  def project_params
296 297
    params.require(:project)
      .permit(project_params_ce)
298
  end
299

300 301 302 303 304 305
  def project_params_ce
    [
      :avatar,
      :build_allow_git_fetch,
      :build_coverage_regex,
      :build_timeout_in_minutes,
306
      :container_registry_enabled,
307 308 309 310 311 312 313 314 315
      :default_branch,
      :description,
      :import_url,
      :issues_tracker,
      :issues_tracker_id,
      :last_activity_at,
      :lfs_enabled,
      :name,
      :namespace_id,
316
      :only_allow_merge_if_all_discussions_are_resolved,
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
      :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
      ]
    ]
334
  end
335

336
  def repo_exists?
337 338 339 340 341 342
    project.repository_exists? && !project.empty_repo? && project.repo

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

    false
343 344
  end

345 346 347 348
  def project_view_files?
    current_user && current_user.project_view == 'files'
  end

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