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



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

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 Also known as: octokit



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



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

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



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

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



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

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

#reposObject



87
88
89
90
91
92
93
94
95
# File 'lib/gitlab/legacy_github_import/client.rb', line 87

def repos
  repositories = request(:repos, nil)

  if repositories.is_a?(Array)
    repositories.map(&:to_h)
  else
    repositories
  end
end

#repository(id) ⇒ Object



83
84
85
# File 'lib/gitlab/legacy_github_import/client.rb', line 83

def repository(id)
  request(:repository, id).to_h
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#user(login) ⇒ Object



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

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

  @users[] = api.user()
end