BigW Consortium Gitlab

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

  # Authorize
7
  before_filter :authorize_read_project!, except: [:index, :new, :create]
8
  before_filter :authorize_admin_project!, only: [:edit, :update, :destroy, :transfer, :archive, :unarchive, :retry_import]
9
  before_filter :require_non_empty_project, only: [:blob, :tree, :graph]
gitlabhq committed
10

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

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

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

  def create
23
    @project = ::Projects::CreateService.new(current_user, project_params).execute
24
    flash[:notice] = 'Project was successfully created.' if @project.saved?
gitlabhq committed
25 26 27 28 29

    respond_to do |format|
      format.js
    end
  end
gitlabhq committed
30

gitlabhq committed
31
  def update
32
    status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
33

gitlabhq committed
34
    respond_to do |format|
35
      if status
36
        flash[:notice] = 'Project was successfully updated.'
37
        format.html { redirect_to edit_project_path(@project), notice: 'Project was successfully updated.' }
Nihad Abbasov committed
38
        format.js
gitlabhq committed
39
      else
40
        format.html { render "edit", layout: "project_settings" }
Nihad Abbasov committed
41
        format.js
gitlabhq committed
42
      end
gitlabhq committed
43
    end
44
  end
45

46
  def transfer
47
    ::Projects::TransferService.new(project, current_user, project_params).execute
gitlabhq committed
48 49 50
  end

  def show
51 52 53 54 55
    if @project.import_in_progress?
      redirect_to import_project_path(@project)
      return
    end

56
    return authenticate_user! unless @project.public? || current_user
57

58
    limit = (params[:limit] || 20).to_i
59 60 61
    @events = @project.events.recent
    @events = event_filter.apply_filter(@events)
    @events = @events.limit(limit).offset(params[:offset] || 0)
Dmitriy Zaporozhets committed
62

Ciro Santilli committed
63
    @show_star = !(current_user && current_user.starred?(@project))
64

Dmitriy Zaporozhets committed
65
    respond_to do |format|
Nihad Abbasov committed
66
      format.html do
67
        if @project.empty_repo?
68
          render "projects/empty", layout: user_layout
69
        else
70
          @last_push = current_user.recent_push(@project.id) if current_user
71
          render :show, layout: user_layout
72
        end
Dmitriy Zaporozhets committed
73
      end
74
      format.json { pager_json("events/_events", @events.count) }
Dmitriy Zaporozhets committed
75
    end
76 77
  end

78
  def import
79
    if @project.import_finished?
80 81 82 83 84 85 86 87 88 89
      redirect_to @project
      return
    end
  end

  def retry_import
    unless @project.import_failed?
      redirect_to import_project_path(@project)
    end

90
    @project.import_url = project_params[:import_url]
91 92 93 94 95 96 97 98 99

    if @project.save
      @project.reload
      @project.import_retry
    end

    redirect_to import_project_path(@project)
  end

gitlabhq committed
100
  def destroy
101
    return access_denied! unless can?(current_user, :remove_project, @project)
102

103
    ::Projects::DestroyService.new(@project, current_user, {}).execute
gitlabhq committed
104 105

    respond_to do |format|
106 107
      format.html do
        flash[:alert] = "Project deleted."
108 109 110 111 112 113

        if request.referer.include?("/admin")
          redirect_to admin_projects_path
        else
          redirect_to projects_dashboard_path
        end
114
      end
gitlabhq committed
115 116
    end
  end
117 118

  def fork
119
    @forked_project = ::Projects::ForkService.new(project, current_user).execute
120 121 122

    respond_to do |format|
      format.html do
123 124
        if @forked_project.saved? && @forked_project.forked?
          redirect_to(@forked_project, notice: 'Project was successfully forked.')
125
        else
126
          @title = 'Fork project'
127
          render "fork"
128 129 130 131 132
        end
      end
      format.js
    end
  end
133 134

  def autocomplete_sources
Marin Jankovski committed
135 136
    note_type = params['type']
    note_id = params['type_id']
137
    participants = ::Projects::ParticipantsService.new(@project).execute(note_type, note_id)
138
    @suggestions = {
139
      emojis: Emoji.names.map { |e| { name: e, path: view_context.image_url("emoji/#{e}.png") } },
140
      issues: @project.issues.select([:iid, :title, :description]),
141
      mergerequests: @project.merge_requests.select([:iid, :title, :description]),
142
      members: participants
143 144 145 146 147 148
    }

    respond_to do |format|
      format.json { render :json => @suggestions }
    end
  end
149

150
  def archive
151 152
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.archive!
153 154 155 156 157 158 159

    respond_to do |format|
      format.html { redirect_to @project }
    end
  end

  def unarchive
160 161
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.unarchive!
162 163 164 165 166 167

    respond_to do |format|
      format.html { redirect_to @project }
    end
  end

168
  def upload_image
169
    link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute
170

171
    respond_to do |format|
172 173
      if link_to_image
        format.json { render json: { link: link_to_image } }
174 175 176
      else
        format.json { render json: "Invalid file.", status: :unprocessable_entity }
      end
177 178 179
    end
  end

Ciro Santilli committed
180 181
  def toggle_star
    current_user.toggle_star(@project)
182
    @project.reload
Ciro Santilli committed
183 184 185
    render json: { star_count: @project.star_count }
  end

186 187
  private

188 189 190 191 192 193 194 195 196
  def upload_path
    base_dir = FileUploader.generate_dir
    File.join(repository.path_with_namespace, base_dir)
  end

  def accepted_images
    %w(png jpg jpeg gif)
  end

197 198 199
  def set_title
    @title = 'New Project'
  end
200 201 202 203

  def user_layout
    current_user ? "projects" : "public_projects"
  end
204 205 206

  def project_params
    params.require(:project).permit(
207
      :name, :path, :description, :issues_tracker, :tag_list,
208
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
209 210 211
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id
    )
  end
gitlabhq committed
212
end