projects_controller.rb 2.59 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]
gitlabhq's avatar
gitlabhq committed
5 6

  # Authorize
7 8 9
  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
10

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

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

  def edit
  end

  def create
21
    @project = Project.create_by_user(params[:project], current_user)
gitlabhq's avatar
gitlabhq committed
22 23

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

gitlabhq's avatar
gitlabhq committed
36
  def update
37
    status = ProjectUpdateContext.new(project, current_user, params).execute
38

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

  def show
52
    limit = (params[:limit] || 20).to_i
53
    @events = @project.events.recent.limit(limit).offset(params[:offset] || 0)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
54 55

    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
56
      format.html do
57 58 59 60 61 62
        unless @project.empty_repo?
          @last_push = current_user.recent_push(@project.id)
          render :show
        else
          render "projects/empty"
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
63
      end
64
      format.js
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
65
    end
66 67 68
  end

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

gitlabhq's avatar
gitlabhq committed
72 73 74
  #
  # Wall
  #
75

gitlabhq's avatar
gitlabhq committed
76
  def wall
77
    return render_404 unless @project.wall_enabled
gitlabhq's avatar
gitlabhq committed
78
    @note = Note.new
gitlabhq's avatar
gitlabhq committed
79

80
    respond_to do |format|
gitlabhq's avatar
gitlabhq committed
81 82
      format.html
    end
gitlabhq's avatar
gitlabhq committed
83
  end
84

Valery Sizov's avatar
Valery Sizov committed
85
  def graph
Koen Punt's avatar
Koen Punt committed
86 87 88 89
    respond_to do |format|
      format.html
      format.json do
        graph = Gitlab::Graph::JsonBuilder.new(project)
90
        render :json => graph.to_json
Koen Punt's avatar
Koen Punt committed
91 92
      end
    end
Valery Sizov's avatar
Valery Sizov committed
93 94
  end

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

98 99 100
    # Disable the UsersProject update_repository call, otherwise it will be
    # called once for every person removed from the project
    UsersProject.skip_callback(:destroy, :after, :update_repository)
gitlabhq's avatar
gitlabhq committed
101
    project.destroy
102
    UsersProject.set_callback(:destroy, :after, :update_repository)
gitlabhq's avatar
gitlabhq committed
103 104

    respond_to do |format|
randx's avatar
randx committed
105
      format.html { redirect_to root_path }
gitlabhq's avatar
gitlabhq committed
106 107 108
    end
  end
end