2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
module Gitlab
module Ci
class Config
module External
module File
class Project < Base
2019-12-21 20:55:43 +05:30
extend :: Gitlab :: Utils :: Override
2019-02-15 15:39:39 +05:30
include Gitlab :: Utils :: StrongMemoize
attr_reader :project_name , :ref_name
2019-12-21 20:55:43 +05:30
def initialize ( params , context )
2019-02-15 15:39:39 +05:30
@location = params [ :file ]
@project_name = params [ :project ]
@ref_name = params [ :ref ] || 'HEAD'
super
end
def matching?
super && project_name . present?
end
def content
strong_memoize ( :content ) { fetch_local_content }
end
2022-06-21 17:19:12 +05:30
def metadata
super . merge (
type : :file ,
location : masked_location ,
extra : { project : masked_project_name , ref : masked_ref_name }
)
end
2019-02-15 15:39:39 +05:30
private
def validate_content!
if ! can_access_local_content?
2022-06-21 17:19:12 +05:30
errors . push ( " Project ` #{ masked_project_name } ` not found or access denied! Make sure any includes in the pipeline configuration are correctly defined. " )
2019-02-15 15:39:39 +05:30
elsif sha . nil?
2022-06-21 17:19:12 +05:30
errors . push ( " Project ` #{ masked_project_name } ` reference ` #{ masked_ref_name } ` does not exist! " )
2019-02-15 15:39:39 +05:30
elsif content . nil?
2022-06-21 17:19:12 +05:30
errors . push ( " Project ` #{ masked_project_name } ` file ` #{ masked_location } ` does not exist! " )
2019-02-15 15:39:39 +05:30
elsif content . blank?
2022-06-21 17:19:12 +05:30
errors . push ( " Project ` #{ masked_project_name } ` file ` #{ masked_location } ` is empty! " )
2019-02-15 15:39:39 +05:30
end
end
def project
strong_memoize ( :project ) do
:: Project . find_by_full_path ( project_name )
end
end
def can_access_local_content?
Ability . allowed? ( context . user , :download_code , project )
end
def fetch_local_content
return unless can_access_local_content?
return unless sha
project . repository . blob_data_at ( sha , location )
rescue GRPC :: NotFound , GRPC :: Internal
nil
end
def sha
strong_memoize ( :sha ) do
project . commit ( ref_name ) . try ( :sha )
end
end
2019-07-07 11:18:12 +05:30
2019-12-21 20:55:43 +05:30
override :expand_context_attrs
def expand_context_attrs
{
2019-07-07 11:18:12 +05:30
project : project ,
sha : sha ,
2020-04-08 14:13:33 +05:30
user : context . user ,
2021-03-08 18:12:59 +05:30
parent_pipeline : context . parent_pipeline ,
variables : context . variables
2019-12-21 20:55:43 +05:30
}
2019-07-07 11:18:12 +05:30
end
2022-06-21 17:19:12 +05:30
def masked_project_name
strong_memoize ( :masked_project_name ) do
context . mask_variables_from ( project_name )
end
end
def masked_ref_name
strong_memoize ( :masked_ref_name ) do
context . mask_variables_from ( ref_name )
end
end
2019-02-15 15:39:39 +05:30
end
end
end
end
end
end