BigW Consortium Gitlab

projects_controller.rb 9.21 KB
Newer Older
1
class ProjectsController < Projects::ApplicationController
2
  include IssuableCollections
3 4
  include ExtractsPath

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

15
  layout :determine_layout
16

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

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

  def edit
26
    render 'edit'
gitlabhq committed
27 28 29
  end

  def create
30
    @project = ::Projects::CreateService.new(current_user, project_params).execute
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 committed
35
      redirect_to(
36
        project_path(@project),
Phil Hughes committed
37
        notice: "Project '#{@project.name}' was successfully created."
Vinnie Okada committed
38
      )
39 40
    else
      render 'new'
gitlabhq committed
41 42
    end
  end
gitlabhq committed
43

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

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

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

      format.js
gitlabhq committed
64
    end
65
  end
66

67
  def transfer
68 69
    return access_denied! unless can?(current_user, :change_namespace, @project)

70 71 72 73 74
    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 committed
75
    end
gitlabhq committed
76 77
  end

78
  def remove_fork
79 80
    return access_denied! unless can?(current_user, :remove_fork_project, @project)

81
    if ::Projects::UnlinkForkService.new(@project, current_user).execute
Douwe Maan committed
82
      flash[:notice] = 'The fork relationship has been removed.'
83 84 85
    end
  end

86 87 88 89 90 91 92 93 94 95
  def activity
    respond_to do |format|
      format.html
      format.json do
        load_events
        pager_json('events/_events', @events.count)
      end
    end
  end

gitlabhq committed
96
  def show
97
    if @project.import_in_progress?
Vinnie Okada committed
98
      redirect_to namespace_project_import_path(@project.namespace, @project)
99 100 101
      return
    end

102
    if @project.pending_delete?
103
      flash[:alert] = "Project #{@project.name} queued for deletion."
104 105
    end

Dmitriy Zaporozhets committed
106
    respond_to do |format|
Nihad Abbasov committed
107
      format.html do
108
        @notification_setting = current_user.notification_settings_for(@project) if current_user
109
        render_landing_page
Dmitriy Zaporozhets committed
110
      end
111

112 113 114 115
      format.atom do
        load_events
        render layout: false
      end
116 117 118
    end
  end

gitlabhq committed
119
  def destroy
120
    return access_denied! unless can?(current_user, :remove_project, @project)
121

Stan Hu committed
122
    ::Projects::DestroyService.new(@project, current_user, {}).async_execute
123
    flash[:alert] = "Project '#{@project.name}' will be deleted."
gitlabhq committed
124

125
    redirect_to dashboard_projects_path
126 127
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
gitlabhq committed
128
  end
129

130 131 132 133 134 135 136
  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

137
  def archive
138
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan committed
139

140
    @project.archive!
141 142

    respond_to do |format|
143
      format.html { redirect_to project_path(@project) }
144 145 146 147
    end
  end

  def unarchive
148
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan committed
149

150
    @project.unarchive!
151 152

    respond_to do |format|
153
      format.html { redirect_to project_path(@project) }
154 155
    end
  end
156 157

  def housekeeping
158 159 160 161 162 163 164 165 166 167 168
    ::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
    )
169
  end
170

171
  def export
172
    @project.add_export_job(current_user: current_user)
173 174

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

180
  def download_export
181 182
    export_project_path = @project.export_project_path

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

Ciro Santilli committed
213 214
  def toggle_star
    current_user.toggle_star(@project)
215
    @project.reload
216 217

    render json: {
218
      star_count: @project.star_count
219
    }
Ciro Santilli committed
220 221
  end

222
  def preview_markdown
223
    text = params[:text]
224

225 226
    ext = Gitlab::ReferenceExtractor.new(@project, current_user)
    ext.analyze(text, author: current_user)
227 228 229 230 231 232 233

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

236 237
  def refs
    options = {
238
      'Branches' => @repository.branch_names,
239 240
    }

241 242
    unless @repository.tag_count.zero?
      options['Tags'] = VersionSorter.rsort(@repository.tag_names)
243 244
    end

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

    render json: options.to_json
  end

254 255
  private

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

      render :show
    end
  end

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

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

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

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

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

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

    false
342 343
  end

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

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