From ec07641435e9d49686c3eefd9d698caf1b2115dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Thu, 16 May 2019 12:30:42 +0200 Subject: Fix an error in projects admin when statistics are missing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- app/helpers/storage_helper.rb | 2 ++ app/views/admin/projects/_projects.html.haml | 2 +- app/views/admin/projects/show.html.haml | 8 +++--- ...15-fix-error-500-admin-projects-nil-storage.yml | 5 ++++ locale/gitlab.pot | 3 +++ .../admin/admin_sees_project_statistics_spec.rb | 29 ++++++++++++++++++++++ .../admin/admin_sees_projects_statistics_spec.rb | 20 +++++++++++++++ 7 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 changelogs/unreleased/49915-fix-error-500-admin-projects-nil-storage.yml create mode 100644 spec/features/admin/admin_sees_project_statistics_spec.rb create mode 100644 spec/features/admin/admin_sees_projects_statistics_spec.rb diff --git a/app/helpers/storage_helper.rb b/app/helpers/storage_helper.rb index 15041bd5805..e80b3f2b54a 100644 --- a/app/helpers/storage_helper.rb +++ b/app/helpers/storage_helper.rb @@ -2,6 +2,8 @@ module StorageHelper def storage_counter(size_in_bytes) + return s_('StorageSize|Unknown') unless size_in_bytes + precision = size_in_bytes < 1.megabyte ? 0 : 1 number_to_human_size(size_in_bytes, delimiter: ',', precision: precision, significant: false) diff --git a/app/views/admin/projects/_projects.html.haml b/app/views/admin/projects/_projects.html.haml index 5bc695aa7b5..9117f63f939 100644 --- a/app/views/admin/projects/_projects.html.haml +++ b/app/views/admin/projects/_projects.html.haml @@ -13,7 +13,7 @@ .stats %span.badge.badge-pill - = storage_counter(project.statistics.storage_size) + = storage_counter(project.statistics&.storage_size) - if project.archived %span.badge.badge-warning archived .title diff --git a/app/views/admin/projects/show.html.haml b/app/views/admin/projects/show.html.haml index f016a157daf..1e1ad9d5e19 100644 --- a/app/views/admin/projects/show.html.haml +++ b/app/views/admin/projects/show.html.haml @@ -74,10 +74,10 @@ %li %span.light= _('Storage:') - %strong= storage_counter(@project.statistics.storage_size) - ( - = storage_counters_details(@project.statistics) - ) + %strong= storage_counter(@project.statistics&.storage_size) + - if @project.statistics + = surround '(', ')' do + = storage_counters_details(@project.statistics) %li %span.light last commit: diff --git a/changelogs/unreleased/49915-fix-error-500-admin-projects-nil-storage.yml b/changelogs/unreleased/49915-fix-error-500-admin-projects-nil-storage.yml new file mode 100644 index 00000000000..307c2bfb49d --- /dev/null +++ b/changelogs/unreleased/49915-fix-error-500-admin-projects-nil-storage.yml @@ -0,0 +1,5 @@ +--- +title: Fix an error in projects admin when statistics are missing +merge_request: 28355 +author: +type: fixed diff --git a/locale/gitlab.pot b/locale/gitlab.pot index 68397c38700..9883622669c 100644 --- a/locale/gitlab.pot +++ b/locale/gitlab.pot @@ -9146,6 +9146,9 @@ msgstr "" msgid "Storage:" msgstr "" +msgid "StorageSize|Unknown" +msgstr "" + msgid "Subgroups" msgstr "" diff --git a/spec/features/admin/admin_sees_project_statistics_spec.rb b/spec/features/admin/admin_sees_project_statistics_spec.rb new file mode 100644 index 00000000000..95d1fc5b57a --- /dev/null +++ b/spec/features/admin/admin_sees_project_statistics_spec.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe "Admin > Admin sees project statistics" do + let(:current_user) { create(:admin) } + + before do + sign_in(current_user) + + visit admin_project_path(project) + end + + context 'when project has statistics' do + let(:project) { create(:project, :repository) } + + it "shows project statistics" do + expect(page).to have_content("Storage: 0 Bytes (0 Bytes repositories, 0 Bytes build artifacts, 0 Bytes LFS)") + end + end + + context 'when project has no statistics' do + let(:project) { create(:project, :repository) { |project| project.statistics.destroy } } + + it "shows 'Storage: Unknown'" do + expect(page).to have_content("Storage: Unknown") + end + end +end diff --git a/spec/features/admin/admin_sees_projects_statistics_spec.rb b/spec/features/admin/admin_sees_projects_statistics_spec.rb new file mode 100644 index 00000000000..6a6f369ac7c --- /dev/null +++ b/spec/features/admin/admin_sees_projects_statistics_spec.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe "Admin > Admin sees projects statistics" do + let(:current_user) { create(:admin) } + + before do + create(:project, :repository) + create(:project, :repository) { |project| project.statistics.destroy } + + sign_in(current_user) + + visit admin_projects_path + end + + it "shows project statistics for projects that have them" do + expect(page.all('.stats').map(&:text)).to contain_exactly("0 Bytes", "Unknown") + end +end -- cgit v1.2.1