projects_controller.rb 6.3 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]
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

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

D
Douwe Maan 已提交
106
            render :show
107
          end
108
        else
D
Douwe Maan 已提交
109
          render 'projects/no_repo'
110
        end
D
Dmitriy Zaporozhets 已提交
111
      end
112

D
Douwe Maan 已提交
113 114 115 116
      format.atom do
        load_events
        render layout: false
      end
117 118 119
    end
  end

G
gitlabhq 已提交
120
  def destroy
121
    return access_denied! unless can?(current_user, :remove_project, @project)
122

123
    ::Projects::DestroyService.new(@project, current_user, {}).execute
124
    flash[:alert] = "Project '#{@project.name}' was deleted."
G
gitlabhq 已提交
125

126
    redirect_to dashboard_projects_path
127 128
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
G
gitlabhq 已提交
129
  end
130

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

137
    @suggestions = {
138
      emojis: autocomplete_emojis,
139 140
      issues: autocomplete.issues,
      mergerequests: autocomplete.merge_requests,
141
      members: participants
142 143 144
    }

    respond_to do |format|
145
      format.json { render json: @suggestions }
146 147
    end
  end
148

149
  def archive
150
    return access_denied! unless can?(current_user, :archive_project, @project)
D
Douwe Maan 已提交
151

152
    @project.archive!
153 154

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

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

162
    @project.unarchive!
163 164

    respond_to do |format|
165
      format.html { redirect_to project_path(@project) }
166 167 168
    end
  end

C
Ciro Santilli 已提交
169 170
  def toggle_star
    current_user.toggle_star(@project)
171
    @project.reload
172 173 174 175

    render json: {
      html: view_to_html_string("projects/buttons/_star")
    }
C
Ciro Santilli 已提交
176 177
  end

V
Vinnie Okada 已提交
178
  def markdown_preview
179
    text = params[:text]
180 181 182 183 184 185 186 187 188 189

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

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

192 193
  private

D
Douwe Maan 已提交
194 195 196 197 198 199 200 201
  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
202
  end
203

D
Douwe Maan 已提交
204 205 206 207 208 209 210
  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

211 212
  def project_params
    params.require(:project).permit(
K
Kamil Trzcinski 已提交
213
      :name, :path, :description, :issues_tracker, :tag_list, :token,
214
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
215
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar,
K
Kamil Trzcinski 已提交
216
      :builds_enabled, :build_allow_git_fetch, :build_timeout_in_minutes, :build_coverage_regex,
217 218
    )
  end
219 220

  def autocomplete_emojis
221 222
    Rails.cache.fetch("autocomplete-emoji-#{Gemojione::VERSION}") do
      Emoji.emojis.map do |name, emoji|
223
        {
224 225
          name: name,
          path: view_context.image_url("emoji/#{emoji["unicode"]}.png")
226 227 228 229
        }
      end
    end
  end
230 231 232 233 234 235 236 237 238 239

  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
240

241 242 243 244
  def repo_exists?
    project.repository_exists? && !project.empty_repo?
  end

M
Marin Jankovski 已提交
245
  # Override get_id from ExtractsPath, which returns the branch and file path
D
Douwe Maan 已提交
246
  # for the blob/tree, which in this case is just the root of the default branch.
247 248 249
  def get_id
    project.repository.root_ref
  end
G
gitlabhq 已提交
250
end