diff options
author | Matija Čupić <matteeyah@gmail.com> | 2019-07-17 15:33:52 +0200 |
---|---|---|
committer | Matija Čupić <matteeyah@gmail.com> | 2019-07-23 20:55:59 +0200 |
commit | 7cee6139c07db7ba4fd6febf74c24dbfaee8e59d (patch) | |
tree | e140f84706d0067f7a788bb4aa9ee677b3851f2b | |
parent | d892e80bf0161b535389c91ccb53539e4f08d790 (diff) | |
download | gitlab-ce-7cee6139c07db7ba4fd6febf74c24dbfaee8e59d.tar.gz |
Find build by sha from ref
Adds ability to find builds by sha when only specifying a ref.
-rw-r--r-- | app/controllers/projects/artifacts_controller.rb | 5 | ||||
-rw-r--r-- | app/models/ci/pipeline.rb | 7 | ||||
-rw-r--r-- | app/models/project.rb | 13 | ||||
-rw-r--r-- | spec/features/projects/artifacts/user_downloads_artifacts_spec.rb | 4 |
4 files changed, 24 insertions, 5 deletions
diff --git a/app/controllers/projects/artifacts_controller.rb b/app/controllers/projects/artifacts_controller.rb index 2ef18d900f2..da8a371acaa 100644 --- a/app/controllers/projects/artifacts_controller.rb +++ b/app/controllers/projects/artifacts_controller.rb @@ -92,7 +92,10 @@ class Projects::ArtifactsController < Projects::ApplicationController def build_from_ref return unless @ref_name - project.latest_successful_build_for(params[:job], @ref_name) + commit = project.commit(@ref_name) + return unless commit + + project.latest_successful_build_for_sha(params[:job], commit.id) end def artifacts_file diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index 2262282e647..f70ff7e3192 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -230,9 +230,10 @@ module Ci # ref - The name (or names) of the branch(es)/tag(s) to limit the list of # pipelines to. # limit - This limits a backlog search, default to 100. - def self.newest_first(ref: nil, limit: 100) + def self.newest_first(ref: nil, sha: nil, limit: 100) relation = order(id: :desc) relation = relation.where(ref: ref) if ref + relation = relation.where(sha: sha) if sha if limit ids = relation.limit(limit).select(:id) @@ -252,6 +253,10 @@ module Ci newest_first(ref: ref).success.take end + def self.latest_successful_for_sha(sha) + newest_first(sha: sha).success.take + end + def self.latest_successful_for_refs(refs) relation = newest_first(ref: refs).success diff --git a/app/models/project.rb b/app/models/project.rb index 8030c645e2e..9616e8c9748 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -719,14 +719,25 @@ class Project < ApplicationRecord repository.commits_by(oids: oids) end - # ref can't be HEAD, can only be branch/tag name or SHA + # ref can't be HEAD, can only be branch/tag name def latest_successful_build_for(job_name, ref = default_branch) + return unless ref + latest_pipeline = ci_pipelines.latest_successful_for(ref) return unless latest_pipeline latest_pipeline.builds.latest.with_artifacts_archive.find_by(name: job_name) end + def latest_successful_build_for_sha(job_name, sha = commit(default_branch).id) + return unless sha + + latest_pipeline = ci_pipelines.latest_successful_for_sha(sha) + return unless latest_pipeline + + latest_pipeline.builds.latest.with_artifacts_archive.find_by(name: job_name) + end + def latest_successful_build_for!(job_name, ref = default_branch) latest_successful_build_for(job_name, ref) || raise(ActiveRecord::RecordNotFound.new("Couldn't find job #{job_name}")) end diff --git a/spec/features/projects/artifacts/user_downloads_artifacts_spec.rb b/spec/features/projects/artifacts/user_downloads_artifacts_spec.rb index 5cb015e80be..69296ef00fd 100644 --- a/spec/features/projects/artifacts/user_downloads_artifacts_spec.rb +++ b/spec/features/projects/artifacts/user_downloads_artifacts_spec.rb @@ -1,8 +1,8 @@ require "spec_helper" describe "User downloads artifacts" do - set(:project) { create(:project, :public) } - set(:pipeline) { create(:ci_empty_pipeline, status: :success, project: project) } + set(:project) { create(:project, :repository, :public) } + set(:pipeline) { create(:ci_empty_pipeline, status: :success, sha: project.commit.id, project: project) } set(:job) { create(:ci_build, :artifacts, :success, pipeline: pipeline) } shared_examples "downloading" do |