client.rb 1.3 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
Valery Sizov's avatar
Valery Sizov committed
4
      attr_reader :client, :api
Valery Sizov's avatar
Valery Sizov committed
5

Valery Sizov's avatar
Valery Sizov committed
6
      def initialize(access_token)
Valery Sizov's avatar
Valery Sizov committed
7
8
9
10
11
        @client = ::OAuth2::Client.new(
          config.app_id,
          config.app_secret,
          github_options
        )
Valery Sizov's avatar
Valery Sizov committed
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
38
39

        if access_token
          ::Octokit.auto_paginate = true
          @api = ::Octokit::Client.new(access_token: access_token)
        end
      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)
          api.send(method, *args, &block)
        else
          super(method, *args, &block)
        end
      end

      def respond_to?(method)
        api.respond_to?(method) || super
Valery Sizov's avatar
Valery Sizov committed
40
41
42
43
44
      end

      private

      def config
45
        Gitlab.config.omniauth.providers.find{|provider| provider.name == "github"}
Valery Sizov's avatar
Valery Sizov committed
46
47
48
49
      end

      def github_options
        {
50
51
52
          site: 'https://api.github.com',
          authorize_url: 'https://github.com/login/oauth/authorize',
          token_url: 'https://github.com/login/oauth/access_token'
Valery Sizov's avatar
Valery Sizov committed
53
54
55
56
57
        }
      end
    end
  end
end