diff options
author | Camil Staps <info@camilstaps.nl> | 2019-08-17 11:14:51 +0200 |
---|---|---|
committer | Camil Staps <info@camilstaps.nl> | 2019-09-06 09:51:04 +0200 |
commit | 86eae7b2e09fdf52f63adf1ef134b2b0016b8f34 (patch) | |
tree | 3ceae39f1eb5ab978bf0c207b714f323b307431a | |
parent | 913c87c6126c0f1c80f57cf82d14b1e8fbc2397d (diff) | |
download | gitlab-ce-86eae7b2e09fdf52f63adf1ef134b2b0016b8f34.tar.gz |
Fix public/private/internal fork counts
-rw-r--r-- | app/controllers/projects/forks_controller.rb | 15 | ||||
-rw-r--r-- | app/views/projects/forks/index.html.haml | 2 | ||||
-rw-r--r-- | spec/controllers/projects/forks_controller_spec.rb | 61 |
3 files changed, 64 insertions, 14 deletions
diff --git a/app/controllers/projects/forks_controller.rb b/app/controllers/projects/forks_controller.rb index ac1c4bc7fd3..4be4bd38367 100644 --- a/app/controllers/projects/forks_controller.rb +++ b/app/controllers/projects/forks_controller.rb @@ -9,16 +9,14 @@ class Projects::ForksController < Projects::ApplicationController before_action :authorize_download_code! before_action :authenticate_user!, only: [:new, :create] - # rubocop: disable CodeReuse/ActiveRecord def index - base_query = project.forks.includes(:creator) - - forks = ForkProjectsFinder.new(project, params: params.merge(search: params[:filter_projects]), current_user: current_user).execute - @total_forks_count = base_query.size - @private_forks_count = @total_forks_count - forks.size - @public_forks_count = @total_forks_count - @private_forks_count + @total_forks_count = project.forks.size + @public_forks_count = project.forks.public_only.size + @private_forks_count = @total_forks_count - project.forks.public_and_internal_only.size + @internal_forks_count = @total_forks_count - @public_forks_count - @private_forks_count - @forks = forks.page(params[:page]) + @forks = ForkProjectsFinder.new(project, params: params.merge(search: params[:filter_projects]), current_user: current_user).execute + @forks = @forks.page(params[:page]) respond_to do |format| format.html @@ -30,7 +28,6 @@ class Projects::ForksController < Projects::ApplicationController end end end - # rubocop: enable CodeReuse/ActiveRecord def new @namespaces = current_user.manageable_namespaces diff --git a/app/views/projects/forks/index.html.haml b/app/views/projects/forks/index.html.haml index 0397a7034c7..3b6e19c5058 100644 --- a/app/views/projects/forks/index.html.haml +++ b/app/views/projects/forks/index.html.haml @@ -1,6 +1,6 @@ .top-area .nav-text - - full_count_title = "#{@public_forks_count} public and #{@private_forks_count} private" + - full_count_title = "#{@public_forks_count} public, #{@internal_forks_count} internal and #{@private_forks_count} private" #{pluralize(@total_forks_count, 'fork')}: #{full_count_title} .nav-controls diff --git a/spec/controllers/projects/forks_controller_spec.rb b/spec/controllers/projects/forks_controller_spec.rb index 5ac5279e997..80b5eb9a7ee 100644 --- a/spec/controllers/projects/forks_controller_spec.rb +++ b/spec/controllers/projects/forks_controller_spec.rb @@ -5,7 +5,7 @@ require 'spec_helper' describe Projects::ForksController do let(:user) { create(:user) } let(:project) { create(:project, :public, :repository) } - let(:forked_project) { Projects::ForkService.new(project, user).execute } + let(:forked_project) { Projects::ForkService.new(project, user, name: 'Some name').execute } let(:group) { create(:group) } before do @@ -13,11 +13,12 @@ describe Projects::ForksController do end describe 'GET index' do - def get_forks + def get_forks(search: nil) get :index, params: { namespace_id: project.namespace, - project_id: project + project_id: project, + search: search } end @@ -31,6 +32,41 @@ describe Projects::ForksController do expect(assigns[:forks]).to be_present end + + it 'forks counts are correct' do + get_forks + + expect(assigns[:total_forks_count]).to eq(1) + expect(assigns[:public_forks_count]).to eq(1) + expect(assigns[:internal_forks_count]).to eq(0) + expect(assigns[:private_forks_count]).to eq(0) + end + + context 'after search' do + it 'forks counts are correct' do + get_forks(search: 'Non-matching query') + + expect(assigns[:total_forks_count]).to eq(1) + expect(assigns[:public_forks_count]).to eq(1) + expect(assigns[:internal_forks_count]).to eq(0) + expect(assigns[:private_forks_count]).to eq(0) + end + end + end + + context 'when fork is internal' do + before do + forked_project.update(visibility_level: Project::INTERNAL, group: group) + end + + it 'forks counts are correct' do + get_forks + + expect(assigns[:total_forks_count]).to eq(1) + expect(assigns[:public_forks_count]).to eq(0) + expect(assigns[:internal_forks_count]).to eq(1) + expect(assigns[:private_forks_count]).to eq(0) + end end context 'when fork is private' do @@ -38,12 +74,25 @@ describe Projects::ForksController do forked_project.update(visibility_level: Project::PRIVATE, group: group) end - it 'is not be visible for non logged in users' do + shared_examples 'forks counts' do + it 'forks counts are correct' do + get_forks + + expect(assigns[:total_forks_count]).to eq(1) + expect(assigns[:public_forks_count]).to eq(0) + expect(assigns[:internal_forks_count]).to eq(0) + expect(assigns[:private_forks_count]).to eq(1) + end + end + + it 'is not visible for non logged in users' do get_forks expect(assigns[:forks]).to be_blank end + include_examples 'forks counts' + context 'when user is logged in' do before do sign_in(project.creator) @@ -67,6 +116,8 @@ describe Projects::ForksController do expect(assigns[:forks]).to be_present end + + include_examples 'forks counts' end context 'when user is a member of the Group' do @@ -79,6 +130,8 @@ describe Projects::ForksController do expect(assigns[:forks]).to be_present end + + include_examples 'forks counts' end end end |