projects_controller.rb 6.6 KB
Newer Older
1
class ProjectsController < ApplicationController
2 3
  include ExtractsPath

K
KON YUICHI 已提交
4
  prepend_before_action :render_go_import, only: [:show]
5
  skip_before_action :authenticate_user!, only: [:show, :activity]
6 7
  before_action :project, except: [:new, :create]
  before_action :repository, except: [:new, :create]
8
  before_action :assign_ref_vars, :tree, only: [:show], if: :repo_exists?
G
gitlabhq 已提交
9 10

  # Authorize
11
  before_action :authorize_admin_project!, only: [:edit, :update, :housekeeping]
12
  before_action :event_filter, only: [:show, :activity]
G
gitlabhq 已提交
13

D
Douwe Maan 已提交
14
  layout :determine_layout
C
Cyril 已提交
15

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

G
gitlabhq 已提交
20 21 22 23 24
  def new
    @project = Project.new
  end

  def edit
D
Douwe Maan 已提交
25
    render 'edit'
G
gitlabhq 已提交
26 27 28
  end

  def create
29
    @project = ::Projects::CreateService.new(current_user, project_params).execute
G
gitlabhq 已提交
30

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

G
gitlabhq 已提交
41
  def update
42
    status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
43

G
gitlabhq 已提交
44
    respond_to do |format|
45
      if status
46
        flash[:notice] = "Project '#{@project.name}' was successfully updated."
V
Vinnie Okada 已提交
47 48
        format.html do
          redirect_to(
49
            edit_project_path(@project),
50
            notice: "Project '#{@project.name}' was successfully updated."
V
Vinnie Okada 已提交
51 52
          )
        end
N
Nihad Abbasov 已提交
53
        format.js
G
gitlabhq 已提交
54
      else
D
Douwe Maan 已提交
55
        format.html { render 'edit' }
N
Nihad Abbasov 已提交
56
        format.js
G
gitlabhq 已提交
57
      end
G
gitlabhq 已提交
58
    end
59
  end
60

61
  def transfer
62 63
    return access_denied! unless can?(current_user, :change_namespace, @project)

64 65 66 67 68
    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
S
skv-headless 已提交
69
    end
G
gitlabhq 已提交
70 71
  end

72
  def remove_fork
73 74
    return access_denied! unless can?(current_user, :remove_fork_project, @project)

M
Marin Jankovski 已提交
75
    if @project.unlink_fork
D
Douwe Maan 已提交
76
      flash[:notice] = 'The fork relationship has been removed.'
77 78 79
    end
  end

80 81 82 83 84 85 86 87 88 89
  def activity
    respond_to do |format|
      format.html
      format.json do
        load_events
        pager_json('events/_events', @events.count)
      end
    end
  end

G
gitlabhq 已提交
90
  def show
91
    if @project.import_in_progress?
V
Vinnie Okada 已提交
92
      redirect_to namespace_project_import_path(@project.namespace, @project)
93 94 95
      return
    end

96 97 98 99
    if @project.pending_delete?
      flash[:alert] = "Project queued for delete."
    end

D
Dmitriy Zaporozhets 已提交
100
    respond_to do |format|
N
Nihad Abbasov 已提交
101
      format.html do
102 103
        if @project.repository_exists?
          if @project.empty_repo?
D
Douwe Maan 已提交
104
            render 'projects/empty'
105
          else
D
Douwe Maan 已提交
106
            if current_user
D
Douwe Maan 已提交
107
              @membership = @project.team.find_member(current_user.id)
108
            end
D
Douwe Maan 已提交
109

D
Douwe Maan 已提交
110
            render :show
111
          end
112
        else
D
Douwe Maan 已提交
113
          render 'projects/no_repo'
114
        end
D
Dmitriy Zaporozhets 已提交
115
      end
116

D
Douwe Maan 已提交
117 118 119 120
      format.atom do
        load_events
        render layout: false
      end
121 122 123
    end
  end

G
gitlabhq 已提交
124
  def destroy
125
    return access_denied! unless can?(current_user, :remove_project, @project)
126

