Commit 86eae7b2 authored by Camil Staps's avatar Camil Staps

Fix public/private/internal fork counts

parent 913c87c6
...@@ -9,16 +9,14 @@ class Projects::ForksController < Projects::ApplicationController ...@@ -9,16 +9,14 @@ class Projects::ForksController < Projects::ApplicationController
before_action :authorize_download_code! before_action :authorize_download_code!
before_action :authenticate_user!, only: [:new, :create] before_action :authenticate_user!, only: [:new, :create]
# rubocop: disable CodeReuse/ActiveRecord
def index def index
base_query = project.forks.includes(:creator) @total_forks_count = project.forks.size
@public_forks_count = project.forks.public_only.size
forks = ForkProjectsFinder.new(project, params: params.merge(search: params[:filter_projects]), current_user: current_user).execute @private_forks_count = @total_forks_count - project.forks.public_and_internal_only.size
@total_forks_count = base_query.size @internal_forks_count = @total_forks_count - @public_forks_count - @private_forks_count
@private_forks_count = @total_forks_count - forks.size
@public_forks_count = @total_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| respond_to do |format|
format.html format.html
...@@ -30,7 +28,6 @@ class Projects::ForksController < Projects::ApplicationController ...@@ -30,7 +28,6 @@ class Projects::ForksController < Projects::ApplicationController
end end
end end
end end
# rubocop: enable CodeReuse/ActiveRecord
def new def new
@namespaces = current_user.manageable_namespaces @namespaces = current_user.manageable_namespaces
......
.top-area .top-area
.nav-text .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} #{pluralize(@total_forks_count, 'fork')}: #{full_count_title}
.nav-controls .nav-controls
......
...@@ -5,7 +5,7 @@ require 'spec_helper' ...@@ -5,7 +5,7 @@ require 'spec_helper'
describe Projects::ForksController do describe Projects::ForksController do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project, :public, :repository) } 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) } let(:group) { create(:group) }
before do before do
...@@ -13,11 +13,12 @@ describe Projects::ForksController do ...@@ -13,11 +13,12 @@ describe Projects::ForksController do
end end
describe 'GET index' do describe 'GET index' do
def get_forks def get_forks(search: nil)
get :index, get :index,
params: { params: {
namespace_id: project.namespace, namespace_id: project.namespace,
project_id: project project_id: project,
search: search
} }
end end
...@@ -31,6 +32,41 @@ describe Projects::ForksController do ...@@ -31,6 +32,41 @@ describe Projects::ForksController do
expect(assigns[:forks]).to be_present expect(assigns[:forks]).to be_present
end 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 end
context 'when fork is private' do context 'when fork is private' do
...@@ -38,12 +74,25 @@ describe Projects::ForksController do ...@@ -38,12 +74,25 @@ describe Projects::ForksController do
forked_project.update(visibility_level: Project::PRIVATE, group: group) forked_project.update(visibility_level: Project::PRIVATE, group: group)
end 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 get_forks
expect(assigns[:forks]).to be_blank expect(assigns[:forks]).to be_blank
end end
include_examples 'forks counts'
context 'when user is logged in' do context 'when user is logged in' do
before do before do
sign_in(project.creator) sign_in(project.creator)
...@@ -67,6 +116,8 @@ describe Projects::ForksController do ...@@ -67,6 +116,8 @@ describe Projects::ForksController do
expect(assigns[:forks]).to be_present expect(assigns[:forks]).to be_present
end end
include_examples 'forks counts'
end end
context 'when user is a member of the Group' do context 'when user is a member of the Group' do
...@@ -79,6 +130,8 @@ describe Projects::ForksController do ...@@ -79,6 +130,8 @@ describe Projects::ForksController do
expect(assigns[:forks]).to be_present expect(assigns[:forks]).to be_present
end end
include_examples 'forks counts'
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment