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

33_triage_ops.rb « development « fixtures « db - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5cdb1496c3dd370b9ae731fc8fc9775e8cfd5dfc (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# frozen_string_literal: true

require './spec/support/sidekiq_middleware'
require './spec/support/helpers/test_env'

class Gitlab::Seeder::TriageOps
  WEBHOOK_URL = 'http://0.0.0.0:$PORT$'
  WEBHOOK_TOKEN = "triage-ops-webhook-token"

  WORK_TYPE_LABELS = <<~LABELS.split("\n")
    bug::availability
    bug::mobile
    bug::performance
    bug::vulnerability
    feature::addition
    feature::consolidation
    feature::enhancement
    feature::removal
    maintenance::dependency
    maintenance::pipelines
    maintenance::refactor
    maintenance::test-gap
    maintenance::usability
    maintenance::workflow
    type::bug
    type::feature
    type::maintenance
  LABELS

  WORKFLOW_LABELS = <<~LABELS.split("\n")
    workflow::blocked
    workflow::design
    workflow::in dev
    workflow::in review
    workflow::planning breakdown
    workflow::production
    workflow::ready
    workflow::ready for design
    workflow::ready for development
    workflow::ready for review
    workflow::refinement
    workflow::validation backlog
    workflow::verification
  LABELS

  OTHER_LABELS = <<~LABELS.split("\n")
    ep::contributor tooling
    ep::meta
    ep::metrics
    ep::pipeline
    ep::review-apps
    ep::triage
    master-broken::caching
    master-broken::ci-config
    master-broken::dependency-upgrade
    master-broken::flaky-test
    master-broken::fork-repo-test-gap
    master-broken::infrastructure
    master-broken::need-merge-train
    master-broken::pipeline-skipped-before-merge
    master-broken::test-selection-gap
    master-broken::undetermined
    pipeline:expedite
    pipeline:expedite-master-fixing
    pipeline:mr-approved
    pipeline:run-all-jest
    pipeline:run-all-rspec
    pipeline:run-as-if-foss
    pipeline:run-as-if-jh
    pipeline:run-flaky-tests
    pipeline:run-praefect-with-db
    pipeline:run-review-app
    pipeline:run-single-db
    pipeline:skip-undercoverage
    pipeline:update-cache
    documentation
    Community contribution
  LABELS

  def seed!
    puts "Updating settings to allow web hooks to localhost"
    ApplicationSetting.current_without_cache.update!(allow_local_requests_from_web_hooks_and_services: true)

    Sidekiq::Worker.skipping_transaction_check do
      Sidekiq::Testing.inline! do
        puts "Ensuring required groups"
        ensure_group('gitlab-com')
        ensure_group('gitlab-com/support')
        ensure_group('gitlab-com/gl-security/appsec')
        ensure_group('gitlab-jh/jh-team')
        ensure_group('gitlab-org')
        ensure_group('gitlab-org/gitlab-core-team/community-members')
        ensure_group('gitlab-org/security')

        puts "Ensuring required projects"
        ensure_project('gitlab-org/gitlab')
        ensure_project('gitlab-org/security/gitlab')

        puts "Ensuring required bot user"
        ensure_bot_user

        puts "Setting up webhooks"
        ensure_webhook_for('gitlab-com')
        ensure_webhook_for('gitlab-org')

        puts "Ensuring work type labels"
        ensure_labels_for(WORK_TYPE_LABELS, 'gitlab-com')
        ensure_labels_for(WORK_TYPE_LABELS, 'gitlab-org')

        puts "Ensuring workflow type labels"
        ensure_labels_for(WORKFLOW_LABELS, 'gitlab-com')
        ensure_labels_for(WORKFLOW_LABELS, 'gitlab-org')

        puts "Ensuring other labels"
        ensure_labels_for(OTHER_LABELS, 'gitlab-com')
        ensure_labels_for(OTHER_LABELS, 'gitlab-org')
      end
    end
  end

  private

  def ensure_bot_user
    bot = User.find_by_username('triagebot')
    bot ||= User.create!(
      username: 'triagebot',
      name: 'Triage Bot',
      email: 'triagebot@example.com',
      confirmed_at: DateTime.now,
      password: SecureRandom.hex.slice(0, 16)
    )

    ensure_group('gitlab-org').add_maintainer(bot)
    ensure_group('gitlab-com').add_maintainer(bot)

    params = {
      scopes: ['api'],
      name: "API Token #{Time.zone.now}"
    }
    response = PersonalAccessTokens::CreateService.new(current_user: bot, target_user: bot, params: params).execute

    unless response.success?
      raise "Can't create Triage Bot access token: #{response.message}"
    end

    puts "Bot with API_TOKEN=#{response[:personal_access_token].token} is present now."

    bot
  end

  def ensure_webhook_for(group_path)
    group = Group.find_by_full_path(group_path)

    hook_params = {
      enable_ssl_verification: false,
      token: WEBHOOK_TOKEN,
      url: WEBHOOK_URL.gsub("$PORT$", ENV.fetch('TRIAGE_OPS_WEBHOOK_PORT', '8091'))
    }
    # Subscribe the hook to all possible events.
    all_group_hook_events = GroupHook.triggers.values
    all_group_hook_events.each { |value| hook_params[value] = true }

    group.hooks.delete_all

    hook = group.hooks.new(hook_params)
    hook.save!

    puts "Hook with url '#{hook.url}' and token '#{hook.token}' for '#{group_path}' is present now."
  end

  def ensure_labels_for(label_titles, group_path)
    group = Group.find_by_full_path(group_path)

    label_titles.each do |label_title|
      color = ::Gitlab::Color.color_for(label_title[/[^:]+/])

      Labels::CreateService
        .new(title: label_title, color: "#{color}")
        .execute(group: group)
    end
  end

  def ensure_group(full_path)
    group = Group.find_by_full_path(full_path)

    return group if group

    parent_path = full_path.split('/')[0..-2].join('/')
    parent = ensure_group(parent_path) if parent_path.present?

    group_path = full_path.split('/').last

    group = Group.new(
      name: group_path.titleize,
      path: group_path,
      parent: parent
    )
    group.description = FFaker::Lorem.sentence
    group.save!

    group.add_owner(User.first)
    group.create_namespace_settings

    group
  end

  def ensure_project(project_fullpath)
    project = Project.find_by_full_path(project_fullpath)

    return project if project

    group_path = project_fullpath.split('/')[0..-2].join('/')
    project_path = project_fullpath.split('/').last

    group = ensure_group(group_path)

    params = {
      namespace_id: group.id,
      name: project_path.titleize,
      path: project_path,
      description: FFaker::Lorem.sentence,
      visibility_level: Gitlab::VisibilityLevel::PRIVATE,
      skip_disk_validation: true
    }

    project = ::Projects::CreateService.new(User.first, params).execute

    raise "Can't create project '#{project_fullpath}' : #{project.errors.full_messages}" unless project.persisted?

    project
  end
end

if ENV['SEED_TRIAGE_OPS']
  Gitlab::Seeder.quiet do
    Gitlab::Seeder::TriageOps.new.seed!
  end
else
  puts "Skipped. Use the `SEED_TRIAGE_OPS` environment variable to enable seeding data for triage ops project."
end