127 128
    ::Projects::DestroyService.new(@project, current_user, {}).pending_delete!
    flash[:alert] = "Project '#{@project.name}' will be deleted."
G
gitlabhq 已提交
129

130
    redirect_to dashboard_projects_path
131 132
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
G
gitlabhq 已提交
133
  end
134

135
  def autocomplete_sources
M
Marin Jankovski 已提交
136 137
    note_type = params['type']
    note_id = params['type_id']
138
    autocomplete = ::Projects::AutocompleteService.new(@project)
D
Douwe Maan 已提交
139
    participants = ::Projects::ParticipantsService.new(@project, current_user).execute(note_type, note_id)
140

141
    @suggestions = {
142
      emojis: autocomplete_emojis,
143 144
      issues: autocomplete.issues,
      mergerequests: autocomplete.merge_requests,
145
      members: participants
146 147 148
    }

    respond_to do |format|
149
      format.json { render json: @suggestions }
150 151
    end
  end
152

153
  def archive
154
    return access_denied! unless can?(current_user, :archive_project, @project)
D
Douwe Maan 已提交
155

156
    @project.archive!
157 158

    respond_to do |format|
159
      format.html { redirect_to project_path(@project) }
160 161 162 163
    end
  end

  def unarchive
164
    return access_denied! unless can?(current_user, :archive_project, @project)
D
Douwe Maan 已提交
165

166
    @project.unarchive!
167 168

    respond_to do |format|
169
      format.html { redirect_to project_path(@project) }
170 171
    end
  end
172 173

  def housekeeping
174
    ::Projects::HousekeepingService.new(@project).execute
175 176

    respond_to do |format|
177
      flash[:notice] = "Housekeeping successfully started."
178 179 180
      format.html { redirect_to project_path(@project) }
    end
  end
181

C
Ciro Santilli 已提交
182 183
  def toggle_star
    current_user.toggle_star(@project)
184
    @project.reload
185 186

    render json: {
187
      star_count: @project.star_count
188
    }
C
Ciro Santilli 已提交
189 190
  end

V
Vinnie Okada 已提交
191
  def markdown_preview
192
    text = params[:text]
193

194
    ext = Gitlab::ReferenceExtractor.new(@project, current_user, current_user)
195 196 197 198 199 200 201 202
    ext.analyze(text)

    render json: {
      body:       view_context.markdown(text),
      references: {
        users: ext.users.map(&:username)
      }
    }
V
Vinnie Okada 已提交
203 204
  end

205 206
  private

D
Douwe Maan 已提交
207 208 209 210 211 212 213 214
  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
215
  end
216

D
Douwe Maan 已提交
217 218 219 220 221 222 223
  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

224 225
  def project_params
    params.require(:project).permit(
K
Kamil Trzcinski 已提交
226
      :name, :path, :description, :issues_tracker, :tag_list, :runners_token,
227
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
228
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar,
K
Kamil Trzcinski 已提交
229
      :builds_enabled, :build_allow_git_fetch, :build_timeout_in_minutes, :build_coverage_regex,
230
      :public_builds,
231 232
    )
  end
233 234

  def autocomplete_emojis
235 236
    Rails.cache.fetch("autocomplete-emoji-#{Gemojione::VERSION}") do
      Emoji.emojis.map do |name, emoji|
237
        {
238 239
          name: name,
          path: view_context.image_url("emoji/#{emoji["unicode"]}.png")
240 241 242 243
        }
      end
    end
  end
244 245 246 247 248 249 250 251 252 253

  def render_go_import
    return unless params["go-get"] == "1"

    @namespace = params[:namespace_id]
    @id = params[:project_id] || params[:id]
    @id = @id.gsub(/\.git\Z/, "")

    render "go_import", layout: false
  end
254

255 256 257 258
  def repo_exists?
    project.repository_exists? && !project.empty_repo?
  end

M
Marin Jankovski 已提交
259
  # Override get_id from ExtractsPath, which returns the branch and file path
D
Douwe Maan 已提交
260
  # for the blob/tree, which in this case is just the root of the default branch.
261 262 263
  def get_id
    project.repository.root_ref
  end
G
gitlabhq 已提交
264
end