Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

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

  # Authorize
7
  before_filter :authorize_admin_project!, only: [:edit, :update, :destroy, :transfer, :archive, :unarchive]
gitlabhq's avatar
gitlabhq committed
8

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

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

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

  def create
21
    @project = ::Projects::CreateService.new(current_user, project_params).execute
gitlabhq's avatar
gitlabhq committed
22

23
24
25
26
    if @project.saved?
      redirect_to project_path(@project), notice: 'Project was successfully created.'
    else
      render 'new'
gitlabhq's avatar
gitlabhq committed
27
28
    end
  end
gitlabhq's avatar
gitlabhq committed
29

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

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

45
  def transfer
46
    ::Projects::TransferService.new(project, current_user, project_params).execute
skv-headless's avatar
skv-headless committed
47
48
49
    if @project.errors[:namespace_id].present?
      flash[:alert] = @project.errors[:namespace_id].first
    end
gitlabhq's avatar
gitlabhq committed
50
51
52
  end

  def show
53
    if @project.import_in_progress?
54
      redirect_to project_import_path(@project)
55
56
57
      return
    end

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's avatar
Dmitriy Zaporozhets committed
62

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

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
65
    respond_to do |format|
Nihad Abbasov's avatar
Nihad Abbasov committed
66
      format.html do
67
68
        if @project.repository_exists?
          if @project.empty_repo?
69
            render 'projects/empty', layout: user_layout
70
71
72
73
          else
            @last_push = current_user.recent_push(@project.id) if current_user
            render :show, layout: user_layout
          end
74
        else
75
          render 'projects/no_repo', layout: user_layout
76
        end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
77
      end
78

79
      format.json { pager_json('events/_events', @events.count) }
80
81
82
    end
  end

gitlabhq's avatar
gitlabhq committed
83
  def destroy
84
    return access_denied! unless can?(current_user, :remove_project, @project)
85

86
    ::Projects::DestroyService.new(@project, current_user, {}).execute
gitlabhq's avatar
gitlabhq committed
87
88

    respond_to do |format|
89
      format.html do
90
        flash[:alert] = 'Project deleted.'
91

92
        if request.referer.include?('/admin')
93
94
95
96
          redirect_to admin_projects_path
        else
          redirect_to projects_dashboard_path
        end
97
      end
gitlabhq's avatar
gitlabhq committed
98
99
    end
  end
100

101
  def autocomplete_sources
Marin Jankovski's avatar
Marin Jankovski committed
102
103
    note_type = params['type']
    note_id = params['type_id']
104
    autocomplete = ::Projects::AutocompleteService.new(@project)
105
    participants = ::Projects::ParticipantsService.new(@project).execute(note_type, note_id)
106

107
    @suggestions = {
108
      emojis: autocomplete_emojis,
109
110
      issues: autocomplete.issues,
      mergerequests: autocomplete.merge_requests,
111
      members: participants
112
113
114
    }

    respond_to do |format|
115
      format.json { render json: @suggestions }
116
117
    end
  end
118

119
  def archive
120
121
    return access_denied! unless can?(current_user, :archive_project, @project)
    @project.archive!
122
123
124
125
126
127
128

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

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

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

137
  def upload_image
Marin Jankovski's avatar
Marin Jankovski committed
138
    link_to_image = ::Projects::ImageService.new(repository, params, root_url).execute
139

140
    respond_to do |format|
Marin Jankovski's avatar
Marin Jankovski committed
141
142
      if link_to_image
        format.json { render json: { link: link_to_image } }
143
      else
144
        format.json { render json: 'Invalid file.', status: :unprocessable_entity }
145
      end
146
147
148
    end
  end

Ciro Santilli's avatar
Ciro Santilli committed
149
150
  def toggle_star
    current_user.toggle_star(@project)
151
    @project.reload
Ciro Santilli's avatar
Ciro Santilli committed
152
153
154
    render json: { star_count: @project.star_count }
  end

155
156
157
158
  def markdown_preview
    render text: view_context.markdown(params[:md_text])
  end

159
160
  private

161
162
163
164
165
166
167
168
169
  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

170
171
172
  def set_title
    @title = 'New Project'
  end
173
174

  def user_layout
175
    current_user ? 'projects' : 'public_projects'
176
  end
177
178
179

  def project_params
    params.require(:project).permit(
180
      :name, :path, :description, :issues_tracker, :tag_list,
181
      :issues_enabled, :merge_requests_enabled, :snippets_enabled, :issues_tracker_id, :default_branch,
182
      :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar
183
184
    )
  end
185
186
187
188
189
190
191
192
193
194
195

  def autocomplete_emojis
    Rails.cache.fetch("autocomplete-emoji-#{Emoji::VERSION}") do
      Emoji.names.map do |e|
        {
          name: e,
          path: view_context.image_url("emoji/#{e}.png")
        }
      end
    end
  end
gitlabhq's avatar
gitlabhq committed
196
end