projects_controller.rb 5.19 KB
Newer Older
1
class ProjectsController < ApplicationController
2
  prepend_before_filter :render_go_import, only: [:show]
3 4 5
  skip_before_action :authenticate_user!, only: [:show]
  before_action :project, except: [:new, :create]
  before_action :repository, except: [:new, :create]
gitlabhq's avatar
gitlabhq committed
6 7

  # Authorize
8 9
  before_action :authorize_admin_project!, only: [:edit, :update, :destroy, :transfer, :archive, :unarchive]
  before_action :event_filter, only: :show
gitlabhq's avatar
gitlabhq committed
10

11
  layout :determine_layout
Cyril's avatar
Cyril committed
12

gitlabhq's avatar
gitlabhq committed
13 14 15 16 17
  def new
    @project = Project.new
  end

  def edit
18
    render 'edit'
gitlabhq's avatar
gitlabhq committed
19 20 21
  end

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

24
    if @project.saved?
Vinnie Okada's avatar
Vinnie Okada committed
25
      redirect_to(
26
        project_path(@project),
Vinnie Okada's avatar
Vinnie Okada committed
27 28
        notice: 'Project was successfully created.'
      )
29 30
    else
      render 'new'
gitlabhq's avatar
gitlabhq committed
31 32
    end
  end
gitlabhq's avatar
gitlabhq committed
33

gitlabhq's avatar
gitlabhq committed
34
  def update
35
    status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
36

gitlabhq's avatar
gitlabhq committed
37
    respond_to do |format|
38
      if status
39
        flash[:notice] = 'Project was successfully updated.'
Vinnie Okada's avatar
Vinnie Okada committed
40 41
        format.html do
          redirect_to(
42
            edit_project_path(@project),
Vinnie Okada's avatar
Vinnie Okada committed
43 44 45
            notice: 'Project was successfully updated.'
          )
        end
Nihad Abbasov's avatar
Nihad Abbasov committed
46
        format.js
gitlabhq's avatar
gitlabhq committed
47
      else
48
        format.html { render 'edit' }
Nihad Abbasov's avatar
Nihad Abbasov committed
49
        format.js
gitlabhq's avatar
gitlabhq committed
50
      end
gitlabhq's avatar
gitlabhq committed
51
    end
52
  end
53

54
  def transfer
Vinnie Okada's avatar
Vinnie Okada committed
55 56
    transfer_params = params.permit(:new_namespace_id)
    ::Projects::TransferService.new(project, current_user, transfer_params).execute
skv-headless's avatar
skv-headless committed
57 58 59
    if @project.errors[:namespace_id].present?
      flash[:alert] = @project.errors[:namespace_id].first
    end
gitlabhq's avatar
gitlabhq committed
60 61 62
  end

  def show
63
    if @project.import_in_progress?
Vinnie Okada's avatar
Vinnie Okada committed
64
      redirect_to namespace_project_import_path(@project.namespace, @project)
65 66 67
      return
    end

Ciro Santilli's avatar
Ciro Santilli committed
68
    @show_star = !(current_user && current_user.starred?(@project))
69

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
70
    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
71
      format.html do
72 73
        if @project.repository_exists?
          if @project.empty_repo?
74
            render 'projects/empty'
75 76
          else
            @last_push = current_user.recent_push(@project.id) if current_user
77
            render :show
78
          end
79
        else
80
          render 'projects/no_repo'
81
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82
      end
83

84
      format.json do
85
        load_events
86 87
        pager_json('events/_events', @events.count)
      end
88 89 90 91 92

      format.atom do
        load_events
        render layout: false
      end
93 94 95
    end
  end

gitlabhq's avatar
gitlabhq committed
96
  def destroy
97
    return access_denied! unless can?(current_user, :remove_project, @project)
98

99
    ::Projects::DestroyService.new(@project, current_user, {}).execute
100
    flash[:alert] = 'Project deleted.'
gitlabhq's avatar
gitlabhq committed
101

102 103 104 105
    if request.referer.include?('/admin')
      redirect_to admin_namespaces_projects_path
    else
      redirect_to dashboard_path
gitlabhq's avatar
gitlabhq committed
106
    end
107 108
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
gitlabhq's avatar
gitlabhq committed
109
  end
110

111
  def autocomplete_sources
Marin Jankovski's avatar
Marin Jankovski committed
112 113
    note_type = params['type']
    note_id = params['type_id']
114
    autocomplete = ::Projects::AutocompleteService.new(@project)
Douwe Maan's avatar
Douwe Maan committed
115
    participants = ::Projects::ParticipantsService.new(@project, current_user).execute(note_type, note_id)
116

117
    @suggestions = {
118
      emojis: autocomplete_emojis,
119 120
      issues: autocomplete.issues,
      mergerequests: autocomplete.merge_requests,
121
      members: participants
122 123 124
    }

    respond_to do |format|
125
      format.json { render json: @suggestions }
126 127
    end
  end
128

129
  def archive
130 131
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.archive!
132 133

    respond_to do |format|
134
      format.html { redirect_to project_path(@project) }
135 136 137 138
    end
  end

  def unarchive
139 140
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.unarchive!
141 142

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

Ciro Santilli's avatar
Ciro Santilli committed
147 148
  def toggle_star
    current_user.toggle_star(@project)
149
    @project.reload
Ciro Santilli's avatar
Ciro Santilli committed
150 151 152
    render json: { star_count: @project.star_count }
  end

153 154 155 156
  def markdown_preview
    render text: view_context.markdown(params[:md_text])
  end

157 158
  private

159 160 161 162 163 164 165 166
  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
167
  end
168

169 170 171 172 173 174 175
  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

176 177
  def project_params
    params.require(:project).permit(
178
      :name, :path, :description, :issues_tracker, :tag_list,
179
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
180
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar
181 182
    )
  end
183 184

  def autocomplete_emojis
185 186
    Rails.cache.fetch("autocomplete-emoji-#{Gemojione::VERSION}") do
      Emoji.emojis.map do |name, emoji|
187
        {
188 189
          name: name,
          path: view_context.image_url("emoji/#{emoji["unicode"]}.png")
190 191 192 193
        }
      end
    end
  end
194 195 196 197 198 199 200 201 202 203

  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
gitlabhq's avatar
gitlabhq committed
204
end