Class: Gitlab::LegacyGithubImport::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/legacy_github_import/client.rb

Constant Summary collapse

GITHUB_SAFE_REMAINING_REQUESTS =
100
GITHUB_SAFE_SLEEP_TIME =
500

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token, host: nil, api_version: 'v3', wait_for_rate_limit_reset: true, hostname: nil) ⇒ Client

Returns a new instance of Client.


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gitlab/legacy_github_import/client.rb', line 11

def initialize(access_token, host: nil, api_version: 'v3', wait_for_rate_limit_reset: true, hostname: nil)
  @access_token = access_token
  @host = host.to_s.sub(%r{/+\z}, '')
  @hostname = hostname
  @api_version = api_version
  @users = {}
  @wait_for_rate_limit_reset = wait_for_rate_limit_reset

  if access_token
    ::Octokit.auto_paginate = false
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


63
64
65
66
67
68
69
# File 'lib/gitlab/legacy_github_import/client.rb', line 63

def method_missing(method, *args, &block)
  if api.respond_to?(method)
    request(method, *args, &block)
  else
    super(method, *args, &block)
  end
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.


9
10
11
# File 'lib/gitlab/legacy_github_import/client.rb', line 9

def access_token
  @access_token
end

#api_versionObject (readonly)

Returns the value of attribute api_version.


9
10
11
# File 'lib/gitlab/legacy_github_import/client.rb', line 9

def api_version
  @api_version
end

#hostObject (readonly)

Returns the value of attribute host.


9
10
11
# File 'lib/gitlab/legacy_github_import/client.rb', line 9

def host
  @host
end

#wait_for_rate_limit_resetObject (readonly)

Returns the value of attribute wait_for_rate_limit_reset.


9
10
11
# File 'lib/gitlab/legacy_github_import/client.rb', line 9

def wait_for_rate_limit_reset
  @wait_for_rate_limit_reset
end

Instance Method Details

#apiObject


24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gitlab/legacy_github_import/client.rb', line 24

def api
  @api ||= ::Octokit::Client.new(
    access_token: access_token,
    api_endpoint: api_endpoint,
    web_endpoint: web_endpoint,
    # If there is no config, we're connecting to github.com and we
    # should verify ssl.
    connection_options: {
      ssl: { verify: config ? config['verify_ssl'] : true },
      headers: { host: @hostname }.compact
    }
  )
end

#authorize_url(redirect_uri, state = nil) ⇒ Object


51
52
53
54
55
56
57
# File 'lib/gitlab/legacy_github_import/client.rb', line 51

def authorize_url(redirect_uri, state = nil)
  client.auth_code.authorize_url({
    redirect_uri: redirect_uri,
    scope: "repo, user, user:email",
    state: state
  })
end

#clientObject


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gitlab/legacy_github_import/client.rb', line 38

def client
  unless config
    raise Projects::ImportService::Error,
      'OAuth configuration for GitHub missing.'
  end

  @client ||= ::OAuth2::Client.new(
    config.app_id,
    config.app_secret,
    github_options.merge(ssl: { verify: config['verify_ssl'] })
  )
end

#get_token(code) ⇒ Object


59
60
61
# File 'lib/gitlab/legacy_github_import/client.rb', line 59

def get_token(code)
  client.auth_code.get_token(code).token
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)

71
72
73
# File 'lib/gitlab/legacy_github_import/client.rb', line 71

def respond_to?(method)
  api.respond_to?(method) || super
end

#user(login) ⇒ Object


75
76
77
78
79
80
# File 'lib/gitlab/legacy_github_import/client.rb', line 75

def user()
  return unless .present?
  return @users[] if @users.key?()

  @users[] = api.user()
end