BigW Consortium Gitlab

projects_controller.rb 5.45 KB
Newer Older
1
class ProjectsController < ApplicationController
2
  prepend_before_filter :render_go_import, only: [:show]
3
  skip_before_action :authenticate_user!, only: [:show, :activity]
4 5
  before_action :project, except: [:new, :create]
  before_action :repository, except: [:new, :create]
gitlabhq committed
6 7

  # Authorize
8
  before_action :authorize_admin_project!, only: [:edit, :update, :destroy, :transfer, :archive, :unarchive]
9
  before_action :event_filter, only: [:show, :activity]
gitlabhq committed
10

11
  layout :determine_layout
12

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

  def edit
18
    render 'edit'
gitlabhq committed
19 20 21
  end

  def create
22
    @project = ::Projects::CreateService.new(current_user, project_params).execute
gitlabhq committed
23

24
    if @project.saved?
Vinnie Okada committed
25
      redirect_to(
26
        project_path(@project),
27
        notice: "Project '#{@project.name}' was successfully created."
Vinnie Okada committed
28
      )
29 30
    else
      render 'new'
gitlabhq committed
31 32
    end
  end
gitlabhq committed
33

gitlabhq committed
34
  def update
35
    status = ::Projects::UpdateService.new(@project, current_user, project_params).execute
36

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

54
  def transfer
55 56 57 58 59
    namespace = Namespace.find_by(id: params[:new_namespace_id])
    ::Projects::TransferService.new(project, current_user).execute(namespace)

    if @project.errors[:new_namespace].present?
      flash[:alert] = @project.errors[:new_namespace].first
skv-headless committed
60
    end
gitlabhq committed
61 62
  end

63 64 65 66 67 68 69 70 71 72
  def activity
    respond_to do |format|
      format.html
      format.json do
        load_events
        pager_json('events/_events', @events.count)
      end
    end
  end

gitlabhq committed
73
  def show
74
    if @project.import_in_progress?
Vinnie Okada committed
75
      redirect_to namespace_project_import_path(@project.namespace, @project)
76 77 78
      return
    end

Dmitriy Zaporozhets committed
79
    respond_to do |format|
Nihad Abbasov committed
80
      format.html do
81 82
        if @project.repository_exists?
          if @project.empty_repo?
83
            render 'projects/empty'
84
          else
85
            render :show
86
          end
87
        else
88
          render 'projects/no_repo'
89
        end
Dmitriy Zaporozhets committed
90
      end
91

92 93 94 95
      format.atom do
        load_events
        render layout: false
      end
96 97 98
    end
  end

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

102
    ::Projects::DestroyService.new(@project, current_user, {}).execute
103
    flash[:alert] = "Project '#{@project.name}' was deleted."
gitlabhq committed
104

105 106 107 108
    if request.referer.include?('/admin')
      redirect_to admin_namespaces_projects_path
    else
      redirect_to dashboard_path
gitlabhq committed
109
    end
110 111
  rescue Projects::DestroyService::DestroyError => ex
    redirect_to edit_project_path(@project), alert: ex.message
gitlabhq committed
112
  end
113

114
  def autocomplete_sources
Marin Jankovski committed
115 116
    note_type = params['type']
    note_id = params['type_id']
117
    autocomplete = ::Projects::AutocompleteService.new(@project)
118
    participants = ::Projects::ParticipantsService.new(@project, current_user).execute(note_type, note_id)
119

120
    @suggestions = {
121
      emojis: autocomplete_emojis,
122 123
      issues: autocomplete.issues,
      mergerequests: autocomplete.merge_requests,
124
      members: participants
125 126 127
    }

    respond_to do |format|
128
      format.json { render json: @suggestions }
129 130
    end
  end
131

132
  def archive
133 134
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.archive!
135 136

    respond_to do |format|
137
      format.html { redirect_to project_path(@project) }
138 139 140 141
    end
  end

  def unarchive
142 143
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.unarchive!
144 145

    respond_to do |format|
146
      format.html { redirect_to project_path(@project) }
147 148 149
    end
  end

Ciro Santilli committed
150 151
  def toggle_star
    current_user.toggle_star(@project)
152
    @project.reload
153 154 155 156

    render json: {
      html: view_to_html_string("projects/buttons/_star")
    }
Ciro Santilli committed
157 158
  end

159
  def markdown_preview
160
    text = params[:text]
161 162 163 164 165 166 167 168 169 170

    ext = Gitlab::ReferenceExtractor.new(@project, current_user)
    ext.analyze(text)

    render json: {
      body:       view_context.markdown(text),
      references: {
        users: ext.users.map(&:username)
      }
    }
171 172
  end

173 174
  private

175 176 177 178 179 180 181 182
  def determine_layout
    if [:new, :create].include?(action_name.to_sym)
      'application'
    elsif [:edit, :update].include?(action_name.to_sym)
      'project_settings'
    else
      'project'
    end
183
  end
184

185 186 187 188 189 190 191
  def load_events
    @events = @project.events.recent
    @events = event_filter.apply_filter(@events).with_associations
    limit = (params[:limit] || 20).to_i
    @events = @events.limit(limit).offset(params[:offset] || 0)
  end

192 193
  def project_params
    params.require(:project).permit(
194
      :name, :path, :description, :issues_tracker, :tag_list,
195
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
196
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar
197 198
    )
  end
199 200

  def autocomplete_emojis
201 202
    Rails.cache.fetch("autocomplete-emoji-#{Gemojione::VERSION}") do
      Emoji.emojis.map do |name, emoji|
203
        {
204 205
          name: name,
          path: view_context.image_url("emoji/#{emoji["unicode"]}.png")
206 207 208 209
        }
      end
    end
  end
210 211 212 213 214 215 216 217 218 219

  def render_go_import
    return unless params["go-get"] == "1"

    @namespace = params[:namespace_id]
    @id = params[:project_id] || params[:id]
    @id = @id.gsub(/\.git\Z/, "")

    render "go_import", layout: false
  end
gitlabhq committed
220
end