Class: Import::GithubService

Inherits:
BaseService show all
Includes:
ActiveSupport::NumberHelper, SafeFormatHelper
Defined in:
app/services/import/github_service.rb

Constant Summary

Constants inherited from BaseService

BaseService::UnauthorizedError

Instance Attribute Summary collapse

Attributes inherited from BaseService

#project

Instance Method Summary collapse

Methods included from SafeFormatHelper

#safe_format, #tag_pair

Methods inherited from BaseService

#authorized?, #initialize

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

This class inherits a constructor from Import::BaseService

Instance Attribute Details

#clientObject

Returns the value of attribute client.



8
9
10
# File 'app/services/import/github_service.rb', line 8

def client
  @client
end

#current_userObject (readonly)

Returns the value of attribute current_user.



9
10
11
# File 'app/services/import/github_service.rb', line 9

def current_user
  @current_user
end

#paramsObject (readonly)

Returns the value of attribute params.



9
10
11
# File 'app/services/import/github_service.rb', line 9

def params
  @params
end

Instance Method Details

#allow_local_requests?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/services/import/github_service.rb', line 74

def allow_local_requests?
  Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
end

#blocked_url?Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
# File 'app/services/import/github_service.rb', line 78

def blocked_url?
  Gitlab::HTTP_V2::UrlBlocker.blocked_url?(
    url,
    allow_localhost: allow_local_requests?,
    allow_local_network: allow_local_requests?,
    schemes: %w[http https],
    deny_all_requests_except_allowed: Gitlab::CurrentSettings.deny_all_requests_except_allowed?,
    outbound_local_requests_allowlist: Gitlab::CurrentSettings.outbound_local_requests_whitelist # rubocop:disable Naming/InclusiveLanguage -- existing setting
  )
end

#create_project(access_params, provider) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'app/services/import/github_service.rb', line 43

def create_project(access_params, provider)
  Gitlab::LegacyGithubImport::ProjectCreator.new(
    repo,
    project_name,
    target_namespace,
    current_user,
    type: provider,
    **access_params
  ).execute(extra_project_attrs)
end

#execute(access_params, provider) ⇒ Object



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
# File 'app/services/import/github_service.rb', line 11

def execute(access_params, provider)
  if rate_limited?
    return error(_('This endpoint has been requested too many times. Try again later.'), :too_many_requests)
  end

  context_error = validate_context
  return context_error if context_error

  if provider == :github # we skip access token validation for Gitea importer calls
    access_token_error = validate_access_token
    return access_token_error if access_token_error
  end

  project = create_project(access_params, provider)
  track_access_level(provider.to_s) # provider may be :gitea

  if project.persisted?
    store_import_settings(project)
    success(project)
  elsif project.errors[:import_source_disabled].present?
    error(project.errors[:import_source_disabled], :forbidden)
  else
    error(project_save_error(project), :unprocessable_entity)
  end
rescue Octokit::Error => e
  log_error(e)
end

#extra_project_attrsObject



66
67
68
# File 'app/services/import/github_service.rb', line 66

def extra_project_attrs
  {}
end

#project_nameObject



58
59
60
# File 'app/services/import/github_service.rb', line 58

def project_name
  @project_name ||= params[:new_name].presence || repo[:name]
end

#rate_limited?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/services/import/github_service.rb', line 39

def rate_limited?
  Gitlab::ApplicationRateLimiter.throttled?(:github_import, scope: current_user)
end

#repoObject



54
55
56
# File 'app/services/import/github_service.rb', line 54

def repo
  @repo ||= client.repository(params[:repo_id].to_i)
end

#target_namespaceObject



62
63
64
# File 'app/services/import/github_service.rb', line 62

def target_namespace
  @target_namespace ||= Namespace.find_by_full_path(target_namespace_path)
end

#urlObject



70
71
72
# File 'app/services/import/github_service.rb', line 70

def url
  @url ||= params[:github_hostname]
end