Welcome to mirror list, hosted at ThFree Co, Russian Federation.

project_relation_type.rb « github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a6e598172eea8a1f59f504af3fc6d77be9327bed (plain)
1
2
3
4
5
6
7
8
9
10
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

module Gitlab
  module GithubImport
    class ProjectRelationType
      CACHE_ORGS_EXPIRES_IN = 5.minutes
      CACHE_USER_EXPIRES_IN = 1.hour

      def initialize(client)
        @client = client
      end

      def for(import_source)
        namespace = import_source.split('/')[0]
        if user?(namespace)
          'owned'
        elsif organization?(namespace)
          'organization'
        else
          'collaborated'
        end
      end

      private

      attr_reader :client

      def user?(namespace)
        github_user_login == namespace
      end

      def organization?(namespace)
        github_org_logins.include? namespace
      end

      def github_user_login
        ::Rails.cache.fetch(cache_key('user_login'), expire_in: CACHE_USER_EXPIRES_IN) do
          client.user(nil)[:login]
        end
      end

      def github_org_logins
        ::Rails.cache.fetch(cache_key('organization_logins'), expires_in: CACHE_ORGS_EXPIRES_IN) do
          logins = []
          client.each_object(:organizations) { |org| logins.push(org[:login]) }
          logins
        end
      end

      def cache_key(subject)
        ['github_import', Gitlab::CryptoHelper.sha256(client.octokit.access_token), subject].join('/')
      end
    end
  end
end