From 0538e1e934484e76575164314fe8451374e4a4c8 Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin <godfat@godfat.org>
Date: Tue, 19 Jul 2016 17:09:32 +0800
Subject: [PATCH] Support SHA for downloading artifacts:

So if we also query against SHA, we could actually support SHA.
If there's a branch or tag also named like SHA this could be
ambiguous, but since we could already do that in Git, I think
it's probably fine, people would be aware they shouldn't use
the same name anyway.
---
 app/models/project.rb                     | 9 ++++++---
 spec/requests/shared/artifacts_context.rb | 8 ++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/app/models/project.rb b/app/models/project.rb
index 30e8ade99ff..c1cb1558132 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -429,12 +429,15 @@ class Project < ActiveRecord::Base
     repository.commit(ref)
   end
 
-  # ref can't be HEAD or SHA, can only be branch/tag name
+  # ref can't be HEAD, can only be branch/tag name or SHA
   def latest_success_pipeline_for(ref = 'master')
-    pipelines.where(ref: ref).success.order(id: :desc)
+    table = Ci::Pipeline.quoted_table_name
+    # TODO: Use `where(ref: ref).or(sha: ref)` in Rails 5
+    pipelines.where("#{table}.ref = ? OR #{table}.sha = ?", ref, ref).
+      success.order(id: :desc)
   end
 
-  # ref can't be HEAD or SHA, can only be branch/tag name
+  # ref can't be HEAD, can only be branch/tag name or SHA
   def latest_success_builds_for(ref = 'master')
     Ci::Build.joins(:pipeline).
       merge(latest_success_pipeline_for(ref)).
diff --git a/spec/requests/shared/artifacts_context.rb b/spec/requests/shared/artifacts_context.rb
index 635c5646f91..102ae392844 100644
--- a/spec/requests/shared/artifacts_context.rb
+++ b/spec/requests/shared/artifacts_context.rb
@@ -38,6 +38,14 @@ shared_examples 'artifacts from ref successfully' do
     create(:ci_build, status, :artifacts, pipeline: new_pipeline)
   end
 
+  context 'with sha' do
+    before do
+      get path_from_ref(pipeline.sha)
+    end
+
+    it('gives the file') { verify }
+  end
+
   context 'with regular branch' do
     before do
       pipeline.update(ref: 'master',
-- 
GitLab