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

KON YUICHI's avatar
KON YUICHI committed
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?
gitlabhq's avatar
gitlabhq committed
9 10

  # Authorize
11
  before_action :authorize_admin_project!, only: [:edit, :update]
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

gitlabhq's avatar
gitlabhq committed
44
    respond_to do |format|
45
      if status
46
        flash[:notice] = "Project '#{@project.name}' was successfully updated."
Vinnie Okada's avatar
Vinnie Okada committed
47 48
        format.html do
          redirect_to(
49
            edit_project_path(@project),
50
            notice: "Project '#{@project.name}' was successfully updated."
Vinnie Okada's avatar
Vinnie Okada committed
51 52
          )
        end
Nihad Abbasov's avatar
Nihad Abbasov committed
53
        format.js
gitlabhq's avatar
gitlabhq committed
54
      else
55
        format.html { render 'edit' }
Nihad Abbasov's avatar
Nihad Abbasov committed
56
        format.js
gitlabhq's avatar
gitlabhq committed
57
      end
gitlabhq's avatar
gitlabhq committed
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
skv-headless's avatar
skv-headless committed
69
    end
gitlabhq's avatar
gitlabhq committed
70 71
  end

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

Marin Jankovski's avatar
Marin Jankovski committed
75
    if @project.unlink_fork
Douwe Maan's avatar
Douwe Maan committed
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

gitlabhq's avatar
gitlabhq committed
90
  def show
91
    if @project.import_in_progress?
Vinnie Okada's avatar
Vinnie Okada committed
92
      redirect_to namespace_project_import_path(@project.namespace, @project)
93 94 95
      return
    end

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

106
            render :show
107
          end
108
        else
109
          render 'projects/no_repo'
110
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
111
      end
112

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

gitlabhq's avatar
gitlabhq committed
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."
gitlabhq's avatar
gitlabhq committed
125

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

131
  def autocomplete_sources
Marin Jankovski's avatar
Marin Jankovski committed
132 133
    note_type = params['type']
    note_id = params['type_id']
134
    autocomplete = ::Projects::AutocompleteService.new(@project)
Douwe Maan's avatar
Douwe Maan committed
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)
Douwe Maan's avatar
Douwe Maan committed
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)
Douwe Maan's avatar
Douwe Maan committed
161

162
    @project.unarchive!
163 164

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

Ciro Santilli's avatar
Ciro Santilli committed
169 170
  def toggle_star
    current_user.toggle_star(@project)
171
    @project.reload
172 173

    render json: {
174
      star_count: @project.star_count
175
    }
Ciro Santilli's avatar
Ciro Santilli committed
176 177
  end

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)
      }
    }
190 191
  end

192 193
  private

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

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(
Kamil Trzcinski's avatar
Kamil Trzcinski committed
213
      :name, :path, :description, :issues_tracker, :tag_list, :runners_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,
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

Marin Jankovski's avatar
Marin Jankovski committed
245
  # Override get_id from ExtractsPath, which returns the branch and file path
Douwe Maan's avatar
Douwe Maan committed
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
gitlabhq's avatar
gitlabhq committed
250
end