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

settings.rb « github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a4170f4147f8d0ae64a21d916e191b9d04a18ba9 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# frozen_string_literal: true

module Gitlab
  module GithubImport
    class Settings
      OPTIONAL_STAGES = {
        single_endpoint_issue_events_import: {
          label: 'Import issue and pull request events',
          selected: false,
          details: <<-TEXT.split("\n").map(&:strip).join(' ')
            For example, opened or closed, renamed, and labeled or unlabeled.
            Time required to import these events depends on how many issues or pull requests your project has.
          TEXT
        },
        single_endpoint_notes_import: {
          label: 'Use alternative comments import method',
          selected: false,
          details: <<-TEXT.split("\n").map(&:strip).join(' ')
            The default method can skip some comments in large projects because of limitations of the GitHub API.
          TEXT
        },
        attachments_import: {
          label: 'Import Markdown attachments (links)',
          selected: false,
          details: <<-TEXT.split("\n").map(&:strip).join(' ')
            Import Markdown attachments (links) from repository comments, release posts, issue descriptions,
            and pull request descriptions. These can include images, text, or binary attachments.
            If not imported, links in Markdown to attachments break after you remove the attachments from GitHub.
          TEXT
        },
        collaborators_import: {
          label: 'Import collaborators',
          selected: true,
          details: <<-TEXT.split("\n").map(&:strip).join(' ')
            Import direct repository collaborators who are not outside collaborators.
            Imported collaborators who aren't members of the group you imported the project into consume seats on your GitLab instance.
          TEXT
        }
      }.freeze

      def self.stages_array
        OPTIONAL_STAGES.map do |stage_name, data|
          {
            name: stage_name.to_s,
            label: s_(format("GitHubImport|%{text}", text: data[:label])),
            selected: data[:selected],
            details: s_(format("GitHubImport|%{text}", text: data[:details]))
          }
        end
      end

      def initialize(project)
        @project = project
      end

      def write(user_settings)
        user_settings = user_settings.to_h.with_indifferent_access

        optional_stages = fetch_stages_from_params(user_settings[:optional_stages])
        credentials = project.import_data&.credentials&.merge(
          additional_access_tokens: user_settings[:additional_access_tokens]
        )

        import_data = project.build_or_assign_import_data(
          data: {
            optional_stages: optional_stages,
            timeout_strategy: user_settings[:timeout_strategy]
          },
          credentials: credentials
        )

        import_data.save!
      end

      def enabled?(stage_name)
        project.import_data&.data&.dig('optional_stages', stage_name.to_s) || false
      end

      def disabled?(stage_name)
        !enabled?(stage_name)
      end

      private

      attr_reader :project

      def fetch_stages_from_params(user_settings)
        user_settings = user_settings.to_h.with_indifferent_access

        OPTIONAL_STAGES.keys.to_h do |stage_name|
          enabled = Gitlab::Utils.to_boolean(user_settings[stage_name], default: false)
          [stage_name, enabled]
        end
      end
    end
  end
end