-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add model to track merges made while detecting conflicts
- Loading branch information
1 parent
7e4a97a
commit 24ede13
Showing
7 changed files
with
149 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Merge < ActiveRecord::Base | ||
fields do | ||
timestamps | ||
end | ||
|
||
belongs_to :source_branch, class_name: Branch, required: true | ||
belongs_to :target_branch, class_name: Branch, required: true | ||
has_one :repository, through: :source_branch | ||
|
||
validates :source_branch, :target_branch, presence: true | ||
validates :source_branch, uniqueness: { scope: :target_branch, message: "Merges must be unique" } | ||
validates_each :source_branch do |record, attr, value| | ||
# specifically ignore nil branches, those will be caught by a different validator | ||
if (value.present? && record.target_branch.present?) | ||
if value.id == record.target_branch.id | ||
record.errors.add(attr, 'Branch cannot merge with itself') | ||
end | ||
if value.repository.id != record.target_branch.repository.id | ||
record.errors.add(attr, 'Branches from different repositories cannot be merged') | ||
end | ||
end | ||
end | ||
|
||
scope :by_target_user, lambda { |user| | ||
joins(:target_branch).where( | ||
'(branches.author_id = ?)', | ||
user.id) | ||
} | ||
|
||
scope :created_after, lambda { |after_date| | ||
where('created_at >= ?', after_date) | ||
} | ||
|
||
scope :from_repository, lambda { |repository_name| joins(:repository).where("repositories.name = ?", repository_name) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class HoboMigration14 < ActiveRecord::Migration | ||
def self.up | ||
create_table :merges do |t| | ||
t.datetime :created_at | ||
t.datetime :updated_at | ||
t.integer :source_branch_id | ||
t.integer :target_branch_id | ||
end | ||
add_index :merges, [:source_branch_id] | ||
add_index :merges, [:target_branch_id] | ||
end | ||
|
||
def self.down | ||
drop_table :merges | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Merge' do | ||
|
||
DIFFERENT_NAME = 'Different Name' | ||
|
||
before do | ||
@branches = create_test_branches(author_name: 'Author Name', count: 3) | ||
end | ||
|
||
it 'can be created' do | ||
create_test_merge(@branches[0], @branches[1]) | ||
expect(Merge.all.size).to eq(1) | ||
end | ||
|
||
it 'does not allow duplicate merges' do | ||
create_test_merge(@branches[0], @branches[1]) | ||
expect { create_test_merge(@branches[0], @branches[1]) }.to raise_exception(ActiveRecord::RecordInvalid) | ||
end | ||
|
||
it 'cannot merge with itself' do | ||
expect { create_test_merge(@branches[0], @branches[0]) }.to raise_exception(ActiveRecord::RecordInvalid) | ||
end | ||
|
||
it 'requires two branches' do | ||
expect { create_test_merge(@branches[0], nil) }.to raise_exception(ActiveRecord::RecordInvalid) | ||
expect { create_test_merge(nil, @branches[0]) }.to raise_exception(ActiveRecord::RecordInvalid) | ||
expect { create_test_merge(nil, nil) }.to raise_exception(ActiveRecord::RecordInvalid) | ||
end | ||
|
||
it 'does not allow merges between repositories' do | ||
branch_in_different_repository = create_test_branch(repository_name: 'other_repository_name') | ||
expect { create_test_merge(@branches[0], branch_in_different_repository) }.to raise_exception(ActiveRecord::RecordInvalid) | ||
end | ||
|
||
context 'with branches from multiple users' do | ||
before do | ||
@other_branches = create_test_branches(author_name: DIFFERENT_NAME, count: 2) | ||
@merge_1 = create_test_merge(@branches[0], @branches[1]) | ||
@merge_2 = create_test_merge(@other_branches[0], @other_branches[1]) | ||
end | ||
|
||
it 'can be looked up by user' do | ||
merges = Merge.by_target_user(User.where(name: DIFFERENT_NAME).first) | ||
expect(merges.size).to eq(1) | ||
expect(merges[0].target_branch.author.name).to eq(DIFFERENT_NAME) | ||
end | ||
|
||
it 'can be filtered by status change date' do | ||
merges = Merge.created_after(2.minutes.from_now) | ||
expect(merges.size).to eq(0) | ||
|
||
merges = Merge.created_after(2.minutes.ago) | ||
expect(merges.size).to eq(2) | ||
end | ||
end | ||
|
||
context 'with branches from multiple repositories' do | ||
it 'returns branches from a repository' do | ||
@merge_1 = create_test_merge(@branches[0], @branches[1]) | ||
@merge_2 = create_test_merge( | ||
create_test_branch(repository_name: 'repository_b', name: 'name_a'), | ||
create_test_branch(repository_name: 'repository_b', name: 'name_b')) | ||
|
||
merges = Merge.from_repository('repository_b').all | ||
expect(merges.size).to eq(1) | ||
expect(merges[0].id).to eq(@merge_2.id) | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters