summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamil Trzcinski <ayufan@ayufan.eu>2015-08-06 12:20:31 +0200
committerKamil Trzcinski <ayufan@ayufan.eu>2015-08-21 14:49:36 +0100
commit1ba150e1cf346239eafec3d530b3de3325501893 (patch)
treead4dabdd78669894eb2f3597b6805c4992d7bd70
parent1fdd1fa6ae7bd1bda3235bfa8685463fb5597646 (diff)
downloadgitlab-ci-1ba150e1cf346239eafec3d530b3de3325501893.tar.gz
Add committed_at to commits to properly order last commit (the force push issue)
-rw-r--r--CHANGELOG1
-rw-r--r--app/controllers/projects_controller.rb4
-rw-r--r--app/models/commit.rb4
-rw-r--r--app/models/project.rb6
-rw-r--r--app/models/project_status.rb4
-rw-r--r--app/services/create_commit_service.rb1
-rw-r--r--db/migrate/20150806091503_add_committed_at_to_commits.rb6
-rw-r--r--db/migrate/20150806091655_update_committed_at_with_created_at.rb5
-rw-r--r--db/schema.rb4
-rw-r--r--spec/models/project_spec.rb4
10 files changed, 29 insertions, 10 deletions
diff --git a/CHANGELOG b/CHANGELOG
index f2c33fa..b575eec 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -11,6 +11,7 @@ v7.14.0 (unreleased)
- Allow to define variables from YAML
- Added support for CI skipped status
- Fix broken yaml error saving
+ - Add committed_at to commits to properly order last commit (the force push issue)
- Rename type(s) to stage(s)
- Add missing stage when doing retry
- Require variable keys to be not-empty and unique
diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb
index 8ba0f92..32e5a98 100644
--- a/app/controllers/projects_controller.rb
+++ b/app/controllers/projects_controller.rb
@@ -40,9 +40,9 @@ class ProjectsController < ApplicationController
def show
@ref = params[:ref]
- @commits = @project.commits
+ @commits = @project.commits.reverse_order
@commits = @commits.where(ref: @ref) if @ref
- @commits = @commits.order('id DESC').page(params[:page]).per(20)
+ @commits = @commits.page(params[:page]).per(20)
end
def integration
diff --git a/app/models/commit.rb b/app/models/commit.rb
index 68057b7..a27c86b 100644
--- a/app/models/commit.rb
+++ b/app/models/commit.rb
@@ -245,6 +245,10 @@ class Commit < ActiveRecord::Base
commits.present? && commits.last[:message] =~ /(\[ci skip\])/
end
+ def update_committed!
+ update!(committed_at: DateTime.now)
+ end
+
private
def save_yaml_error(error)
diff --git a/app/models/project.rb b/app/models/project.rb
index 55d8445..9cc3678 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -28,7 +28,7 @@
class Project < ActiveRecord::Base
include ProjectStatus
- has_many :commits, dependent: :destroy
+ has_many :commits, ->() { order(:committed_at) }, dependent: :destroy
has_many :builds, through: :commits, dependent: :destroy
has_many :runner_projects, dependent: :destroy
has_many :runners, through: :runner_projects
@@ -110,9 +110,9 @@ ls -la
end
def ordered_by_last_commit_date
- last_commit_subquery = "(SELECT project_id, MAX(created_at) created_at FROM commits GROUP BY project_id)"
+ last_commit_subquery = "(SELECT project_id, MAX(committed_at) committed_at FROM commits GROUP BY project_id)"
joins("LEFT JOIN #{last_commit_subquery} AS last_commit ON projects.id = last_commit.project_id").
- order("CASE WHEN last_commit.created_at IS NULL THEN 1 ELSE 0 END, last_commit.created_at DESC")
+ order("CASE WHEN last_commit.committed_at IS NULL THEN 1 ELSE 0 END, last_commit.committed_at DESC")
end
def search(query)
diff --git a/app/models/project_status.rb b/app/models/project_status.rb
index b14cb4f..7c22785 100644
--- a/app/models/project_status.rb
+++ b/app/models/project_status.rb
@@ -30,7 +30,7 @@ module ProjectStatus
# only check for toggling build status within same ref.
def last_commit_changed_status?
ref = last_commit.ref
- last_commits = commits.where(ref: ref).order('id DESC').limit(2)
+ last_commits = commits.where(ref: ref).last(2)
if last_commits.size < 2
false
@@ -40,6 +40,6 @@ module ProjectStatus
end
def last_commit_for_ref(ref)
- commits.where(ref: ref).order('id DESC').first
+ commits.where(ref: ref).last
end
end
diff --git a/app/services/create_commit_service.rb b/app/services/create_commit_service.rb
index 4ae1852..912117a 100644
--- a/app/services/create_commit_service.rb
+++ b/app/services/create_commit_service.rb
@@ -40,6 +40,7 @@ class CreateCommitService
commit = project.commits.create(data)
end
+ commit.update_committed!
commit.create_builds unless commit.builds.any?
commit
diff --git a/db/migrate/20150806091503_add_committed_at_to_commits.rb b/db/migrate/20150806091503_add_committed_at_to_commits.rb
new file mode 100644
index 0000000..2825b99
--- /dev/null
+++ b/db/migrate/20150806091503_add_committed_at_to_commits.rb
@@ -0,0 +1,6 @@
+class AddCommittedAtToCommits < ActiveRecord::Migration
+ def up
+ add_column :commits, :committed_at, :timestamp
+ add_index :commits, [:project_id, :committed_at]
+ end
+end
diff --git a/db/migrate/20150806091655_update_committed_at_with_created_at.rb b/db/migrate/20150806091655_update_committed_at_with_created_at.rb
new file mode 100644
index 0000000..a2646c3
--- /dev/null
+++ b/db/migrate/20150806091655_update_committed_at_with_created_at.rb
@@ -0,0 +1,5 @@
+class UpdateCommittedAtWithCreatedAt < ActiveRecord::Migration
+ def up
+ execute('UPDATE commits SET committed_at=created_at WHERE committed_at IS NULL')
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 0e99323..e88caed 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20150803142346) do
+ActiveRecord::Schema.define(version: 20150806091655) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -58,8 +58,10 @@ ActiveRecord::Schema.define(version: 20150803142346) do
t.datetime "updated_at"
t.boolean "tag", default: false
t.text "yaml_errors"
+ t.datetime "committed_at"
end
+ add_index "commits", ["project_id", "committed_at"], name: "index_commits_on_project_id_and_committed_at", using: :btree
add_index "commits", ["project_id", "sha"], name: "index_commits_on_project_id_and_sha", using: :btree
add_index "commits", ["project_id"], name: "index_commits_on_project_id", using: :btree
add_index "commits", ["sha"], name: "index_commits_on_sha", using: :btree
diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb
index c15fa2b..aa76b99 100644
--- a/spec/models/project_spec.rb
+++ b/spec/models/project_spec.rb
@@ -54,8 +54,8 @@ describe Project do
oldest_project = FactoryGirl.create :project
project_without_commits = FactoryGirl.create :project
- FactoryGirl.create :commit, created_at: 1.hour.ago, project: newest_project
- FactoryGirl.create :commit, created_at: 2.hour.ago, project: oldest_project
+ FactoryGirl.create :commit, committed_at: 1.hour.ago, project: newest_project
+ FactoryGirl.create :commit, committed_at: 2.hour.ago, project: oldest_project
Project.ordered_by_last_commit_date.should == [newest_project, oldest_project, project_without_commits]
end