diff options
author | Lin Jen-Shin <godfat@godfat.org> | 2016-07-19 15:59:38 +0300 |
---|---|---|
committer | Lin Jen-Shin <godfat@godfat.org> | 2016-07-19 15:59:38 +0300 |
commit | 08b9532e376eae369cd04d9f86ea560acfd19ed0 (patch) | |
tree | 69ffbbe3425b96c3f097c7e8f9d23798bef21451 /app/models | |
parent | 1cd573ee7f03bcb356478aab5900fae4618b53ec (diff) |
API for downloading latest successful build:
This was extracted from !5142 and implementing part of #4255.
We split it from !5142 because we want to ship it in 8.10
while !5142 was not ready yet.
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/ci/build.rb | 3 | ||||
-rw-r--r-- | app/models/ci/pipeline.rb | 10 | ||||
-rw-r--r-- | app/models/project.rb | 7 |
3 files changed, 19 insertions, 1 deletions
diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb index ffac3a22efc..65dfe4f0190 100644 --- a/app/models/ci/build.rb +++ b/app/models/ci/build.rb @@ -15,6 +15,9 @@ module Ci scope :with_artifacts, ->() { where.not(artifacts_file: nil) } scope :with_expired_artifacts, ->() { with_artifacts.where('artifacts_expire_at < ?', Time.now) } scope :last_month, ->() { where('created_at > ?', Date.today - 1.month) } + scope :latest_successful_with_artifacts, ->() do + with_artifacts.success.order(id: :desc) + end mount_uploader :artifacts_file, ArtifactUploader mount_uploader :artifacts_metadata, ArtifactUploader diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb index a65a826536d..f5b4124d1ee 100644 --- a/app/models/ci/pipeline.rb +++ b/app/models/ci/pipeline.rb @@ -20,6 +20,14 @@ module Ci after_touch :update_state after_save :keep_around_commits + # ref can't be HEAD, can only be branch/tag name or SHA + scope :latest_successful_for, ->(ref) do + table = quoted_table_name + # TODO: Use `where(ref: ref).or(sha: ref)` in Rails 5 + where("#{table}.ref = ? OR #{table}.sha = ?", ref, ref). + success.order(id: :desc) + end + def self.truncate_sha(sha) sha[0...8] end @@ -222,7 +230,7 @@ module Ci def keep_around_commits return unless project - + project.repository.keep_around(self.sha) project.repository.keep_around(self.before_sha) end diff --git a/app/models/project.rb b/app/models/project.rb index a805f5d97bc..29aaaf5117f 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -429,6 +429,13 @@ class Project < ActiveRecord::Base repository.commit(ref) end + # ref can't be HEAD, can only be branch/tag name or SHA + def latest_successful_builds_for(ref = 'master') + Ci::Build.joins(:pipeline). + merge(pipelines.latest_successful_for(ref)). + latest_successful_with_artifacts + end + def merge_base_commit(first_commit_id, second_commit_id) sha = repository.merge_base(first_commit_id, second_commit_id) repository.commit(sha) if sha |