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

4
  before_action :authenticate_user!, except: [:show, :activity, :refs]
5 6
  before_action :project, except: [:new, :create]
  before_action :repository, except: [:new, :create]
7
  before_action :assign_ref_vars, only: [:show], if: :repo_exists?
8
  before_action :tree, only: [:show], if: [:repo_exists?, :project_view_files?]
gitlabhq's avatar
gitlabhq committed
9 10

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

14
  layout :determine_layout
Cyril's avatar
Cyril committed
15

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

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

  def edit
25
    render 'edit'
gitlabhq's avatar
gitlabhq committed
26 27 28
  end

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

31
    if @project.saved?
Vinnie Okada's avatar
Vinnie Okada committed
32
      redirect_to(
33
        project_path(@project),
34
        notice: "Project '#{@project.name}' was successfully created."
Vinnie Okada's avatar
Vinnie Okada committed
35
      )
36 37
    else
      render 'new'
gitlabhq's avatar
gitlabhq committed
38 39
    end
  end
gitlabhq's avatar
gitlabhq committed
40

gitlabhq's avatar
gitlabhq committed
41
  def update
42
    status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
43

44 45 46
    # Refresh the repo in case anything changed
    @repository = project.repository

gitlabhq's avatar
gitlabhq committed
47
    respond_to do |format|
48
      if status
49
        flash[:notice] = "Project '#{@project.name}' was successfully updated."
Vinnie Okada's avatar
Vinnie Okada committed
50 51
        format.html do
          redirect_to(
52
            edit_project_path(@project),
53
            notice: "Project '#{@project.name}' was successfully updated."
Vinnie Okada's avatar
Vinnie Okada committed
54 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

107 108
        if @project.repository_exists?
          if @project.empty_repo?
109
            render 'projects/empty'
110
          else
111
            render :show
112
          end
113
        else
114
          render 'projects/no_repo'
115
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
116
      end
117

118 119 120 121
      format.atom do
        load_events
        render layout: false
      end
122 123 124
    end
  end

gitlabhq's avatar
gitlabhq committed
125
  def destroy
126
    return access_denied! unless can?(current_user, :remove_project, @project)
127

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

131
    redirect_to dashboard_projects_path
132 133
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
gitlabhq's avatar
gitlabhq committed
134
  end
135

136
  def autocomplete_sources
137 138 139
    noteable =
      case params[:type]
      when 'Issue'
140
        IssuesFinder.new(current_user, project_id: @project.id, state: 'all').
141 142
          execute.find_by(iid: params[:type_id])
      when 'MergeRequest'
143
        MergeRequestsFinder.new(current_user, project_id: @project.id, state: 'all').
144 145
          execute.find_by(iid: params[:type_id])
      when 'Commit'
146
        @project.commit(params[:type_id])
147 148 149 150 151 152
      else
        nil
      end

    autocomplete = ::Projects::AutocompleteService.new(@project, current_user)
    participants = ::Projects::ParticipantsService.new(@project, current_user).execute(noteable)
153

154
    @suggestions = {
155
      emojis: Gitlab::AwardEmoji.urls,
156
      issues: autocomplete.issues,
157
      milestones: autocomplete.milestones,
158
      mergerequests: autocomplete.merge_requests,
159
      labels: autocomplete.labels,
160
      members: participants,
161
      commands: autocomplete.commands(noteable, params[:type])
162 163 164
    }

    respond_to do |format|
165
      format.json { render json: @suggestions }
166 167
    end
  end
168

169
  def archive
170
    return access_denied! unless can?(current_user, :archive_project, @project)
Douwe Maan's avatar
Douwe Maan committed
171

172
    @project.archive!
173 174

    respond_to do |format|
175
      format.html { redirect_to project_path(@project) }
176 177 178 179
    end
  end

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

182
    @project.unarchive!
183 184

    respond_to do |format|
185
      format.html { redirect_to project_path(@project) }
186 187
    end
  end
188 189

  def housekeeping
190 191 192 193 194 195 196 197 198 199 200
    ::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
    )
201
  end
202

203
  def export
204
    @project.add_export_job(current_user: current_user)
205 206

    redirect_to(
James Lopez's avatar
James Lopez committed
207
      edit_project_path(@project),
208
      notice: "Project export started. A download link will be sent by email."
209 210 211
    )
  end

James Lopez's avatar
James Lopez committed
212
  def download_export
213 214
    export_project_path = @project.export_project_path

James Lopez's avatar
James Lopez committed
215 216 217
    if export_project_path
      send_file export_project_path, disposition: 'attachment'
    else
218 219 220 221 222 223 224 225 226
      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
227 228 229 230 231 232 233 234 235 236
      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
237 238 239 240 241
    else
      redirect_to(
        edit_project_path(@project),
        alert: "Project export could not be deleted."
      )
James Lopez's avatar
James Lopez committed
242
    end
James Lopez's avatar
James Lopez committed
243 244
  end

Ciro Santilli's avatar
Ciro Santilli committed
245 246
  def toggle_star
    current_user.toggle_star(@project)
247
    @project.reload
248 249

    render json: {
250
      star_count: @project.star_count
251
    }
Ciro Santilli's avatar
Ciro Santilli committed
252 253
  end

254
  def preview_markdown
255
    text = params[:text]
256

257 258
    ext = Gitlab::ReferenceExtractor.new(@project, current_user)
    ext.analyze(text, author: current_user)
259 260 261 262 263 264 265

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

268 269
  def refs
    options = {
270
      'Branches' => @repository.branch_names,
271 272
    }

273 274
    unless @repository.tag_count.zero?
      options['Tags'] = VersionSorter.rsort(@repository.tag_names)
Phil Hughes's avatar
Phil Hughes committed
275 276
    end

277
    # If reference is commit id - we should add it to branch/tag selectbox
278 279
    ref = Addressable::URI.unescape(params[:ref])
    if ref && options.flatten(2).exclude?(ref) && ref =~ /\A[0-9a-zA-Z]{6,52}\z/
280
      options['Commits'] = [ref]
281 282 283 284 285
    end

    render json: options.to_json
  end

286 287
  private

288 289 290 291 292 293 294 295
  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
296
  end
297

298 299 300 301 302 303 304
  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

305 306
  def project_params
    params.require(:project).permit(
Kamil Trzcinski's avatar
Kamil Trzcinski committed
307
      :name, :path, :description, :issues_tracker, :tag_list, :runners_token,
308
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :container_registry_enabled,
309
      :issues_tracker_id, :default_branch,
310
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar,
311
      :builds_enabled, :build_allow_git_fetch, :build_timeout_in_minutes, :build_coverage_regex,
312
      :public_builds, :only_allow_merge_if_build_succeeds, :request_access_enabled
313 314
    )
  end
315

316 317 318 319
  def repo_exists?
    project.repository_exists? && !project.empty_repo?
  end

320 321 322 323
  def project_view_files?
    current_user && current_user.project_view == 'files'
  end

324
  # Override extract_ref from ExtractsPath, which returns the branch and file path
Douwe Maan's avatar
Douwe Maan committed
325
  # for the blob/tree, which in this case is just the root of the default branch.
326 327 328 329 330 331
  # 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.
332 333 334
  def get_id
    project.repository.root_ref
  end
gitlabhq's avatar
gitlabhq committed
335
end