Read about our upcoming Code of Conduct on this issue

project_presenter.rb 9.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
class ProjectPresenter < Gitlab::View::Presenter::Delegated
  include ActionView::Helpers::NumberHelper
  include ActionView::Helpers::UrlHelper
  include GitlabRoutingHelper
  include StorageHelper
  include TreeHelper

  presents :project

  def project_stat_anchor_items(show_auto_devops_callout:)
    [
      files_anchor_data,
      commits_anchor_data,
      branches_anchor_data,
      tags_anchor_data,
      readme_anchor_data,
      changelog_anchor_data,
      license_anchor_data,
      contribution_guide_anchor_data,
      gitlab_ci_anchor_data,
      autodevops_anchor_data(show_auto_devops_callout: show_auto_devops_callout),
      kubernetes_cluster_anchor_data
    ].compact.reject { |i| !i[:enabled] }
  end

  def project_stat_button_items(show_auto_devops_callout:)
    [
      changelog_anchor_data,
      license_anchor_data,
      contribution_guide_anchor_data,
      autodevops_anchor_data(show_auto_devops_callout: show_auto_devops_callout),
      kubernetes_cluster_anchor_data,
      gitlab_ci_anchor_data,
      koding_anchor_data
    ].compact.reject { |i| i[:enabled] }
  end

  def empty_project_stat_anchor_items
    [
      autodevops_anchor_data,
      kubernetes_cluster_anchor_data
    ].compact.reject { |i| !i[:enabled] }
  end

  def empty_project_stat_button_items
    [
      new_file_anchor_data,
      readme_anchor_data,
      license_anchor_data,
      autodevops_anchor_data,
      kubernetes_cluster_anchor_data
    ].compact.reject { |i| i[:enabled] }
  end

  def default_project_view
    return anonymous_project_view unless current_user

    user_view = current_user.project_view

    if can?(current_user, :download_code, project)
      user_view
    elsif user_view == "activity"
      "activity"
    elsif can?(current_user, :read_wiki, project)
      "wiki"
    elsif feature_available?(:issues, current_user)
      "projects/issues/issues"
    else
      "customize_workflow"
    end
  end

  def readme_path
    filename_path(:readme)
  end

  def changelog_path
    filename_path(:changelog)
  end

  def license_path
    filename_path(:license_blob)
  end

  def ci_configuration_path
    filename_path(:gitlab_ci_yml)
  end

  def contribution_guide_path
    if project && contribution_guide = repository.contribution_guide
      project_blob_path(
        project,
        tree_join(project.default_branch,
                  contribution_guide.name)
      )
    end
  end

  def add_license_path
    add_special_file_path(file_name: 'LICENSE')
  end

  def add_ci_yml_path
    add_special_file_path(file_name: '.gitlab-ci.yml')
  end

  def add_readme_path
    add_special_file_path(file_name: 'README.md')
  end

  def add_koding_stack_path
    project_new_blob_path(
      project,
      default_branch || 'master',
      file_name:      '.koding.yml',
      commit_message: "Add Koding stack script",
      content: <<-CONTENT.strip_heredoc
        provider:
          aws:
            access_key: '${var.aws_access_key}'
            secret_key: '${var.aws_secret_key}'
        resource:
          aws_instance:
            #{project.path}-vm:
              instance_type: t2.nano
              user_data: |-

                # Created by GitLab UI for :>

                echo _KD_NOTIFY_@Installing Base packages...@

                apt-get update -y
                apt-get install git -y

                echo _KD_NOTIFY_@Cloning #{project.name}...@

                export KODING_USER=${var.koding_user_username}
                export REPO_URL=#{root_url}${var.koding_queryString_repo}.git
                export BRANCH=${var.koding_queryString_branch}

                sudo -i -u $KODING_USER git clone $REPO_URL -b $BRANCH

                echo _KD_NOTIFY_@#{project.name} cloned.@
      CONTENT
    )
  end

  def license_short_name
    license = repository.license
    license&.nickname || license&.name || 'LICENSE'
  end

  private

  def filename_path(filename)
    if blob = repository.public_send(filename) # rubocop:disable GitlabSecurity/PublicSend
      project_blob_path(
        project,
        tree_join(default_branch, blob.name)
      )
    end
  end

  def anonymous_project_view
    if !project.empty_repo? && can?(current_user, :download_code, project)
      'files'
    else
      'activity'
    end
  end

  def add_special_file_path(file_name:, commit_message: nil, branch_name: nil)
    commit_message ||= s_("CommitMessage|Add %{file_name}") % { file_name: file_name }
    project_new_blob_path(
      project,
      project.default_branch || 'master',
      file_name:      file_name,
      commit_message: commit_message,
      branch_name:    branch_name
    )
  end

  def can_current_user_push_code?
    if empty_repo?
      can?(current_user, :push_code, project)
    else
      user_can_push_to_branch?(current_user, default_branch)
    end
  end

  def files_anchor_data
    {
      enabled: true,
      label: _('Files (%{human_size})') % { human_size: storage_counter(statistics.total_repository_size) },
      link: project_tree_path(project)
    }
  end

  def commits_anchor_data
    {
      enabled: true,
      label: n_('Commit (%{commit_count})', 'Commits (%{commit_count})', statistics.commit_count) % { commit_count: number_with_delimiter(statistics.commit_count) },
      link: project_commits_path(project, repository.root_ref)
    }
  end

  def branches_anchor_data
    {
      enabled: true,
      label: n_('Branch (%{branch_count})', 'Branches (%{branch_count})', repository.branch_count) % { branch_count: number_with_delimiter(repository.branch_count) },
      link: project_branches_path(project)
    }
  end

  def tags_anchor_data
    {
      enabled: true,
      label: n_('Tag (%{tag_count})', 'Tags (%{tag_count})', repository.tag_count) % { tag_count: number_with_delimiter(repository.tag_count) },
      link: project_tags_path(project)
    }
  end

  def new_file_anchor_data
    if current_user && can_current_user_push_code?
      {
        enabled: false,
        label: _('New file'),
        link: project_new_blob_path(project, default_branch || 'master'),
        class_modifier: 'new'
      }
    end
  end

  def readme_anchor_data
    if current_user && can_current_user_push_code? && repository.readme.blank?
      {
        enabled: false,
        label: _('Add Readme'),
        link: add_readme_path
      }
    elsif repository.readme.present?
      {
        enabled: true,
        label: _('Readme'),
        link: default_project_view != 'readme' ? readme_path : '#readme'
      }
    end
  end

  def changelog_anchor_data
    if current_user && can_current_user_push_code? && repository.changelog.blank?
      {
        enabled: false,
        label: _('Add Changelog'),
        link: add_special_file_path(file_name: 'CHANGELOG')
      }
    elsif repository.changelog.present?
      {
        enabled: true,
        label: _('Changelog'),
        link: changelog_path
      }
    end
  end

  def license_anchor_data
    if current_user && can_current_user_push_code? && repository.license_blob.blank?
      {
        enabled: false,
        label: _('Add License'),
        link: add_license_path
      }
    elsif repository.license_blob.present?
      {
        enabled: true,
        label: license_short_name,
        link: license_path
      }
    end
  end

  def contribution_guide_anchor_data
    if current_user && can_current_user_push_code? && repository.contribution_guide.blank?
      {
        enabled: false,
        label: _('Add Contribution guide'),
        link: add_special_file_path(file_name: 'CONTRIBUTING.md', commit_message: 'Add contribution guide')
      }
    elsif repository.contribution_guide.present?
      {
        enabled: true,
        label: _('Contribution guide'),
        link: contribution_guide_path
      }
    end
  end

  def autodevops_anchor_data(show_auto_devops_callout: false)
    if current_user && can?(current_user, :admin_pipeline, project) && repository.gitlab_ci_yml.blank? && !show_auto_devops_callout
      {
        enabled: auto_devops_enabled?,
        label: auto_devops_enabled? ? _('Auto DevOps enabled') : _('Enable Auto DevOps'),
        link: project_settings_ci_cd_path(project, anchor: 'js-general-pipeline-settings')
      }
    elsif auto_devops_enabled?
      {
        enabled: true,
        label: _('Auto DevOps enabled'),
        link: nil
      }
    end
  end

  def kubernetes_cluster_anchor_data
    if current_user && can?(current_user, :create_cluster, project)
      cluster_link = clusters.size == 1 ? project_cluster_path(project, clusters.first) : project_clusters_path(project)

      if clusters.empty?
        cluster_link = new_project_cluster_path(project)
      end

      {
        enabled: !clusters.empty?,
        label: clusters.empty? ? _('Add Kubernetes cluster') : n_('Kubernetes cluster', 'Kubernetes clusters', clusters.size),
        link: cluster_link
      }
    end
  end

  def gitlab_ci_anchor_data
    if current_user && can_current_user_push_code? && repository.gitlab_ci_yml.blank? && !auto_devops_enabled?
      {
        enabled: false,
        label: _('Set up CI/CD'),
        link: add_ci_yml_path
      }
    elsif repository.gitlab_ci_yml.present?
      {
        enabled: true,
        label: _('CI/CD configuration'),
        link: ci_configuration_path
      }
    end
  end

  def koding_anchor_data
    if current_user && can_current_user_push_code? && koding_enabled? && repository.koding_yml.blank?
      {
        enabled: false,
        label: _('Set up Koding'),
        link: add_koding_stack_path
      }
    end
  end

  def koding_enabled?
    Gitlab::CurrentSettings.koding_enabled?
  end
end