2019-09-27 14:06:20 -04:00
|
|
|
# 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?
|
2019-11-19 22:06:28 -05:00
|
|
|
return unless enabled?
|
|
|
|
|
2019-09-27 14:06:20 -04:00
|
|
|
ref_exists?(path)
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-11-29 07:10:06 -05:00
|
|
|
return unless enabled?
|
2019-09-27 14:06:20 -04:00
|
|
|
|
|
|
|
create_ref(sha, path)
|
|
|
|
rescue => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking
|
2019-12-13 07:07:41 -05:00
|
|
|
.track_exception(e, pipeline_id: pipeline.id)
|
2019-09-27 14:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
2019-11-19 22:06:28 -05:00
|
|
|
return unless enabled?
|
|
|
|
|
2019-09-27 14:06:20 -04:00
|
|
|
delete_refs(path)
|
|
|
|
rescue Gitlab::Git::Repository::NoRepository
|
|
|
|
# no-op
|
|
|
|
rescue => e
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking
|
2019-12-13 07:07:41 -05:00
|
|
|
.track_exception(e, pipeline_id: pipeline.id)
|
2019-09-27 14:06:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def path
|
|
|
|
"refs/#{Repository::REF_PIPELINES}/#{pipeline.id}"
|
|
|
|
end
|
2019-11-19 22:06:28 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
Feature.enabled?(:depend_on_persistent_pipeline_ref, project, default_enabled: true)
|
|
|
|
end
|
2019-09-27 14:06:20 -04:00
|
|
|
end
|
|
|
|
end
|