Introduced CommitSet for Observerability, more tests and restructuring
to handle this extra step.
| lib/septic/scm/log_svn.rb | |
| 51 | @@ -51,20 +51,27 @@ |
| 51 | |
| 52 | # Update repository starting at revision |
| 53 | def update_from(revision_start) |
| 54 | # Get repository |
| 55 | commit_set = nil |
| 56 | |
| 57 | # Get the latest revision from the repo |
| 58 | repo = get_svn_handle |
| 59 | |
| 60 | # Get current revision |
| 61 | revision_end = repo.fs.youngest_rev |
| 62 | |
| 63 | |
| 64 | # Go through logs and update the database |
| 65 | (revision_start + 1..revision_end).each do |revision| |
| 66 | add_revision(Svn::Info.new(@path, revision)) |
| 67 | if revision_start < revision_end |
| 68 | commit_set = @repository.commit_sets.create(:user_id => @user.id) |
| 69 | (revision_start + 1..revision_end).each do |revision| |
| 70 | info = Svn::Info.new(@path, revision) |
| 71 | add_revision(commit_set, info) |
| 72 | end |
| 73 | commit_set.save |
| 74 | end |
| 75 | |
| 76 | commit_set |
| 77 | end |
| 78 | |
| 79 | # Add single revision to the database |
| 80 | def add_revision(info) |
| 81 | def add_revision(commit_set, info) |
| 82 | # Get title / body |
| 83 | log = info.log.split("\n", 2) |
| 84 | log = [''] if log.empty? |
| ... | |
| 93 | @@ -86,7 +93,7 @@ |
| 93 | commit_data['branch'] = get_branch_name(commit_files) |
| 94 | |
| 95 | # Add data |
| 89 | commit = @repository.commits.create(commit_data) |
| 97 | commit = commit_set.commits.create(commit_data) |
| 98 | |
| 99 | # Add commit files |
| 100 | if commit.valid? |
| ... | |
| test/fixtures/users.yml | |
| 2 | @@ -2,3 +2,10 @@ |
| 2 | login: developer |
| 3 | email: developer@septic |
| 4 | name: Developer Account |
| 5 | crypted_password: xxx |
| 6 | |
| 7 | reporter: |
| 8 | login: reporter |
| 9 | email: reporter@septic |
| 10 | name: Reporter Account |
| 11 | crypted_password: xxx |
| ... | |
| app/models/task.rb | |
| 41 | @@ -41,6 +41,7 @@ |
| 41 | belongs_to :task_component |
| 42 | belongs_to :task_priority |
| 43 | |
| 44 | has_many :task_changes, :order => 'task_changes.created_at' |
| 45 | has_many :task_change_sets, :order => 'task_change_sets.created_at' |
| 46 | has_and_belongs_to_many :dependencies, :uniq => true, :class_name => 'Task', |
| 47 | :association_foreign_key => 'task_dependant_id' |
| ... | |
| app/models/commit.rb | |
| 47 | @@ -47,6 +47,7 @@ |
| 47 | :body => { :store => :yes } } |
| 48 | |
| 49 | belongs_to :repository |
| 50 | belongs_to :commit_set |
| 51 | belongs_to :user |
| 52 | |
| 53 | # Commit browsing |
| ... | |
| 65 | @@ -64,6 +65,7 @@ |
| 65 | |
| 66 | # Each commit belongs to a repository |
| 67 | validates_associated :repository |
| 68 | validates_associated :commit_set |
| 69 | |
| 70 | # Require author, date and revision |
| 71 | validates_presence_of :author |
| ... | |
| test/fixtures/tasks.yml | |
| 1 | @@ -1,2 +1,9 @@ |
| 1 | task1: |
| 2 | id: 1 |
| 3 | reporter: reporter |
| 4 | user: developer |
| 5 | project: test |
| 6 | task_type: enhancement |
| 7 | task_status_id: 1 |
| 8 | name: Task 1 |
| 9 | description: First Task |
| ... | |
| config/environment.rb | |
| 56 | @@ -56,7 +56,7 @@ |
| 56 | # config.active_record.schema_format = :sql |
| 57 | |
| 58 | # Activate observers that should always be running |
| 59 | config.active_record.observers = :commit_observer, :task_change_set_observer |
| 60 | config.active_record.observers = :commit_set_observer, :task_change_set_observer |
| 61 | |
| 62 | # Make Active Record use UTC-base instead of local time |
| 63 | # config.active_record.default_timezone = :utc |
| ... | |
| app/models/repository.rb | |
| 81 | @@ -81,6 +81,7 @@ |
| 81 | |
| 82 | belongs_to :project |
| 83 | has_many :commits, :order => 'commits.id DESC', :dependent => :destroy |
| 84 | has_many :commit_sets, :order => 'commit_sets.id DESC', :dependent => :destroy |
| 85 | has_and_belongs_to_many :watchers, :uniq => true, :class_name => 'User' |
| 86 | |
| 87 | # Repository |
| ... | |
| lib/septic/scm/log_git.rb | |
| 40 | @@ -40,50 +40,47 @@ |
| 40 | raise "no repository at #{@path}" if @repository.nil? |
| 41 | @revision = @repository.commits.find(:first) |
| 42 | |
| 43 | # Get updates |
| 44 | # Get updates and add to the database |
| 45 | commits_data = update_branch_log |
| 46 | |
| 47 | # Add commits to database |
| 48 | commits = add_commits(commits_data) |
| 49 | commit_set = add_commits(commits_data) |
| 50 | |
| 51 | [@repository, commits] |
| 52 | return commit_set |
| 53 | end |
| 54 | |
| 55 | private |
| 56 | # Add commits to database |
| 57 | def add_commits(commits_data) |
| 58 | commits_added = [] |
| 59 | |
| 60 | return nil if commits_data.empty? |
| 61 | |
| 62 | commit_set = @repository.commit_sets.create(:user_id => @user.id) |
| 63 | commits_data.reverse_each do |commit_data| |
| 64 | # Split body / files |
| 65 | commit_data['body'], commit_files = parse_body_files(commit_data['body']) |
| 66 | |
| 67 | # Lookup user |
| 68 | user = get_user_from_email(commit_data['email']) |
| 69 | if user |
| 70 | commit_data['user_id'] = user.id |
| 71 | end |
| 72 | add_commit(commit_set, commit_data) |
| 73 | end |
| 74 | commit_set.save |
| 75 | |
| 76 | commit_set |
| 77 | end |
| 78 | |
| 79 | # Add commit |
| 80 | commit = @repository.commits.create(commit_data) |
| 81 | def add_commit(commit_set, commit_data) |
| 82 | # Split body / files, lookup user and create commit |
| 83 | commit_data['body'], commit_files = parse_body_files(commit_data['body']) |
| 84 | user = get_user_from_email(commit_data['email']) |
| 85 | if user |
| 86 | commit_data['user_id'] = user.id |
| 87 | end |
| 88 | commit = commit_set.commits.create(commit_data) |
| 89 | |
| 90 | # Add commit files |
| 91 | if commit.valid? |
| 92 | commit_files.each do |file| |
| 93 | commit.commit_files.create(file) |
| 94 | end |
| 95 | |
| 96 | # Update commit links |
| 97 | set_revision_relations(commit) |
| 98 | |
| 99 | # Keep track of added commits |
| 100 | commits_added << commit |
| 101 | else |
| 102 | raise "failed creating commit with errors %s" % commit.errors.full_messages.join('\n') |
| 103 | # Add commit files |
| 104 | if commit.valid? |
| 105 | commit_files.each do |file| |
| 106 | commit.commit_files.create(file) |
| 107 | end |
| 108 | |
| 109 | # Update commit links |
| 110 | set_revision_relations(commit) |
| 111 | else |
| 112 | raise "failed creating commit with errors %s" % commit.errors.full_messages.join('\n') |
| 113 | end |
| 114 | |
| 115 | commits_added |
| 116 | end |
| 117 | |
| 118 | # Update branch log to current heads |
| ... | |
| app/models/task_change_set.rb | |
| 4 | @@ -4,6 +4,7 @@ |
| 4 | |
| 5 | has_many :task_changes, :order => 'task_changes.id' |
| 6 | |
| 7 | validates_associated :user |
| 8 | validates_associated :task |
| 9 | |
| 10 | def field(field) |
| ... | |
| app/controllers/services_controller.rb | |
| 45 | @@ -45,12 +45,11 @@ |
| 45 | end |
| 46 | |
| 47 | # Update log |
| 48 | repository, commits = log.update |
| 49 | if commits.size > 0 |
| 50 | message = 'log updated with %d new entries' % [commits.size] |
| 51 | send_scm_update_mail(repository, commits) |
| 52 | else |
| 53 | commit_set = log.update |
| 54 | if commit_set.nil? |
| 55 | message = 'no changes in repository' |
| 56 | else |
| 57 | message = 'log updated with %d new entries' % [commits.size] |
| 58 | end |
| 59 | |
| 60 | status = 200 |
| ... | |
| 68 | @@ -69,15 +68,4 @@ |
| 68 | raise 'no key specified' unless params.include?('key') |
| 69 | raise 'invalid key' if params['key'] != CONFIGURATION['services']['key'] |
| 70 | end |
| 72 | |
| 73 | # Send SCM update e-mail |
| 74 | def send_scm_update_mail(repository, commits) |
| 75 | begin |
| 76 | mail = CommitMailer.create_update_message(CONFIGURATION['site_email'], CONFIGURATION['site_url'], |
| 77 | repository, commits) |
| 78 | CommitMailer.deliver(mail) |
| 79 | rescue |
| 80 | log_error($!) |
| 81 | end |
| 82 | end |
| 82 | end |
| ... | |
| test/fixtures/commits.yml | |
| 1 | @@ -1,6 +1,8 @@ |
| 1 | r1: |
| 2 | id: 1 |
| 3 | revision: 1 |
| 4 | repository: test |
| 5 | commit_set: repository1cs1 |
| 6 | user: developer |
| 7 | commit_next: r2 |
| 8 | commit_next_branch: r2 |
| ... | |
| 10 | @@ -8,8 +10,10 @@ |
| 10 | body: Body of the commit message here |
| 11 | |
| 12 | r2: |
| 13 | id: 2 |
| 14 | revision: 2 |
| 15 | repository: test |
| 16 | commit_set: repository1cs1 |
| 17 | user: developer |
| 18 | commit_prev: r2 |
| 19 | commit_prev_branch: r2 |
| ... | |
| 22 | @@ -18,8 +22,10 @@ |
| 22 | title: Second commit, without body |
| 23 | |
| 24 | r3: |
| 25 | id: 3 |
| 26 | revision: 3 |
| 27 | repository: test |
| 28 | commit_set: repository1cs2 |
| 29 | user: developer |
| 30 | commit_prev: r2 |
| 31 | commit_prev_branch: r2 |
| ... | |