Skip to content

Commit deb9efc

Browse files
committed
Make super group additive
1 parent 677e905 commit deb9efc

14 files changed

+389
-79
lines changed

app/commands/add_committer_command.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# encoding: utf-8
2+
#--
3+
# Copyright (C) 2014 Gitorious AS
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#++
18+
require "use_case"
19+
require "virtus"
20+
21+
class AddCommitterCommand
22+
def initialize(user, repository)
23+
@user = user
24+
@repository = repository
25+
end
26+
27+
def execute(committership)
28+
committership.save
29+
committership
30+
end
31+
32+
def build(params)
33+
if params.super_group?
34+
@repository.committerships.add_super_group!
35+
else
36+
committership = @repository.committerships.new_committership
37+
committership.committer = committer(params)
38+
committership.creator = @user
39+
committership.build_permissions(params.permissions)
40+
committership
41+
end
42+
end
43+
44+
private
45+
46+
def committer(params)
47+
if params.login
48+
return User.find_by_login(params.login)
49+
end
50+
Team.find_by_name!(params.group_name)
51+
end
52+
end
53+
54+
class AddCommitterParams
55+
include Virtus.model
56+
attribute :permissions, Array[String]
57+
attribute :user, Hash
58+
attribute :group, Hash
59+
60+
def login
61+
user["login"]
62+
end
63+
64+
def group_name
65+
group["name"]
66+
end
67+
68+
def super_group?
69+
group_name == "Super Group"
70+
end
71+
end

app/controllers/committerships_controller.rb

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,18 @@ def index
2929
end
3030

3131
def create
32-
committership = @repository.committerships.new_committership
33-
committership.committer = committer
34-
committership.creator = current_user
35-
committership.build_permissions(params[:permissions])
32+
outcome = AddCommitter.new(current_user, @repository).execute(params)
33+
outcome.failure do |committership|
34+
render_index(@repository, committership)
35+
end
3636

37-
if committership.save
37+
outcome.success do |committership|
3838
if committership.committer.is_a?(User)
3939
flash[:success] = "User added as committer"
4040
else
4141
flash[:success] = "Team added as committers"
4242
end
4343
redirect_to([@repository.project, @repository, :committerships])
44-
else
45-
render_index(@repository, committership)
4644
end
4745
end
4846

@@ -99,13 +97,6 @@ def find_repository
9997
authorize_access_to(@repository.project)
10098
end
10199

