From 0b1655e7b2e2aa57cb7ea8401743d709bf246074 Mon Sep 17 00:00:00 2001
From: Kamil Trzcinski <ayufan@ayufan.eu>
Date: Sat, 16 Apr 2016 21:46:26 +0200
Subject: [PATCH] Rename CiStatus to Statusable

---
 app/models/ci/commit.rb                                   | 2 +-
 app/models/commit_status.rb                               | 8 ++++++--
 app/models/concerns/{ci_status.rb => statuseable.rb}      | 4 ++--
 app/models/project.rb                                     | 2 +-
 app/views/projects/commit/_ci_commit.html.haml            | 2 +-
 spec/models/commit_spec.rb                                | 8 ++++++++
 .../concerns/statuseable_spec.rb}                         | 4 ++--
 7 files changed, 21 insertions(+), 9 deletions(-)
 rename app/models/concerns/{ci_status.rb => statuseable.rb} (97%)
 rename spec/{lib/ci/status_spec.rb => models/concerns/statuseable_spec.rb} (97%)

diff --git a/app/models/ci/commit.rb b/app/models/ci/commit.rb
index ae30407bcae..412ab44aaf6 100644
--- a/app/models/ci/commit.rb
+++ b/app/models/ci/commit.rb
@@ -19,7 +19,7 @@
 module Ci
   class Commit < ActiveRecord::Base
     extend Ci::Model
-    include CiStatus
+    include Statuseable
 
     belongs_to :project, class_name: '::Project', foreign_key: :gl_project_id
     has_many :statuses, class_name: 'CommitStatus'
diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb
index 66eb5dcecf9..06d296eef08 100644
--- a/app/models/commit_status.rb
+++ b/app/models/commit_status.rb
@@ -33,7 +33,7 @@
 #
 
 class CommitStatus < ActiveRecord::Base
-  include CiStatus
+  include Statuseable
 
   self.table_name = 'ci_builds'
 
@@ -81,7 +81,11 @@ class CommitStatus < ActiveRecord::Base
     end
   end
 
-  delegate :before_sha, :sha, :short_sha, to: :commit, prefix: false
+  delegate :sha, :short_sha, to: :commit
+
+  def before_sha
+    commit.before_sha || Gitlab::Git::BLANK_SHA
+  end
 
   def self.stages
     order_by = 'max(stage_idx)'
diff --git a/app/models/concerns/ci_status.rb b/app/models/concerns/statuseable.rb
similarity index 97%
rename from app/models/concerns/ci_status.rb
rename to app/models/concerns/statuseable.rb
index 8190b2a20c6..f34dca29120 100644
--- a/app/models/concerns/ci_status.rb
+++ b/app/models/concerns/statuseable.rb
@@ -1,4 +1,4 @@
-module CiStatus
+module Statuseable
   extend ActiveSupport::Concern
 
   AVAILABLE_STATUSES = %w(pending running success failed canceled skipped)
@@ -41,7 +41,7 @@ module CiStatus
     end
 
     def finished_at
-      all.minimum(:finished_at)
+      all.maximum(:finished_at)
     end
   end
 
diff --git a/app/models/project.rb b/app/models/project.rb
index 95eb7c51b80..3a55e6c5dd6 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -958,7 +958,7 @@ class Project < ActiveRecord::Base
   end
 
   def ci_commit(sha, ref)
-    ci_commits.find_by(sha: sha, ref: ref)
+    ci_commits.order(id: :desc).find_by(sha: sha, ref: ref)
   end
 
   def ensure_ci_commit(sha, ref)
diff --git a/app/views/projects/commit/_ci_commit.html.haml b/app/views/projects/commit/_ci_commit.html.haml
index 25714e6cb47..0c01f2ed95f 100644
--- a/app/views/projects/commit/_ci_commit.html.haml
+++ b/app/views/projects/commit/_ci_commit.html.haml
@@ -1,6 +1,6 @@
 .gray-content-block.middle-block
   .pull-right
-    - if can?(current_user, :update_build, @project)
+    - if can?(current_user, :update_build, @ci_commit.project)
       - if ci_commit.builds.latest.failed.any?(&:retryable?)
         = link_to "Retry failed", retry_builds_namespace_project_commit_path(@project.namespace, @project, ci_commit.sha), class: 'btn btn-grouped btn-primary', method: :post
 
diff --git a/spec/models/commit_spec.rb b/spec/models/commit_spec.rb
index 0e9111c8029..ad47e338a33 100644
--- a/spec/models/commit_spec.rb
+++ b/spec/models/commit_spec.rb
@@ -163,4 +163,12 @@ eos
       it { expect(commit.reverts_commit?(another_commit)).to be_truthy }
     end
   end
+
+  describe '#ci_commits' do
+    # TODO: kamil
+  end
+
+  describe '#status' do
+    # TODO: kamil
+  end
 end
diff --git a/spec/lib/ci/status_spec.rb b/spec/models/concerns/statuseable_spec.rb
similarity index 97%
rename from spec/lib/ci/status_spec.rb
rename to spec/models/concerns/statuseable_spec.rb
index 886b82a7afa..dacbd3034c0 100644
--- a/spec/lib/ci/status_spec.rb
+++ b/spec/models/concerns/statuseable_spec.rb
@@ -1,9 +1,9 @@
 require 'spec_helper'
 
-describe CiStatus do
+describe Statuseable do
   before do
     @object = Object.new
-    @object.extend(CiStatus::ClassMethods)
+    @object.extend(Statuseable::ClassMethods)
   end
 
   describe '.status' do
-- 
2.30.9