summaryrefslogtreecommitdiff
path: root/lib/gitlab/badge/build.rb
blob: e5e9fab3f5c8c8a7cff53de608a79c95ff199678 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module Gitlab
  module Badge
    ##
    # Build badge
    #
    class Build
      include Gitlab::Application.routes.url_helpers
      include ActionView::Helpers::AssetTagHelper
      include ActionView::Helpers::UrlHelper

      def initialize(project, ref)
        @project, @ref = project, ref
        @image = ::Ci::ImageForBuildService.new.execute(project, ref: ref)
      end

      def type
        'image/svg+xml'
      end

      def data
        File.read(@image[:path])
      end

      def to_s
        @image[:name].sub(/\.svg$/, '')
      end

      def to_html
        link_to(image_tag(image_url, alt: 'build status'), link_url)
      end

      def to_markdown
        "[![build status](#{image_url})](#{link_url})"
      end

      def image_url
        build_namespace_project_badges_url(@project.namespace,
                                           @project, @ref, format: :svg)
      end

      def link_url
        namespace_project_commits_url(@project.namespace, @project, id: @ref)
      end
    end
  end
end