Read about our upcoming Code of Conduct on this issue

build_runner_presenter.rb 2.42 KB
Newer Older
1
2
# frozen_string_literal: true

3
4
module Ci
  class BuildRunnerPresenter < SimpleDelegator
5
6
7
8
9
    include Gitlab::Utils::StrongMemoize

    RUNNER_REMOTE_TAG_PREFIX = 'refs/tags/'.freeze
    RUNNER_REMOTE_BRANCH_PREFIX = 'refs/remotes/origin/'.freeze

10
11
12
13
14
15
16
17
18
    def artifacts
      return unless options[:artifacts]

      list = []
      list << create_archive(options[:artifacts])
      list << create_reports(options[:artifacts][:reports], expire_in: options[:artifacts][:expire_in])
      list.flatten.compact
    end

19
20
21
22
23
24
25
26
27
    def ref_type
      if tag
        'tag'
      else
        'branch'
      end
    end

    def git_depth
28
29
30
31
32
      if git_depth_variable
        git_depth_variable[:value]
      else
        project.default_git_depth
      end.to_i
33
34
35
36
    end

    def refspecs
      specs = []
37
      specs << refspec_for_merge_request_ref if merge_request_ref?
38
39

      if git_depth > 0
40
        specs << refspec_for_branch(ref) if branch? || legacy_detached_merge_request_pipeline?
41
42
43
44
45
46
47
48
49
        specs << refspec_for_tag(ref) if tag?
      else
        specs << refspec_for_branch
        specs << refspec_for_tag
      end

      specs
    end

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    private

    def create_archive(artifacts)
      return unless artifacts[:untracked] || artifacts[:paths]

      {
        artifact_type: :archive,
        artifact_format: :zip,
        name: artifacts[:name],
        untracked: artifacts[:untracked],
        paths: artifacts[:paths],
        when: artifacts[:when],
        expire_in: artifacts[:expire_in]
      }
    end

    def create_reports(reports, expire_in:)
      return unless reports&.any?

69
      reports.map do |report_type, report_paths|
70
        {
71
72
73
74
          artifact_type: report_type.to_sym,
          artifact_format: ::Ci::JobArtifact::TYPE_AND_FORMAT_PAIRS.fetch(report_type.to_sym),
          name: ::Ci::JobArtifact::DEFAULT_FILE_NAMES.fetch(report_type.to_sym),
          paths: report_paths,
75
76
77
78
79
          when: 'always',
          expire_in: expire_in
        }
      end
    end
80
81
82
83
84
85
86
87

    def refspec_for_branch(ref = '*')
      "+#{Gitlab::Git::BRANCH_REF_PREFIX}#{ref}:#{RUNNER_REMOTE_BRANCH_PREFIX}#{ref}"
    end

    def refspec_for_tag(ref = '*')
      "+#{Gitlab::Git::TAG_REF_PREFIX}#{ref}:#{RUNNER_REMOTE_TAG_PREFIX}#{ref}"
    end
88
89
90
91

    def refspec_for_merge_request_ref
      "+#{ref}:#{ref}"
    end
92
93
94
95
96
97

    def git_depth_variable
      strong_memoize(:git_depth_variable) do
        variables&.find { |variable| variable[:key] == 'GIT_DEPTH' }
      end
    end
98
99
  end
end