projects_controller.rb 2.6 KB
Newer Older
R
randx 已提交
1
require Rails.root.join('lib', 'gitlab', 'graph', 'json_builder')
V
Valery Sizov 已提交
2

3
class ProjectsController < ProjectResourceController
4
  skip_before_filter :project, only: [:new, :create]
5
  skip_before_filter :repository, only: [:new, :create]
G
gitlabhq 已提交
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]
G
gitlabhq 已提交
11

C
Cyril 已提交
12 13
  layout 'application', only: [:new, :create]

G
gitlabhq 已提交
14 15 16 17 18 19 20 21
  def new
    @project = Project.new
  end

  def edit
  end

  def create
D
Dmitriy Zaporozhets 已提交
22
    @project = Projects::CreateContext.new(current_user, params[:project]).execute
G
gitlabhq 已提交
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
G
gitlabhq 已提交
32 33 34 35
      end
      format.js
    end
  end
G
gitlabhq 已提交
36

G
gitlabhq 已提交
37
  def update
38
    status = Projects::UpdateContext.new(project, current_user, params).execute
39

G
gitlabhq 已提交
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.' }
N
Nihad Abbasov 已提交
44
        format.js
G
gitlabhq 已提交
45 46
      else
        format.html { render action: "edit" }
N
Nihad Abbasov 已提交
47
        format.js
G
gitlabhq 已提交
48
      end
G
gitlabhq 已提交
49
    end
50 51 52 53

  rescue Project::TransferError => ex
    @error = ex
    render :update_failed
G
gitlabhq 已提交
54 55 56
  end

  def show
57
    limit = (params[:limit] || 20).to_i
R
randx 已提交
58
    @events = @project.events.recent.limit(limit).offset(params[:offset] || 0)
D
Dmitriy Zaporozhets 已提交
59 60

    respond_to do |format|
N
Nihad Abbasov 已提交
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
D
Dmitriy Zaporozhets 已提交
68
      end
R
randx 已提交
69
      format.js
D
Dmitriy Zaporozhets 已提交
70
    end
71 72 73
  end

  def files
D
Dmitriy Zaporozhets 已提交
74
    @notes = @project.notes.where("attachment != 'NULL'").order("created_at DESC").limit(100)
75 76
  end

G
gitlabhq 已提交
77 78 79
  #
  # Wall
  #
80

G
gitlabhq 已提交
81
  def wall
82
    return render_404 unless @project.wall_enabled
R
Riyad Preukschas 已提交
83 84 85 86

    @target_type = :wall
    @target_id = nil
    @note = @project.notes.new
G
gitlabhq 已提交
87

N
Nihad Abbasov 已提交
88
    respond_to do |format|
G
gitlabhq 已提交
89 90
      format.html
    end
G
gitlabhq 已提交
91
  end
92

V
Valery Sizov 已提交
93
  def graph
K
Koen Punt 已提交
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
K
Koen Punt 已提交
99 100
      end
    end
V
Valery Sizov 已提交
101 102
  end

G
gitlabhq 已提交
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
D
Dmitriy Zaporozhets 已提交
107
    project.team.truncate
108

G
gitlabhq 已提交
109 110 111
    project.destroy

    respond_to do |format|
R
randx 已提交
112
      format.html { redirect_to root_path }
G
gitlabhq 已提交
113 114 115
    end
  end
end