summaryrefslogtreecommitdiff
path: root/app/models/ci/persistent_ref.rb
blob: be3d4aa3203a060ed0a16bfcf9016b245ba238ac (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
# 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, to: :repository

    def exist?
      ref_exists?(path)
    rescue
      false
    end

    def create
      return if exist?

      create_ref(sha, path)
    rescue => e
      Gitlab::Sentry
        .track_acceptable_exception(e, extra: { pipeline_id: pipeline.id })
    end

    def delete
      delete_refs(path)
    rescue Gitlab::Git::Repository::NoRepository
      # no-op
    rescue => e
      Gitlab::Sentry
        .track_acceptable_exception(e, extra: { pipeline_id: pipeline.id })
    end

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