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

gist.rb « representation « github_gists_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 674da4f34009df9b759e2ac8f0940a388f4926b3 (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
64
65
66
67
68
69
70
71
72
73
74
75
# frozen_string_literal: true

module Gitlab
  module GithubGistsImport
    module Representation
      class Gist
        include Gitlab::GithubImport::Representation::ToHash
        include Gitlab::GithubImport::Representation::ExposeAttribute

        attr_reader :attributes

        expose_attribute :id, :description, :is_public, :created_at, :updated_at, :files, :git_pull_url

        # Builds a gist from a GitHub API response.
        #
        # gist - An instance of `Hash` containing the gist
        #         details.
        def self.from_api_response(gist, additional_data = {})
          hash = {
            id: gist[:id],
            description: gist[:description],
            is_public: gist[:public],
            files: gist[:files],
            git_pull_url: gist[:git_pull_url],
            created_at: gist[:created_at],
            updated_at: gist[:updated_at]
          }

          new(hash)
        end

        # Builds a new gist using a Hash that was built from a JSON payload.
        def self.from_json_hash(raw_hash)
          new(Gitlab::GithubImport::Representation.symbolize_hash(raw_hash))
        end

        # attributes - A hash containing the raw gist details. The keys of this
        #              Hash (and any nested hashes) must be symbols.
        def initialize(attributes)
          @attributes = attributes
        end

        # Gist description can be an empty string, so we returning nil to use first file
        # name as a title in such case on snippet creation
        # Gist description has a limit of 256, while the snippet's title can be up to 255
        def truncated_title
          title = description.presence || first_file[:file_name]

          title.truncate(255)
        end

        def visibility_level
          is_public ? Gitlab::VisibilityLevel::PUBLIC : Gitlab::VisibilityLevel::PRIVATE
        end

        def first_file
          _key, value = files.first

          {
            file_name: value[:filename],
            file_content: Gitlab::HTTP.try_get(value[:raw_url])&.body
          }
        end

        def github_identifiers
          { id: id }
        end

        def total_files_size
          files.values.sum { |f| f[:size].to_i }
        end
      end
    end
  end
end