projects_controller.rb 2.94 KB
Newer Older
1
class ProjectsController < ProjectResourceController
2
  skip_before_filter :project, only: [:new, :create]
3
  skip_before_filter :repository, only: [:new, :create]
gitlabhq's avatar
gitlabhq committed
4
5

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

10
  layout 'navless', only: [:new, :create, :fork]
11
  before_filter :set_title, only: [:new, :create]
12

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

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

  def create
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
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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.'
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
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
      else
46
        format.html { render action: "edit", layout: "project_settings" }
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
  end
51

52
53
  def transfer
    ::Projects::TransferContext.new(project, current_user, params).execute
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
63
64
        if @project.empty_repo?
          render "projects/empty"
        else
65
66
67
          @last_push = current_user.recent_push(@project.id)
          render :show
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
68
      end
69
      format.js
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
70
    end
71
72
  end

gitlabhq's avatar
gitlabhq committed
73
  def destroy
74
75
    return access_denied! unless can?(current_user, :remove_project, project)

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
76
    project.team.truncate
gitlabhq's avatar
gitlabhq committed
77
78
79
    project.destroy

    respond_to do |format|
randx's avatar
randx committed
80
      format.html { redirect_to root_path }
gitlabhq's avatar
gitlabhq committed
81
82
    end
  end
83
84

  def fork
85
    @forked_project = ::Projects::ForkContext.new(project, current_user).execute
86
87
88

    respond_to do |format|
      format.html do
89
90
        if @forked_project.saved? && @forked_project.forked?
          redirect_to(@forked_project, notice: 'Project was successfully forked.')
91
        else
92
93
          @title = 'Fork project'
          render action: "fork"
94
95
96
97
98
        end
      end
      format.js
    end
  end
99
100
101
102
103

  def autocomplete_sources
    @suggestions = {
      emojis: Emoji.names,
      issues: @project.issues.select([:id, :title, :description]),
104
      members: @project.team.members.sort_by(&:username).map { |user| { username: user.username, name: user.name } }
105
106
107
108
109
110
    }

    respond_to do |format|
      format.json { render :json => @suggestions }
    end
  end
111
112
113
114
115
116

  private

  def set_title
    @title = 'New Project'
  end
gitlabhq's avatar
gitlabhq committed
117
end