PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/app/helpers/projects_helper.rb

https://gitlab.com/jlogandavison/gitlab-ce
Ruby | 461 lines | 366 code | 90 blank | 5 comment | 48 complexity | e40e413809569ef4b3274d38d851fb27 MD5 | raw file
  1. module ProjectsHelper
  2. def link_to_project(project)
  3. link_to [project.namespace.becomes(Namespace), project], title: h(project.name) do
  4. title = content_tag(:span, project.name, class: 'project-name')
  5. if project.namespace
  6. namespace = content_tag(:span, "#{project.namespace.human_name} / ", class: 'namespace-name')
  7. title = namespace + title
  8. end
  9. title
  10. end
  11. end
  12. def link_to_member_avatar(author, opts = {})
  13. default_opts = { avatar: true, name: true, size: 16, author_class: 'author', title: ":name" }
  14. opts = default_opts.merge(opts)
  15. image_tag(avatar_icon(author, opts[:size]), width: opts[:size], class: "avatar avatar-inline #{"s#{opts[:size]}" if opts[:size]}", alt: '') if opts[:avatar]
  16. end
  17. def link_to_member(project, author, opts = {}, &block)
  18. default_opts = { avatar: true, name: true, size: 16, author_class: 'author', title: ":name", tooltip: false }
  19. opts = default_opts.merge(opts)
  20. return "(deleted)" unless author
  21. author_html = ""
  22. # Build avatar image tag
  23. author_html << image_tag(avatar_icon(author, opts[:size]), width: opts[:size], class: "avatar avatar-inline #{"s#{opts[:size]}" if opts[:size]} #{opts[:avatar_class] if opts[:avatar_class]}", alt: '') if opts[:avatar]
  24. # Build name span tag
  25. if opts[:by_username]
  26. author_html << content_tag(:span, sanitize("@#{author.username}"), class: opts[:author_class]) if opts[:name]
  27. else
  28. tooltip_data = { placement: 'top' }
  29. author_html << content_tag(:span, sanitize(author.name), class: [opts[:author_class], ('has-tooltip' if opts[:tooltip])], title: (author.to_reference if opts[:tooltip]), data: (tooltip_data if opts[:tooltip])) if opts[:name]
  30. end
  31. author_html << capture(&block) if block
  32. author_html = author_html.html_safe
  33. if opts[:name]
  34. link_to(author_html, user_path(author), class: "author_link #{"#{opts[:extra_class]}" if opts[:extra_class]} #{"#{opts[:mobile_classes]}" if opts[:mobile_classes]}").html_safe
  35. else
  36. title = opts[:title].sub(":name", sanitize(author.name))
  37. link_to(author_html, user_path(author), class: "author_link has-tooltip", title: title, data: { container: 'body' }).html_safe
  38. end
  39. end
  40. def project_title(project)
  41. namespace_link =
  42. if project.group
  43. group_title(project.group)
  44. else
  45. owner = project.namespace.owner
  46. link_to(simple_sanitize(owner.name), user_path(owner))
  47. end
  48. project_link = link_to simple_sanitize(project.name), project_path(project), { class: "project-item-select-holder" }
  49. if current_user
  50. project_link << button_tag(type: 'button', class: 'dropdown-toggle-caret js-projects-dropdown-toggle', aria: { label: 'Toggle switch project dropdown' }, data: { target: '.js-dropdown-menu-projects', toggle: 'dropdown', order_by: 'last_activity_at' }) do
  51. icon("chevron-down")
  52. end
  53. end
  54. "#{namespace_link} / #{project_link}".html_safe
  55. end
  56. def remove_project_message(project)
  57. "You are going to remove #{project.name_with_namespace}.\n Removed project CANNOT be restored!\n Are you ABSOLUTELY sure?"
  58. end
  59. def transfer_project_message(project)
  60. "You are going to transfer #{project.name_with_namespace} to another owner. Are you ABSOLUTELY sure?"
  61. end
  62. def remove_fork_project_message(project)
  63. "You are going to remove the fork relationship to source project #{@project.forked_from_project.name_with_namespace}. Are you ABSOLUTELY sure?"
  64. end
  65. def project_nav_tabs
  66. @nav_tabs ||= get_project_nav_tabs(@project, current_user)
  67. end
  68. def project_nav_tab?(name)
  69. project_nav_tabs.include? name
  70. end
  71. def project_for_deploy_key(deploy_key)
  72. if deploy_key.has_access_to?(@project)
  73. @project
  74. else
  75. deploy_key.projects.find do |project|
  76. can?(current_user, :read_project, project)
  77. end
  78. end
  79. end
  80. def can_change_visibility_level?(project, current_user)
  81. return false unless can?(current_user, :change_visibility_level, project)
  82. if project.forked?
  83. project.forked_from_project.visibility_level > Gitlab::VisibilityLevel::PRIVATE
  84. else
  85. true
  86. end
  87. end
  88. def license_short_name(project)
  89. license = project.repository.license
  90. license&.nickname || license&.name || 'LICENSE'
  91. end
  92. def last_push_event
  93. return unless current_user
  94. project_ids = [@project.id]
  95. if fork = current_user.fork_of(@project)
  96. project_ids << fork.id
  97. end
  98. current_user.recent_push(project_ids)
  99. end
  100. def project_feature_access_select(field)
  101. # Don't show option "everyone with access" if project is private
  102. options = project_feature_options
  103. if @project.private?
  104. level = @project.project_feature.send(field)
  105. options.delete('Everyone with access')
  106. highest_available_option = options.values.max if level == ProjectFeature::ENABLED
  107. end
  108. options = options_for_select(options, selected: highest_available_option || @project.project_feature.public_send(field))
  109. content_tag(
  110. :select,
  111. options,
  112. name: "project[project_feature_attributes][#{field}]",
  113. id: "project_project_feature_attributes_#{field}",
  114. class: "pull-right form-control #{repo_children_classes(field)}",
  115. data: { field: field }
  116. ).html_safe
  117. end
  118. def link_to_autodeploy_doc
  119. link_to 'About auto deploy', help_page_path('ci/autodeploy/index'), target: '_blank'
  120. end
  121. def autodeploy_flash_notice(branch_name)
  122. "Branch <strong>#{truncate(sanitize(branch_name))}</strong> was created. To set up auto deploy, \
  123. choose a GitLab CI Yaml template and commit your changes. #{link_to_autodeploy_doc}".html_safe
  124. end
  125. def project_list_cache_key(project)
  126. key = [
  127. project.route.cache_key,
  128. project.cache_key,
  129. controller.controller_name,
  130. controller.action_name,
  131. current_application_settings.cache_key,
  132. 'v2.4'
  133. ]
  134. key << pipeline_status_cache_key(project.pipeline_status) if project.pipeline_status.has_status?
  135. key
  136. end
  137. def load_pipeline_status(projects)
  138. Gitlab::Cache::Ci::ProjectPipelineStatus.
  139. load_in_batch_for_projects(projects)
  140. end
  141. private
  142. def repo_children_classes(field)
  143. needs_repo_check = [:merge_requests_access_level, :builds_access_level]
  144. return unless needs_repo_check.include?(field)
  145. classes = "project-repo-select js-repo-select"
  146. classes << " disabled" unless @project.feature_available?(:repository, current_user)
  147. classes
  148. end
  149. def get_project_nav_tabs(project, current_user)
  150. nav_tabs = [:home]
  151. if !project.empty_repo? && can?(current_user, :download_code, project)
  152. nav_tabs << [:files, :commits, :network, :graphs, :forks]
  153. end
  154. if project.repo_exists? && can?(current_user, :read_merge_request, project)
  155. nav_tabs << :merge_requests
  156. end
  157. if Gitlab.config.registry.enabled && can?(current_user, :read_container_image, project)
  158. nav_tabs << :container_registry
  159. end
  160. tab_ability_map = {
  161. environments: :read_environment,
  162. milestones: :read_milestone,
  163. pipelines: :read_pipeline,
  164. snippets: :read_project_snippet,
  165. settings: :admin_project,
  166. builds: :read_build,
  167. labels: :read_label,
  168. issues: :read_issue,
  169. team: :read_project_member,
  170. wiki: :read_wiki
  171. }
  172. tab_ability_map.each do |tab, ability|
  173. if can?(current_user, ability, project)
  174. nav_tabs << tab
  175. end
  176. end
  177. nav_tabs.flatten
  178. end
  179. def project_lfs_status(project)
  180. if project.lfs_enabled?
  181. content_tag(:span, class: 'lfs-enabled') do
  182. 'Enabled'
  183. end
  184. else
  185. content_tag(:span, class: 'lfs-disabled') do
  186. 'Disabled'
  187. end
  188. end
  189. end
  190. def git_user_name
  191. if current_user
  192. current_user.name
  193. else
  194. "Your name"
  195. end
  196. end
  197. def git_user_email
  198. if current_user
  199. current_user.email
  200. else
  201. "your@email.com"
  202. end
  203. end
  204. def default_url_to_repo(project = @project)
  205. case default_clone_protocol
  206. when 'ssh'
  207. project.ssh_url_to_repo
  208. else
  209. project.http_url_to_repo(current_user)
  210. end
  211. end
  212. def default_clone_protocol
  213. if allowed_protocols_present?
  214. enabled_protocol
  215. else
  216. if !current_user || current_user.require_ssh_key?
  217. gitlab_config.protocol
  218. else
  219. 'ssh'
  220. end
  221. end
  222. end
  223. def project_last_activity(project)
  224. if project.last_activity_at
  225. time_ago_with_tooltip(project.last_activity_at, placement: 'bottom', html_class: 'last_activity_time_ago')
  226. else
  227. "Never"
  228. end
  229. end
  230. def add_special_file_path(project, file_name:, commit_message: nil, branch_name: nil, context: nil)
  231. namespace_project_new_blob_path(
  232. project.namespace,
  233. project,
  234. project.default_branch || 'master',
  235. file_name: file_name,
  236. commit_message: commit_message || "Add #{file_name.downcase}",
  237. branch_name: branch_name,
  238. context: context
  239. )
  240. end
  241. def add_koding_stack_path(project)
  242. namespace_project_new_blob_path(
  243. project.namespace,
  244. project,
  245. project.default_branch || 'master',
  246. file_name: '.koding.yml',
  247. commit_message: "Add Koding stack script",
  248. content: <<-CONTENT.strip_heredoc
  249. provider:
  250. aws:
  251. access_key: '${var.aws_access_key}'
  252. secret_key: '${var.aws_secret_key}'
  253. resource:
  254. aws_instance:
  255. #{project.path}-vm:
  256. instance_type: t2.nano
  257. user_data: |-
  258. # Created by GitLab UI for :>
  259. echo _KD_NOTIFY_@Installing Base packages...@
  260. apt-get update -y
  261. apt-get install git -y
  262. echo _KD_NOTIFY_@Cloning #{project.name}...@
  263. export KODING_USER=${var.koding_user_username}
  264. export REPO_URL=#{root_url}${var.koding_queryString_repo}.git
  265. export BRANCH=${var.koding_queryString_branch}
  266. sudo -i -u $KODING_USER git clone $REPO_URL -b $BRANCH
  267. echo _KD_NOTIFY_@#{project.name} cloned.@
  268. CONTENT
  269. )
  270. end
  271. def koding_project_url(project = nil, branch = nil, sha = nil)
  272. if project
  273. import_path = "/Home/Stacks/import"
  274. repo = project.path_with_namespace
  275. branch ||= project.default_branch
  276. sha ||= project.commit.short_id
  277. path = "#{import_path}?repo=#{repo}&branch=#{branch}&sha=#{sha}"
  278. return URI.join(current_application_settings.koding_url, path).to_s
  279. end
  280. current_application_settings.koding_url
  281. end
  282. def contribution_guide_path(project)
  283. if project && contribution_guide = project.repository.contribution_guide
  284. namespace_project_blob_path(
  285. project.namespace,
  286. project,
  287. tree_join(project.default_branch,
  288. contribution_guide.name)
  289. )
  290. end
  291. end
  292. def readme_path(project)
  293. filename_path(project, :readme)
  294. end
  295. def changelog_path(project)
  296. filename_path(project, :changelog)
  297. end
  298. def license_path(project)
  299. filename_path(project, :license_blob)
  300. end
  301. def version_path(project)
  302. filename_path(project, :version)
  303. end
  304. def ci_configuration_path(project)
  305. filename_path(project, :gitlab_ci_yml)
  306. end
  307. def project_wiki_path_with_version(proj, page, version, is_newest)
  308. url_params = is_newest ? {} : { version_id: version }
  309. namespace_project_wiki_path(proj.namespace, proj, page, url_params)
  310. end
  311. def project_status_css_class(status)
  312. case status
  313. when "started"
  314. "active"
  315. when "failed"
  316. "danger"
  317. when "finished"
  318. "success"
  319. end
  320. end
  321. def readme_cache_key
  322. sha = @project.commit.try(:sha) || 'nil'
  323. [@project.path_with_namespace, sha, "readme"].join('-')
  324. end
  325. def current_ref
  326. @ref || @repository.try(:root_ref)
  327. end
  328. def filename_path(project, filename)
  329. if project && blob = project.repository.send(filename)
  330. namespace_project_blob_path(
  331. project.namespace,
  332. project,
  333. tree_join(project.default_branch, blob.name)
  334. )
  335. end
  336. end
  337. def sanitize_repo_path(project, message)
  338. return '' unless message.present?
  339. exports_path = File.join(Settings.shared['path'], 'tmp/project_exports')
  340. filtered_message = message.strip.gsub(exports_path, "[REPO EXPORT PATH]")
  341. filtered_message.gsub(project.repository_storage_path.chomp('/'), "[REPOS PATH]")
  342. end
  343. def project_feature_options
  344. {
  345. 'Disabled' => ProjectFeature::DISABLED,
  346. 'Only team members' => ProjectFeature::PRIVATE,
  347. 'Everyone with access' => ProjectFeature::ENABLED
  348. }
  349. end
  350. def project_child_container_class(view_path)
  351. view_path == "projects/issues/issues" ? "prepend-top-default" : "project-show-#{view_path}"
  352. end
  353. def project_issues(project)
  354. IssuesFinder.new(current_user, project_id: project.id).execute
  355. end
  356. def visibility_select_options(project, selected_level)
  357. level_options = Gitlab::VisibilityLevel.values.each_with_object([]) do |level, level_options|
  358. next if restricted_levels.include?(level)
  359. level_options << [
  360. visibility_level_label(level),
  361. { data: { description: visibility_level_description(level, project) } },
  362. level
  363. ]
  364. end
  365. options_for_select(level_options, selected_level)
  366. end
  367. def restricted_levels
  368. return [] if current_user.admin?
  369. current_application_settings.restricted_visibility_levels || []
  370. end
  371. end