102-
def committer
103-
if params.key?(:user) && params[:user][:login]
104-
return User.find_by_login(params[:user][:login])
105-
end
106-
Team.find_by_name!(params[:group][:name])
107-
end
108-
109100
def render_index(repository, committership)
110101
committerships = CommittershipPresenter.collection(repository.committerships.all, view_context)
111102
render(:index, :locals => {

app/models/committership.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#++
2222

2323
class Committership < ActiveRecord::Base
24-
2524
CAN_REVIEW = 1 << 4
2625
CAN_COMMIT = 1 << 5
2726
CAN_ADMIN = 1 << 6
@@ -37,10 +36,6 @@ class Committership < ActiveRecord::Base
3736
belongs_to :creator, :class_name => "User"
3837
has_many :messages, :as => :notifiable
3938

40-
validates_presence_of :committer_id, :committer_type, :repository_id
41-
validates_uniqueness_of :committer_id, :scope => [:committer_type, :repository_id],
42-
:message => "is already a committer to this repository"
43-
4439
attr_accessible :committer, :repository, :creator, :creator_id
4540

4641
after_create :notify_repository_owners
@@ -54,6 +49,14 @@ class Committership < ActiveRecord::Base
5449
scope :committers, :conditions => ["(permissions & ?) != 0", CAN_COMMIT]
5550
scope :admins, :conditions => ["(permissions & ?) != 0", CAN_ADMIN]
5651

52+
def uniq?
53+
committership = Committership.where(committer_type: committer_type,
54+
repository_id: repository_id,
55+
committer_id: committer_id).first
56+
57+
committership.nil? || committership == self
58+
end
59+
5760
def permission_mask_for(*perms)
5861
perms.inject(0) do |memo, perm_symbol|
5962
memo | PERMISSION_TABLE[perm_symbol]

app/models/repository_committerships.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ def reload
128128
raw_all.reload
129129
end
130130

131+
def add_super_group!
132+
repository.update_attribute(:super_group_removed, false)
133+
super_group
134+
end
135+
131136
private
132137

133138
def raw_all

app/models/super_group.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def cs.persisted?
4545
def cs.id
4646
SuperGroup.id
4747
end
48+
def cs.save
49+
nil
50+
end
4851
cs
4952
end
5053
end

app/use_cases/add_committer.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# encoding: utf-8
2+
#--
3+
# Copyright (C) 2014 Gitorious AS
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#++
18+
require "commands/add_committer_command"
19+
20+
class AddCommitter
21+
include UseCase
22+
23+
def initialize(user, repository)
24+
input_class(AddCommitterParams)
25+
step(AddCommitterCommand.new(user, repository), validator: CommittershipValidator)
26+
end
27+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# encoding: utf-8
2+
#--
3+
# Copyright (C) 2014 Gitorious AS
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#++
18+
require "use_case"
19+
20+
CommittershipValidator = UseCase::Validator.define do
21+
validates_presence_of :committer_id, :committer_type, :repository_id, unless: :super_group?
22+
validate :uniqueness, unless: :super_group?
23+
24+
def uniqueness
25+
errors.add(:committer_id, 'is already a committer') if !uniq?
26+
end
27+
28+
def super_group?
29+
Gitorious::Configuration.get("enable_super_group") && SuperGroup.id == id
30+
end
31+
end

app/views/site/faq.html.erb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
<p>
5858
Super group makes all the repositories available for registered
5959
user. It automatically adds a SuperGroup, that contains everyone,
60-
to every project. It can be disabled in Gitorious configuration
60+
to every repository. It can be removed from collaborators list as
61+
any other group or disabled globallty in Gitorious configuration
6162
<code>config/gitorious.yml</code> by setting <code>enable_super_group: false</code>.
6263
</p>
6364
<% end %>

test/fake_use_case_helper.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# encoding: utf-8
2+
#--
3+
# Copyright (C) 2014 Gitorious AS
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU Affero General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU Affero General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU Affero General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
#++
18+
19+
module FakeUseCaseHelper
20+
class FakeUseCase < Struct.new(:result)
21+
def execute(*)
22+
self
23+
end
24+
25+
def success
26+
if result[:success]
27+
yield(result[:success])
28+
end
29+
end
30+
31+
def failure
32+
if result[:failure]
33+
yield(result[:failure])
34+
end
35+
end
36+
end
37+
38+
def fake_use_case(opts)
39+
FakeUseCase.new(opts)
40+
end
41+
end

test/functional/committerships_controller_test.rb

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -74,44 +74,47 @@ def setup
7474
end
7575

7676
context "POST create" do
77+
include FakeUseCaseHelper
78+
7779
should "add a Group as having committership" do
78-
assert_difference("@repository.committerships.count") do
79-
post :create, params(:group => {:name => @group.name}, :user => {}, :permissions => ["review"])
80-
end
80+
committership = Committership.new(committer: @group)
81+
AddCommitter.stubs(:new)
82+
.with(@user, @repository)
83+
.returns(fake_use_case(success: committership))
84+
85+
post :create, params(:group => {:name => @group.name}, :user => {}, :permissions => ["review"])
8186
assert_response :redirect
82-
assert_equal @group, Committership.last.committer
83-
assert_equal @user, Committership.last.creator
8487
assert_equal "Team added as committers", flash[:success]
85-
assert_equal [:review], Committership.last.permission_list
8688
end
8789

8890
should "add a User as having committership" do
89-
assert_difference("@repository.committerships.count") do
90-
post :create, {
91-
:project_id => @project.to_param,
92-
:repository_id => @repository.to_param,
93-
:user => {:login => users(:moe).login}, :group => {},
94-
:permissions => ["review","commit"]
95-
}
96-
assert_nil flash[:error]
97-
assert_response :redirect
98-
end
99-
assert_equal users(:moe), Committership.last.committer
100-
assert_equal @user, Committership.last.creator
91+
committership = Committership.new(committer: @user)
92+
AddCommitter.stubs(:new)
93+
.with(@user, @repository)
94+
.returns(fake_use_case(success: committership))
95+
96+
post :create, {
97+
:project_id => @project.to_param,
98+
:repository_id => @repository.to_param,
99+
:user => {:login => users(:moe).login}, :group => {},
100+
:permissions => ["review","commit"]
101+
}
102+
assert_nil flash[:error]
103+
assert_response :redirect
101104
assert_equal "User added as committer", flash[:success]
102-
assert Committership.last.reviewer?
103-
assert Committership.last.committer?
104-
assert !Committership.last.admin?
105105
end
106106

107107
should "not fail when the same user is added twice" do
108-
assert_no_difference("@repository.committerships.count") do
109-
post :create, params(
110-
:group => {},
111-
:user => { :login => users(:johan).login},
112-
:permissions => ["review"]
113-
)
114-
end
108+
committership = Committership.new(committer: @user)
109+
AddCommitter.stubs(:new)
110+
.with(@user, @repository)
111+
.returns(fake_use_case(failure: committership))
112+
113+
post :create, params(
114+
:group => {},
115+
:user => { :login => users(:johan).login},
116+
:permissions => ["review"]
117+
)
115118
assert_response :success
116119
end
117120
end

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
require "capybara_minitest_spec"
4646
require "capybara_test_case"
4747
require "view_context_helper"
48+
require "fake_use_case_helper"
4849

4950
Minitest::Reporters.use!(Minitest::Reporters::DefaultReporter.new)
5051

0 commit comments

Comments
 (0)