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

resource_seeder.rb « catalog « ci « seeders « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c29075cff320626d79842ba7abe7be8e5e607a9f (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true

module Gitlab
  module Seeders
    module Ci
      module Catalog
        class ResourceSeeder
          # Initializes the class
          #
          # @param [String] Path of the group to find
          # @param [Integer] Number of resources to create
          # @param[Boolean] If the created resources should be published or not, defaults to false
          def initialize(group_path:, seed_count:, publish:)
            @group = Group.find_by_full_path(group_path)
            @seed_count = seed_count
            @publish = publish
            @current_user = @group&.first_owner
          end

          def seed
            return warn 'ERROR: Group was not found.' if @group.nil?

            @seed_count.times do |i|
              seed_catalog_resource(i)
            end
          end

          private

          def create_project(name, index)
            project = ::Projects::CreateService.new(
              @current_user,
              description: "This is Catalog resource ##{index}",
              name: name,
              namespace_id: @group.id,
              path: name,
              visibility_level: @group.visibility_level
            ).execute

            if project.saved?
              project
            else
              warn project.errors.full_messages.to_sentence
              nil
            end
          end

          def create_template_yml(project)
            template_content = <<~YAML
            spec:
              inputs:
                stage:
                  default: test
            ---
            component-job:
              script: echo job 1
              stage: $[[ inputs.stage ]]
            YAML

            project.repository.create_dir(
              @current_user,
              'templates',
              message: 'Add template dir',
              branch_name: project.default_branch_or_main
            )

            project.repository.create_file(
              @current_user,
              'templates/component.yml',
              template_content,
              message: 'Add template.yml',
              branch_name: project.default_branch_or_main
            )
          end

          def create_readme(project, index)
            project.repository.create_file(
              @current_user,
              '/README.md',
              "## Component stuff #{index}",
              message: 'Add README.md',
              branch_name: project.default_branch_or_main
            )
          end

          def create_catalog_resource(project)
            result = ::Ci::Catalog::Resources::CreateService.new(project, @current_user).execute
            if result.success?
              result.payload
            else
              warn "Catalog resource could not be created for Project '#{project.name}': #{result.errors.join}"
              nil
            end
          end

          def seed_catalog_resource(index)
            name = "ci_seed_resource_#{index}"
            existing_project = Project.find_by_name(name)

            if existing_project.present? && existing_project.group.path == @group.path
              warn "Project '#{@group.path}/#{name}' already exists!"
              return
            end

            project = create_project(name, index)

            return unless project

            create_readme(project, index)
            create_template_yml(project)

            new_catalog_resource = create_catalog_resource(project)
            return unless new_catalog_resource

            warn "Project '#{@group.path}/#{name}' was saved successfully!"

            new_catalog_resource.publish! if @publish
          end
        end
      end
    end
  end
end