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
|
# 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"
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::Testing.inline! do
puts "Ensuring required groups"
ensure_group('gitlab-com')
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')
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_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_id: parent&.id
)
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
|