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

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

12
  layout 'navless', only: [:new, :create, :fork]
Cyril's avatar
Cyril committed
13

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

  def edit
19
    render 'edit', layout: 'project_settings'
gitlabhq's avatar
gitlabhq committed
20 21 22
  end

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

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

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

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

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

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

69
    limit = (params[:limit] || 20).to_i
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
70

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

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

87 88
      format.json do
        @events = @project.events.recent
89
        @events = event_filter.apply_filter(@events).with_associations
90 91 92
        @events = @events.limit(limit).offset(params[:offset] || 0)
        pager_json('events/_events', @events.count)
      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
gitlabhq's avatar
gitlabhq committed
100 101

    respond_to do |format|
102
      format.html do
103
        flash[:alert] = 'Project deleted.'
104

105
        if request.referer.include?('/admin')
106
          redirect_to admin_namespaces_projects_path
107
        else
108
          redirect_to dashboard_path
109
        end
110
      end
gitlabhq's avatar
gitlabhq committed
111 112
    end
  end
113

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

120
    @suggestions = {
121
      emojis: autocomplete_emojis,
122 123
      issues: autocomplete.issues,
      mergerequests: autocomplete.merge_requests,
124
      members: participants
125 126 127
    }

    respond_to do |format|
128
      format.json { render json: @suggestions }
129 130
    end
  end
131

132
  def archive
133 134
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.archive!
135 136

    respond_to do |format|
137
      format.html { redirect_to project_path(@project) }
138 139 140 141
    end
  end

  def unarchive
142 143
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.unarchive!
144 145

    respond_to do |format|
146
      format.html { redirect_to project_path(@project) }
147 148 149
    end
  end

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

156 157 158 159
  def markdown_preview
    render text: view_context.markdown(params[:md_text])
  end

160 161 162 163 164
  private

  def set_title
    @title = 'New Project'
  end
165 166

  def user_layout
167
    current_user ? 'projects' : 'public_projects'
168
  end
169 170 171

  def project_params
    params.require(:project).permit(
172
      :name, :path, :description, :issues_tracker, :tag_list,
173
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
174
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar
175 176
    )
  end
177 178

  def autocomplete_emojis
179 180
    Rails.cache.fetch("autocomplete-emoji-#{Gemojione::VERSION}") do
      Emoji.emojis.map do |name, emoji|
181
        {
182 183
          name: name,
          path: view_context.image_url("emoji/#{emoji["unicode"]}.png")
184 185 186 187
        }
      end
    end
  end
188 189 190 191 192 193 194 195 196 197

  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
198
end