summaryrefslogtreecommitdiff
path: root/lib/gitlab/gitorious_import/client.rb
blob: 5043f6a2ebd72ae661eddf710d25af438d698bf0 (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
56
57
58
59
60
61
62
63
module Gitlab
  module GitoriousImport
    GITORIOUS_HOST = "https://gitorious.org"

    class Client
      attr_reader :repo_list

      def initialize(repo_list)
        @repo_list = repo_list
      end

      def authorize_url(redirect_uri)
        "#{GITORIOUS_HOST}/gitlab-import?callback_url=#{redirect_uri}"
      end

      def repos
        @repos ||= repo_names.map { |full_name| Repository.new(full_name) }
      end

      def repo(id)
        repos.find { |repo| repo.id == id }
      end

      private

      def repo_names
        repo_list.to_s.split(',').map(&:strip).reject(&:blank?)
      end
    end

    Repository = Struct.new(:full_name) do
      def id
        Digest::SHA1.hexdigest(full_name)
      end

      def namespace
        segments.first
      end

      def path
        segments.last
      end

      def name
        path.titleize
      end

      def description
        ""
      end

      def import_url
        "#{GITORIOUS_HOST}/#{full_name}.git"
      end

      private

      def segments
        full_name.split('/')
      end
    end
  end
end