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

discussion_id.rb « diff_notes « representation « github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 38b560f21c02bb940102ff5dff0d427ccb83f8d4 (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
# frozen_string_literal: true

module Gitlab
  module GithubImport
    module Representation
      module DiffNotes
        class DiscussionId
          NOTEABLE_TYPE = 'MergeRequest'
          DISCUSSION_CACHE_REGEX = %r{/(?<repo>[^/]*)/pull/(?<iid>\d+)}i.freeze
          DISCUSSION_CACHE_KEY = 'github-importer/discussion-id-map/%{project}/%{noteable_id}/%{original_note_id}'

          def initialize(note)
            @note = note
            @matches = note[:html_url].match(DISCUSSION_CACHE_REGEX)
          end

          def find_or_generate
            (note[:in_reply_to_id].present? && current_discussion_id) || generate_discussion_id
          end

          private

          attr_reader :note, :matches

          def generate_discussion_id
            discussion_id = Discussion.discussion_id(
              Struct
              .new(:noteable_id, :noteable_type)
              .new(matches[:iid].to_i, NOTEABLE_TYPE)
            )
            cache_discussion_id(discussion_id)
          end

          def cache_discussion_id(discussion_id)
            Gitlab::Cache::Import::Caching.write(
              discussion_id_cache_key(note[:id]), discussion_id
            )
          end

          def current_discussion_id
            Gitlab::Cache::Import::Caching.read(
              discussion_id_cache_key(note[:in_reply_to_id])
            )
          end

          def discussion_id_cache_key(id)
            format(DISCUSSION_CACHE_KEY,
              project: matches[:repo],
              noteable_id: matches[:iid].to_i,
              original_note_id: id
            )
          end
        end
      end
    end
  end
end