Module: ExtractsPath

Overview

Module providing methods for dealing with separating a tree-ish string and a file path string when combined in a request parameter rubocop:disable Gitlab/ModuleWithInstanceVariables – will be fixed in gitlab.com/gitlab-org/gitlab/-/issues/425379

Constant Summary collapse

InvalidPathError =
ExtractsRef::RefExtractor::InvalidPathError
BRANCH_REF_TYPE =
ExtractsRef::RefExtractor::BRANCH_REF_TYPE
TAG_REF_TYPE =
ExtractsRef::RefExtractor::TAG_REF_TYPE
REF_TYPES =
ExtractsRef::RefExtractor::REF_TYPES

Instance Method Summary collapse

Instance Method Details

#assign_ref_varsObject

Extends the method to handle if there is no path and the ref doesn’t exist in the repo, try to resolve the ref without an ‘.atom’ suffix. If that ref is found, set the request’s format to Atom manually.

Automatically renders not_found! if a valid tree path could not be resolved (e.g., when a user inserts an invalid path or ref).

Automatically redirects to the current default branch if the ref matches a previous default branch that has subsequently been deleted.

Assignments are:

  • @id - A string representing the joined ref and path

  • @ref - A string representing the ref (e.g., the branch, tag, or commit SHA)

  • @path - A string representing the filesystem path

  • @commit - A Commit representing the commit from the given ref

  • @ref_type - The type of ref this is, will attempt to detect if not supplied in params

If the :id parameter appears to be requesting a specific response format, that will be handled as well.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/extracts_path.rb', line 32

def assign_ref_vars
  @id = ref_extractor.id
  @ref = ref_extractor.ref
  @path = ref_extractor.path
  @repo = ref_extractor.repo

  if @ref.present?
    @commit = ref_extractor.commit
    @fully_qualified_ref = ref_extractor.fully_qualified_ref
  end

  rectify_format!
  ref_type if Feature.enabled?(:verified_ref_extractor, @project)

  rectify_renamed_default_branch! && return

  raise InvalidPathError unless @commit

  @hex_path = Digest::SHA1.hexdigest(@path)
  @logs_path = logs_file_project_ref_path(@project, @ref, @path)
rescue RuntimeError, NoMethodError, InvalidPathError
  render_404
end

#ref_typeObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/extracts_path.rb', line 56

def ref_type
  if Feature.enabled?(:verified_ref_extractor, @project)
    return @ref_type if defined? @ref_type

    @ref_type = ExtractsRef::VerifiedRefExtractor
      .ref_type(repository_container.repository, ref: ref_extractor.ref, ref_type: ref_extractor.ref_type)
  else
    ExtractsRef::RefExtractor.ref_type(params[:ref_type])
  end
end