-
Notifications
You must be signed in to change notification settings - Fork 3
/
hook.php
210 lines (172 loc) · 6.26 KB
/
hook.php
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
<?php
function plugin_gitlabintegration_install()
{
global $DB;
$config = new Config();
$config->setConfigurationValues('plugin:Gitlab Integration', ['configuration' => false]);
ProfileRight::addProfileRights(['gitlabintegration:read']);
//instanciate migration with version
$migration = new Migration(100);
// //Create table glpi_plugin_gitlab_integration only if it does not exists yet!
plugin_gitlabintegration_create_integration($DB);
//Create table glpi_plugin_gitlab_profiles only if it does not exists yet!
plugin_gitlabintegration_create_profiles($DB);
//Create table glpi_plugin_gitlab_categories_projects only if it does not exists yet!
plugin_gitlabintegration_create_projects($DB);
//Create table glpi_plugin_gitlab_parameters only if it does not exists yet!
plugin_gitlabintegration_create_parameters($DB);
//Insert parameters at table glpi_plugin_gitlab_parameters only if it exist!
plugin_gitlabintegration_insert_parameters($DB);
return true;
}
function plugin_gitlabintegration_uninstall()
{
global $DB;
$config = new Config();
$config->deleteConfigurationValues('plugin:Gitlab Integration', ['configuration' => false]);
ProfileRight::deleteProfileRights(['gitlabintegration:read']);
$notif = new Notification();
$options = [
'itemtype' => 'Ticket',
'event' => 'plugin_gitlabintegration',
'FIELDS' => 'id'
];
foreach ($DB->request('glpi_notifications', $options) as $data) {
$notif->delete($data);
}
//Drop table glpi_plugin_gitlab_integration only if it exists!
plugin_gitlabintegration_delete_integration($DB);
//Drop table glpi_plugin_gitlab_profiles_users only if it exists!
plugin_gitlabintegration_delete_profiles($DB);
//Drop table glpi_plugin_gitlab_parameters only if it exists!
plugin_gitlabintegration_delete_parameters($DB);
//Drop table glpi_plugin_gitlab_projects only if it exists!
plugin_gitlabintegration_delete_projects($DB);
return true;
}
function plugin_gitlabintegration_create_integration($DB)
{
if (!$DB->tableExists('glpi_plugin_gitlab_integration')) {
$query = "CREATE TABLE `glpi_plugin_gitlab_integration` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`ticket_id` INT(11) NOT NULL,
`gitlab_project_id` INT(11) NOT NULL,
`gitlab_member_id` INT (11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, $DB->error());
$query = "ALTER TABLE `glpi_plugin_gitlab_integration`
ADD CONSTRAINT `fk_gitlab_ticket`
FOREIGN KEY (`ticket_id`) REFERENCES `glpi_tickets` (`id`)";
$DB->queryOrDie($query, $DB->error());
}
}
function plugin_gitlabintegration_create_profiles($DB)
{
if (!$DB->tableExists('glpi_plugin_gitlab_profiles_users')) {
$query = "CREATE TABLE `glpi_plugin_gitlab_profiles_users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`profile_id` INT(11) NOT NULL,
`user_id` INT(11) NOT NULL,
`created_at` DATETIME,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, $DB->error());
$query = "ALTER TABLE `glpi_plugin_gitlab_profiles_users`
ADD CONSTRAINT `fk_gitlab_profile`
FOREIGN KEY (`profile_id`) REFERENCES `glpi_profiles` (`id`)";
$DB->queryOrDie($query, $DB->error());
$query = "ALTER TABLE `glpi_plugin_gitlab_profiles_users`
ADD CONSTRAINT `fk_gitlab_user`
FOREIGN KEY (`user_id`) REFERENCES `glpi_users` (`id`)";
$DB->queryOrDie($query, $DB->error());
}
}
function plugin_gitlabintegration_create_projects($DB)
{
if (!$DB->tableExists('glpi_plugin_gitlab_projects')) {
$query = "CREATE TABLE `glpi_plugin_gitlab_projects` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`project_id` INT(11) NOT NULL,
`project_name` VARCHAR(255) NOT NULL,
`category_id` INT(11) NULL,
`general` BOOLEAN NOT NULL DEFAULT 0,
`created_at` DATETIME,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, $DB->error());
$query = "ALTER TABLE `glpi_plugin_gitlab_projects`
ADD CONSTRAINT `fk_gitlab_categories`
FOREIGN KEY (`category_id`) REFERENCES `glpi_itilcategories` (`id`)";
$DB->queryOrDie($query, $DB->error());
}
}
function plugin_gitlabintegration_create_parameters($DB)
{
if (!$DB->tableExists('glpi_plugin_gitlab_parameters')) {
$query = "CREATE TABLE `glpi_plugin_gitlab_parameters` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`value` VARCHAR(125),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$DB->queryOrDie($query, $DB->error());
$query = "ALTER TABLE `glpi_plugin_gitlab_parameters`
ADD CONSTRAINT `uk_name`
UNIQUE (`name`) ";
$DB->queryOrDie($query, $DB->error());
}
}
function plugin_gitlabintegration_delete_integration($DB)
{
if ($DB->tableExists('glpi_plugin_gitlab_integration')) {
$drop_count = "DROP TABLE glpi_plugin_gitlab_integration";
$DB->query($drop_count);
}
}
function plugin_gitlabintegration_delete_profiles($DB)
{
if ($DB->tableExists('glpi_plugin_gitlab_profiles_users')) {
$drop_count = "DROP TABLE glpi_plugin_gitlab_profiles_users";
$DB->query($drop_count);
}
}
function plugin_gitlabintegration_delete_projects($DB)
{
if ($DB->tableExists('glpi_plugin_gitlab_projects')) {
$drop_count = "DROP TABLE glpi_plugin_gitlab_projects";
$DB->query($drop_count);
}
}
function plugin_gitlabintegration_delete_parameters($DB)
{
if ($DB->tableExists('glpi_plugin_gitlab_parameters')) {
$drop_count = "DROP TABLE glpi_plugin_gitlab_parameters";
$DB->query($drop_count);
}
}
function plugin_gitlabintegration_insert_parameters($DB)
{
if ($DB->tableExists('glpi_plugin_gitlab_parameters')) {
$ini_array = parse_ini_file("gitlabintegration.ini");
$parameters = [
[
'name' => 'gitlab_url',
'value' => $ini_array['GITLAB_URL'] == "" ? NULL : $ini_array['GITLAB_URL']
],
[
'name' => 'gitlab_token',
'value' => $ini_array['GITLAB_TOKEN'] == "" ? NULL : $ini_array['GITLAB_TOKEN']
]
];
foreach ($parameters as $parameter) {
$DB->insert(
'glpi_plugin_gitlab_parameters',
[
'name' => $parameter['name'],
'value' => $parameter['value']
]
);
}
}
}