projects_controller.rb 2.65 KB
Newer Older
randx's avatar
randx committed
1
require Rails.root.join('lib', 'gitlab', 'graph', 'json_builder')
Valery Sizov's avatar
Valery Sizov committed
2

3
class ProjectsController < ProjectResourceController
4
  skip_before_filter :project, only: [:new, :create]
5
  skip_before_filter :repository, only: [:new, :create]
gitlabhq's avatar
gitlabhq committed
6 7

  # Authorize
8 9 10
  before_filter :authorize_read_project!, except: [:index, :new, :create]
  before_filter :authorize_admin_project!, only: [:edit, :update, :destroy]
  before_filter :require_non_empty_project, only: [:blob, :tree, :graph]
gitlabhq's avatar
gitlabhq committed
11

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

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

  def edit
  end

  def create
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22
    @project = Projects::CreateContext.new(current_user, params[:project]).execute
gitlabhq's avatar
gitlabhq committed
23 24

    respond_to do |format|
25
      flash[:notice] = 'Project was successfully created.' if @project.saved?
26 27
      format.html do
        if @project.saved?
28
          redirect_to @project
29 30 31
        else
          render action: "new"
        end
gitlabhq's avatar
gitlabhq committed
32 33 34 35
      end
      format.js
    end
  end
gitlabhq's avatar
gitlabhq committed
36

gitlabhq's avatar
gitlabhq committed
37
  def update
38
    status = Projects::UpdateContext.new(project, current_user, params).execute
39

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

  rescue Project::TransferError => ex
    @error = ex
    render :update_failed
gitlabhq's avatar
gitlabhq committed
54 55 56
  end

  def show
57
    limit = (params[:limit] || 20).to_i
58
    @events = @project.events.recent.limit(limit).offset(params[:offset] || 0)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59 60

    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
61
      format.html do
62
        if @project.repository && !@project.repository.empty?
63 64 65 66 67
          @last_push = current_user.recent_push(@project.id)
          render :show
        else
          render "projects/empty"
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
68
      end
69
      format.js
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
70
    end
71 72 73
  end

  def files
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74
    @notes = @project.notes.where("attachment != 'NULL'").order("created_at DESC").limit(100)
75 76
  end

gitlabhq's avatar
gitlabhq committed
77 78 79
  #
  # Wall
  #
80

gitlabhq's avatar
gitlabhq committed
81
  def wall
82
    return render_404 unless @project.wall_enabled
Riyad Preukschas's avatar
Riyad Preukschas committed
83 84 85 86

    @target_type = :wall
    @target_id = nil
    @note = @project.notes.new
gitlabhq's avatar
gitlabhq committed
87

88
    respond_to do |format|
gitlabhq's avatar
gitlabhq committed
89 90
      format.html
    end
gitlabhq's avatar
gitlabhq committed
91
  end
92

Valery Sizov's avatar
Valery Sizov committed
93
  def graph
Koen Punt's avatar
Koen Punt committed
94 95 96 97
    respond_to do |format|
      format.html
      format.json do
        graph = Gitlab::Graph::JsonBuilder.new(project)
98
        render :json => graph.to_json
Koen Punt's avatar
Koen Punt committed
99 100
      end
    end
Valery Sizov's avatar
Valery Sizov committed
101 102
  end

gitlabhq's avatar
gitlabhq committed
103
  def destroy
104 105
    return access_denied! unless can?(current_user, :remove_project, project)

106
    # Delete team first in order to prevent multiple gitolite calls
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
107
    project.team.truncate
108

gitlabhq's avatar
gitlabhq committed
109 110 111
    project.destroy

    respond_to do |format|
randx's avatar
randx committed
112
      format.html { redirect_to root_path }
gitlabhq's avatar
gitlabhq committed
113 114 115
    end
  end
end