File: persistent_ref.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (55 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download
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
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

module Ci
  ##
  # The persistent pipeline ref to ensure runners can safely fetch source code
  # even if force-push/source-branch-deletion happens.
  class PersistentRef
    include ActiveModel::Model

    attr_accessor :pipeline

    delegate :project, :sha, to: :pipeline
    delegate :repository, to: :project
    delegate :ref_exists?, :create_ref, :delete_refs, :async_delete_refs, to: :repository

    def exist?
      ref_exists?(path)
    rescue StandardError
      false
    end

    # This needs to be kept in sync with `Ci::Pipeline#after_transition` calling `pipeline.persistent_ref.delete`
    def should_delete?
      pipeline.status.to_sym.in?(::Ci::Pipeline.stopped_statuses)
    end

    def create
      create_ref(sha, path)
    rescue StandardError => e
      Gitlab::ErrorTracking
        .track_exception(e, pipeline_id: pipeline.id)
    end

    def delete
      return unless should_delete?

      delete_refs(path)
    rescue Gitlab::Git::Repository::NoRepository
      # no-op
    rescue StandardError => e
      Gitlab::ErrorTracking
        .track_exception(e, pipeline_id: pipeline.id)
    end

    def async_delete
      return unless should_delete?

      async_delete_refs(path)
    end

    def path
      "refs/#{Repository::REF_PIPELINES}/#{pipeline.id}"
    end
  end
end