summaryrefslogtreecommitdiff
path: root/app/services/ci/image_for_build_service.rb
blob: 17ce95073cb01b74096022510a049fbc52c5f33e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Ci
  class ImageForBuildService
    def execute(project, opts)
      ref = opts[:ref]
      sha = opts[:sha] || ref_sha(project, ref)

      pipelines = project.pipelines.where(sha: sha)

      latest_pipeline = if ref
                          pipelines.latest_for(ref)
                        else
                          pipelines.latest
                        end.first

      image_name = image_for_status(latest_pipeline.status)

      image_path = Rails.root.join('public/ci', image_name)
      OpenStruct.new(path: image_path, name: image_name)
    end

    private

    def ref_sha(project, ref)
      project.commit(ref).try(:sha) if ref
    end

    def image_for_status(status)
      status ||= 'unknown'
      'build-' + status + ".svg"
    end
  end
end