client.rb 2.99 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1
module Gitlab
2
  module GithubImport
Valery Sizov's avatar
Valery Sizov committed
3
    class Client
4
5
6
      GITHUB_SAFE_REMAINING_REQUESTS = 100
      GITHUB_SAFE_SLEEP_TIME = 500

7
      attr_reader :access_token
Valery Sizov's avatar
Valery Sizov committed
8

Valery Sizov's avatar
Valery Sizov committed
9
      def initialize(access_token)
10
        @access_token = access_token
Valery Sizov's avatar
Valery Sizov committed
11
12

        if access_token
13
          ::Octokit.auto_paginate = false
14
15
16
17
18
19
20
21
22
23
24
25
26
27
        end
      end

      def api
        @api ||= ::Octokit::Client.new(
          access_token: access_token,
          api_endpoint: github_options[:site],
          # 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 }
          }
        )
      end
28

29
30
31
32
      def client
        unless config
          raise Projects::ImportService::Error,
            'OAuth configuration for GitHub missing.'
Valery Sizov's avatar
Valery Sizov committed
33
        end
34
35
36
37
38
39

        @client ||= ::OAuth2::Client.new(
          config.app_id,
          config.app_secret,
          github_options.merge(ssl: { verify: config['verify_ssl'] })
        )
Valery Sizov's avatar
Valery Sizov committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
      end

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

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

      def method_missing(method, *args, &block)
        if api.respond_to?(method)
55
          request(method, *args, &block)
Valery Sizov's avatar
Valery Sizov committed
56
57
58
59
60
61
62
        else
          super(method, *args, &block)
        end
      end

      def respond_to?(method)
        api.respond_to?(method) || super
Valery Sizov's avatar
Valery Sizov committed
63
64
65
66
67
      end

      private

      def config
68
        Gitlab.config.omniauth.providers.find { |provider| provider.name == "github" }
Valery Sizov's avatar
Valery Sizov committed
69
70
71
      end

      def github_options
72
73
74
75
76
        if config
          config["args"]["client_options"].deep_symbolize_keys
        else
          OmniAuth::Strategies::GitHub.default_options[:client_options].symbolize_keys
        end
Valery Sizov's avatar
Valery Sizov committed
77
      end
78
79
80

      def rate_limit
        api.rate_limit!
81
82
83
84
      # GitHub Rate Limit API returns 404 when the rate limit is
      # disabled. In this case we just want to return gracefully
      # instead of spitting out an error.
      rescue Octokit::NotFound
85
86
87
88
        nil
      end

      def has_rate_limit?
89
90
91
        return @has_rate_limit if defined?(@has_rate_limit)

        @has_rate_limit = rate_limit.present?
92
93
94
      end

      def rate_limit_exceed?
95
        has_rate_limit? && rate_limit.remaining <= GITHUB_SAFE_REMAINING_REQUESTS
96
97
98
99
100
101
      end

      def rate_limit_sleep_time
        rate_limit.resets_in + GITHUB_SAFE_SLEEP_TIME
      end

102
      def request(method, *args, &block)
103
104
        sleep rate_limit_sleep_time if rate_limit_exceed?

105
106
        data = api.send(method, *args, &block)
        yield data
107
108
109
110
111
112

        last_response = api.last_response

        while last_response.rels[:next]
          sleep rate_limit_sleep_time if rate_limit_exceed?
          last_response = last_response.rels[:next].get
113
          yield last_response.data if last_response.data.is_a?(Array)
114
115
        end
      end
Valery Sizov's avatar
Valery Sizov committed
116
117
118
    end
  end
end