Commit e53d467e authored by Matija Čupić's avatar Matija Čupić
Browse files

Find build by sha from ref

Adds ability to find builds by sha when only specifying a ref.
parent bfcc4c3b7da0
......@@ -92,7 +92,10 @@ def build_from_id
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
......
......@@ -230,9 +230,10 @@ class Pipeline < ApplicationRecord
# 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 @@ def self.latest_successful_for(ref)
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
......
......@@ -719,14 +719,25 @@ def commits_by(oids:)
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
......
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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment